public void Test_Mapping_StringField( )
        {
            ApiResourceMapping mapping = new ApiResourceMapping( );

            mapping.ResourceMappingIdentityField = Entity.Get <Field>("core:description");
            mapping.Save( );

            ConnectorRequest request = new ConnectorRequest
            {
                ControllerRootPath = "https://whatever/spapi/api/",
                TenantName         = "Tenant1",
                ApiPath            = new string [] { "testapi", "testendpoint" }
            };

            Resource resource = new Resource( );

            resource.Description = "Test1";
            resource.Save( );

            string expected = "https://whatever/spapi/api/Tenant1/testapi/testendpoint/Test1";

            IResourceUriGenerator generator = GetGenerator( );
            string actual = generator.CreateResourceUri(resource, request, mapping);

            Assert.That(actual, Is.EqualTo(expected));
        }
        public void Test_Mapping_EntityGuid( )
        {
            ApiResourceMapping mapping = new ApiResourceMapping( );

            mapping.Save();

            ConnectorRequest request = new ConnectorRequest
            {
                ControllerRootPath = "https://whatever/spapi/api/",
                TenantName         = "Tenant1",
                ApiPath            = new string [] { "testapi", "testendpoint" }
            };

            Resource resource = new Resource( );

            resource.Save( );
            Assert.That(resource.UpgradeId, Is.Not.EqualTo(Guid.Empty));

            string expected = "https://whatever/spapi/api/Tenant1/testapi/testendpoint/" + resource.UpgradeId.ToString( );

            IResourceUriGenerator generator = GetGenerator( );
            string actual = generator.CreateResourceUri(resource, request, mapping);

            Assert.That(actual, Is.EqualTo(expected));
        }
        public void Test_SuppressWorkflow(bool endpointSuppressWorkflows)
        {
            Mock <IReaderToEntityAdapterProvider> m1 = new Mock <IReaderToEntityAdapterProvider>(MockBehavior.Strict);
            Mock <IResourceResolverProvider>      m2 = new Mock <IResourceResolverProvider>(MockBehavior.Loose);
            Mock <IResourceUriGenerator>          m3 = new Mock <IResourceUriGenerator>(MockBehavior.Loose);

            ApiResourceMapping mapping = new ApiResourceMapping();

            long typeId = Factory.ScriptNameResolver.GetTypeByName("AA_Drink");

            if (typeId == 0)
            {
                mapping.MappedType = null;
            }
            else
            {
                mapping.MappedType = Entity.Get(typeId).As <EntityType>( );
            }

            mapping.MappingSuppressWorkflows = endpointSuppressWorkflows;
            mapping.Save();

            ApiResourceEndpoint apiResourceEndpoint = new ApiResourceEndpoint();

            apiResourceEndpoint.EndpointResourceMapping = mapping;
            apiResourceEndpoint.EndpointCanCreate       = true;
            apiResourceEndpoint.ApiEndpointEnabled      = true;
            apiResourceEndpoint.Save();

            ResourceEndpoint re = new ResourceEndpoint(m1.Object, m2.Object, m3.Object);

            ConnectorRequest request = new ConnectorRequest();

            request.Verb = ConnectorVerb.Post;

            m1.Setup <IReaderToEntityAdapter>(m => m.GetAdapter(It.IsAny <long>(), It.IsAny <ReaderToEntityAdapterSettings>())).Returns(
                () =>
            {
                Assert.That(WorkflowRunContext.Current.DisableTriggers, Is.EqualTo(endpointSuppressWorkflows));
                return(null);
            }
                );

            // We don't care what happens .. as long as the callback was called with the correct context setting
            try
            {
                re.HandleRequest(request, apiResourceEndpoint);
            }
            catch { }

            m1.VerifyAll();
        }
예제 #4
0
        public void Test_Resolve_WithMapping_NotFound(string identity, string type, ResourceResolverError expectedError)
        {
            IResourceResolverProvider provider = GetResolverProvider( );
            ApiResourceMapping        mapping  = new ApiResourceMapping( );

            mapping.MappedType = Entity.Get <EntityType>("test:allFields");
            mapping.Save( );
            IResourceResolver resolver = provider.GetResolverForResourceMapping(mapping);

            ResourceResolverEntry entry = resolver.ResolveResource(identity);

            Assert.That(entry.Entity, Is.Null);
            Assert.That(entry.Error, Is.EqualTo(expectedError));
        }
예제 #5
0
        public void Test_ResolveResource_WithMapping_NoIdentityField_ByName(string identity, string type)
        {
            IResourceResolverProvider provider = GetResolverProvider( );
            ApiResourceMapping        mapping  = new ApiResourceMapping( );

            mapping.MappedType = Entity.Get <EntityType>(type);
            mapping.Save( );
            IResourceResolver resolver = provider.GetResolverForResourceMapping(mapping);

            ResourceResolverEntry entry = resolver.ResolveResource(identity);
            IEntity entity = entry.Entity;

            Assert.That(entity, Is.Not.Null);
            Assert.That(entity.As <Resource>( ).Name, Is.EqualTo(identity));
        }
예제 #6
0
        public void Test_ResolveResource_WithMapping_NoIdentityField_ByGuid(string alias, string type, string name)
        {
            IEntity entity1  = Entity.Get(new EntityRef(alias));
            string  identity = entity1.UpgradeId.ToString();

            IResourceResolverProvider provider = GetResolverProvider( );
            ApiResourceMapping        mapping  = new ApiResourceMapping( );

            mapping.MappedType = Entity.Get <EntityType>(type);
            mapping.Save( );
            IResourceResolver resolver = provider.GetResolverForResourceMapping(mapping);

            ResourceResolverEntry entry = resolver.ResolveResource(identity);
            IEntity entity = entry.Entity;

            Assert.That(entity, Is.Not.Null);
            Assert.That(entity.As <Resource>( ).Name, Is.EqualTo(name));
        }
예제 #7
0
        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
            };
        }
예제 #8
0
        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( ));
            }
        }