public void OnSpecificationUpdate_WhenSpecificationIdIsEmpty_ThenErrorLogged()
        {
            // Arrange

            string json = JsonConvert.SerializeObject(new SpecificationVersionComparisonModel()
            {
                Current  = new Models.Messages.SpecificationVersion(),
                Previous = new Models.Messages.SpecificationVersion(),
            });

            Message message = new Message(Encoding.UTF8.GetBytes(json));

            ILogger logger = CreateLogger();

            FundingStreamPermissionService service = CreateService(logger: logger);

            // Act
            Func <Task> func = async() =>
            {
                await service.Process(message);
            };

            // Assert
            func
            .Should()
            .Throw <InvalidModelException>()
            .Which
            .Message
            .Should()
            .Be("The model for type: SpecificationVersionComparisonModel is invalid with the following errors Null or invalid specificationId on model");

            logger
            .Received(1)
            .Error(Arg.Is("A null specificationId was provided to users in model"));
        }
        public async Task GetFundingStreamPermissionsForUser_WhenNoResultsFound_ThenEmptyListReturned()
        {
            // Arrange
            List <FundingStreamPermission> repositoryResult = new List <FundingStreamPermission>();

            IUserRepository userRepository = CreateUserRepository();

            userRepository
            .GetFundingStreamPermissions(Arg.Is(UserId))
            .Returns(repositoryResult);

            FundingStreamPermissionService service = CreateService(userRepository);

            // Act
            IActionResult result = await service.GetFundingStreamPermissionsForUser(UserId);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeEquivalentTo <IEnumerable <FundingStreamPermissionCurrent> >(Enumerable.Empty <FundingStreamPermissionCurrent>());

            await userRepository
            .Received(1)
            .GetFundingStreamPermissions(Arg.Is(UserId));
        }
        public void OnSpecificationUpdate_WhenMessageBodyIsNull_ThenErrorLogged()
        {
            // Arrange
            Message message = new Message(null);

            ILogger logger = CreateLogger();

            FundingStreamPermissionService service = CreateService(logger: logger);

            // Act
            Func <Task> func = async() =>
            {
                await service.Process(message);
            };

            // Assert
            func
            .Should()
            .Throw <InvalidModelException>()
            .Which
            .Message
            .Should()
            .Be("The model for type: SpecificationVersionComparisonModel is invalid with the following errors Null or invalid model provided");

            logger
            .Received(1)
            .Error(Arg.Is("A null versionComparison was provided to users"));
        }
