Ejemplo n.º 1
0
        public async Task OnGetAsync_InvalidModel()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync_InvalidModel)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var user = new User();
            var id   = Guid.NewGuid();

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(user);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            RolesModel    model;
            IActionResult get;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _userManager.Setup(x => x.Users).Returns(context.Users);
                model = new RolesModel(_userManager.Object, _roleManager.Object);
                get   = await model.OnGetAsync(id).ConfigureAwait(false);
            }

            // Assert
            Assert.Null(model.UserModel);
            Assert.Null(model.Roles);
            Assert.IsType <NotFoundResult>(get);
        }
Ejemplo n.º 2
0
        public async Task OnPostAsync_AllRemoved()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            PropertiesModel properties;
            IActionResult   post;
            var             identityResourceId = Random.Next();
            var             identityResource   = new IdentityResource
            {
                Id         = identityResourceId,
                Properties = new List <IdentityResourceProperty>
                {
                    new IdentityResourceProperty {
                        Id = Random.Next()
                    }
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(identityResource);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                properties = new PropertiesModel(context)
                {
                    IdentityResource = new IdentityResource {
                        Id = identityResourceId
                    }
                };
                post = await properties.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                identityResource = await context.IdentityResources
                                   .Include(x => x.Properties)
                                   .SingleOrDefaultAsync(x => x.Id.Equals(identityResourceId))
                                   .ConfigureAwait(false);

                Assert.Empty(identityResource.Properties);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Properties", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(IdentityResource.Id), key);
                Assert.Equal(properties.IdentityResource.Id, value);
            });
        }
Ejemplo n.º 3
0
        public async Task OnGetAsync_InvalidId()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync_InvalidId)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var role = new Role();

            role.RoleClaims.Add(new RoleClaim());
            role.RoleClaims.Add(new RoleClaim());
            role.RoleClaims.Add(new RoleClaim());
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(role);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            ClaimsModel   model;
            IActionResult get;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _roleManager.Setup(x => x.Roles).Returns(context.Roles);
                model = new ClaimsModel(_roleManager.Object);
                get   = await model.OnGetAsync(Guid.Empty).ConfigureAwait(false);
            }

            // Assert
            Assert.Null(model.Role);
            Assert.Null(model.Claims);
            Assert.IsType <NotFoundResult>(get);
        }
Ejemplo n.º 4
0
        public async Task OnPostAsync_AllRemoved()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            RedirectUrisModel redirectUris;
            IActionResult     post;
            var clientId = Random.Next();
            var client   = new Client
            {
                Id           = clientId,
                RedirectUris = new List <ClientRedirectUri>
                {
                    new ClientRedirectUri {
                        Id = Random.Next()
                    }
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(client);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                redirectUris = new RedirectUrisModel(context)
                {
                    Client = new Client {
                        Id = clientId
                    }
                };
                post = await redirectUris.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                client = await context.Clients
                         .Include(x => x.RedirectUris)
                         .SingleOrDefaultAsync(x => x.Id.Equals(clientId))
                         .ConfigureAwait(false);

                Assert.Empty(client.RedirectUris);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/RedirectUris", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Client.Id), key);
                Assert.Equal(redirectUris.Client.Id, value);
            });
        }
Ejemplo n.º 5
0
        public async Task OnPostAsync_InvalidModel()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            IActionResult post;
            var           client = new Client {
                Id = Random.Next()
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(client);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                var redirectUris = new RedirectUrisModel(context)
                {
                    Client = new Client {
                        Id = Random.Next()
                    }
                };
                post = await redirectUris.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            Assert.IsType <PageResult>(post);
        }
Ejemplo n.º 6
0
        public async Task OnPostAsync_AllRemoved()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var userClaim = new UserClaim
            {
                ClaimType  = "Claim Type",
                ClaimValue = "Claim Value"
            };
            var user = new User
            {
                Email = "Email"
            };

            user.Claims.Add(userClaim);
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(user);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            IActionResult get;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _userManager.Setup(x => x.Users).Returns(context.Users);
                var model = new ClaimsModel(_userManager.Object)
                {
                    UserModel = new User {
                        Id = user.Id
                    }
                };
                get = await model.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            _userManager.Verify(
                x => x.RemoveClaimsAsync(
                    It.Is <User>(y => y.Id.Equals(user.Id)),
                    It.Is <IEnumerable <Claim> >(y => y.SingleOrDefault(z => z.Type.Equals(userClaim.ClaimType) && z.Value.Equals(userClaim.ClaimValue)) != null)),
                Times.Once);
            var result = Assert.IsType <RedirectToPageResult>(get);

            Assert.Equal("../Details/Claims", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(User.Id), key);
                Assert.Equal(user.Id, value);
            });
        }
