Ejemplo n.º 1
0
        public async Task Details_WithCorrectUsername_ShouldReturnValidViewModel()
        {
            //Arrange
            IQueryable <IdentityRole> roles = new List <IdentityRole>()
            {
                new IdentityRole("firstRole"), new IdentityRole("secondRole"), new IdentityRole("thirdRole")
            }.AsQueryable();

            Mock <RoleManager <IdentityRole> > roleManager = RoleManagerMock.New();

            roleManager
            .Setup(r => r.Roles)
            .Returns(roles);

            Mock <UserManager <User> > userManager = UserManagerMock.New();

            userManager
            .Setup(u => u.FindByNameAsync(username))
            .ReturnsAsync(user);
            userManager
            .Setup(u => u.GetRolesAsync(user))
            .ReturnsAsync(new List <string>()
            {
                "firstRole", "secondRole", "thirdRole"
            });

            Mock <IAdminUserService> adminUserService = new Mock <IAdminUserService>();

            adminUserService
            .Setup(a => a.GetDetailsByUsernameAsync(username))
            .ReturnsAsync(new AdminUserDetailsServiceModel());

            UsersController usersController = new UsersController(roleManager.Object, userManager.Object, adminUserService.Object, null, null);

            //Assert
            var result = await usersController.Details(username);

            //Assert
            result.Should().BeOfType <ViewResult>();

            result.As <ViewResult>().Model.Should().BeOfType <AdminUserDetailsServiceModel>();

            AdminUserDetailsServiceModel model = result.As <ViewResult>().Model.As <AdminUserDetailsServiceModel>();

            model.CurrentRoles.First().Text.Should().Be(roles.First().Name);
            model.CurrentRoles.First().Value.Should().Be(roles.First().Name);
            model.CurrentRoles.Last().Text.Should().Be(roles.Last().Name);
            model.CurrentRoles.Last().Value.Should().Be(roles.Last().Name);
            model.AllRoles.Should().HaveCount(0);
        }
Ejemplo n.º 2
0
        public async Task RemoveFromRole_WithRemoveFromRoleSuccessResult_ShouldShowErrorAndReturnToDetails()
        {
            string successMessage = null;

            //Arrange
            Mock <RoleManager <IdentityRole> > roleManager = RoleManagerMock.New();

            roleManager
            .Setup(r => r.RoleExistsAsync(role))
            .ReturnsAsync(true);

            Mock <UserManager <User> > userManager = UserManagerMock.New();

            userManager
            .Setup(u => u.FindByNameAsync(username))
            .ReturnsAsync(user);
            userManager
            .Setup(u => u.RemoveFromRoleAsync(user, role))
            .ReturnsAsync(IdentityResult.Success);

            Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>();

            tempData
            .SetupSet(t => t[TempDataSuccessMessageKey] = It.IsAny <string>())
            .Callback((string key, object message) => successMessage = message as string);

            UsersController usersController = new UsersController(roleManager.Object, userManager.Object, null, null, null)
            {
                TempData = tempData.Object
            };

            //Assert
            var result = await usersController.RemoveFromRole(new UserWithRoleFormViewModel()
            {
                Role = role, Username = username
            });

            //Assert
            successMessage.Should().Be(string.Format(RemoveFromRoleSuccessMessage, username, role));

            result.Should().BeOfType <RedirectToActionResult>();

            result.As <RedirectToActionResult>().ActionName.Should().Be("Details");
            result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("Username");
            result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(username);
        }
Ejemplo n.º 3
0
        public async Task AddToRole_WithInvalidModelState_ShouldShowErrorAndReturnToDetails()
        {
            string errorMessage = null;

            //Arrange
            Mock <RoleManager <IdentityRole> > roleManager = RoleManagerMock.New();

            roleManager
            .Setup(r => r.RoleExistsAsync(role))
            .ReturnsAsync(true);

            Mock <UserManager <User> > userManager = UserManagerMock.New();

            userManager
            .Setup(u => u.FindByNameAsync(username))
            .ReturnsAsync(user);

            Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>();

            tempData
            .SetupSet(t => t[TempDataErrorMessageKey] = It.IsAny <string>())
            .Callback((string key, object message) => errorMessage = message as string);

            UsersController usersController = new UsersController(roleManager.Object, userManager.Object, null, null, null)
            {
                TempData = tempData.Object
            };

            usersController.ModelState.AddModelError(string.Empty, "Error");

            //Assert
            var result = await usersController.AddToRole(new UserWithRoleFormViewModel()
            {
                Role = role, Username = username
            });

            //Assert
            errorMessage.Should().Be(InvalidIdentityDetailsErroMessage);

            result.Should().BeOfType <RedirectToActionResult>();

            result.As <RedirectToActionResult>().ActionName.Should().Be("Details");
            result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("Username");
            result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(username);
        }