Beispiel #1
0
        public async Task CanEvaluateAsync_NormalCase()
        {
            var expectedResult = new List <PolicyResult>()
            {
                new PolicyResult()
                {
                    Name        = "None",
                    Description = "The default deny policy",
                    Result      = PolicyEvaluation.Deny,
                }
            };

            var microsoftGraphRepository = serviceProvider.GetRequiredService <IMicrosoftGraphRepository>();
            var thingRepository          = serviceProvider.GetRequiredService <IDataRepository <Thing> >();
            var authorizationRepository  = new AuthorizationRepository(microsoftGraphRepository, thingRepository);

            var mockClaimsPrincipal = new Mock <ClaimsPrincipal>();
            var mockClaimsIdentity  = new Mock <ClaimsIdentity>();

            mockClaimsIdentity.Setup(ci => ci.Name).Returns("*****@*****.**");
            mockClaimsPrincipal.Setup(cp => cp.Identity).Returns(mockClaimsIdentity.Object);

            var authenticationHeaderValue = new AuthenticationHeaderValue("test");
            var initialState = new InitialState <Group>()
            {
                ClaimsPrincipal = mockClaimsPrincipal.Object,
                Identifier      = "/api/Customer/1",
                Groups          = new List <Group>()
                {
                    new Group()
                    {
                        DisplayName = "Finance"
                    },
                }
            };

            var actualResult = await authorizationRepository.EvaluateAsync(authenticationHeaderValue, initialState);

            actualResult.Should().BeEquivalentTo(expectedResult);
        }
Beispiel #2
0
        public async Task CanEvaluateAsync_WithEnvironmentPolicies()
        {
            var expectedResult = new List <PolicyResult>()
            {
                new PolicyResult()
                {
                    Name   = "Test Environment Policy",
                    Result = PolicyEvaluation.Allow,
                }
            };

            var expectedUser = new User()
            {
                Id = Guid.NewGuid().ToString(),
            };

            mockMicrosoftGraphRepository.Setup(ur => ur.FetchMeAsync(It.IsAny <AuthenticationHeaderValue>()))
            .Returns(Task.FromResult(expectedUser));

            var expectedThing = new Thing()
            {
                Id = Guid.NewGuid(),
            };

            expectedThing.ThingPolicies.Add(new ThingPolicy()
            {
                Id         = Guid.NewGuid(),
                Name       = "Test Environment Policy",
                Expression = "userPrincipalName = \"[email protected]\"",
            });

            mockThingRepository.Setup(tr => tr.FindAsync(It.IsAny <Expression <Func <Thing, bool> > >(), It.IsAny <Expression <Func <Thing, object> >[]>()))
            .Returns(Task.FromResult(new List <Thing>()
            {
                expectedThing
            }.AsEnumerable()));

            var microsoftGraphRepository = serviceProvider.GetRequiredService <IMicrosoftGraphRepository>();
            var thingRepository          = serviceProvider.GetRequiredService <IDataRepository <Thing> >();
            var authorizationRepository  = new AuthorizationRepository(microsoftGraphRepository, thingRepository);

            var mockClaimsPrincipal = new Mock <ClaimsPrincipal>();
            var mockClaimsIdentity  = new Mock <ClaimsIdentity>();

            mockClaimsIdentity.Setup(ci => ci.Name).Returns("*****@*****.**");
            mockClaimsPrincipal.Setup(cp => cp.Identity).Returns(mockClaimsIdentity.Object);

            var authenticationHeaderValue = new AuthenticationHeaderValue("test");
            var initialState = new InitialState <Group>()
            {
                ClaimsPrincipal = mockClaimsPrincipal.Object,
                Groups          = new List <Group>()
                {
                    new Group()
                    {
                        DisplayName = "Finance"
                    },
                }
            };

            var actualResult = await authorizationRepository.EvaluateAsync(authenticationHeaderValue, initialState);

            actualResult.Should().BeEquivalentTo(expectedResult);
        }