Ejemplo n.º 7
0
        public async Task OnPostAsync_AllRemoved()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var roleClaim = new RoleClaim
            {
                ClaimType  = "Claim Type",
                ClaimValue = "Claim Value"
            };
            var role = new Role
            {
                Name = "Role Name"
            };

            role.RoleClaims.Add(roleClaim);
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(role);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            IActionResult get;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _roleManager.Setup(x => x.Roles).Returns(context.Roles);
                var model = new ClaimsModel(_roleManager.Object)
                {
                    Role = new Role {
                        Id = role.Id
                    }
                };
                get = await model.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            _roleManager.Verify(
                x => x.RemoveClaimAsync(
                    It.Is <Role>(y => y.Id.Equals(role.Id)),
                    It.Is <Claim>(y => y.Type.Equals(roleClaim.ClaimType) && y.Value.Equals(roleClaim.ClaimValue))),
                Times.Once);
            var result = Assert.IsType <RedirectToPageResult>(get);

            Assert.Equal("../Details/Claims", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Role.Id), key);
                Assert.Equal(role.Id, value);
            });
        }
Ejemplo n.º 8
0
        public async Task OnGetAsync()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var user = new User();

            user.UserRoles.Add(new UserRole {
                Role = new Role()
            });
            user.UserRoles.Add(new UserRole {
                Role = new Role()
            });
            user.UserRoles.Add(new UserRole {
                Role = new Role()
            });
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(user);
                await context.SaveChangesAsync().ConfigureAwait(false);

                var rolesList = await context.Roles.ToListAsync().ConfigureAwait(false);

                _roleManager.Setup(x => x.Roles).Returns(rolesList.AsQueryable());
            }

            RolesModel    model;
            IActionResult get;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _userManager.Setup(x => x.Users).Returns(context.Users);
                model = new RolesModel(_userManager.Object, _roleManager.Object);
                get   = await model.OnGetAsync(user.Id).ConfigureAwait(false);
            }

            // Assert
            Assert.NotNull(model.UserModel);
            Assert.Equal(user.Id, model.UserModel.Id);
            var roles = Assert.IsAssignableFrom <IEnumerable <Role> >(model.Roles);

            Assert.Equal(user.UserRoles.Count, roles.Count());
            Assert.IsType <PageResult>(get);
        }
Ejemplo n.º 9
0
        public async Task OnGetAsync()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var identityResource = new IdentityResource
            {
                Id         = Random.Next(),
                Properties = new List <IdentityResourceProperty>
                {
                    new IdentityResourceProperty(),
                    new IdentityResourceProperty(),
                    new IdentityResourceProperty()
                }
            };
            PropertiesModel model;
            IActionResult   get;

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(identityResource);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                model = new PropertiesModel(context);
                get   = await model.OnGetAsync(identityResource.Id).ConfigureAwait(false);
            }

            // Assert
            Assert.NotNull(model.IdentityResource);
            Assert.Equal(identityResource.Id, model.IdentityResource.Id);
            var properties = Assert.IsAssignableFrom <IEnumerable <IdentityResourceProperty> >(model.Properties);

            Assert.Equal(identityResource.Properties.Count, properties.Count());
            Assert.IsType <PageResult>(get);
        }
Ejemplo n.º 10
0
        public async Task OnGetAsync()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var client = new Client
            {
                Id           = Random.Next(),
                RedirectUris = new List <ClientRedirectUri>
                {
                    new ClientRedirectUri(),
                    new ClientRedirectUri(),
                    new ClientRedirectUri()
                }
            };
            RedirectUrisModel model;
            IActionResult     get;

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(client);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                model = new RedirectUrisModel(context);
                get   = await model.OnGetAsync(client.Id).ConfigureAwait(false);
            }

            // Assert
            Assert.NotNull(model.Client);
            Assert.Equal(client.Id, model.Client.Id);
            var redirectUris = Assert.IsAssignableFrom <IEnumerable <ClientRedirectUri> >(model.RedirectUris);

            Assert.Equal(client.RedirectUris.Count, redirectUris.Count());
            Assert.IsType <PageResult>(get);
        }
