public void Change_Password_With_Uncorrect_Data() { var userMock = new Mock <IUserRepository>(); var eventBusMock = new Mock <IEventBus>(); userMock.Setup(m => m.GetAll()).Returns(new[] { new User { Id = 1, Login = "******", Password = "******", Status = UserStatus.Active } }); var changingPasswordUser = new ChangingPasswordUser { Email = "user1", Password = "******" }; IChangePasswordToUser target = new ChangePasswordToUser(userMock.Object, eventBusMock.Object); var ex = Assert.Throws <Exception>(() => target.Invoke(changingPasswordUser)); Assert.True( ex.Message.Contains("Cannot change password for user with email: user1, because password was wrong.")); }
public void change_Password_to_Non_Existing_User() { var userMock = new Mock <IUserRepository>(); var eventBusMock = new Mock <IEventBus>(); userMock.Setup(m => m.GetAll()).Returns(new[] { new User { Id = 1, Login = "******", Status = UserStatus.Active } }); IChangePasswordToUser target = new ChangePasswordToUser(userMock.Object, eventBusMock.Object); var changingPasswordUser = new ChangingPasswordUser { Email = "user2" }; Exception ex = Assert.Throws <ArgumentNullException>(() => target.Invoke(changingPasswordUser)); Assert.True( ex.Message.Contains( "Cannot change password for user with email: user2, because this user does not exist.")); }
public void Change_Password() { var userMock = new Mock <IUserRepository>(); var eventBusMock = new Mock <IEventBus>(); var user1 = new User { Id = 1, Login = "******", Password = "******", Status = UserStatus.Active }; userMock.Setup(m => m.GetAll()).Returns(new[] { user1 }); var changingPasswordUser = new ChangingPasswordUser { Email = "user1", Password = "******", NewPassword = "******" }; IChangePasswordToUser target = new ChangePasswordToUser(userMock.Object, eventBusMock.Object); target.Invoke(changingPasswordUser); Assert.True(user1.Password == "zzzz"); userMock.Verify(x => x.Edit(user1)); }
public void PasswordChangeForUser(ChangingPasswordUser user) { var parameters = new Dictionary <string, string> { { "userEmail", user.Email }, { "password", user.Password }, { "newPassword", user.NewPassword }, { "confirmedNewPassword", user.ConfirmNewPassword } }; string authServerAnswer; JsonObject parsed; try { authServerAnswer = _postService.SendAndGetAnswer(Configuration.ChangingPasswordUserAddress, parameters); } catch (Exception) { throw new ServerConnectionException(); } try { parsed = JsonConvert.DeserializeObject <JsonObject>(authServerAnswer); if (parsed == null) { throw new Exception(); } } catch { throw new ServerNotValidAnswerException(); } switch (parsed.Result) { case JsonValues.RequiredCharactersViolation: throw new RequiredCharactersViolationException(); case JsonValues.PasswordLengthIncorrect: throw new PasswordLengthIncorrectException(); case JsonValues.PasswordTooCommon: throw new PasswordTooCommonException(); case JsonValues.UsersPasswordChanged: break; default: throw new PasswordNotChangedException(); } }
public void Invoke(ChangingPasswordUser userWithNewPassword) { var user = _userRepository.GetAll().FirstOrDefault(u => u.Login == userWithNewPassword.Email); if (user == null) { throw new ArgumentNullException( $"Cannot change password for user with email: {userWithNewPassword.Email}, because this user does not exist."); } if (user.Password != userWithNewPassword.Password) { throw new Exception( $"Cannot change password for user with email: {userWithNewPassword.Email}, because password was wrong."); } user.Password = userWithNewPassword.NewPassword; user.LastPasswordChange = DateTime.UtcNow; _userRepository.Edit(user); _eventBus.Publish(new UserChangedPassword(user.Id, user.FirstName, user.LastName)); }
public IActionResult ChangePassword(ChangingPasswordUser user, string login) { if (user.NewPassword != user.ConfirmNewPassword) { Alert(AlertType.Warning, "New password must be the same as confirmed password."); ViewBag.userName = login; return(View("ChangePassword")); } if ((user.Password == null) || (user.NewPassword == null) || (user.ConfirmNewPassword == null)) { Alert(AlertType.Warning, "You have to fill each field."); ViewBag.UserName = login; return(View("ChangePassword")); } user.Email = login; try { _remoteAccountService.PasswordChangeForUser(user); } catch (ServerConnectionException) { Alert(AlertType.Warning, ServerConnectionException.Message); ViewBag.UserName = login; return(View("ChangePassword")); } catch (ServerNotValidAnswerException) { Alert(AlertType.Warning, ServerNotValidAnswerException.Message); ViewBag.UserName = login; return(View("ChangePassword")); } catch (PasswordNotChangedException) { Alert(AlertType.Warning, PasswordNotChangedException.Message); ViewBag.UserName = login; return(View("ChangePassword")); } catch (PasswordLengthIncorrectException) { Alert(AlertType.Warning, PasswordLengthIncorrectException.Message); ViewBag.UserName = login; return(View("ChangePassword")); } catch (RequiredCharactersViolationException) { Alert(AlertType.Warning, RequiredCharactersViolationException.Message); ViewBag.UserName = login; return(View("ChangePassword")); } catch (PasswordTooCommonException) { Alert(AlertType.Warning, PasswordTooCommonException.Message); ViewBag.UserName = login; return(View("ChangePassword")); } _sessionService.LogOut(HttpContext); Alert(AlertType.Information, "Please log in with new password."); return(RedirectToAction("LoginPage")); }