public void ShouldEditParentResourcesOnClaimSet()
        {
            var testApplication = new Application
            {
                ApplicationName = $"Test Application {DateTime.Now:O}"
            };

            Save(testApplication);

            var testClaimSet = new ClaimSet {
                ClaimSetName = "TestClaimSet", Application = testApplication
            };

            Save(testClaimSet);

            var testResources = SetupParentResourceClaimsWithChildren(testClaimSet, testApplication);

            var testResource1ToEdit    = testResources.Select(x => x.ResourceClaim).Single(x => x.ResourceName == "TestParentResourceClaim1");
            var testResource2ToNotEdit = testResources.Select(x => x.ResourceClaim).Single(x => x.ResourceName == "TestParentResourceClaim2");

            var editedResource = new ResourceClaim
            {
                Id     = testResource1ToEdit.ResourceClaimId,
                Name   = testResource1ToEdit.ResourceName,
                Create = false,
                Read   = false,
                Update = true,
                Delete = true
            };

            var editResourceOnClaimSetModel = new Mock <IEditResourceOnClaimSetModel>();

            editResourceOnClaimSetModel.Setup(x => x.ClaimSetId).Returns(testClaimSet.ClaimSetId);
            editResourceOnClaimSetModel.Setup(x => x.ResourceClaim).Returns(editedResource);

            var command = new EditResourceOnClaimSetCommand(TestContext);

            command.Execute(editResourceOnClaimSetModel.Object);

            var resourceClaimsForClaimSet = new GetResourcesByClaimSetIdQuery(TestContext, GetMapper()).AllResources(testClaimSet.ClaimSetId).ToList();
            var parentResources           = testResources.Where(x =>
                                                                x.ClaimSet.ClaimSetId == testClaimSet.ClaimSetId && x.ResourceClaim.ParentResourceClaim == null).Select(x => x.ResourceClaim).ToList();

            resourceClaimsForClaimSet.Count().ShouldBe(parentResources.Count);

            var resultResourceClaim1 = resourceClaimsForClaimSet.Single(x => x.Id == editedResource.Id);

            resultResourceClaim1.Create.ShouldBe(editedResource.Create);
            resultResourceClaim1.Read.ShouldBe(editedResource.Read);
            resultResourceClaim1.Update.ShouldBe(editedResource.Update);
            resultResourceClaim1.Delete.ShouldBe(editedResource.Delete);

            var resultResourceClaim2 = resourceClaimsForClaimSet.Single(x => x.Id == testResource2ToNotEdit.ResourceClaimId);

            resultResourceClaim2.Create.ShouldBe(true);
            resultResourceClaim2.Read.ShouldBe(false);
            resultResourceClaim2.Update.ShouldBe(false);
            resultResourceClaim2.Delete.ShouldBe(false);
        }
