private async Task ResetPassword(string userId, string token, string password, string passwordConfirm, string email, bool attemptReset = true, ResultType expectedResult = ResultType.Success) { var userManager = MockHelpers.CreateMockUserManager(); var signInManager = MockHelpers.CreateMockSignInManager(); var urlHelper = MockHelpers.CreateMockUrlHelper(); var logService = CreateMockLogService(); var container = new UnityContainer(); container.RegisterInstance(logService.Object); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); var controller = new AccountController(userManager.Object, signInManager.Object, urlHelper.Object); var model = new ResetPasswordViewModel { Code = token, Password = password, ConfirmPassword = passwordConfirm, Email = email }; MockHelpers.Validate(model, controller); var result = await controller.ResetPassword(model); if (attemptReset) { userManager.Verify(a => a.ResetPasswordAsync(userId, token, password)); } switch (expectedResult) { case ResultType.ModelError: var view = result as ViewResult; Assert.NotNull(view); break; case ResultType.Success: var redirect = result as RedirectToRouteResult; Assert.NotNull(redirect); Assert.Equal("ResetPasswordConfirmation", redirect.RouteValues["action"]); Assert.Equal("Account", redirect.RouteValues["controller"]); break; } if (attemptReset && expectedResult == ResultType.Success) { logService.Verify(s => s.LogUserUpdated(email, email, It.Is <IEnumerable <string> >(i => i.Single() == "Password"))); } else { logService.Verify(s => s.LogUserUpdated(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IEnumerable <string> >()), Times.Never); } }
public void LogOff() { var userManager = MockHelpers.CreateMockUserManager(); var signInManager = MockHelpers.CreateMockSignInManager(); var urlHelper = MockHelpers.CreateMockUrlHelper(); var authManager = MockHelpers.CreateMockAuthenticationManager(); var controller = new AccountController(userManager.Object, signInManager.Object, urlHelper.Object, authManager.Object); var result = controller.LogOff(); var redirect = result as RedirectToRouteResult; authManager.Verify(a => a.SignOut(DefaultAuthenticationTypes.ApplicationCookie)); Assert.NotNull(redirect); Assert.Equal("Index", redirect.RouteValues["action"]); Assert.Equal("Home", redirect.RouteValues["controller"]); }
private async Task ContinueForgotPassword(string email, string id = null, bool sendEmail = true, ResultType expectedResult = ResultType.Success) { var userManager = MockHelpers.CreateMockUserManager(); var signInManager = MockHelpers.CreateMockSignInManager(); var urlHelper = MockHelpers.CreateMockUrlHelper(); var controller = new AccountController(userManager.Object, signInManager.Object, urlHelper.Object); var model = new ForgotPasswordViewModel { Email = email }; MockHelpers.Validate(model, controller); var result = await controller.ForgotPassword(model); if (sendEmail) { userManager.Verify(a => a.GeneratePasswordResetEmailAsync(urlHelper.Object, id)); } else { userManager.Verify(a => a.GeneratePasswordResetEmailAsync(urlHelper.Object, It.IsAny <string>()), Times.Never); } switch (expectedResult) { case ResultType.Success: var redirect = result as RedirectToRouteResult; Assert.NotNull(redirect); Assert.Equal("Account", redirect.RouteValues["controller"]); Assert.Equal("ForgotPasswordConfirmation", redirect.RouteValues["action"]); break; case ResultType.ModelError: var view = result as ViewResult; Assert.NotNull(view); break; } }
private async Task SubmitLogin(LoginViewModel loginModel, ApplicationUser user, string returnUrl, Result expectedResult) { var userManager = MockHelpers.CreateMockUserManager(); var signInManager = MockHelpers.CreateMockSignInManager(); var urlHelper = MockHelpers.CreateMockUrlHelper(); var logService = CreateMockLogService(); var controller = new AccountController(userManager.Object, signInManager.Object, urlHelper.Object, logService: logService.Object); MockHelpers.Validate(loginModel, controller); var result = await controller.Login(loginModel, returnUrl); ViewResult vr; switch (expectedResult) { case Result.VerificationFailure: vr = result as ViewResult; userManager.Verify(m => m.FindAsync(loginModel.Email, loginModel.Password), Times.AtLeastOnce); signInManager.Verify(m => m.SignInAsync(It.IsAny <ApplicationUser>(), It.IsAny <bool>(), It.IsAny <bool>()), Times.Never); logService.Verify(l => l.LogUserLoggedIn(It.IsAny <string>()), Times.Never); Assert.NotNull(vr); Assert.Equal(loginModel, vr.Model); break; case Result.ValidationFailure: vr = result as ViewResult; userManager.Verify(m => m.FindAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Never); signInManager.Verify(m => m.SignInAsync(It.IsAny <ApplicationUser>(), It.IsAny <bool>(), It.IsAny <bool>()), Times.Never); logService.Verify(l => l.LogUserLoggedIn(It.IsAny <string>()), Times.Never); Assert.NotNull(vr); Assert.Equal(loginModel, vr.Model); break; case Result.RedirectToUri: var rr = result as RedirectResult; userManager.Verify(m => m.FindAsync(loginModel.Email, loginModel.Password), Times.AtLeastOnce); signInManager.Verify(m => m.SignInAsync(user, loginModel.RememberMe, false), Times.Once); urlHelper.Verify(m => m.IsLocalUrl(returnUrl), Times.AtLeastOnce); logService.Verify(l => l.LogUserLoggedIn(loginModel.Email)); Assert.NotNull(rr); Assert.Equal(returnUrl, rr.Url); Assert.Equal(false, rr.Permanent); break; case Result.RedirectToHome: var rtr = result as RedirectToRouteResult; userManager.Verify(m => m.FindAsync(loginModel.Email, loginModel.Password), Times.AtLeastOnce); signInManager.Verify(m => m.SignInAsync(user, loginModel.RememberMe, false), Times.Once); urlHelper.Verify(m => m.IsLocalUrl(returnUrl), Times.AtLeastOnce); logService.Verify(l => l.LogUserLoggedIn(loginModel.Email)); Assert.NotNull(rtr); Assert.Equal("Index", rtr.RouteValues["action"]); Assert.Equal("Home", rtr.RouteValues["controller"]); Assert.Equal(false, rtr.Permanent); break; default: Assert.True(false, "Bad expected result."); break; } }