Ejemplo n.º 11
0
        public async Task OnGetAsync()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var role = new Role
            {
                Id = Guid.NewGuid()
            };

            role.RoleClaims.Add(new RoleClaim());
            role.RoleClaims.Add(new RoleClaim());
            role.RoleClaims.Add(new RoleClaim());
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(role);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            ClaimsModel   model;
            IActionResult get;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _roleManager.Setup(x => x.Roles).Returns(context.Roles);
                model = new ClaimsModel(_roleManager.Object);
                get   = await model.OnGetAsync(role.Id).ConfigureAwait(false);
            }

            // Assert
            Assert.NotNull(model.Role);
            Assert.Equal(role.Id, model.Role.Id);
            var claims = Assert.IsAssignableFrom <IEnumerable <RoleClaim> >(model.Claims);

            Assert.Equal(role.RoleClaims.Count, claims.Count());
            Assert.IsType <PageResult>(get);
        }
Ejemplo n.º 12
0
        public async Task OnPostAsync_InvalidModel()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_InvalidModel)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var role = new Role {
                Id = Guid.NewGuid()
            };
            var id = Guid.NewGuid();

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(role);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            IActionResult post;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _roleManager.Setup(x => x.Roles).Returns(context.Roles);
                var model = new ClaimsModel(_roleManager.Object)
                {
                    Role = new Role {
                        Id = id
                    }
                };
                post = await model.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            _roleManager.Verify(x => x.UpdateAsync(It.IsAny <Role>()), Times.Never);
            Assert.IsType <PageResult>(post);
        }
Ejemplo n.º 13
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string scope1OriginalScope = "Original Scope";
            const string scope1EditedScope   = "Edited Scope";
            const string newScopeScope       = "New Scope";
            var          databaseName        = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options             = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                               .UseInMemoryDatabase(databaseName)
                                               .Options;
            ScopesModel   scopes;
            IActionResult post;
            var           scope1 = new ClientScope
            {
                Id    = Random.Next(),
                Scope = scope1OriginalScope
            };
            var scope2 = new ClientScope {
                Id = Random.Next()
            };
            var clientId = Random.Next();
            var client   = new Client
            {
                Id            = clientId,
                AllowedScopes = new List <ClientScope>
                {
                    scope1,
                    scope2
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(client);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                scopes = new ScopesModel(context)
                {
                    Client = new Client
                    {
                        Id            = clientId,
                        AllowedScopes = new List <ClientScope>
                        {
                            new ClientScope
                            {
                                Id    = scope1.Id,
                                Scope = scope1EditedScope
                            },
                            new ClientScope {
                                Scope = newScopeScope
                            }
                        }
                    }
                };
                post = await scopes.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                client = await context.Clients
                         .Include(x => x.AllowedScopes)
                         .SingleOrDefaultAsync(x => x.Id.Equals(clientId))
                         .ConfigureAwait(false);

                scope1 = client.AllowedScopes.SingleOrDefault(x => x.Id.Equals(scope1.Id));
                scope2 = client.AllowedScopes.SingleOrDefault(x => x.Id.Equals(scope2.Id));
                var newScope = client.AllowedScopes.SingleOrDefault(x => x.Scope.Equals(newScopeScope));

                Assert.NotNull(scope1);
                Assert.Equal(scope1EditedScope, scope1.Scope);
                Assert.Null(scope2);
                Assert.NotNull(newScope);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Scopes", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Client.Id), key);
                Assert.Equal(scopes.Client.Id, value);
            });
        }
