コード例 #1
0
        public async Task ShouldThrowExceptionWhenServiceFails()
        {
            var baatMock = new Mock <IBaatAuthzApi>();

            baatMock.Setup(b => b.Info(Username)).ThrowsAsync(new Exception("http 500 error"));

            var            authorizationService = new GeonorgeAuthorizationService(baatMock.Object);
            ClaimsIdentity identity             = new ClaimsIdentity(new List <Claim>()
            {
                new Claim(GeonorgeAuthorizationService.ClaimIdentifierUsername, Username)
            });

            var exception = await Assert.ThrowsAsync <Exception>(() => authorizationService.GetClaims(identity));

            exception.Message.Contains("Error while communicating with BaatAutzApi").Should().BeTrue();
        }
コード例 #2
0
        public async Task ShouldReturnClaimsForUser()
        {
            var baatMock = new Mock <IBaatAuthzApi>();
            var response = new BaatAuthzUserInfoResponse()
            {
                Email           = "*****@*****.**",
                Name            = "John Doe",
                User            = Username,
                AuthorizedFrom  = "20090101",
                AuthorizedUntil = "20301231"
            };

            baatMock.Setup(b => b.Info(Username)).ReturnsAsync(response);

            var rolesResponse = new BaatAuthzUserRolesResponse
            {
                Services = new List <string>
                {
                    GeonorgeRoles.MetadataAdmin, GeonorgeRoles.MetadataEditor
                }
            };

            baatMock.Setup(b => b.GetRoles(Username)).ReturnsAsync(rolesResponse);

            var            authorizationService = new GeonorgeAuthorizationService(baatMock.Object);
            ClaimsIdentity identity             = new ClaimsIdentity(new List <Claim>()
            {
                new Claim(GeonorgeAuthorizationService.ClaimIdentifierUsername, Username)
            });

            List <Claim> claims = await authorizationService.GetClaims(identity);

            GetValue(claims, GeonorgeClaims.Name).Should().Be("John Doe");
            GetValue(claims, GeonorgeClaims.Email).Should().Be("*****@*****.**");
            GetValue(claims, GeonorgeClaims.AuthorizedFrom).Should().Be("20090101");
            GetValue(claims, GeonorgeClaims.AuthorizedUntil).Should().Be("20301231");

            List <Claim> roles = claims.FindAll(c => c.Type == GeonorgeAuthorizationService.ClaimIdentifierRole);

            roles.FirstOrDefault(r => r.Value == GeonorgeRoles.MetadataAdmin).Should().NotBeNull();
            roles.FirstOrDefault(r => r.Value == GeonorgeRoles.MetadataEditor).Should().NotBeNull();
        }