public void UserChangePasswordTest() { using (var lifetime = container.BeginLifetimeScope()) { IUserRepository userRepository = lifetime.Resolve<IUserRepository>(); DefaultCommandBus commandBus = lifetime.Resolve<DefaultCommandBus>(); User user = userRepository.Get(c => c.Email == "*****@*****.**"); Assert.IsNotNull(user, "Error: User was not found"); ChangePasswordCommand command = new ChangePasswordCommand(); command.UserId = user.UserId; command.OldPassword = "******"; command.NewPassword = "******"; IValidationHandler<ChangePasswordCommand> validationHandler = lifetime.Resolve<IValidationHandler<ChangePasswordCommand>>(); IEnumerable<ValidationResult> validations = commandBus.Validate(command, validationHandler); foreach (var val in validations) { Assert.IsNull(val, "Error: User password change did not validate " + val.Message); } ICommandHandler<ChangePasswordCommand> commnadHandler = lifetime.Resolve<ICommandHandler<ChangePasswordCommand>>(); ICommandResult result = commandBus.Submit(command, commnadHandler); Assert.IsNotNull(result, "Error: User password change did not work"); Assert.IsTrue(result.Success, "Error: User password change did not work"); } }
public ActionResult ChangePassword(ChangePasswordFormModel form) { if (ModelState.IsValid) { var user = HttpContext.User; var command = new ChangePasswordCommand { UserId = int.Parse(user.Identity.GetUserId()), OldPassword = form.OldPassword, NewPassword = form.NewPassword }; IEnumerable<ValidationResult> errors = commandBus.Validate(command); ModelState.AddModelErrors(errors); if (ModelState.IsValid) { var result = commandBus.Submit(command); if (result.Success) { return RedirectToAction("ChangePasswordSuccess"); } else { ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); } } } return View(form); }