public async Task Edit_RemoveUserFromRole_SuccessSaveChanges() { // Setup var usrMgr = TestHelper.GetUserManager(); var roleMgr = TestHelper.GetRoleManager(); var controller = new RoleAdminController(roleMgr, usrMgr); RoleModification model = new RoleModification { RoleName = "Members" }; var user1 = new AppUser { Id = Guid.NewGuid().ToString(), UserName = "******" }; model.IdsToDelete = new string[] { user1.Id }; usrMgr.FindByIdAsync(user1.Id).Returns(user1); usrMgr.RemoveFromRoleAsync(user1, model.RoleName).Returns(IdentityResult.Success); // Act IActionResult result = await controller.Edit(model); // Assert Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Index", (result as RedirectToActionResult).ActionName); await usrMgr.Received().FindByIdAsync(user1.Id); await usrMgr.Received().RemoveFromRoleAsync(user1, model.RoleName); }
public async Task Edit_AddUserToRole_FailedSaveChanges() { // Setup var usrMgr = TestHelper.GetUserManager(); var roleMgr = TestHelper.GetRoleManager(); var controller = new RoleAdminController(roleMgr, usrMgr); RoleModification model = new RoleModification { RoleName = "Members" }; var user1 = new AppUser { Id = Guid.NewGuid().ToString(), UserName = "******" }; model.IdsToAdd = new string[] { user1.Id }; usrMgr.FindByIdAsync(user1.Id).Returns(user1); var errorMessage = "Error 1"; usrMgr.AddToRoleAsync(user1, model.RoleName).Returns(TestHelper.GetIdentityFailedResult(errorMessage)); // Act IActionResult result = await controller.Edit(model); // Assert Assert.Equal(1, controller.ModelState.ErrorCount); Assert.Equal(errorMessage, controller.ModelState[""].Errors[0].ErrorMessage); await usrMgr.Received().FindByIdAsync(user1.Id); await usrMgr.Received().AddToRoleAsync(user1, model.RoleName); }
public async Task Edit_Post_ModelStateIsValid_RedirectToIndex() { RoleAdminController controller = GetNewRoleAdminController(null, null); RedirectToRouteResult result = (await controller.Edit(new RoleModificationModel { })) as RedirectToRouteResult; Assert.AreEqual("Index", result.RouteValues["action"]); }
public async Task Edit_Get_RoleNotFound_AsksForErrorView() { Mock <RoleStore <AppRole> > roleStore = new Mock <RoleStore <AppRole> >(); Mock <AppRoleManager> roleManager = new Mock <AppRoleManager>(roleStore.Object); roleManager.Setup(m => m.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(null as AppRole); RoleAdminController controller = GetNewRoleAdminController(null, roleManager.Object); ViewResult result = (await controller.Edit("")) as ViewResult; Assert.AreEqual("Error", result.ViewName); }
public async Task Edit_Post_ModelStateIsInvalid_AsksForErrorView() { RoleAdminController controller = GetNewRoleAdminController(null, null); controller.ModelState.AddModelError("", "Error"); ViewResult result = (await controller.Edit(new RoleModificationModel { })) as ViewResult; Assert.AreEqual("Error", result.ViewName); string[] model = result.ViewData.Model as string[]; Assert.AreEqual("Роль не найдена", model[0]); }
private void Get_Correct_RoleEditModel() { IdentityUser user1 = new IdentityUser(); IdentityUser user2 = new IdentityUser(); IdentityUser user3 = new IdentityUser(); IdentityRole role = new IdentityRole("Admin"); _mockUserManager.SetupGet(m => m.Users).Returns((new List <IdentityUser> { user1, user2, user3 }).AsQueryable()); _mockUserManager.Setup(m => m.IsInRoleAsync(user1, role.Name)).ReturnsAsync(true); _mockUserManager.Setup(m => m.IsInRoleAsync(user2, role.Name)).ReturnsAsync(false); _mockUserManager.Setup(m => m.IsInRoleAsync(user3, role.Name)).ReturnsAsync(false); _mockRoleManager.Setup(m => m.FindByNameAsync("Admin")).ReturnsAsync(role); ViewResult result = (ViewResult)_roleAdminController.Edit().Result; RoleEditModel editModel = (RoleEditModel)result.Model; Assert.True(editModel.Members.Count() == 1); Assert.True(editModel.NonМembers.Count() == 2); Assert.True(editModel.Role.Name == "Admin"); }
public async Task Edit_Post_ModelStateIsValidAndRemoveIdentityResultNotSucceeded_AsksForErrorView() { Mock <IUserStore <AppUser> > userStore = new Mock <IUserStore <AppUser> >(); Mock <AppUserManager> userManager = new Mock <AppUserManager>(userStore.Object); userManager.Setup(m => m.RemoveFromRoleAsync(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(IdentityResult.Failed("Remove Error")); RoleAdminController controller = GetNewRoleAdminController(userManager.Object, null); ViewResult result = (await controller.Edit(new RoleModificationModel { IdsToDelete = new string[] { "1" } })) as ViewResult; Assert.AreEqual("Error", result.ViewName); string[] model = result.ViewData.Model as string[]; Assert.AreEqual("Remove Error", model[0]); }
public async Task Edit_Get_RoleFound_RetrievesRoleNamePropertyFromModel() { Mock <RoleStore <AppRole> > roleStore = new Mock <RoleStore <AppRole> >(); Mock <AppRoleManager> roleManager = new Mock <AppRoleManager>(roleStore.Object); roleManager.Setup(m => m.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new AppRole { Name = "Users" }); Mock <IUserStore <AppUser> > userStore = new Mock <IUserStore <AppUser> >(); Mock <AppUserManager> userManager = new Mock <AppUserManager>(userStore.Object); RoleAdminController controller = GetNewRoleAdminController(userManager.Object, roleManager.Object); ViewResult result = (await controller.Edit("")) as ViewResult; RoleEditModel model = result.ViewData.Model as RoleEditModel; Assert.AreEqual("Users", model.Role.Name); }
public async Task Edit_RoleId_ReturnsResult() { // Setup var usrMgr = TestHelper.GetUserManager(); var roleMgr = TestHelper.GetRoleManager(); var controller = new RoleAdminController(roleMgr, usrMgr); var identityRole = new IdentityRole("Members") { Id = Guid.NewGuid().ToString() }; roleMgr.FindByIdAsync(identityRole.Id).Returns(identityRole); var user1 = new AppUser { Id = Guid.NewGuid().ToString(), UserName = "******" }; var user2 = new AppUser { Id = Guid.NewGuid().ToString(), UserName = "******" }; var user3 = new AppUser { Id = Guid.NewGuid().ToString(), UserName = "******" }; var users = new List <AppUser> { user1, user2, user3 }; usrMgr.Users.Returns(users.AsQueryable()); usrMgr.IsInRoleAsync(user1, identityRole.Name).Returns(true); // Act RoleEdit res = TestHelper.GetViewModel <RoleEdit>(await controller.Edit(identityRole.Id)); // Assert Assert.Equal(identityRole.Id, res.Role.Id); Assert.Equal(user1.UserName, res.Members.ElementAt(0).UserName); Assert.Equal(user2.UserName, res.NonMembers.ElementAt(0).UserName); Assert.Equal(user3.UserName, res.NonMembers.ElementAt(1).UserName); }