public async Task <GrantRoleResult> RemoveFromRoleAsync(string userId, string role) { var result = await userManager.RemoveFromRoleAsync(userId, role); if (result.Succeeded) { return(GrantRoleResult.Success); } return(GrantRoleResult.Failed(result.Errors)); }
public async Task GrantRole_ShouldReturnError_IfFailsToAddUserToRole() { var fixture = FixtureExtensions.CreateFixture(); var grantRoleVM = fixture.Create <GrantRoleVM>(); var errorMsgs = fixture.CreateMany <string>().ToList(); var uService = fixture.Freeze <Mock <IUserService> >(); uService.Setup(s => s.FindUserByIdAsync(It.IsAny <string>())) .Returns(Task.FromResult(new UserInfo())); uService.Setup(s => s.IsInRole(It.IsAny <string>(), It.IsAny <string>())) .Returns(false); uService.Setup(s => s.AddToRoleAsync(It.IsAny <string>(), It.IsAny <string>())) .Returns(Task.FromResult(GrantRoleResult.Failed(errorMsgs))); var sut = fixture.CreateController <AdminController>(); // Act var action = await sut.GrantRole(grantRoleVM); var pView = action as PartialViewResult; // Assert uService.Verify(s => s.IsInRole(It.Is <string>(v => v == grantRoleVM.UserId), It.Is <string>(v => v == grantRoleVM.RoleName))); Assert.IsType <PartialViewResult>(action); Assert.Equal("_AjaxValidation", pView.ViewName); Assert.False(sut.ModelState.IsValid); Assert.Contains("GrantRoleAction", sut.ModelState.Keys); ModelState actualErrMsgs = sut.ModelState["GrantRoleAction"]; Assert.Equal(errorMsgs.Count, actualErrMsgs.Errors.Count); Assert.Equal(errorMsgs, actualErrMsgs.Errors.Select(e => e.ErrorMessage)); }