Пример #4
0
        public async Task GetEffectivePermissionsForUser_WhenCachedEffectivePermissionFound_ThenOkResultReturned()
        {
            // Arrange
            ICacheProvider cacheProvider = CreateCacheProvider();
            EffectiveSpecificationPermission cachedPermission = new EffectiveSpecificationPermission()
            {
                UserId                 = UserId,
                SpecificationId        = SpecificationId,
                CanApproveFunding      = true,
                CanCreateSpecification = true,
                CanMapDatasets         = false,
            };

            cacheProvider
            .GetHashValue <EffectiveSpecificationPermission>(Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"), Arg.Is(SpecificationId))
            .Returns(cachedPermission);

            FundingStreamPermissionService service = CreateService(cacheProvider: cacheProvider);


            // Act
            IActionResult result = await service.GetEffectivePermissionsForUser(UserId, SpecificationId, null);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeEquivalentTo(new EffectiveSpecificationPermission()
            {
                UserId                     = UserId,
                SpecificationId            = SpecificationId,
                CanApproveFunding          = true,
                CanCreateSpecification     = true,
                CanMapDatasets             = false,
                CanChooseFunding           = false,
                CanEditCalculations        = false,
                CanEditSpecification       = false,
                CanPublishFunding          = false,
                CanAdministerFundingStream = false,
                CanApproveSpecification    = false,
                CanCreateQaTests           = false,
                CanEditQaTests             = false,
                CanRefreshFunding          = false,
                CanDeleteSpecification     = false
            });

            await cacheProvider
            .Received(1)
            .GetHashValue <EffectiveSpecificationPermission>(Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"), Arg.Is(SpecificationId));

            await cacheProvider
            .Received(0)
            .SetHashValue(
                Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"),
                Arg.Is(SpecificationId),
                Arg.Any <EffectiveSpecificationPermission>());
        }
        public async Task UpdatePermissionForUser_WhenUserIdIsEmpty_ThenBadRequestReturned()
        {
            // Arrange
            FundingStreamPermissionService service = CreateService();

            // Act
            IActionResult result = await service.UpdatePermissionForUser(null, FundingStreamId, null, null);

            // Assert
            result
            .Should()
            .BeOfType <BadRequestObjectResult>()
            .Which
            .Value
            .Should()
            .Be("userId is empty or null");
        }
Пример #6
0
        public async Task GetEffectivePermissionsForUser_WhenNotFoundInCacheButSpecificationNotFound_ThenPreconditionFailedResultReturned()
        {
            // Arrange
            ICacheProvider cacheProvider = CreateCacheProvider();
            EffectiveSpecificationPermission cachedPermission = null;

            cacheProvider
            .GetHashValue <EffectiveSpecificationPermission>(Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"), Arg.Is(SpecificationId))
            .Returns(cachedPermission);

            ISpecificationRepository specificationRepository = CreateSpecificationRepository();

            SpecificationSummary specificationSummary = null;

            specificationRepository
            .GetSpecificationSummaryById(Arg.Is(SpecificationId))
            .Returns(specificationSummary);

            FundingStreamPermissionService service = CreateService(
                specificationRepository: specificationRepository,
                cacheProvider: cacheProvider);


            // Act
            IActionResult result = await service.GetEffectivePermissionsForUser(UserId, SpecificationId, null);

            // Assert
            result
            .Should()
            .BeOfType <PreconditionFailedResult>()
            .Which
            .Value
            .Should()
            .Be("Specification not found");

            await cacheProvider
            .Received(1)
            .GetHashValue <EffectiveSpecificationPermission>(Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"), Arg.Is(SpecificationId));

            await cacheProvider
            .Received(0)
            .SetHashValue(
                Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"),
                Arg.Is(SpecificationId),
                Arg.Any <EffectiveSpecificationPermission>());
        }
Пример #7
0
        public async Task GetEffectivePermissionsForUser_WhenSpecificationIsIsEmpty_ThenBadRequestReturned()
        {
            // Arrange
            FundingStreamPermissionService service = CreateService();


            // Act
            IActionResult result = await service.GetEffectivePermissionsForUser(UserId, null, null);

            // Assert
            result
            .Should()
            .BeOfType <BadRequestObjectResult>()
            .Which
            .Value
            .Should()
            .Be("specificationId is empty or null");
        }
        public async Task GetFundingStreamPermissionsForUser_WhenUserIdIsNull_ThenBadRequestReturned()
        {
            // Arrange
            string userId = null;

            FundingStreamPermissionService service = CreateService();

            // Act
            IActionResult result = await service.GetFundingStreamPermissionsForUser(userId);

            // Assert
            result
            .Should()
            .BeOfType <BadRequestObjectResult>()
            .Which
            .Value
            .Should()
            .Be("userId is null or empty");
        }
        public async Task UpdatePermissionForUser_WhenUserNotFound_ThenPreconditionFailedReturned()
        {
            // Arrange
            IUserRepository userRepository = CreateUserRepository();
            User            user           = null;

            userRepository
            .GetUserById(Arg.Is(UserId))
            .Returns(user);

            FundingStreamPermissionService service = CreateService(userRepository);

            // Act
            IActionResult result = await service.UpdatePermissionForUser(UserId, FundingStreamId, null, null);

            // Assert
            result
            .Should()
            .BeOfType <PreconditionFailedResult>()
            .Which
            .Value
            .Should()
            .Be("userId not found");
        }
        public async Task UpdatePermissionForUser_WhenSavingPermissionsFails_ThenInternalServerErrorReturned()
        {
            // Arrange
            IUserRepository userRepository = CreateUserRepository();
            User            user           = new User()
            {
                UserId = UserId
            };

            userRepository
            .GetUserById(Arg.Is(UserId))
            .Returns(user);

            FundingStreamPermission existingPermission = new FundingStreamPermission()
            {
                UserId                       = UserId,
                FundingStreamId              = FundingStreamId,
                CanApproveFunding            = true,
                CanChooseFunding             = false,
                CanCreateSpecification       = false,
                CanEditCalculations          = false,
                CanEditSpecification         = false,
                CanMapDatasets               = false,
                CanReleaseFunding            = false,
                CanCreateProfilePattern      = false,
                CanEditProfilePattern        = false,
                CanDeleteProfilePattern      = false,
                CanAssignProfilePattern      = false,
                CanApplyCustomProfilePattern = false,
                CanApproveCalculations       = true,
                CanApproveAnyCalculations    = false,
                CanApproveAllCalculations    = false
            };

            userRepository
            .GetFundingStreamPermission(Arg.Is(UserId), Arg.Is(FundingStreamId))
            .Returns(existingPermission);

            userRepository
            .UpdateFundingStreamPermission(Arg.Any <FundingStreamPermission>())
            .Returns(HttpStatusCode.InternalServerError);

            ICacheProvider cacheProvider = CreateCacheProvider();

            FundingStreamPermissionUpdateModel updateModel = new FundingStreamPermissionUpdateModel()
            {
                CanApproveFunding            = true,
                CanChooseFunding             = true,
                CanCreateSpecification       = false,
                CanEditCalculations          = false,
                CanEditSpecification         = false,
                CanMapDatasets               = false,
                CanReleaseFunding            = false,
                CanCreateProfilePattern      = false,
                CanEditProfilePattern        = false,
                CanDeleteProfilePattern      = false,
                CanAssignProfilePattern      = false,
                CanApplyCustomProfilePattern = false,
                CanApproveCalculations       = false,
                CanApproveAnyCalculations    = true,
                CanApproveAllCalculations    = true
            };

            FundingStreamPermissionService service = CreateService(userRepository, cacheProvider: cacheProvider);

            // Act
            IActionResult result = await service.UpdatePermissionForUser(UserId, FundingStreamId, updateModel, null);

            // Assert
            result
            .Should()
            .BeOfType <InternalServerErrorResult>()
            .Which
            .Value
            .Should()
            .Be("Saving funding stream permission to repository returned 'InternalServerError'");

            await userRepository
            .Received(1)
            .UpdateFundingStreamPermission(Arg.Any <FundingStreamPermission>());

            await cacheProvider
            .Received(0)
            .DeleteHashSet(Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"));
        }
        public async Task OnSpecificationUpdate_WhenFundingStreamOnSpecificationHasNotChanged_ThenNoEffectiveUserPermissionsCleared()
        {
            // Arrange
            SpecificationVersionComparisonModel comparisonModel = new SpecificationVersionComparisonModel()
            {
                Current = new Models.Messages.SpecificationVersion()
                {
                    SpecificationId = SpecificationId,
                    FundingStreams  = new List <Reference>()
                    {
                        new Reference("fs1", "Funding Stream 1"),
                    }
                },
                Previous = new Models.Messages.SpecificationVersion()
                {
                    SpecificationId = SpecificationId,
                    FundingStreams  = new List <Reference>()
                    {
                        new Reference("fs1", "Funding Stream 1"),
                    }
                },
                Id = SpecificationId,
            };

            string json = JsonConvert.SerializeObject(comparisonModel);

            Message message = new Message(Encoding.UTF8.GetBytes(json));

            ICacheProvider cacheProvider = CreateCacheProvider();

            IUserRepository userRepository = CreateUserRepository();

            ILogger logger = CreateLogger();

            FundingStreamPermissionService service = CreateService(
                userRepository: userRepository,
                cacheProvider: cacheProvider,
                logger: logger);

            // Act
            await service.Process(message);

            // Assert
            await userRepository
            .Received(0)
            .GetUsersWithFundingStreamPermissions(Arg.Any <string>());

            await cacheProvider
            .Received(0)
            .DeleteHashKey <EffectiveSpecificationPermission>(Arg.Is <string>(c => c.StartsWith(CacheKeys.EffectivePermissions)), Arg.Is(SpecificationId));

            logger
            .Received(1)
            .Information(
                Arg.Is("No funding streams have changed for specification '{SpecificationId}' which require effective permission clearing."),
                Arg.Is(SpecificationId)
                );

            logger
            .Received(0)
            .Information(
                Arg.Is("Clearing effective permissions for userId '{UserId}' for specification '{SpecificationId}'"),
                Arg.Any <string>(),
                Arg.Is(SpecificationId));
        }
        public async Task OnSpecificationUpdate_WhenFundingStreamOnSpecificationIsAddedAndNoUserPermissionsExist_ThenNoEffectiveUserPermissionsCleared()
        {
            // Arrange
            SpecificationVersionComparisonModel comparisonModel = new SpecificationVersionComparisonModel()
            {
                Current = new Models.Messages.SpecificationVersion()
                {
                    SpecificationId = SpecificationId,
                    FundingStreams  = new List <Reference>()
                    {
                        new Reference("fs1", "Funding Stream 1"),
                        new Reference("fs2", "Funding Stream 2"),
                    }
                },
                Previous = new Models.Messages.SpecificationVersion()
                {
                    SpecificationId = SpecificationId,
                    FundingStreams  = new List <Reference>()
                    {
                        new Reference("fs1", "Funding Stream 1"),
                    }
                },
                Id = SpecificationId,
            };

            string json = JsonConvert.SerializeObject(comparisonModel);

            Message message = new Message(Encoding.UTF8.GetBytes(json));

            ICacheProvider cacheProvider = CreateCacheProvider();

            IUserRepository userRepository = CreateUserRepository();

            List <FundingStreamPermission> fs1Permissions = new List <FundingStreamPermission>();

            List <FundingStreamPermission> fs2Permissions = new List <FundingStreamPermission>();

            userRepository
            .GetUsersWithFundingStreamPermissions(Arg.Is("fs1"))
            .Returns(fs1Permissions);

            userRepository
            .GetUsersWithFundingStreamPermissions(Arg.Is("fs2"))
            .Returns(fs2Permissions);

            ILogger logger = CreateLogger();

            FundingStreamPermissionService service = CreateService(
                userRepository: userRepository,
                cacheProvider: cacheProvider,
                logger: logger);

            // Act
            await service.Process(message);

            // Assert
            await userRepository
            .Received(1)
            .GetUsersWithFundingStreamPermissions(Arg.Is("fs1"));

            await userRepository
            .Received(1)
            .GetUsersWithFundingStreamPermissions(Arg.Is("fs2"));

            await cacheProvider
            .Received(0)
            .DeleteHashKey <EffectiveSpecificationPermission>(Arg.Is <string>(c => c.StartsWith(CacheKeys.EffectivePermissions)), Arg.Is(SpecificationId));

            logger
            .Received(1)
            .Information(
                Arg.Is("Found changed funding streams for specification '{SpecificationId}' Previous: {PreviousFundingStreams} Current {CurrentFundingStreams}"),
                Arg.Is(SpecificationId),
                Arg.Is <IEnumerable <string> >(c => c.Count() == 1),
                Arg.Is <IEnumerable <string> >(c => c.Count() == 2)
                );

            logger
            .Received(0)
            .Information(
                Arg.Is("Clearing effective permissions for userId '{UserId}' for specification '{SpecificationId}'"),
                Arg.Any <string>(),
                Arg.Is(SpecificationId));
        }
Пример #13
0
        public async Task UpdatePermissionForUser_WhenPermissionsAreSetOnAFundingStreamAndNoneHaveBeenSetBefore_ThenPermissionsSaved()
        {
            // Arrange
            IUserRepository userRepository = CreateUserRepository();
            User            user           = new User()
            {
                UserId = UserId
            };

            userRepository
            .GetUserById(Arg.Is(UserId))
            .Returns(user);

            FundingStreamPermission existingPermission = null;

            userRepository
            .GetFundingStreamPermission(Arg.Is(UserId), Arg.Is(FundingStreamId))
            .Returns(existingPermission);

            userRepository
            .UpdateFundingStreamPermission(Arg.Any <FundingStreamPermission>())
            .Returns(HttpStatusCode.Created);

            ICacheProvider cacheProvider = CreateCacheProvider();

            FundingStreamPermissionUpdateModel updateModel = new FundingStreamPermissionUpdateModel()
            {
                CanApproveFunding      = true,
                CanChooseFunding       = false,
                CanCreateSpecification = false,
                CanEditCalculations    = false,
                CanEditSpecification   = false,
                CanMapDatasets         = false,
                CanPublishFunding      = false,
            };

            string json = JsonConvert.SerializeObject(updateModel);

            byte[]       byteArray = Encoding.UTF8.GetBytes(json);
            MemoryStream stream    = new MemoryStream(byteArray);

            HttpRequest request = Substitute.For <HttpRequest>();

            request
            .Body
            .Returns(stream);

            IVersionRepository <FundingStreamPermissionVersion> versionRepository = CreateFundingStreamPermissionRepository();

            versionRepository
            .GetNextVersionNumber(Arg.Any <FundingStreamPermissionVersion>(), 0, Arg.Is(UserId))
            .Returns(1);

            FundingStreamPermissionService service = CreateService(
                userRepository,
                cacheProvider: cacheProvider,
                fundingStreamPermissionVersionRepository: versionRepository);

            // Act
            IActionResult result = await service.UpdatePermissionForUser(UserId, FundingStreamId, request);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeEquivalentTo <FundingStreamPermissionCurrent>(new FundingStreamPermissionCurrent()
            {
                UserId                 = UserId,
                FundingStreamId        = FundingStreamId,
                CanApproveFunding      = true,
                CanChooseFunding       = false,
                CanCreateSpecification = false,
                CanEditCalculations    = false,
                CanEditSpecification   = false,
                CanMapDatasets         = false,
                CanPublishFunding      = false,
            });

            await userRepository
            .Received(1)
            .UpdateFundingStreamPermission(Arg.Is <FundingStreamPermission>(p =>
                                                                            p.FundingStreamId == FundingStreamId &&
                                                                            p.UserId == UserId &&
                                                                            p.CanApproveFunding &&
                                                                            !p.CanChooseFunding &&
                                                                            !p.CanCreateSpecification &&
                                                                            !p.CanEditCalculations &&
                                                                            !p.CanEditSpecification &&
                                                                            !p.CanMapDatasets &&
                                                                            !p.CanPublishFunding
                                                                            ));

            await cacheProvider
            .Received(1)
            .DeleteHashSet(Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"));

            await versionRepository
            .Received(1)
            .GetNextVersionNumber(Arg.Is <FundingStreamPermissionVersion>(v => v.EntityId == $"{UserId}_{FundingStreamId}"), partitionKeyId: Arg.Is(UserId));

            await versionRepository
            .Received(1)
            .SaveVersion(Arg.Is <FundingStreamPermissionVersion>(v =>
                                                                 v.EntityId == $"{UserId}_{FundingStreamId}" &&
                                                                 v.Version == 1
                                                                 ), Arg.Is(UserId));
        }
        public async Task GetFundingStreamPermissionsForUser_WhenResultsFound_ThenFundingStreamPermissionsReturned()
        {
            // Arrange
            List <FundingStreamPermission> repositoryResult = new List <FundingStreamPermission>()
            {
                new FundingStreamPermission()
                {
                    CanApproveFunding            = true,
                    CanChooseFunding             = true,
                    CanCreateSpecification       = false,
                    CanEditCalculations          = false,
                    CanEditSpecification         = false,
                    CanMapDatasets               = false,
                    CanReleaseFunding            = true,
                    CanCreateProfilePattern      = true,
                    CanEditProfilePattern        = true,
                    CanDeleteProfilePattern      = true,
                    CanAssignProfilePattern      = false,
                    CanApplyCustomProfilePattern = false,
                    CanApproveCalculations       = true,
                    CanApproveAnyCalculations    = false,
                    CanApproveAllCalculations    = false,
                    FundingStreamId              = FundingStreamId,
                    UserId = UserId
                },
                new FundingStreamPermission()
                {
                    CanApproveFunding            = true,
                    CanChooseFunding             = true,
                    CanCreateSpecification       = true,
                    CanEditCalculations          = true,
                    CanEditSpecification         = false,
                    CanMapDatasets               = false,
                    CanReleaseFunding            = true,
                    CanCreateProfilePattern      = false,
                    CanEditProfilePattern        = false,
                    CanDeleteProfilePattern      = false,
                    CanAssignProfilePattern      = true,
                    CanApplyCustomProfilePattern = false,
                    CanApproveCalculations       = false,
                    CanApproveAnyCalculations    = true,
                    CanApproveAllCalculations    = true,
                    FundingStreamId              = "fs2",
                    UserId = UserId
                }
            };

            IUserRepository userRepository = CreateUserRepository();

            userRepository
            .GetFundingStreamPermissions(Arg.Is(UserId))
            .Returns(repositoryResult);

            FundingStreamPermissionService service = CreateService(userRepository);

            // Act
            IActionResult result = await service.GetFundingStreamPermissionsForUser(UserId);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeEquivalentTo <IEnumerable <FundingStreamPermissionCurrent> >(new List <FundingStreamPermissionCurrent>()
            {
                new FundingStreamPermissionCurrent()
                {
                    CanApproveFunding            = true,
                    CanChooseFunding             = true,
                    CanCreateSpecification       = false,
                    CanEditCalculations          = false,
                    CanEditSpecification         = false,
                    CanMapDatasets               = false,
                    CanReleaseFunding            = true,
                    CanCreateProfilePattern      = true,
                    CanEditProfilePattern        = true,
                    CanDeleteProfilePattern      = true,
                    CanAssignProfilePattern      = false,
                    CanApplyCustomProfilePattern = false,
                    CanApproveCalculations       = true,
                    CanApproveAnyCalculations    = false,
                    CanApproveAllCalculations    = false,
                    FundingStreamId              = FundingStreamId,
                    UserId = UserId
                },
                new FundingStreamPermissionCurrent()
                {
                    CanApproveFunding            = true,
                    CanChooseFunding             = true,
                    CanCreateSpecification       = true,
                    CanEditCalculations          = true,
                    CanEditSpecification         = false,
                    CanMapDatasets               = false,
                    CanReleaseFunding            = true,
                    CanCreateProfilePattern      = false,
                    CanEditProfilePattern        = false,
                    CanDeleteProfilePattern      = false,
                    CanAssignProfilePattern      = true,
                    CanApplyCustomProfilePattern = false,
                    CanApproveCalculations       = false,
                    CanApproveAnyCalculations    = true,
                    CanApproveAllCalculations    = true,
                    FundingStreamId              = "fs2",
                    UserId = UserId
                }
            });

            await userRepository
            .Received(1)
            .GetFundingStreamPermissions(Arg.Is(UserId));
        }
Пример #15
0
        public async Task GetEffectivePermissionsForUser_WhenNotFoundInCacheResultsAreQueriedWithMultipleFundingStreamAndUserHasPermissionsButNotAcrossAllFundingStreams_ThenOkResultReturnedWithPermissionsThatOnlyAllFundingStreamsHave()
        {
            // Arrange
            IUserRepository          userRepository          = CreateUserRepository();
            ISpecificationsApiClient specificationsApiClient = CreateSpecificationsApiClient();
            ICacheProvider           cacheProvider           = CreateCacheProvider();
            IMapper mapper = CreateMappingConfiguration();

            EffectiveSpecificationPermission cachedPermission = null;

            cacheProvider
            .GetHashValue <EffectiveSpecificationPermission>(Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"), Arg.Is(SpecificationId))
            .Returns(cachedPermission);

            SpecModel.SpecificationSummary specificationSummary = new SpecModel.SpecificationSummary()
            {
                Id             = SpecificationId,
                FundingStreams = new List <Reference>()
                {
                    new Reference("fs1", "Funding Stream 1"),
                    new Reference("fs2", "Funding Stream 2"),
                }
            };

            specificationsApiClient
            .GetSpecificationSummaryById(Arg.Is(SpecificationId))
            .Returns(new ApiResponse <SpecModel.SpecificationSummary>(HttpStatusCode.OK, specificationSummary));

            FundingStreamPermission fs1Permission = new FundingStreamPermission()
            {
                UserId                       = UserId,
                FundingStreamId              = "fs1",
                CanChooseFunding             = false,
                CanCreateSpecification       = true,
                CanApproveFunding            = false,
                CanEditCalculations          = false,
                CanEditSpecification         = false,
                CanMapDatasets               = false,
                CanReleaseFunding            = false,
                CanAdministerFundingStream   = false,
                CanApproveSpecification      = false,
                CanCreateQaTests             = false,
                CanEditQaTests               = false,
                CanRefreshFunding            = false,
                CanCreateProfilePattern      = false,
                CanEditProfilePattern        = false,
                CanDeleteProfilePattern      = false,
                CanAssignProfilePattern      = false,
                CanApplyCustomProfilePattern = false,
                CanApproveCalculations       = true,
                CanApproveAnyCalculations    = false,
                CanApproveAllCalculations    = false
            };

            FundingStreamPermission fs2Permission = new FundingStreamPermission()
            {
                UserId                       = UserId,
                FundingStreamId              = "fs1",
                CanChooseFunding             = false,
                CanCreateSpecification       = true,
                CanApproveFunding            = true,
                CanEditCalculations          = false,
                CanEditSpecification         = false,
                CanMapDatasets               = false,
                CanReleaseFunding            = false,
                CanAdministerFundingStream   = false,
                CanApproveSpecification      = false,
                CanCreateQaTests             = false,
                CanEditQaTests               = false,
                CanRefreshFunding            = false,
                CanCreateProfilePattern      = false,
                CanEditProfilePattern        = false,
                CanDeleteProfilePattern      = false,
                CanAssignProfilePattern      = false,
                CanApplyCustomProfilePattern = false,
                CanApproveCalculations       = true,
                CanApproveAnyCalculations    = false,
                CanApproveTemplates          = false,
                CanCreateTemplates           = false,
                CanDeleteCalculations        = false,
                CanDeleteQaTests             = false,
                CanDeleteSpecification       = false,
                CanDeleteTemplates           = false,
                CanEditTemplates             = false,
                CanApproveAllCalculations    = false
            };

            userRepository
            .GetFundingStreamPermission(Arg.Is(UserId), Arg.Is("fs1"))
            .Returns(fs1Permission);

            userRepository
            .GetFundingStreamPermission(Arg.Is(UserId), Arg.Is("fs2"))
            .Returns(fs2Permission);

            FundingStreamPermissionService service = CreateService(userRepository, specificationsApiClient, cacheProvider: cacheProvider, mapper: mapper);


            // Act
            IActionResult result = await service.GetEffectivePermissionsForUser(UserId, SpecificationId);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeEquivalentTo(new EffectiveSpecificationPermission()
            {
                UserId                       = UserId,
                SpecificationId              = SpecificationId,
                CanApproveFunding            = false,
                CanCreateSpecification       = true,
                CanMapDatasets               = false,
                CanChooseFunding             = false,
                CanEditCalculations          = false,
                CanEditSpecification         = false,
                CanReleaseFunding            = false,
                CanAdministerFundingStream   = false,
                CanApproveSpecification      = false,
                CanCreateQaTests             = false,
                CanEditQaTests               = false,
                CanRefreshFunding            = false,
                CanApproveAnyCalculations    = false,
                CanAssignProfilePattern      = false,
                CanDeleteSpecification       = false,
                CanDeleteQaTests             = false,
                CanDeleteCalculations        = false,
                CanApplyCustomProfilePattern = false,
                CanApproveCalculations       = true,
                CanApproveAllCalculations    = false
            });

            await cacheProvider
            .Received(1)
            .SetHashValue(
                Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"),
                Arg.Is(SpecificationId),
                Arg.Is <EffectiveSpecificationPermission>(p =>
                                                          !p.CanApproveFunding &&
                                                          !p.CanChooseFunding &&
                                                          p.CanCreateSpecification &&
                                                          !p.CanEditCalculations &&
                                                          !p.CanEditSpecification &&
                                                          !p.CanMapDatasets &&
                                                          !p.CanReleaseFunding &&
                                                          !p.CanAdministerFundingStream &&
                                                          !p.CanApproveSpecification &&
                                                          !p.CanCreateQaTests &&
                                                          !p.CanEditQaTests &&
                                                          !p.CanRefreshFunding &&
                                                          p.SpecificationId == SpecificationId &&
                                                          p.UserId == UserId
                                                          ));
        }
Пример #16
0
        public async Task GetEffectivePermissionsForUser_WhenNotFoundInCacheResultsAreQueriedWithMultipleFundingStreamAndNoPermissionsAreInRepository_ThenOkResultReturnedWithNoPermissions()
        {
            // Arrange
            IUserRepository          userRepository          = CreateUserRepository();
            ISpecificationRepository specificationRepository = CreateSpecificationRepository();
            ICacheProvider           cacheProvider           = CreateCacheProvider();
            IMapper mapper = CreateMappingConfiguration();

            EffectiveSpecificationPermission cachedPermission = null;

            cacheProvider
            .GetHashValue <EffectiveSpecificationPermission>(Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"), Arg.Is(SpecificationId))
            .Returns(cachedPermission);

            SpecificationSummary specificationSummary = new SpecificationSummary()
            {
                Id             = SpecificationId,
                FundingStreams = new List <Reference>()
                {
                    new Reference("fs1", "Funding Stream 1"),
                    new Reference("fs2", "Funding Stream 2")
                }
            };

            specificationRepository
            .GetSpecificationSummaryById(Arg.Is(SpecificationId))
            .Returns(specificationSummary);

            FundingStreamPermission fs1Permission = null;

            userRepository
            .GetFundingStreamPermission(Arg.Is(UserId), Arg.Is("fs1"))
            .Returns(fs1Permission);

            FundingStreamPermission fs2Permission = null;

            userRepository
            .GetFundingStreamPermission(Arg.Is(UserId), Arg.Is("fs2"))
            .Returns(fs2Permission);

            FundingStreamPermissionService service = CreateService(userRepository, specificationRepository, cacheProvider: cacheProvider, mapper: mapper);


            // Act
            IActionResult result = await service.GetEffectivePermissionsForUser(UserId, SpecificationId, null);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeEquivalentTo(new EffectiveSpecificationPermission()
            {
                UserId                     = UserId,
                SpecificationId            = SpecificationId,
                CanApproveFunding          = false,
                CanCreateSpecification     = false,
                CanMapDatasets             = false,
                CanChooseFunding           = false,
                CanEditCalculations        = false,
                CanEditSpecification       = false,
                CanPublishFunding          = false,
                CanAdministerFundingStream = false,
                CanApproveSpecification    = false,
                CanCreateQaTests           = false,
                CanEditQaTests             = false,
                CanRefreshFunding          = false,
            });

            await cacheProvider
            .Received(1)
            .SetHashValue(
                Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"),
                Arg.Is(SpecificationId),
                Arg.Is <EffectiveSpecificationPermission>(p =>
                                                          !p.CanApproveFunding &&
                                                          !p.CanChooseFunding &&
                                                          !p.CanCreateSpecification &&
                                                          !p.CanEditCalculations &&
                                                          !p.CanEditSpecification &&
                                                          !p.CanMapDatasets &&
                                                          !p.CanPublishFunding &&
                                                          !p.CanAdministerFundingStream &&
                                                          !p.CanApproveSpecification &&
                                                          !p.CanCreateQaTests &&
                                                          !p.CanEditQaTests &&
                                                          !p.CanRefreshFunding &&
                                                          p.SpecificationId == SpecificationId &&
                                                          p.UserId == UserId
                                                          ));
        }
        public async Task UpdatePermissionForUser_WhenPermissionsAreSetOnAFundingStreamAndNoneHaveBeenSetBefore_ThenPermissionsSaved()
        {
            // Arrange
            IUserRepository userRepository = CreateUserRepository();
            User            user           = new User()
            {
                UserId = UserId
            };

            userRepository
            .GetUserById(Arg.Is(UserId))
            .Returns(user);

            FundingStreamPermission existingPermission = null;

            userRepository
            .GetFundingStreamPermission(Arg.Is(UserId), Arg.Is(FundingStreamId))
            .Returns(existingPermission);

            userRepository
            .UpdateFundingStreamPermission(Arg.Any <FundingStreamPermission>())
            .Returns(HttpStatusCode.Created);

            ICacheProvider cacheProvider = CreateCacheProvider();

            FundingStreamPermissionUpdateModel updateModel = new FundingStreamPermissionUpdateModel()
            {
                CanApproveFunding            = true,
                CanChooseFunding             = false,
                CanCreateSpecification       = false,
                CanEditCalculations          = false,
                CanEditSpecification         = false,
                CanMapDatasets               = false,
                CanReleaseFunding            = false,
                CanCreateTemplates           = true,
                CanEditTemplates             = true,
                CanDeleteTemplates           = true,
                CanApproveTemplates          = true,
                CanCreateProfilePattern      = true,
                CanEditProfilePattern        = true,
                CanDeleteProfilePattern      = true,
                CanAssignProfilePattern      = false,
                CanApplyCustomProfilePattern = false,
                CanApproveCalculations       = true,
                CanApproveAnyCalculations    = false,
                CanApproveAllCalculations    = false
            };

            IVersionRepository <FundingStreamPermissionVersion> versionRepository = CreateFundingStreamPermissionRepository();

            versionRepository
            .GetNextVersionNumber(Arg.Any <FundingStreamPermissionVersion>(), 0, Arg.Is(UserId))
            .Returns(1);

            FundingStreamPermissionService service = CreateService(
                userRepository,
                cacheProvider: cacheProvider,
                fundingStreamPermissionVersionRepository: versionRepository);

            // Act
            IActionResult result = await service.UpdatePermissionForUser(UserId, FundingStreamId, updateModel, null);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeEquivalentTo <FundingStreamPermissionCurrent>(new FundingStreamPermissionCurrent()
            {
                UserId                       = UserId,
                FundingStreamId              = FundingStreamId,
                CanApproveFunding            = true,
                CanChooseFunding             = false,
                CanCreateSpecification       = false,
                CanEditCalculations          = false,
                CanEditSpecification         = false,
                CanMapDatasets               = false,
                CanReleaseFunding            = false,
                CanCreateTemplates           = true,
                CanEditTemplates             = true,
                CanDeleteTemplates           = true,
                CanApproveTemplates          = true,
                CanCreateProfilePattern      = true,
                CanEditProfilePattern        = true,
                CanDeleteProfilePattern      = true,
                CanAssignProfilePattern      = false,
                CanApplyCustomProfilePattern = false,
                CanApproveCalculations       = true,
                CanApproveAnyCalculations    = false,
                CanApproveAllCalculations    = false,
            });

            await userRepository
            .Received(1)
            .UpdateFundingStreamPermission(Arg.Is <FundingStreamPermission>(p =>
                                                                            p.FundingStreamId == FundingStreamId &&
                                                                            p.UserId == UserId &&
                                                                            p.CanApproveFunding &&
                                                                            !p.CanChooseFunding &&
                                                                            !p.CanCreateSpecification &&
                                                                            !p.CanEditCalculations &&
                                                                            !p.CanEditSpecification &&
                                                                            !p.CanMapDatasets &&
                                                                            !p.CanReleaseFunding &&
                                                                            p.CanCreateTemplates &&
                                                                            p.CanEditTemplates &&
                                                                            p.CanDeleteTemplates &&
                                                                            p.CanApproveTemplates &&
                                                                            p.CanCreateProfilePattern &&
                                                                            p.CanEditProfilePattern &&
                                                                            p.CanDeleteProfilePattern &&
                                                                            !p.CanAssignProfilePattern &&
                                                                            !p.CanApplyCustomProfilePattern &&
                                                                            p.CanApproveCalculations &&
                                                                            !p.CanApproveAnyCalculations &&
                                                                            !p.CanApproveAllCalculations
                                                                            ));

            await cacheProvider
            .Received(1)
            .DeleteHashSet(Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"));

            await versionRepository
            .Received(1)
            .GetNextVersionNumber(Arg.Is <FundingStreamPermissionVersion>(v => v.EntityId == $"{UserId}_{FundingStreamId}"), partitionKeyId: Arg.Is(UserId));

            await versionRepository
            .Received(1)
            .SaveVersion(Arg.Is <FundingStreamPermissionVersion>(v =>
                                                                 v.EntityId == $"{UserId}_{FundingStreamId}" &&
                                                                 v.Version == 1
                                                                 ), Arg.Is(UserId));
        }
Пример #18
0
        public async Task UpdatePermissionForUser_WhenSavingPermissionsFails_ThenInternalServerErrorReturned()
        {
            // Arrange
            IUserRepository userRepository = CreateUserRepository();
            User            user           = new User()
            {
                UserId = UserId
            };

            userRepository
            .GetUserById(Arg.Is(UserId))
            .Returns(user);

            FundingStreamPermission existingPermission = new FundingStreamPermission()
            {
                UserId                 = UserId,
                FundingStreamId        = FundingStreamId,
                CanApproveFunding      = true,
                CanChooseFunding       = false,
                CanCreateSpecification = false,
                CanEditCalculations    = false,
                CanEditSpecification   = false,
                CanMapDatasets         = false,
                CanPublishFunding      = false,
            };

            userRepository
            .GetFundingStreamPermission(Arg.Is(UserId), Arg.Is(FundingStreamId))
            .Returns(existingPermission);

            userRepository
            .UpdateFundingStreamPermission(Arg.Any <FundingStreamPermission>())
            .Returns(HttpStatusCode.InternalServerError);

            ICacheProvider cacheProvider = CreateCacheProvider();

            FundingStreamPermissionUpdateModel updateModel = new FundingStreamPermissionUpdateModel()
            {
                CanApproveFunding      = true,
                CanChooseFunding       = true,
                CanCreateSpecification = false,
                CanEditCalculations    = false,
                CanEditSpecification   = false,
                CanMapDatasets         = false,
                CanPublishFunding      = false,
            };

            string json = JsonConvert.SerializeObject(updateModel);

            byte[]       byteArray = Encoding.UTF8.GetBytes(json);
            MemoryStream stream    = new MemoryStream(byteArray);

            HttpRequest request = Substitute.For <HttpRequest>();

            request
            .Body
            .Returns(stream);

            FundingStreamPermissionService service = CreateService(userRepository, cacheProvider: cacheProvider);

            // Act
            IActionResult result = await service.UpdatePermissionForUser(UserId, FundingStreamId, request);

            // Assert
            result
            .Should()
            .BeOfType <InternalServerErrorResult>()
            .Which
            .Value
            .Should()
            .Be("Saving funding stream permission to repository returned 'InternalServerError'");

            await userRepository
            .Received(1)
            .UpdateFundingStreamPermission(Arg.Any <FundingStreamPermission>());

            await cacheProvider
            .Received(0)
            .DeleteHashSet(Arg.Is($"{CacheKeys.EffectivePermissions}:{UserId}"));
        }