Ejemplo n.º 14
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string originalRoleClaim2Type = "Claim 2 Type";
            const string newRoleClaim2Type      = "New Claim Type";
            var          databaseName           = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                   .UseInMemoryDatabase(databaseName)
                                   .Options;
            var roleClaim1 = new RoleClaim
            {
                ClaimType  = "Claim 1 Type",
                ClaimValue = "Claim 1 Value"
            };
            var roleClaim2 = new RoleClaim
            {
                ClaimType  = originalRoleClaim2Type,
                ClaimValue = "Claim 2 Value"
            };
            var roleClaim3 = new RoleClaim
            {
                ClaimType  = "Claim 3 Type",
                ClaimValue = "Claim 3 Value"
            };
            var roleClaim4 = new RoleClaim
            {
                ClaimType  = "Claim 4 Type",
                ClaimValue = "Claim 4 Value"
            };
            var role = new Role
            {
                Name = "Role Name"
            };

            role.RoleClaims.Add(roleClaim1);
            role.RoleClaims.Add(roleClaim2);
            role.RoleClaims.Add(roleClaim3);
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(role);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            IActionResult post;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _roleManager.Setup(x => x.Roles).Returns(context.Roles);
                var model = new ClaimsModel(_roleManager.Object)
                {
                    Role = role
                };
                model.Role.RoleClaims.Single(x => x.ClaimType.Equals(roleClaim2.ClaimType)).ClaimType = newRoleClaim2Type;
                model.Role.RoleClaims.Remove(roleClaim3);
                model.Role.RoleClaims.Add(roleClaim4);
                post = await model.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            _roleManager.Verify(x => x.RemoveClaimAsync(
                                    It.Is <Role>(y => y.Id.Equals(role.Id)),
                                    It.Is <Claim>(y => y.Type.Equals(roleClaim1.ClaimType) && y.Value.Equals(roleClaim1.ClaimValue))),
                                Times.Never);
            _roleManager.Verify(x => x.AddClaimAsync(
                                    It.Is <Role>(y => y.Id.Equals(role.Id)),
                                    It.Is <Claim>(y => y.Type.Equals(roleClaim1.ClaimType) && y.Value.Equals(roleClaim1.ClaimValue))),
                                Times.Never);
            _roleManager.Verify(x => x.RemoveClaimAsync(
                                    It.Is <Role>(y => y.Id.Equals(role.Id)),
                                    It.Is <Claim>(y => y.Type.Equals(originalRoleClaim2Type) && y.Value.Equals(roleClaim2.ClaimValue))),
                                Times.Once);
            _roleManager.Verify(x => x.AddClaimAsync(
                                    It.Is <Role>(y => y.Id.Equals(role.Id)),
                                    It.Is <Claim>(y => y.Type.Equals(newRoleClaim2Type) && y.Value.Equals(roleClaim2.ClaimValue))),
                                Times.Once);
            _roleManager.Verify(
                x => x.AddClaimAsync(
                    It.Is <Role>(y => y.Id.Equals(role.Id)),
                    It.Is <Claim>(y => y.Type.Equals(roleClaim4.ClaimType) && y.Value.Equals(roleClaim4.ClaimValue))),
                Times.Once);
            _roleManager.Verify(
                x => x.RemoveClaimAsync(
                    It.Is <Role>(y => y.Id.Equals(role.Id)),
                    It.Is <Claim>(y => y.Type.Equals(roleClaim3.ClaimType) && y.Value.Equals(roleClaim3.ClaimValue))),
                Times.Once);
            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Claims", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Role.Id), key);
                Assert.Equal(role.Id, value);
            });
        }
Ejemplo n.º 15
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string corsOrigin1OriginalOrigin = "Original Origin";
            const string corsOrigin1EditedOrigin   = "Edited Origin";
            const string newCorsOriginOrigin       = "New Origin";
            var          databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                        .UseInMemoryDatabase(databaseName)
                                        .Options;
            CorsOriginsModel corsOrigins;
            IActionResult    post;
            var corsOrigin1 = new ClientCorsOrigin
            {
                Id     = Random.Next(),
                Origin = corsOrigin1OriginalOrigin
            };
            var corsOrigin2 = new ClientCorsOrigin {
                Id = Random.Next()
            };
            var clientId = Random.Next();
            var client   = new Client
            {
                Id = clientId,
                AllowedCorsOrigins = new List <ClientCorsOrigin>
                {
                    corsOrigin1,
                    corsOrigin2
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(client);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                corsOrigins = new CorsOriginsModel(context)
                {
                    Client = new Client
                    {
                        Id = clientId,
                        AllowedCorsOrigins = new List <ClientCorsOrigin>
                        {
                            new ClientCorsOrigin
                            {
                                Id     = corsOrigin1.Id,
                                Origin = corsOrigin1EditedOrigin
                            },
                            new ClientCorsOrigin {
                                Origin = newCorsOriginOrigin
                            }
                        }
                    }
                };
                post = await corsOrigins.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                client = await context.Clients
                         .Include(x => x.AllowedCorsOrigins)
                         .SingleOrDefaultAsync(x => x.Id.Equals(clientId))
                         .ConfigureAwait(false);

                corsOrigin1 = client.AllowedCorsOrigins.SingleOrDefault(x => x.Id.Equals(corsOrigin1.Id));
                corsOrigin2 = client.AllowedCorsOrigins.SingleOrDefault(x => x.Id.Equals(corsOrigin2.Id));
                var newCorsOrigin = client.AllowedCorsOrigins.SingleOrDefault(x => x.Origin.Equals(newCorsOriginOrigin));

                Assert.NotNull(corsOrigin1);
                Assert.Equal(corsOrigin1EditedOrigin, corsOrigin1.Origin);
                Assert.Null(corsOrigin2);
                Assert.NotNull(newCorsOrigin);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/CorsOrigins", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Client.Id), key);
                Assert.Equal(corsOrigins.Client.Id, value);
            });
        }
