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

            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 RolesModel(_userManager.Object, _roleManager.Object)
                {
                    UserModel = new User {
                        Id = id
                    }
                };
                post = await model.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            _userManager.Verify(x => x.UpdateAsync(It.IsAny <User>()), Times.Never);
            Assert.IsType <PageResult>(post);
        }
Ejemplo n.º 2
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"
            };
            var user = new User
            {
                Email = "Email"
            };

            user.Claims.Add(new UserClaim
            {
                ClaimType  = ClaimTypes.Role,
                ClaimValue = role.Name
            });
            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());
            }

            IActionResult get;

            // 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 = 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(ClaimTypes.Role) && z.Value.Equals(role.Name)) != null)),
                Times.Once);

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

            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);
            });
        }
Ejemplo n.º 3
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);
            });
        }