示例#1
0
        public async Task ThenFailsIfEmployerIdIsNotInUrl(
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var employerIdentifier = new EmployerIdentifier
            {
                AccountId    = "1234",
                EmployerName = "Test Employer",
                Role         = "Owner"
            };

            var employerAccounts = new Dictionary <string, EmployerIdentifier> {
                { "1234", employerIdentifier }
            };
            var claim           = new Claim(EmployerClaims.AccountsClaimsTypeIdentifier, JsonConvert.SerializeObject(employerAccounts));
            var claimsPrinciple = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { claim }) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);

            //Act
            await handler.HandleAsync(context);

            //Assert
            Assert.IsFalse(context.HasSucceeded);
        }
示例#2
0
        public async Task ThenSucceedsIfEmployerIsAuthorised(
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var employerIdentifier = new EmployerIdentifier
            {
                AccountId    = "1234",
                EmployerName = "Test Employer",
                Role         = "Owner"
            };

            var employerAccounts = new Dictionary <string, EmployerIdentifier> {
                { "1234", employerIdentifier }
            };
            var claim           = new Claim(EmployerClaims.AccountsClaimsTypeIdentifier, JsonConvert.SerializeObject(employerAccounts));
            var claimsPrinciple = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { claim }) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);
            var filter  = context.Resource as AuthorizationFilterContext;

            filter.RouteData.Values.Add(RouteValues.EmployerAccountId, 1234);

            //Act
            await handler.HandleAsync(context);

            //Assert
            Assert.IsTrue(context.HasSucceeded);
        }
示例#3
0
        public async Task ThenFailsIfEmployerAccountIdNotFoundEvenAfterAccountIdRefresh(
            [Frozen] Mock <IEmployerAccountService> employerAccountService,
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var employerAccounts     = new Dictionary <string, EmployerIdentifier>();
            var employerAccountClaim = new Claim(EmployerClaims.AccountsClaimsTypeIdentifier, JsonConvert.SerializeObject(employerAccounts));

            var userId    = Guid.NewGuid().ToString();
            var userClaim = new Claim(EmployerClaims.IdamsUserIdClaimTypeIdentifier, userId);

            var claimsPrinciple = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { employerAccountClaim, userClaim }) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);
            var filter  = context.Resource as AuthorizationFilterContext;

            filter.RouteData.Values.Add(RouteValues.EmployerAccountId, 1234);

            employerAccountService.Setup(s => s.GetClaim(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(employerAccountClaim);

            //Act
            await handler.HandleAsync(context);

            //Assert
            Assert.IsFalse(context.HasSucceeded);
        }
示例#4
0
        public void ThenReturnsFalseIfEmployerIdIsNotInUrl(
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var employerIdentifier = new EmployerIdentifier
            {
                AccountId    = "1234",
                EmployerName = "Test Employer",
                Role         = "Owner"
            };

            var employerAccounts = new Dictionary <string, EmployerIdentifier> {
                { "1234", employerIdentifier }
            };
            var claim           = new Claim(EmployerClaims.AccountsClaimsTypeIdentifier, JsonConvert.SerializeObject(employerAccounts));
            var claimsPrinciple = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { claim }) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);

            //Act
            var result = handler.IsEmployerAuthorised(context, false);

            //Assert
            Assert.IsFalse(result);
        }
示例#5
0
        public void ThenReturnsTrueIfEmployerIsAuthorised(
            [Frozen] Mock <IEmployerAccountService> employerAccountService,
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var employerIdentifier = new EmployerIdentifier
            {
                AccountId    = "1234",
                EmployerName = "Test Employer",
                Role         = "Owner"
            };

            var employerAccounts = new Dictionary <string, EmployerIdentifier> {
                { "1234", employerIdentifier }
            };
            var claim           = new Claim(EmployerClaims.AccountsClaimsTypeIdentifier, JsonConvert.SerializeObject(employerAccounts));
            var claimsPrinciple = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { claim }) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);
            var filter  = context.Resource as AuthorizationFilterContext;

            filter.RouteData.Values.Add(RouteValues.EmployerAccountId, 1234);

            //Act
            var result = handler.IsEmployerAuthorised(context, false);

            //Assert
            Assert.IsTrue(result);
        }