Ejemplo n.º 16
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string claim1OriginalType = "Original Type";
            const string claim1EditedType   = "Edited Type";
            const string newClaimType       = "New Type";
            var          databaseName       = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options            = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                              .UseInMemoryDatabase(databaseName)
                                              .Options;
            ClaimTypesModel claims;
            IActionResult   post;
            var             claim1 = new ApiResourceClaim
            {
                Id   = Random.Next(),
                Type = claim1OriginalType
            };
            var claim2 = new ApiResourceClaim {
                Id = Random.Next()
            };
            var apiResourceId = Random.Next();
            var apiResource   = new ApiResource
            {
                Id         = apiResourceId,
                UserClaims = new List <ApiResourceClaim>
                {
                    claim1,
                    claim2
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(apiResource);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                claims = new ClaimTypesModel(context)
                {
                    ApiResource = new ApiResource
                    {
                        Id         = apiResourceId,
                        UserClaims = new List <ApiResourceClaim>
                        {
                            new ApiResourceClaim
                            {
                                Id   = claim1.Id,
                                Type = claim1EditedType
                            },
                            new ApiResourceClaim {
                                Type = newClaimType
                            }
                        }
                    }
                };
                post = await claims.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                apiResource = await context.ApiResources
                              .Include(x => x.UserClaims)
                              .SingleOrDefaultAsync(x => x.Id.Equals(apiResourceId))
                              .ConfigureAwait(false);

                claim1 = apiResource.UserClaims.SingleOrDefault(x => x.Id.Equals(claim1.Id));
                claim2 = apiResource.UserClaims.SingleOrDefault(x => x.Id.Equals(claim2.Id));
                var newClaim = apiResource.UserClaims.SingleOrDefault(x => x.Type.Equals(newClaimType));

                Assert.NotNull(claim1);
                Assert.Equal(claim1EditedType, claim1.Type);
                Assert.Null(claim2);
                Assert.NotNull(newClaim);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/ClaimTypes", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(ApiResource.Id), key);
                Assert.Equal(claims.ApiResource.Id, value);
            });
        }
Ejemplo n.º 17
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string redirectUri1OriginalRedirectUri = "Original RedirectUri";
            const string redirectUri1EditedRedirectUri   = "Edited RedirectUri";
            const string newRedirectUriRedirectUri       = "New RedirectUri";
            var          databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                        .UseInMemoryDatabase(databaseName)
                                        .Options;
            RedirectUrisModel redirectUris;
            IActionResult     post;
            var redirectUri1 = new ClientRedirectUri
            {
                Id          = Random.Next(),
                RedirectUri = redirectUri1OriginalRedirectUri
            };
            var redirectUri2 = new ClientRedirectUri {
                Id = Random.Next()
            };
            var clientId = Random.Next();
            var client   = new Client
            {
                Id           = clientId,
                RedirectUris = new List <ClientRedirectUri>
                {
                    redirectUri1,
                    redirectUri2
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(client);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                redirectUris = new RedirectUrisModel(context)
                {
                    Client = new Client
                    {
                        Id           = clientId,
                        RedirectUris = new List <ClientRedirectUri>
                        {
                            new ClientRedirectUri
                            {
                                Id          = redirectUri1.Id,
                                RedirectUri = redirectUri1EditedRedirectUri
                            },
                            new ClientRedirectUri {
                                RedirectUri = newRedirectUriRedirectUri
                            }
                        }
                    }
                };
                post = await redirectUris.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                client = await context.Clients
                         .Include(x => x.RedirectUris)
                         .SingleOrDefaultAsync(x => x.Id.Equals(clientId))
                         .ConfigureAwait(false);

                redirectUri1 = client.RedirectUris.SingleOrDefault(x => x.Id.Equals(redirectUri1.Id));
                redirectUri2 = client.RedirectUris.SingleOrDefault(x => x.Id.Equals(redirectUri2.Id));
                var newRedirectUri = client.RedirectUris.SingleOrDefault(x => x.RedirectUri.Equals(newRedirectUriRedirectUri));

                Assert.NotNull(redirectUri1);
                Assert.Equal(redirectUri1EditedRedirectUri, redirectUri1.RedirectUri);
                Assert.Null(redirectUri2);
                Assert.NotNull(newRedirectUri);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/RedirectUris", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Client.Id), key);
                Assert.Equal(redirectUris.Client.Id, value);
            });
        }
