public void EditUserGetReturnsCorrectViewModelWhenOrganizationIdIsNull() { { var mediator = new Mock<IMediator>(); string userId = It.IsAny<string>(); mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))) .Returns(new ApplicationUser()); var controller = new SiteController(null, null, mediator.Object); var result = controller.EditUser(userId); var model = ((ViewResult)result).ViewData.Model as EditUserModel; Assert.Equal(model.Organization, null); Assert.IsType<EditUserModel>(model); } }
public async Task EditUserPostRedirectsToCorrectAction() { var mediator = new Mock<IMediator>(); string userId = It.IsAny<string>(); mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))) .Returns(new ApplicationUser()); var controller = new SiteController(null, null, mediator.Object); var result = await controller.EditUser(new EditUserViewModel()); Assert.IsType<RedirectToActionResult>(result); Assert.Equal("Index", ((RedirectToActionResult)result).ActionName); }
public void EditUserGetSendsUserByUserIdQueryWithCorrectUserId() { var mediator = new Mock<IMediator>(); var logger = new Mock<ILogger<SiteController>>(); string userId = It.IsAny<string>(); mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))) .Returns(new ApplicationUser()); var controller = new SiteController(null, logger.Object, mediator.Object); controller.EditUser(userId); mediator.Verify(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId)), Times.Once); }
public async Task EditUserPostInvokesRemoveClaimAsyncWithCorrectParametersWhenUserIsAnOrganizationAdmin() { var mediator = new Mock<IMediator>(); var userManager = CreateApplicationUserMock(); EditUserViewModel model = new EditUserViewModel() { IsOrganizationAdmin = false, UserId = It.IsAny<string>() }; var user = new ApplicationUser() { Id = model.UserId, Email = "*****@*****.**" }; user.Claims.Add(new Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserClaim<string>() { ClaimType = AllReady.Security.ClaimTypes.UserType, ClaimValue = Enum.GetName(typeof(UserType), UserType.OrgAdmin) }); mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))) .Returns(user); userManager.Setup(x => x.RemoveClaimAsync(It.IsAny<ApplicationUser>(), It.IsAny<Claim>())) .Returns(() => Task.FromResult(IdentityResult.Success)); var controller = new SiteController(userManager.Object, null, mediator.Object); var result = await controller.EditUser(model); userManager.Verify(x => x.RemoveClaimAsync(user, It.Is<Claim>(c => c.Value == "OrgAdmin")), Times.Once); }
public async Task EditUserPostReturnsRedirectResultWithCorrectUrlWhenUserIsAnOrganizationAdminAndRemovClaimAsyncDoesNotSucceed() { var mediator = new Mock<IMediator>(); var userManager = CreateApplicationUserMock(); EditUserViewModel model = new EditUserViewModel() { IsOrganizationAdmin = false, UserId = It.IsAny<string>() }; var user = new ApplicationUser() { Id = model.UserId, Email = "*****@*****.**" }; user.Claims.Add(new Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserClaim<string>() { ClaimType = AllReady.Security.ClaimTypes.UserType, ClaimValue = Enum.GetName(typeof(UserType), UserType.OrgAdmin) }); mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))) .Returns(user); userManager.Setup(x => x.RemoveClaimAsync(It.IsAny<ApplicationUser>(), It.IsAny<Claim>())) .Returns(() => Task.FromResult(IdentityResult.Failed(null))); var controller = new SiteController(userManager.Object, null, mediator.Object); var result = await controller.EditUser(model); Assert.IsType<RedirectResult>(result); Assert.Equal("Error", ((RedirectResult)result).Url); }
public async Task EditUserPostSendsSendAccountApprovalEmailWithCorrectDataWhenModelsIsOrganizationAdminIsTrueAndOrganizationAdminClaimWasAddedSuccessfully() { var mediator = new Mock<IMediator>(); var userManager = CreateApplicationUserMock(); EditUserViewModel model = new EditUserViewModel() { IsOrganizationAdmin = true, UserId = It.IsAny<string>() }; var user = new ApplicationUser() { Id = model.UserId, Email = "*****@*****.**" }; mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))) .Returns(user); userManager.Setup(x => x.AddClaimAsync(It.IsAny<ApplicationUser>(), It.IsAny<Claim>())) .Returns(() => Task.FromResult(IdentityResult.Success)); var controller = new SiteController(userManager.Object, null, mediator.Object); controller.SetDefaultHttpContext(); var expectedUrl = String.Format("Login/Admin?Email={0}", user.Email); controller.Url = GetMockUrlHelper(expectedUrl); await controller.EditUser(model); mediator.Verify(m => m.SendAsync(It.Is<SendAccountApprovalEmail>(q => q.Email == user.Email && q.CallbackUrl == expectedUrl )) , Times.Once); }
public async Task EditUserPostReturnsRedirectResultWithCorrectUrlWhenModelsIsOrganizationAdminIsTrueAndOrganizationAdminClaimWasNotAddedSuccessfully() { var mediator = new Mock<IMediator>(); var userManager = CreateApplicationUserMock(); EditUserViewModel model = new EditUserViewModel() { IsOrganizationAdmin = true, UserId = It.IsAny<string>() }; var user = new ApplicationUser() { Id = model.UserId, Email = "*****@*****.**" }; mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))) .Returns(user); userManager.Setup(x => x.AddClaimAsync(It.IsAny<ApplicationUser>(), It.IsAny<Claim>())) .Returns(() => Task.FromResult( IdentityResult.Failed(null))); var controller = new SiteController(userManager.Object, null, mediator.Object); var result = await controller.EditUser(model); Assert.IsType<RedirectResult>(result); Assert.Equal("Error", ((RedirectResult)result).Url); }
public async Task EditUserPostInvokesAddClaimAsyncWhenModelsIsOrganizationAdminIsTrue() { var mediator = new Mock<IMediator>(); var userManager = CreateApplicationUserMock(); EditUserViewModel model = new EditUserViewModel() { IsOrganizationAdmin = true, UserId = It.IsAny<string>() }; var user = new ApplicationUser() { Id = model.UserId, Email = "*****@*****.**" }; mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))) .Returns(user); userManager.Setup(x => x.AddClaimAsync(It.IsAny<ApplicationUser>(), It.IsAny<Claim>())) .Returns(() => Task.FromResult(IdentityResult.Success)); var controller = new SiteController(userManager.Object, null, mediator.Object); controller.SetDefaultHttpContext(); controller.Url = GetMockUrlHelper("any"); await controller.EditUser(model); userManager.Verify(x => x.AddClaimAsync(user, It.Is<Claim>(c => c.Value == "OrgAdmin")), Times.Once); }
public async Task EditUserPostInvokesUrlActionWithCorrectParametersWhenModelsIsOrganizationAdminIsTrueAndOrganizationAdminClaimWasAddedSuccessfully() { const string requestScheme = "requestScheme"; var mediator = new Mock<IMediator>(); var userManager = CreateApplicationUserMock(); EditUserViewModel model = new EditUserViewModel() { IsOrganizationAdmin = true, UserId = It.IsAny<string>() }; var user = new ApplicationUser() { Id = model.UserId, Email = "*****@*****.**" }; mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))) .Returns(user); userManager.Setup(x => x.AddClaimAsync(It.IsAny<ApplicationUser>(), It.IsAny<Claim>())) .Returns(() => Task.FromResult(IdentityResult.Success)); var controller = new SiteController(userManager.Object, null, mediator.Object); controller.SetFakeHttpRequestSchemeTo(requestScheme); var urlHelper = new Mock<IUrlHelper>(); controller.Url = urlHelper.Object; await controller.EditUser(model); urlHelper.Verify(mock => mock.Action(It.Is<UrlActionContext>(uac => uac.Action == "Login" && uac.Controller == "Admin" && uac.Protocol == requestScheme)), Times.Once); }
public async Task EditUserPostSendsUpdateUserWithCorrectUserWhenUsersAssociatedSkillsAreNotNull() { var mediator = new Mock<IMediator>(); EditUserViewModel model = new EditUserViewModel() { UserId = It.IsAny<string>(), AssociatedSkills = new List<UserSkill>() { new UserSkill() {Skill = It.IsAny<Skill>() } } }; mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))) .Returns(new ApplicationUser()); var controller = new SiteController(null, null, mediator.Object); await controller.EditUser(model); mediator.Verify(m => m.SendAsync(It.Is<UpdateUser>(q => q.User.AssociatedSkills[0].Skill == model.AssociatedSkills[0].Skill)), Times.Once); }
public async Task EditUserPostInvokesUpdateUserWithCorrectUserWhenUsersAssociatedSkillsAreNotNullAndThereIsAtLeastOneAssociatedSkillForTheUser() { var mediator = new Mock<IMediator>(); EditUserViewModel model = new EditUserViewModel() { UserId = It.IsAny<string>(), AssociatedSkills = new List<UserSkill>() { new UserSkill() { SkillId = 1, Skill = new Skill() { Id = 1 } } } }; var user = new ApplicationUser() { Id = model.UserId, AssociatedSkills = new List<UserSkill>() { new UserSkill() { SkillId = 2, Skill = new Skill() { Id = 2 } } } }; mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))) .Returns(user); var controller = new SiteController(null, null, mediator.Object); await controller.EditUser(model); mediator.Verify(m => m.SendAsync(It.Is<UpdateUser>(q => q.User.AssociatedSkills[0] == model.AssociatedSkills[0])), Times.Once); }
public async Task EditUserPostSendsUserByUserIdQueryWithCorrectUserId() { var mediator = new Mock<IMediator>(); EditUserViewModel model = new EditUserViewModel() { UserId = "1234", }; mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))) .Returns(new ApplicationUser()); var controller = new SiteController(null, null, mediator.Object); await controller.EditUser(model); mediator.Verify(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId)), Times.Once); }
public async Task EditUserPostReturnsSameViewAndViewModelWhenModelStateIsInvalid() { EditUserViewModel model = new EditUserViewModel() { UserId = "1234", }; var controller = new SiteController(null, null, null); controller.ModelState.AddModelError("FakeKey", "FakeMessage"); var result = (ViewResult)await controller.EditUser(model); Assert.Equal(result.Model, model); }
public void EditUserGetReturnsCorrectValueForOrganiztionOnViewModelWhenOrganizationIdIsNotNull() { var mediator = new Mock<IMediator>(); var user = new ApplicationUser(); int orgId = 99; string orgName = "Test Org"; var org = new Organization() { Id = orgId, Name = orgName }; string userId = It.IsAny<string>(); user.Claims.Add(new Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserClaim<string>() { ClaimType = AllReady.Security.ClaimTypes.Organization, ClaimValue = orgId.ToString() }); mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))) .Returns(user); mediator.Setup(x => x.Send(It.Is<OrganizationByIdQuery>(q => q.OrganizationId == orgId))) .Returns(org); var controller = new SiteController(null, null, mediator.Object); var result = controller.EditUser(userId); var model = ((ViewResult)result).ViewData.Model as EditUserViewModel; Assert.NotNull(model.Organization); Assert.IsType<EditUserViewModel>(model); }
public async Task EditUserPostReturnsRedirectResultWithCorrectUrlWhenUserIsAnOrganizationAdminAndRemovClaimAsyncDoesNotSucceed() { var model = new EditUserViewModel { IsOrganizationAdmin = false, UserId = It.IsAny<string>() }; var user = new ApplicationUser { Id = model.UserId, Email = "*****@*****.**" }; user.MakeOrgAdmin(); var mediator = new Mock<IMediator>(); mediator.Setup(x => x.SendAsync(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))).ReturnsAsync(user); var userManager = CreateApplicationUserMock(); userManager.Setup(x => x.RemoveClaimAsync(It.IsAny<ApplicationUser>(), It.IsAny<Claim>())).Returns(() => Task.FromResult(IdentityResult.Failed(null))); var controller = new SiteController(userManager.Object, null, mediator.Object); var result = await controller.EditUser(model); Assert.IsType<RedirectResult>(result); Assert.Equal("Error", ((RedirectResult)result).Url); }
public async Task EditUserPostInvokesRemoveClaimAsyncWithCorrectParameters_WhenUserIsAnOrganizationAdmin() { var model = new EditUserViewModel { IsOrganizationAdmin = false, UserId = It.IsAny<string>() }; var user = new ApplicationUser { Id = model.UserId, Email = "*****@*****.**" }; user.MakeOrgAdmin(); var mediator = new Mock<IMediator>(); mediator.Setup(x => x.SendAsync(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))).ReturnsAsync(user); var userManager = CreateApplicationUserMock(); userManager.Setup(x => x.RemoveClaimAsync(It.IsAny<ApplicationUser>(), It.IsAny<Claim>())).Returns(() => Task.FromResult(IdentityResult.Success)); var controller = new SiteController(userManager.Object, null, mediator.Object); await controller.EditUser(model); userManager.Verify(x => x.RemoveClaimAsync(user, It.Is<Claim>(c => c.Value == "OrgAdmin")), Times.Once); }