public void DoesNotSendEmailChangeConfirmationNoticeWhenUserWasNotConfirmed() { var user = new User { Username = "******", UnconfirmedEmailAddress = "*****@*****.**", }; var controller = GetController <UsersController>(); controller.SetUser(user); GetMock <IUserService>() .Setup(u => u.FindByUsernameAndPassword(It.IsAny <string>(), It.IsAny <string>())) .Returns(user); GetMock <IUserService>() .Setup(u => u.ChangeEmailAddress(It.IsAny <User>(), It.IsAny <string>())) .Callback(() => user.UpdateEmailAddress("*****@*****.**", () => "new-token")); var model = new ChangeEmailRequestModel { NewEmail = "*****@*****.**" }; controller.ChangeEmail(model); Assert.Equal("Your new email address was saved!", controller.TempData["Message"]); GetMock <IUserService>() .Verify(u => u.ChangeEmailAddress(user, "*****@*****.**")); GetMock <IMessageService>() .Verify(m => m.SendEmailChangeConfirmationNotice(It.IsAny <MailAddress>(), It.IsAny <string>()), Times.Never()); }
public void DoesNotSendEmailChangeConfirmationNoticeWhenAddressDoesntChange() { var user = new User { EmailAddress = "*****@*****.**", Username = "******", }; GetMock <AuthenticationService>() .Setup(u => u.Authenticate("aUsername", "password")) .Returns(new AuthenticatedUser(user, new Credential())); GetMock <IUserService>() .Setup(u => u.ChangeEmailAddress(It.IsAny <User>(), It.IsAny <string>())) .Callback(() => user.UpdateEmailAddress("*****@*****.**", () => "new-token")); var controller = GetController <UsersController>(); controller.SetCurrentUser(user); var model = new ChangeEmailRequestModel { NewEmail = "*****@*****.**", Password = "******" }; controller.ChangeEmail(model); GetMock <IUserService>() .Verify(u => u.ChangeEmailAddress(user, "*****@*****.**"), Times.Never()); GetMock <IMessageService>() .Verify(m => m.SendEmailChangeConfirmationNotice(It.IsAny <MailAddress>(), It.IsAny <string>()), Times.Never()); }
public void SendsEmailChangeConfirmationNoticeWhenChangingAConfirmedEmailAddress() { var user = new User { Username = "******", EmailAddress = "*****@*****.**", EmailAllowed = true }; var controller = GetController <UsersController>(); controller.SetUser(user); GetMock <IUserService>() .Setup(u => u.FindByUsernameAndPassword(It.IsAny <string>(), It.IsAny <string>())) .Returns(user); GetMock <IUserService>() .Setup(u => u.ChangeEmailAddress(user, "*****@*****.**")) .Callback(() => user.UpdateEmailAddress("*****@*****.**", () => "token")); var model = new ChangeEmailRequestModel { NewEmail = "*****@*****.**" }; var result = controller.ChangeEmail(model); GetMock <IMessageService>() .Verify(m => m.SendEmailChangeConfirmationNotice(It.IsAny <MailAddress>(), It.IsAny <string>())); }
public virtual ActionResult ChangeEmail() { User user = UserService.FindByUsername(User.Identity.Name); var model = new ChangeEmailRequestModel { NewEmail = String.IsNullOrEmpty(user.UnconfirmedEmailAddress) ? user.EmailAddress : user.UnconfirmedEmailAddress }; return(View(model)); }
public virtual ActionResult ChangeEmail(ChangeEmailRequestModel model) { if (!ModelState.IsValid) { return(View(model)); } User user = UserService.FindByUsernameAndPassword(Identity.Name, model.Password); if (user == null) { ModelState.AddModelError("Password", Strings.CurrentPasswordIncorrect); return(View(model)); } if (String.Equals(model.NewEmail, user.LastSavedEmailAddress, StringComparison.OrdinalIgnoreCase)) { // email address unchanged - accept return(RedirectToAction(MVC.Users.Edit())); } try { UserService.ChangeEmailAddress(user, model.NewEmail); } catch (EntityException e) { ModelState.AddModelError("NewEmail", e.Message); return(View(model)); } if (user.Confirmed) { var confirmationUrl = Url.ConfirmationUrl( MVC.Users.Confirm(), user.Username, user.EmailConfirmationToken, protocol: Request.Url.Scheme); MessageService.SendEmailChangeConfirmationNotice(new MailAddress(user.UnconfirmedEmailAddress, user.Username), confirmationUrl); TempData["Message"] = "Your email address has been changed! We sent a confirmation email to verify your new email. When you confirm the new email address, it will take effect and we will forget the old one."; } else { TempData["Message"] = "Your new email address was saved!"; } return(RedirectToAction(MVC.Users.Edit())); }
public virtual ActionResult ChangeEmail(ChangeEmailRequestModel model) { if (!ModelState.IsValid) { return View(model); } User user = UserService.FindByUsernameAndPassword(Identity.Name, model.Password); if (user == null) { ModelState.AddModelError("Password", Strings.CurrentPasswordIncorrect); return View(model); } if (String.Equals(model.NewEmail, user.LastSavedEmailAddress, StringComparison.OrdinalIgnoreCase)) { // email address unchanged - accept return RedirectToAction(MVC.Users.Edit()); } try { UserService.ChangeEmailAddress(user, model.NewEmail); } catch (EntityException e) { ModelState.AddModelError("NewEmail", e.Message); return View(model); } if (user.Confirmed) { var confirmationUrl = Url.ConfirmationUrl( MVC.Users.Confirm(), user.Username, user.EmailConfirmationToken, protocol: Request.Url.Scheme); MessageService.SendEmailChangeConfirmationNotice(new MailAddress(user.UnconfirmedEmailAddress, user.Username), confirmationUrl); TempData["Message"] = "Your email address has been changed! We sent a confirmation email to verify your new email. When you confirm the new email address, it will take effect and we will forget the old one."; } else { TempData["Message"] = "Your new email address was saved!"; } return RedirectToAction(MVC.Users.Edit()); }
public virtual ActionResult ChangeEmail() { User user = UserService.FindByUsername(User.Identity.Name); var model = new ChangeEmailRequestModel { NewEmail = String.IsNullOrEmpty(user.UnconfirmedEmailAddress) ? user.EmailAddress : user.UnconfirmedEmailAddress }; return View(model); }
public void DoesNotSendEmailChangeConfirmationNoticeWhenUserWasNotConfirmed() { var user = new User { Username = "******", UnconfirmedEmailAddress = "*****@*****.**", }; GetMock<AuthenticationService>() .Setup(u => u.Authenticate("aUsername", "password")) .Returns(new AuthenticatedUser(user, new Credential())); GetMock<IUserService>() .Setup(u => u.ChangeEmailAddress(It.IsAny<User>(), It.IsAny<string>())) .Callback(() => user.UpdateEmailAddress("*****@*****.**", () => "new-token")); var controller = GetController<UsersController>(); controller.SetCurrentUser(user); var model = new ChangeEmailRequestModel { NewEmail = "*****@*****.**", Password = "******" }; controller.ChangeEmail(model); Assert.Equal("Your new email address was saved!", controller.TempData["Message"]); GetMock<IUserService>() .Verify(u => u.ChangeEmailAddress(user, "*****@*****.**")); GetMock<IMessageService>() .Verify(m => m.SendEmailChangeConfirmationNotice(It.IsAny<MailAddress>(), It.IsAny<string>()), Times.Never()); }
public void SendsEmailChangeConfirmationNoticeWhenChangingAConfirmedEmailAddress() { var user = new User { Username = "******", EmailAddress = "*****@*****.**", EmailAllowed = true }; GetMock<AuthenticationService>() .Setup(u => u.Authenticate("theUsername", "password")) .Returns(new AuthenticatedUser(user, new Credential())); GetMock<IUserService>() .Setup(u => u.ChangeEmailAddress(user, "*****@*****.**")) .Callback(() => user.UpdateEmailAddress("*****@*****.**", () => "token")); var controller = GetController<UsersController>(); controller.SetCurrentUser(user); var model = new ChangeEmailRequestModel { NewEmail = "*****@*****.**", Password = "******" }; var result = controller.ChangeEmail(model); GetMock<IMessageService>() .Verify(m => m.SendEmailChangeConfirmationNotice(It.IsAny<MailAddress>(), It.IsAny<string>())); }
public void DoesNotSendEmailChangeConfirmationNoticeWhenAddressDoesntChange() { var user = new User { EmailAddress = "*****@*****.**", Username = "******", }; var controller = GetController<UsersController>(); controller.SetUser(user); GetMock<IUserService>() .Setup(u => u.FindByUsernameAndPassword(It.IsAny<string>(), It.IsAny<string>())) .Returns(user); GetMock<IUserService>() .Setup(u => u.ChangeEmailAddress(It.IsAny<User>(), It.IsAny<string>())) .Callback(() => user.UpdateEmailAddress("*****@*****.**", () => "new-token")); var model = new ChangeEmailRequestModel { NewEmail = "*****@*****.**" }; controller.ChangeEmail(model); GetMock<IUserService>() .Verify(u => u.ChangeEmailAddress(user, "*****@*****.**"), Times.Never()); GetMock<IMessageService>() .Verify(m => m.SendEmailChangeConfirmationNotice(It.IsAny<MailAddress>(), It.IsAny<string>()), Times.Never()); }