Ejemplo n.º 18
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string originalUserClaim2Type = "Claim 2 Type";
            const string newUserClaim2Type      = "New Claim Type";
            var          databaseName           = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                   .UseInMemoryDatabase(databaseName)
                                   .Options;
            var userClaim1 = new UserClaim
            {
                ClaimType  = "Claim 1 Type",
                ClaimValue = "Claim 1 Value"
            };
            var userClaim2 = new UserClaim
            {
                ClaimType  = originalUserClaim2Type,
                ClaimValue = "Claim 2 Value"
            };
            var userClaim3 = new UserClaim
            {
                ClaimType  = "Claim 3 Type",
                ClaimValue = "Claim 3 Value"
            };
            var userClaim4 = new UserClaim
            {
                ClaimType  = "Claim 4 Type",
                ClaimValue = "Claim 4 Value"
            };
            var user = new User
            {
                Email = "Email"
            };

            user.Claims.Add(userClaim1);
            user.Claims.Add(userClaim2);
            user.Claims.Add(userClaim3);
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(user);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            IActionResult post;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _userManager.Setup(x => x.Users).Returns(context.Users);
                var model = new ClaimsModel(_userManager.Object)
                {
                    UserModel = user
                };
                model.UserModel.Claims.Single(x => x.ClaimType.Equals(userClaim2.ClaimType)).ClaimType = newUserClaim2Type;
                model.UserModel.Claims.Remove(userClaim3);
                model.UserModel.Claims.Add(userClaim4);
                post = await model.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            _userManager.Verify(x => x.ReplaceClaimAsync(
                                    It.Is <User>(y => y.Id.Equals(user.Id)),
                                    It.Is <Claim>(y => y.Type.Equals(userClaim1.ClaimType) && y.Value.Equals(userClaim1.ClaimValue)),
                                    It.IsAny <Claim>()),
                                Times.Never);
            _userManager.Verify(x => x.ReplaceClaimAsync(
                                    It.Is <User>(y => y.Id.Equals(user.Id)),
                                    It.Is <Claim>(y => y.Type.Equals(originalUserClaim2Type) && y.Value.Equals(userClaim2.ClaimValue)),
                                    It.Is <Claim>(y => y.Type.Equals(newUserClaim2Type) && y.Value.Equals(userClaim2.ClaimValue))),
                                Times.Once);
            _userManager.Verify(
                x => x.AddClaimsAsync(
                    It.Is <User>(y => y.Id.Equals(user.Id)),
                    It.Is <IEnumerable <Claim> >(y => y.SingleOrDefault(z => z.Type.Equals(userClaim4.ClaimType) && z.Value.Equals(userClaim4.ClaimValue)) != null)),
                Times.Once);
            _userManager.Verify(
                x => x.RemoveClaimsAsync(
                    It.Is <User>(y => y.Id.Equals(user.Id)),
                    It.Is <IEnumerable <Claim> >(y => y.SingleOrDefault(z => z.Type.Equals(userClaim3.ClaimType) && z.Value.Equals(userClaim3.ClaimValue)) != null)),
                Times.Once);
            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Claims", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(User.Id), key);
                Assert.Equal(user.Id, value);
            });
        }
Ejemplo n.º 19
0
        public async Task OnPostAsync_AllRemoved()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var role = new Role
            {
                Name = "Role Name"
            };

            role.UserRoles.Add(new UserRole {
                User = new User()
            });
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(role);
                await context.SaveChangesAsync().ConfigureAwait(false);

                var usersList = await context.Users.ToListAsync().ConfigureAwait(false);

                _userManager.Setup(x => x.Users).Returns(usersList.AsQueryable());
                _userManager.Setup(x => x.GetUsersInRoleAsync(role.Name)).ReturnsAsync(usersList);
            }

            IActionResult get;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _roleManager.Setup(x => x.Roles).Returns(context.Roles);
                var model = new UsersModel(_roleManager.Object, _userManager.Object)
                {
                    Role = new Role {
                        Id = role.Id
                    }
                };
                get = await model.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            foreach (var user in _userManager.Object.Users)
            {
                _userManager.Verify(
                    x => x.RemoveClaimAsync(
                        user,
                        It.Is <Claim>(y => y.Type.Equals(ClaimTypes.Role) && y.Value.Equals(role.Name))),
                    Times.Once);
            }

            _roleManager.Verify(
                x => x.UpdateAsync(It.Is <Role>(y => y.Id.Equals(role.Id) && y.UserRoles.Count == 0)),
                Times.Once);
            var result = Assert.IsType <RedirectToPageResult>(get);

            Assert.Equal("../Details/Users", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Role.Id), key);
                Assert.Equal(role.Id, value);
            });
        }
