public ActionResult ForgotPassword(PasswordResetInfo model) { var redirect = this.RedirectAuthenticatedUser(); if (redirect != null) { return redirect; } if (!this.ModelState.IsValid) { return this.View(model); } if (!this.accountRepository.Exists(model.Email)) { this.ModelState.AddModelError(nameof(model.Email), Errors.UserDoesNotExist); return this.View(model); } try { var newPassword = this.accountRepository.RestorePassword(model.Email); this.notificationService.SendPassword(model.Email, newPassword); return this.View("InfoMessage", new InfoMessage(Captions.ResetPasswordSuccess)); } catch (Exception ex) { Log.Error($"Can't reset password for user {model.Email}", ex, this); this.ModelState.AddModelError(nameof(model.Email), ex.Message); return this.View(model); } }
public void ForgotPasswordShouldReturnModelIfItsNotValid(PasswordResetInfo model, [Frozen] IAccountRepository repo, [NoAutoProperties] AccountsController controller) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { repo.RestorePassword(Arg.Any<string>()).Returns("new password"); repo.Exists(Arg.Any<string>()).Returns(true); controller.ModelState.AddModelError("Error", "Error"); var result = controller.ForgotPassword(model); result.Should().BeOfType<ViewResult>().Which.Model.Should().Be(model); } }
public void ForgotPasswordShouldReturnModelIfUserNotExist(PasswordResetInfo model, [Frozen] IAccountRepository repo) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { repo.RestorePassword(Arg.Any<string>()).Returns("new password"); repo.Exists(Arg.Any<string>()).Returns(false); var controller = new AccountsController(repo, null); var result = controller.ForgotPassword(model); result.Should().BeOfType<ViewResult>().Which.Model.Should().Be(model); result.Should().BeOfType<ViewResult>().Which.ViewData.ModelState.Should().ContainKey(nameof(model.Email)) .WhichValue.Errors.Should().Contain(x => x.ErrorMessage == Errors.UserDoesNotExist); } }
public void ForgotPasswordShoudCatchAndReturnViewWithError(PasswordResetInfo model, [Frozen] IAccountRepository repo, INotificationService notificationService, IAccountsSettingsService settingService) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { repo.RestorePassword(Arg.Any<string>()).ThrowsForAnyArgs(new Exception("Error")); repo.Exists(Arg.Any<string>()).Returns(true); var controller = new AccountsController(repo, notificationService, settingService); var result = controller.ForgotPassword(model); result.Should().BeOfType<ViewResult>().Which.Model.Should().Be(model); result.Should().BeOfType<ViewResult>().Which.ViewData.ModelState.Should().ContainKey(nameof(model.Email)) .WhichValue.Errors.Should().Contain(x => x.ErrorMessage == "Error"); } }
public void ForgotPasswordShouldRedirectLoggedUser(PasswordResetInfo model, IAccountRepository repo, INotificationService ns) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { using (new UserSwitcher(@"extranet\fake", true)) { repo.RestorePassword(Arg.Any<string>()).Returns("new password"); repo.Exists(Arg.Any<string>()).Returns(true); var controller = new AccountsController(repo, ns); var result = controller.ForgotPassword(model); result.Should().BeOfType<RedirectResult>(); } } }
public void ForgotPasswordShouldRedirectLoggedUser(Database db, [Content] DbItem item, PasswordResetInfo model, [Frozen]IAccountRepository repo, [NoAutoProperties] AccountsController controller) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" }, { "rootPath", "/sitecore/content" }, { "startItem", item.Name } }) as SiteContext; fakeSite.Database = db; Language.Current = Language.Invariant; using (new SiteContextSwitcher(fakeSite)) using (new UserSwitcher(@"extranet\fake", true)) { repo.RestorePassword(Arg.Any<string>()).Returns("new password"); repo.Exists(Arg.Any<string>()).Returns(true); var result = controller.ForgotPassword(model); result.Should().BeOfType<RedirectResult>(); } }
public void ForgotPasswordShouldReturnSuccessView([Frozen] IAccountRepository repo, INotificationService ns, PasswordResetInfo model) { var fakeSite = new FakeSiteContext(new StringDictionary { { "displayMode", "normal" } }) as SiteContext; using (new SiteContextSwitcher(fakeSite)) { var controller = new AccountsController(repo, ns); repo.RestorePassword(Arg.Any<string>()).Returns("new password"); repo.Exists(Arg.Any<string>()).Returns(true); var result = controller.ForgotPassword(model); result.Should().BeOfType<ViewResult>().Which.ViewName.Should().Be("ForgotPasswordSuccess"); } }