示例#6
0
        public void ThenFailsIfUserDoesNotHaveAValidRole(
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var employerIdentifier = new EmployerIdentifier
            {
                AccountId    = "1234",
                EmployerName = "Test Employer",
                Role         = "I'm not a role"
            };

            var employerAccounts = new Dictionary <string, EmployerIdentifier> {
                { "1234", employerIdentifier }
            };
            var claim           = new Claim(EmployerClaims.AccountsClaimsTypeIdentifier, JsonConvert.SerializeObject(employerAccounts));
            var claimsPrinciple = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { claim }) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);
            var filter  = context.Resource as AuthorizationFilterContext;

            filter.RouteData.Values.Add(RouteValues.EmployerAccountId, 1234);

            //Act
            var result = handler.IsEmployerAuthorised(context, false);

            //Assert
            Assert.IsFalse(result);
        }
示例#7
0
        public async Task ThenFailsIfEmployerClaimNotFound(
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var claimsPrinciple = new ClaimsPrincipal(new[] { new ClaimsIdentity(new Claim[0]) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);
            var filter  = context.Resource as AuthorizationFilterContext;

            filter.RouteData.Values.Add(RouteValues.EmployerAccountId, 1234);

            //Act
            await handler.HandleAsync(context);

            //Assert
            Assert.IsFalse(context.HasSucceeded);
        }
示例#8
0
        public void ThenReturnsFalseIfEmployerClaimNotFound(
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var claimsPrinciple = new ClaimsPrincipal(new[] { new ClaimsIdentity(new Claim[0]) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);
            var filter  = context.Resource as AuthorizationFilterContext;

            filter.RouteData.Values.Add(RouteValues.EmployerAccountId, 1234);

            //Act
            var result = handler.IsEmployerAuthorised(context, false);

            //Assert
            Assert.IsFalse(result);
        }
示例#9
0
        public void ThenSucceedsIfEmployerAccountIdIsFoundAfterAccountIdRefresh(
            [Frozen] Mock <IEmployerAccountService> employerAccountService,
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var employerAccounts     = new Dictionary <string, EmployerIdentifier>();
            var employerAccountClaim = new Claim(EmployerClaims.AccountsClaimsTypeIdentifier, JsonConvert.SerializeObject(employerAccounts));

            var userId    = Guid.NewGuid().ToString();
            var userClaim = new Claim(EmployerClaims.IdamsUserIdClaimTypeIdentifier, userId);

            var claimsPrinciple = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { employerAccountClaim, userClaim }) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);
            var filter  = context.Resource as AuthorizationFilterContext;

            filter.RouteData.Values.Add(RouteValues.EmployerAccountId, 1234);


            var employerIdentifier = new EmployerIdentifier
            {
                AccountId    = "1234",
                EmployerName = "Test Corp",
                Role         = "Owner"
            };
            var refreshedEmployerAccounts = new Dictionary <string, EmployerIdentifier> {
                { "1234", employerIdentifier }
            };
            var refreshedEmployerAccountClaim = new Claim(EmployerClaims.AccountsClaimsTypeIdentifier, JsonConvert.SerializeObject(refreshedEmployerAccounts));

            employerAccountService.Setup(s => s.GetClaim(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(refreshedEmployerAccountClaim);

            //Act
            var result = handler.IsEmployerAuthorised(context, false);

            //Assert
            Assert.IsTrue(result);
        }
示例#10
0
        public async Task ThenFailsIfEmployerAccountIdNotFoundAndUserIdNotFound(
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var employerAccounts = new Dictionary <string, EmployerIdentifier>();
            var claim            = new Claim(EmployerClaims.AccountsClaimsTypeIdentifier, JsonConvert.SerializeObject(employerAccounts));
            var claimsPrinciple  = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { claim }) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);
            var filter  = context.Resource as AuthorizationFilterContext;

            filter.RouteData.Values.Add(RouteValues.EmployerAccountId, 1234);

            //Act
            await handler.HandleAsync(context);

            //Assert
            Assert.IsFalse(context.HasSucceeded);
        }
示例#11
0
        public void ThenReturnsFalseIfEmployerAccountIdNotFoundAndUserIdNotFound(
            EmployerAccountRequirement requirement,
            AuthorizationFilterContext contextFilter,
            EmployerAccountAuthorizationHandler handler)
        {
            //Assign
            var employerAccounts = new Dictionary <string, EmployerIdentifier>();
            var claim            = new Claim(EmployerClaims.AccountsClaimsTypeIdentifier, JsonConvert.SerializeObject(employerAccounts));
            var claimsPrinciple  = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { claim }) });

            var context = new AuthorizationHandlerContext(new[] { requirement }, claimsPrinciple, contextFilter);
            var filter  = context.Resource as AuthorizationFilterContext;

            filter.RouteData.Values.Add(RouteValues.EmployerAccountId, 1234);

            //Act
            var result = handler.IsEmployerAuthorised(context, false);

            //Assert
            Assert.IsFalse(result);
        }