Ejemplo n.º 20
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string property1OriginalValue = "Original Value";
            const string property1EditedValue   = "Edited Value";
            const string newPropertyValue       = "New Value";
            var          databaseName           = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                   .UseInMemoryDatabase(databaseName)
                                   .Options;
            PropertiesModel properties;
            IActionResult   post;
            var             property1 = new IdentityResourceProperty
            {
                Id    = Random.Next(),
                Value = property1OriginalValue
            };
            var property2 = new IdentityResourceProperty {
                Id = Random.Next()
            };
            var identityResourceId = Random.Next();
            var identityResource   = new IdentityResource
            {
                Id         = identityResourceId,
                Properties = new List <IdentityResourceProperty>
                {
                    property1,
                    property2
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(identityResource);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                properties = new PropertiesModel(context)
                {
                    IdentityResource = new IdentityResource
                    {
                        Id         = identityResourceId,
                        Properties = new List <IdentityResourceProperty>
                        {
                            new IdentityResourceProperty
                            {
                                Id    = property1.Id,
                                Value = property1EditedValue
                            },
                            new IdentityResourceProperty {
                                Value = newPropertyValue
                            }
                        }
                    }
                };
                post = await properties.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                identityResource = await context.IdentityResources
                                   .Include(x => x.Properties)
                                   .SingleOrDefaultAsync(x => x.Id.Equals(identityResourceId))
                                   .ConfigureAwait(false);

                property1 = identityResource.Properties.SingleOrDefault(x => x.Id.Equals(property1.Id));
                property2 = identityResource.Properties.SingleOrDefault(x => x.Id.Equals(property2.Id));
                var newProperty = identityResource.Properties.SingleOrDefault(x => x.Value.Equals(newPropertyValue));

                Assert.NotNull(property1);
                Assert.Equal(property1EditedValue, property1.Value);
                Assert.Null(property2);
                Assert.NotNull(newProperty);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Properties", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(IdentityResource.Id), key);
                Assert.Equal(properties.IdentityResource.Id, value);
            });
        }
Ejemplo n.º 21
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string idPRestriction1OriginalProvider = "Original Provider";
            const string idPRestriction1EditedProvider   = "Edited Provider";
            const string newIdPRestrictionProvider       = "New Provider";
            var          databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                        .UseInMemoryDatabase(databaseName)
                                        .Options;
            IdPRestrictionsModel idPRestrictions;
            IActionResult        post;
            var idPRestriction1 = new ClientIdPRestriction
            {
                Id       = Random.Next(),
                Provider = idPRestriction1OriginalProvider
            };
            var idPRestriction2 = new ClientIdPRestriction {
                Id = Random.Next()
            };
            var clientId = Random.Next();
            var client   = new Client
            {
                Id = clientId,
                IdentityProviderRestrictions = new List <ClientIdPRestriction>
                {
                    idPRestriction1,
                    idPRestriction2
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(client);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                idPRestrictions = new IdPRestrictionsModel(context)
                {
                    Client = new Client
                    {
                        Id = clientId,
                        IdentityProviderRestrictions = new List <ClientIdPRestriction>
                        {
                            new ClientIdPRestriction
                            {
                                Id       = idPRestriction1.Id,
                                Provider = idPRestriction1EditedProvider
                            },
                            new ClientIdPRestriction {
                                Provider = newIdPRestrictionProvider
                            }
                        }
                    }
                };
                post = await idPRestrictions.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                client = await context.Clients
                         .Include(x => x.IdentityProviderRestrictions)
                         .SingleOrDefaultAsync(x => x.Id.Equals(clientId))
                         .ConfigureAwait(false);

                idPRestriction1 = client.IdentityProviderRestrictions.SingleOrDefault(x => x.Id.Equals(idPRestriction1.Id));
                idPRestriction2 = client.IdentityProviderRestrictions.SingleOrDefault(x => x.Id.Equals(idPRestriction2.Id));
                var newIdPRestriction = client.IdentityProviderRestrictions.SingleOrDefault(x => x.Provider.Equals(newIdPRestrictionProvider));

                Assert.NotNull(idPRestriction1);
                Assert.Equal(idPRestriction1EditedProvider, idPRestriction1.Provider);
                Assert.Null(idPRestriction2);
                Assert.NotNull(newIdPRestriction);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/IdPRestrictions", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Client.Id), key);
                Assert.Equal(idPRestrictions.Client.Id, value);
            });
        }
