public void TestSaveFormDefinitionFieldProtectedApp() { EntityType definition; CustomEditForm editForm; UserAccount userAccount; using (new SecurityBypassContext()) { var solution = new Solution { Name = "TestSolution" + Guid.NewGuid(), CanModifyApplication = true }; solution.Save(); definition = new EntityType(); definition.Inherits.Add(UserResource.UserResource_Type); definition.InSolution = solution; editForm = new CustomEditForm { TypeToEditWithForm = definition, InSolution = solution }; editForm.Save(); // Protect the app solution.CanModifyApplication = false; solution.Save(); userAccount = Entity.GetByField <UserAccount>(SpecialStrings.TenantAdministratorUser, false, new EntityRef("core", "name")).FirstOrDefault(); } using (new SetUser(userAccount)) { // Adding new field to the definition. // This should succeed var sField = new StringField(); definition.Fields.Add(sField.As <Field>()); Entity.Save(new IEntity[] { editForm, definition, sField }); // Update the new field // This should succeed sField.Name = Guid.NewGuid().ToString(); Entity.Save(new IEntity[] { editForm, definition, sField }); // Remove the field from the definition // This should succeed sField = Entity.Get <StringField>(sField.Id); definition.Fields.Remove(sField.As <Field>()); Entity.Save(new IEntity[] { editForm, definition, sField }); // Delete the field Entity.Delete(sField.Id); } }
public void TestRemoveProtectedFieldFromDefinitionProtectedApp() { EntityType definition; CustomEditForm editForm; UserAccount userAccount; StringField protectedField; using (new SecurityBypassContext()) { var solution = new Solution { Name = "TestSolution" + Guid.NewGuid(), CanModifyApplication = true }; solution.Save(); definition = new EntityType(); definition.Inherits.Add(UserResource.UserResource_Type); definition.InSolution = solution; protectedField = new StringField { InSolution = solution }; definition.Fields.Add(protectedField.As <Field>()); editForm = new CustomEditForm { TypeToEditWithForm = definition, InSolution = solution }; editForm.Save(); // Protect the app solution.CanModifyApplication = false; solution.Save(); userAccount = Entity.GetByField <UserAccount>(SpecialStrings.TenantAdministratorUser, false, new EntityRef("core", "name")).FirstOrDefault(); } using (new SetUser(userAccount)) { // Adding new field to the definition. // This should fail definition.Fields.Remove(protectedField.As <Field>()); Assert.That(() => Entity.Save(new IEntity[] { editForm, definition }), Throws.TypeOf <PlatformSecurityException>()); // Delete the field Assert.That(() => Entity.Delete(protectedField.Id), Throws.TypeOf <PlatformSecurityException>()); } }
public void RegexDoesNotCheckEmptyStrings() { // Ensure that regex is not applied for empty string, if IsRequired is not set. StringField field = new StringField(); field.Name = "f1"; field.IsRequired = true; field.Pattern = Entity.Get <StringPattern>("emailPattern"); field.IsRequired = false; EntityType type = new EntityType(); type.Name = "t1"; type.Fields.Add(field.As <Field>()); type.Save(); var e = new Entity(type.Id); e.SetField(field, ""); e.Save( ); e.Delete( ); field.Delete(); type.Delete(); }
public void RegexPatternChecksInvalid( ) { StringField field = new StringField(); field.Name = "f1"; field.IsRequired = true; field.Pattern = Entity.Get <StringPattern>("emailPattern"); field.IsRequired = false; EntityType type = new EntityType(); type.Name = "t1"; type.Fields.Add(field.As <Field>()); type.Save(); var e = new Entity(type.Id); try { e.SetField(field, "blah!!!"); e.Save(); } finally { e.Delete(); field.Delete(); type.Delete(); } }
public void StringField_Regex( ) { var sf = new StringField { Pattern = Entity.Get <StringPattern>("core:emailPattern") }; var f = sf.As <Field>( ); string res1 = f.ValidateFieldValue("blah"); Assert.IsNotNull(res1, "Test invalid email"); string res2 = f.ValidateFieldValue("*****@*****.**"); Assert.IsNull(res2, "Test valid email"); }
public void FieldCastingTest_StringMaxLength( ) { var sf = new StringField { MaxLength = 5 }; var f = sf.As <Field>( ); string res1 = f.ValidateFieldValue("hello world"); Assert.IsNotNull(res1, "Test when too long"); string res2 = f.ValidateFieldValue("hello"); Assert.IsNull(res2, "Test exact length"); }
public void Test_MandatoryField(string mandatoryDefinedOn, string data) { // Create JSON string jsonMember = "member"; string jsonData = data.Replace("'", "\""); string json = "{\"" + jsonMember + "\":" + jsonData + "}"; if (data == "'unprovided'") { json = json.Replace(jsonMember, "somethingelse"); } // Create a type mapping EntityType entityTypeResource = new EntityType(); ApiResourceMapping typeMapping = new ApiResourceMapping(); typeMapping.MappedType = entityTypeResource; // Create a member mapping StringField fieldResource = new StringField(); ApiFieldMapping fieldMapping = new ApiFieldMapping(); fieldMapping.MappedField = fieldResource.As <Field>(); fieldMapping.Name = jsonMember; typeMapping.ResourceMemberMappings.Add(fieldMapping.As <ApiMemberMapping>()); if (mandatoryDefinedOn == "Mapping") { fieldMapping.ApiMemberIsRequired = true; } if (mandatoryDefinedOn == "Schema") { fieldResource.IsRequired = true; } entityTypeResource.Save( ); // Fill entity if (data == "'provided'") { IEntity entity = RunTest(json, typeMapping); } else { Assert.Throws <ConnectorRequestException>(() => RunTest(json, typeMapping), "E1010 '" + jsonMember + "' value is required."); } }
public static EntityType CreateDefinition(string name) { var sourceDefn = new EntityType { Name = name }; //add a string field to the definition var strField = new StringField { Name = "String field" }; strField.Save( ); //// cast to type to "Add" field & save definition sourceDefn.Fields.Add(strField.As <Field>( )); sourceDefn.Save( ); return(sourceDefn); }
public void Export_ReverseAlias( ) { IEntityXmlExporter exporter = Factory.EntityXmlExporter; IEntityRepository repository = Factory.EntityRepository; Definition type = new Definition( ); type.Inherits.Add(UserResource.UserResource_Type); StringField field = new StringField( ); type.Fields.Add(field.As <Field>( )); type.Save( ); // Export string xml; using (RunAsImportExportRole( )) { xml = exporter.GenerateXml(type.Id, EntityXmlExportSettings.Default); } XmlDocument doc = new XmlDocument( ); doc.LoadXml(xml); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); // Note: can't get xpath to work with default namespace ns.AddNamespace("c", "core"); ns.AddNamespace("k", "console"); XmlElement e = doc.SelectSingleNode("/c:xml/c:entities/c:group/c:definition", ns) as XmlElement; Assert.That(e, Is.Not.Null); Assert.That(e.SelectSingleNode("c:fields/c:stringField", ns), Is.Not.Null); XmlAttribute alias = doc.SelectSingleNode("/c:xml/c:aliasMap/c:fields/@type", ns) as XmlAttribute; Assert.That(alias.Value, Is.EqualTo("revRel")); }
public void StringFieldNotOver10k() { var tooLargeStringLength = ReadiNow.Model.FieldTypes.StringFieldHelper.RealMaximumStringFieldLength + 1; // Ensure that regex is not applied for empty string, if IsRequired is not set. StringField field = new StringField(); field.Name = "f1"; field.IsRequired = true; field.Pattern = Entity.Get <StringPattern>("emailPattern"); field.IsRequired = false; EntityType type = new EntityType(); type.Name = "t1"; type.Fields.Add(field.As <Field>()); type.Save(); var e = new Entity(type.Id); e.SetField(field, new string('#', tooLargeStringLength)); e.Save(); }
public void Setup( ) { // Getting Forbidden? Or ConnectorConfigException? // Maybe there's duplicate copies of these objects in the DB. // Define key and user using (new TenantAdministratorContext(TenantName)) { // Define schema type = new EntityType( ); type.Inherits.Add(UserResource.UserResource_Type); type.Name = "Test type " + Guid.NewGuid( ); type.Save( ); type2 = new EntityType(); type2.Inherits.Add(UserResource.UserResource_Type); type2.Name = "Test type2 " + Guid.NewGuid(); type2.Save(); stringField = new StringField( ); stringField.Name = "Field 1"; stringField.FieldIsOnType = type; stringField.Save( ); lookup = new Relationship(); lookup.Cardinality_Enum = CardinalityEnum_Enumeration.OneToOne; lookup.FromType = type; lookup.ToType = type2; // Define API mapping = new ApiResourceMapping( ); mapping.Name = "Test mapping " + Guid.NewGuid( );; mapping.MappedType = type; mapping.Save( ); lookupMapping = new ApiRelationshipMapping(); lookupMapping.Name = "lookup1"; lookupMapping.MappedRelationship = lookup; lookupMapping.MemberForResourceMapping = mapping; lookupMapping.Save(); fieldMapping = new ApiFieldMapping( ); fieldMapping.Name = "field1"; fieldMapping.MappedField = stringField.As <Field>( ); fieldMapping.MemberForResourceMapping = mapping; fieldMapping.Save( ); endpoint = new ApiResourceEndpoint( ); endpoint.Name = "Test endpoint " + Guid.NewGuid( ); endpoint.ApiEndpointAddress = EndpointAddress; endpoint.EndpointResourceMapping = mapping; endpoint.ApiEndpointEnabled = true; endpoint.EndpointCanCreate = true; endpoint.EndpointCanDelete = true; endpoint.EndpointCanUpdate = true; endpoint.Save( ); api = new Api( ); api.Name = "Test API " + Guid.NewGuid( );; api.ApiAddress = ApiAddress; api.ApiEnabled = true; api.ApiEndpoints.Add(endpoint.As <ApiEndpoint>( )); api.Save( ); // Define access userAccount = new UserAccount( ); userAccount.Name = "Test user " + Guid.NewGuid( ); userAccount.AccountStatus_Enum = UserAccountStatusEnum_Enumeration.Active; userAccount.Password = "******"; userAccount.Save( ); key = new ApiKey( ); key.Name = ApiKey; key.ApiKeyUserAccount = userAccount; key.ApiKeyEnabled = true; key.KeyForApis.Add(api); key.Save( ); updateInstance = Entity.Create(type).AsWritable <Resource>( ); updateInstance.Name = updateInstanceName = "ResourceToUpdate" + Guid.NewGuid( ); updateInstance.Description = updateInstanceDesc = "ResourceToUpdate" + Guid.NewGuid( ); updateInstance.Save( ); updateInstanceGuid = updateInstance.UpgradeId; IAccessRuleFactory accessControlHelper = new AccessRuleFactory( ); accessRule = accessControlHelper.AddAllowCreate(userAccount.As <Subject>( ), type.As <SecurableEntity>( )); accessRule = accessControlHelper.AddAllowByQuery(userAccount.As <Subject>( ), type.As <SecurableEntity>( ), new[] { Permissions.Read, Permissions.Modify, Permissions.Delete }, TestQueries.Entities(type).ToReport( )); } cleanup = new List <IEntity> { userAccount, key, api, type, mapping, endpoint, fieldMapping, stringField, accessRule, updateInstance }; }
private void CreateScenarioImpl(string testInstanceName, Func <EntityRef[]> permissionsCallback) { // Define key and user using (new TenantAdministratorContext(TenantName)) { // Define schema type = new EntityType( ); type.Inherits.Add(UserResource.UserResource_Type); type.Name = "Test type " + Guid.NewGuid( ); type.Save( ); type2 = new EntityType( ); type2.Inherits.Add(UserResource.UserResource_Type); type2.Name = "Test type2 " + Guid.NewGuid( ); type2.Save( ); stringField = new StringField( ); stringField.Name = "Field 1"; stringField.FieldIsOnType = type; stringField.MaxLength = 50; stringField.Save( ); lookup = new Relationship( ); lookup.Cardinality_Enum = CardinalityEnum_Enumeration.OneToOne; lookup.FromType = type; lookup.ToType = type2; relationship = new Relationship( ); relationship.Cardinality_Enum = CardinalityEnum_Enumeration.ManyToMany; relationship.FromType = type; relationship.ToType = type2; // Define API mapping = new ApiResourceMapping( ); mapping.Name = "Test mapping " + Guid.NewGuid( );; mapping.MappedType = type; mapping.Save( ); fieldMapping = new ApiFieldMapping( ); fieldMapping.Name = "field1"; fieldMapping.MappedField = stringField.As <Field>( ); fieldMapping.MemberForResourceMapping = mapping; fieldMapping.Save( ); lookupMapping = new ApiRelationshipMapping( ); lookupMapping.Name = "lookup1"; lookupMapping.MappedRelationship = lookup; lookupMapping.MemberForResourceMapping = mapping; lookupMapping.Save( ); relationshipMapping = new ApiRelationshipMapping( ); relationshipMapping.Name = "rel1"; relationshipMapping.MappedRelationship = relationship; relationshipMapping.MemberForResourceMapping = mapping; relationshipMapping.Save( ); endpoint = new ApiResourceEndpoint( ); endpoint.Name = "Test endpoint " + Guid.NewGuid( );; endpoint.ApiEndpointAddress = EndpointAddress; endpoint.EndpointResourceMapping = mapping; endpoint.ApiEndpointEnabled = true; endpoint.EndpointCanCreate = true; endpoint.EndpointCanUpdate = true; endpoint.EndpointCanDelete = true; endpoint.Save( ); api = new Api( ); api.Name = "Test API " + Guid.NewGuid( );; api.ApiAddress = ApiAddress; api.ApiEnabled = true; api.ApiEndpoints.Add(endpoint.As <ApiEndpoint>( )); api.Save( ); // Define access userAccount = new UserAccount( ); userAccount.Name = "Test user " + Guid.NewGuid( ); userAccount.AccountStatus_Enum = UserAccountStatusEnum_Enumeration.Active; userAccount.Password = "******"; userAccount.Save( ); key = new ApiKey( ); key.Name = ApiKey; key.ApiKeyUserAccount = userAccount; key.ApiKeyEnabled = true; key.KeyForApis.Add(api); key.Save( ); if (testInstanceName != null) { scenarioInstance = Entity.Create(type); scenarioInstance.SetField("core:name", testInstanceName); scenarioInstance.Save( ); } foreignName = "Foreign" + Guid.NewGuid( ).ToString( ); foreignInstance = Entity.Create(type2); foreignInstance.SetField("core:name", foreignName); foreignInstance.Save( ); // Grant create var permissions = permissionsCallback( ); IAccessRuleFactory accessControlHelper = new AccessRuleFactory( ); if (permissions [0] == Permissions.Create) { accessControlHelper.AddAllowCreate(userAccount.As <Subject>( ), type.As <SecurableEntity>( )); } else if (permissions.Length > 0) { accessControlHelper.AddAllowByQuery(userAccount.As <Subject>( ), type.As <SecurableEntity>( ), permissions, TestQueries.Entities(type).ToReport( )); } accessControlHelper.AddAllowByQuery(userAccount.As <Subject>( ), type2.As <SecurableEntity>( ), new [] { Permissions.Read, Permissions.Modify }, TestQueries.Entities(type2).ToReport( )); } }