public virtual ActionResult ResetPassword(ResetPasswordStepOneModel model) { if (!ModelState.IsValid) { return(View("Step1", model)); } UserProfile profile; if (!string.IsNullOrWhiteSpace(model.UserName)) { profile = userProfileRepository.GetUserProfileByUserName(model.UserName); } else if (!string.IsNullOrWhiteSpace(model.EmailAddress)) { profile = userProfileRepository.GetUserProfileByEmailAddress(model.EmailAddress); } else { ModelState.AddModelError("", "A user name or email address must be specified."); return(View("Step1")); } if (profile == null) { // couldn't find the user, but don't want to give that away return(View("Step2")); } var passwordResetToken = resetPasswordThingy.GeneratePasswordResetToken(profile.UserName, 120); SendEmailToUser(profile.EmailAddress, passwordResetToken); return(View("Step2")); }
public void TestThat_ResetPassword_RedirectsBackToStepOne_WhenTheModelHasAnInvalidUserNameAndEmailAddress() { var controller = new ResetPasswordController(Substitute.For <IUserProfileRepository>(), Substitute.For <IResetPasswordThingy>(), Substitute.For <IResetPasswordEmailSender>()); var model = new ResetPasswordStepOneModel { UserName = null, EmailAddress = null }; var result = (ViewResult)controller.ResetPassword(model); Assert.That(result.ViewName, Is.EqualTo("Step1")); }
public void TestThat_ResetPassword_RedirectsBackToStepOne_WhenTheModelIsInvalid() { var controller = new ResetPasswordController(Substitute.For <IUserProfileRepository>(), Substitute.For <IResetPasswordThingy>(), Substitute.For <IResetPasswordEmailSender>()); controller.ModelState.AddModelError("", "invalid"); var model = new ResetPasswordStepOneModel(); var result = (ViewResult)controller.ResetPassword(model); Assert.That(result.ViewName, Is.EqualTo("Step1")); }
public void TestThat_ResetPassword_RedirectsToStepTwo_WhenTheModelHasAValidEmailAddress_ButTheUserProfileCouldNotBeFound() { var controller = new ResetPasswordController(Substitute.For <IUserProfileRepository>(), Substitute.For <IResetPasswordThingy>(), Substitute.For <IResetPasswordEmailSender>()); var model = new ResetPasswordStepOneModel { EmailAddress = "*****@*****.**" }; var result = (ViewResult)controller.ResetPassword(model); Assert.That(result.ViewName, Is.EqualTo("Step2")); }
public void TestThat_ResetPassword_AddsAValidationError_WhenTheModelHasAnInvalidUserNameAndEmailAddress() { var controller = new ResetPasswordController(Substitute.For <IUserProfileRepository>(), Substitute.For <IResetPasswordThingy>(), Substitute.For <IResetPasswordEmailSender>()); var model = new ResetPasswordStepOneModel { UserName = null, EmailAddress = null }; controller.ResetPassword(model); Assert.That(controller.ModelState.Count, Is.EqualTo(1)); }
public void TestThat_ResetPassword_SendsAnEmailToTheUser_WhenAValidUserIsFound_FromAnEmailAddress() { var userProfileRepository = Substitute.For <IUserProfileRepository>(); userProfileRepository.GetUserProfileByEmailAddress("*****@*****.**").Returns(new UserProfile { UserName = "******", EmailAddress = "*****@*****.**" }); var resetPasswordEmailSender = Substitute.For <IResetPasswordEmailSender>(); var controller = new ResetPasswordController(userProfileRepository, Substitute.For <IResetPasswordThingy>(), resetPasswordEmailSender); controller.SetupWithHttpContextAndUrlHelper(); var model = new ResetPasswordStepOneModel { EmailAddress = "*****@*****.**" }; controller.ResetPassword(model); resetPasswordEmailSender.Received().SendEmail(Arg.Any <string>(), Arg.Any <string>(), "*****@*****.**", Arg.Any <string>()); }
public void TestThat_ResetPassword_GeneratesAPasswordResetTokenForTheUser_WhenAValidUserIsFound_FromAUserName() { var userProfileRepository = Substitute.For <IUserProfileRepository>(); userProfileRepository.GetUserProfileByUserName("bob").Returns(new UserProfile { UserName = "******", EmailAddress = "*****@*****.**" }); var resetPasswordThingy = Substitute.For <IResetPasswordThingy>(); var controller = new ResetPasswordController(userProfileRepository, resetPasswordThingy, Substitute.For <IResetPasswordEmailSender>()); controller.SetupWithHttpContextAndUrlHelper(); var model = new ResetPasswordStepOneModel { UserName = "******" }; controller.ResetPassword(model); resetPasswordThingy.Received().GeneratePasswordResetToken("bob", Arg.Any <int>()); }