Esempio n. 2
0
        public void ShouldOverrideAuthorizationStrategiesForParentResourcesOnClaimSet()
        {
            var testApplication = new Application
            {
                ApplicationName = "TestApplicationName"
            };

            Save(testApplication);

            var testClaimSet = new ClaimSet
            {
                ClaimSetName = "TestClaimSet",
                Application  = testApplication
            };

            Save(testClaimSet);

            var appAuthorizationStrategies = SetupApplicationAuthorizationStrategies(testApplication).ToList();
            var testResourceClaims         = SetupParentResourceClaimsWithChildren(testClaimSet, testApplication);

            SetupResourcesWithDefaultAuthorizationStrategies(appAuthorizationStrategies, testResourceClaims.ToList());

            var testResource1ToEdit    = testResourceClaims.Select(x => x.ResourceClaim).Single(x => x.ResourceName == "TestParentResourceClaim1");
            var testResource2ToNotEdit = testResourceClaims.Select(x => x.ResourceClaim).Single(x => x.ResourceName == "TestParentResourceClaim2");

            var overrideModel = new OverrideDefaultAuthorizationStrategyModel
            {
                ResourceClaimId = testResource1ToEdit.ResourceClaimId,
                ClaimSetId      = testClaimSet.ClaimSetId,
                AuthorizationStrategyForCreate = appAuthorizationStrategies.Single(x => x.AuthorizationStrategyName == "TestAuthStrategy4").AuthorizationStrategyId,
                AuthorizationStrategyForRead   = 0,
                AuthorizationStrategyForUpdate = 0,
                AuthorizationStrategyForDelete = 0
            };

            var command = new OverrideDefaultAuthorizationStrategyCommand(TestContext);

            command.Execute(overrideModel);

            var resourceClaimsForClaimSet = new GetResourcesByClaimSetIdQuery(TestContext, GetMapper()).AllResources(testClaimSet.ClaimSetId).ToList();

            var resultResourceClaim1 = resourceClaimsForClaimSet.Single(x => x.Id == overrideModel.ResourceClaimId);

            resultResourceClaim1.AuthStrategyOverridesForCRUD[0].AuthStrategyName.ShouldBe("TestAuthStrategy4");
            resultResourceClaim1.AuthStrategyOverridesForCRUD[1].ShouldBeNull();
            resultResourceClaim1.AuthStrategyOverridesForCRUD[2].ShouldBeNull();
            resultResourceClaim1.AuthStrategyOverridesForCRUD[3].ShouldBeNull();

            var resultResourceClaim2 = resourceClaimsForClaimSet.Single(x => x.Id == testResource2ToNotEdit.ResourceClaimId);

            resultResourceClaim2.AuthStrategyOverridesForCRUD[0].ShouldBeNull();
            resultResourceClaim2.AuthStrategyOverridesForCRUD[1].ShouldBeNull();
            resultResourceClaim2.AuthStrategyOverridesForCRUD[2].ShouldBeNull();
            resultResourceClaim2.AuthStrategyOverridesForCRUD[3].ShouldBeNull();
        }
        public void ShouldNotAddDuplicateResourcesToClaimSetDuringEdit()
        {
            var testApplication = new Application
            {
                ApplicationName = $"Test Application {DateTime.Now:O}"
            };

            Save(testApplication);

            var testClaimSet = new ClaimSet {
                ClaimSetName = "TestClaimSet", Application = testApplication
            };

            Save(testClaimSet);

            SetupParentResourceClaimsWithChildren(testClaimSet, testApplication);

            var existingResources = new GetResourcesByClaimSetIdQuery(TestContext, GetMapper()).AllResources(testClaimSet.ClaimSetId)
                                    .ToList();

            var duplicateResource = existingResources.Single(x => x.Name == "TestParentResourceClaim1");

            var editResourceOnClaimSetModel = new EditClaimSetResourceModel
            {
                ClaimSetId             = testClaimSet.ClaimSetId,
                ResourceClaim          = duplicateResource,
                ExistingResourceClaims = existingResources
            };

            var validator         = new EditClaimSetResourceModelValidator();
            var validationResults = validator.Validate(editResourceOnClaimSetModel);

            validationResults.IsValid.ShouldBe(false);
            validationResults.Errors.Single().ErrorMessage.ShouldBe("Only unique resource claims can be added. The following is a duplicate resource:\nTestParentResourceClaim1");
        }
        public void ShouldAddChildResourcesToClaimSet()
        {
            var testApplication = new Application
            {
                ApplicationName = $"Test Application {DateTime.Now:O}"
            };

            Save(testApplication);

            var testClaimSet = new ClaimSet {
                ClaimSetName = "TestClaimSet", Application = testApplication
            };

            Save(testClaimSet);

            var testResources       = SetupResourceClaims(testApplication);
            var testParentResource1 = testResources.Single(x => x.ResourceName == "TestParentResourceClaim1");

            var testChildResource1ToAdd = TestContext.ResourceClaims.Single(x => x.ResourceName == "TestChildResourceClaim1" && x.ParentResourceClaimId == testParentResource1.ResourceClaimId);
            var resourceToAdd           = new ResourceClaim()
            {
                Id     = testChildResource1ToAdd.ResourceClaimId,
                Name   = testChildResource1ToAdd.ResourceName,
                Create = true,
                Read   = false,
                Update = true,
                Delete = false
            };
            var existingResources = new GetResourcesByClaimSetIdQuery(TestContext, GetMapper()).AllResources(testClaimSet.ClaimSetId)
                                    .ToList();

            var editResourceOnClaimSetModel = new EditClaimSetResourceModel
            {
                ClaimSetId             = testClaimSet.ClaimSetId,
                ResourceClaim          = resourceToAdd,
                ExistingResourceClaims = existingResources
            };

            var command = new EditResourceOnClaimSetCommand(TestContext);

            command.Execute(editResourceOnClaimSetModel);

            var resourceClaimsForClaimSet = new GetResourcesByClaimSetIdQuery(TestContext, GetMapper()).AllResources(testClaimSet.ClaimSetId).ToList();

            var resultChildResourceClaim1 =
                resourceClaimsForClaimSet.Single(x => x.Name == testChildResource1ToAdd.ResourceName);

            resultChildResourceClaim1.Create.ShouldBe(resourceToAdd.Create);
            resultChildResourceClaim1.Read.ShouldBe(resourceToAdd.Read);
            resultChildResourceClaim1.Update.ShouldBe(resourceToAdd.Update);
            resultChildResourceClaim1.Delete.ShouldBe(resourceToAdd.Delete);
        }