Ejemplo n.º 22
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string secret1OriginalValue = "Original Value";
            const string secret1EditedValue   = "Edited Value";
            const string newSecretValue       = "New Value";
            var          databaseName         = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                   .UseInMemoryDatabase(databaseName)
                                   .Options;
            SecretsModel  secrets;
            IActionResult post;
            var           secret1 = new ApiSecret
            {
                Id    = Random.Next(),
                Value = secret1OriginalValue
            };
            var secret2 = new ApiSecret {
                Id = Random.Next()
            };
            var apiResourceId = Random.Next();
            var apiResource   = new ApiResource
            {
                Id      = apiResourceId,
                Secrets = new List <ApiSecret>
                {
                    secret1,
                    secret2
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(apiResource);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                secrets = new SecretsModel(context)
                {
                    ApiResource = new ApiResource
                    {
                        Id      = apiResourceId,
                        Secrets = new List <ApiSecret>
                        {
                            new ApiSecret
                            {
                                Id    = secret1.Id,
                                Value = secret1EditedValue
                            },
                            new ApiSecret {
                                Value = newSecretValue
                            }
                        }
                    }
                };
                post = await secrets.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                apiResource = await context.ApiResources
                              .Include(x => x.Secrets)
                              .SingleOrDefaultAsync(x => x.Id.Equals(apiResourceId))
                              .ConfigureAwait(false);

                secret1 = apiResource.Secrets.SingleOrDefault(x => x.Id.Equals(secret1.Id));
                secret2 = apiResource.Secrets.SingleOrDefault(x => x.Id.Equals(secret2.Id));
                var newSecret = apiResource.Secrets.SingleOrDefault(x => x.Value.Equals(newSecretValue));

                Assert.NotNull(secret1);
                Assert.Equal(secret1EditedValue, secret1.Value);
                Assert.Null(secret2);
                Assert.NotNull(newSecret);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Secrets", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(ApiResource.Id), key);
                Assert.Equal(secrets.ApiResource.Id, value);
            });
        }
Ejemplo n.º 23
0
        public async Task OnPostAsync()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            var role1 = new Role {
                Name = "Role 1 Name"
            };
            var role2 = new Role {
                Name = "Role 2 Name"
            };
            var userRole = new UserRole {
                Role = role1
            };
            var user = new User
            {
                Email = "Email",
            };

            user.UserRoles.Add(userRole);
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(user);
                context.Add(role2);
                await context.SaveChangesAsync().ConfigureAwait(false);

                var rolesList = await context.Roles.ToListAsync().ConfigureAwait(false);

                _roleManager.Setup(x => x.Roles).Returns(rolesList.AsQueryable());
                foreach (var role in context.Roles)
                {
                    _roleManager.Setup(x => x.FindByIdAsync($"{role.Id}")).ReturnsAsync(role);
                }
            }

            IActionResult post;

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                _userManager.Setup(x => x.Users).Returns(context.Users);
                var model = new RolesModel(_userManager.Object, _roleManager.Object)
                {
                    UserModel = user
                };

                model.UserModel.UserRoles.Remove(userRole);
                model.UserModel.UserRoles.Add(new UserRole {
                    RoleId = role2.Id
                });
                post = await model.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            _userManager.Verify(
                x => x.AddClaimAsync(
                    It.Is <User>(y => y.Id.Equals(user.Id)),
                    It.Is <Claim>(y => y.Type.Equals(ClaimTypes.Role) && y.Value.Equals(role2.Name))),
                Times.Once);
            _userManager.Verify(
                x => x.RemoveClaimAsync(
                    It.Is <User>(y => y.Id.Equals(user.Id)),
                    It.Is <Claim>(y => y.Type.Equals(ClaimTypes.Role) && y.Value.Equals(role1.Name))),
                Times.Once);
            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Roles", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(User.Id), key);
                Assert.Equal(user.Id, value);
            });
        }