public ActionResult Register(RegisterUserCommand registerUserCommand, string confirmPassword) { ViewData["PasswordLength"] = membershipReadModel.MinPasswordLength; if(ValidateRegisterUserCommand(registerUserCommand, confirmPassword)) { var userName = registerUserCommand.UserName; var userId = registerUserCommand.UserId; var createStatus = commandService.RegisterUser(registerUserCommand); if (createStatus == MembershipCreateStatus.Success) { AddAuthenticationTicketToCookie(userId, userName, false, userName); return RedirectToAction("Index", "Home"); } ModelState.AddModelError("_FORM", ErrorCodeToString(createStatus)); } return View(); }
public MembershipCreateStatus RegisterUser(RegisterUserCommand registerUserCommand) { return (MembershipCreateStatus)commandBus.Execute(registerUserCommand); }
public void RegisterPostReturnsViewIfUsernameNotSpecified() { // Arrange var controller = GetAccountController(); var registerUserCommand = new RegisterUserCommand { UserName = "", Email = "email", Password = "******" }; // Act var result = (ViewResult)controller.Register(registerUserCommand, "password"); // Assert Assert.AreEqual(6, result.ViewData["PasswordLength"]); Assert.AreEqual("You must specify a username.", result.ViewData.ModelState["username"].Errors[0].ErrorMessage); }
public void RegisterPostReturnsViewIfRegistrationFails() { // Arrange var controller = GetAccountController(); var registerUserCommand = new RegisterUserCommand { UserName = "******", Email = "DuplicateUserName", Password = "******" }; // Act var result = (ViewResult) controller.Register(registerUserCommand, "badPass"); // Assert Assert.AreEqual(6, result.ViewData["PasswordLength"]); Assert.AreEqual("Username already exists. Please enter a different user name.", result.ViewData.ModelState["_FORM"].Errors[0].ErrorMessage); }
public void RegisterPostReturnsViewIfPasswordIsTooShort() { // Arrange var controller = GetAccountController(); var registerUserCommand = new RegisterUserCommand { UserName = "******", Email = "email", Password = "******" }; // Act var result = (ViewResult) controller.Register(registerUserCommand, "12345"); // Assert Assert.AreEqual(6, result.ViewData["PasswordLength"]); Assert.AreEqual("You must specify a password of 6 or more characters.", result.ViewData.ModelState["password"].Errors[0].ErrorMessage); }
public void RegisterPostReturnsViewIfNewPasswordDoesNotMatchConfirmPassword() { // Arrange var controller = GetAccountController(); var registerUserCommand = new RegisterUserCommand {UserName = "******", Email = "email", Password = "******"}; // Act var result = (ViewResult) controller.Register(registerUserCommand, "password2"); // Assert Assert.AreEqual(6, result.ViewData["PasswordLength"]); Assert.AreEqual("The new password and confirmation password do not match.", result.ViewData.ModelState["_FORM"].Errors[0].ErrorMessage); }
public void RegisterPostRedirectsHomeIfRegistrationSuccessful() { // Arrange var controller = GetAccountController(); var registerUserCommand = new RegisterUserCommand {UserName = "******", Email = "email", Password = "******"}; // Act var result = (RedirectToRouteResult) controller.Register(registerUserCommand, "goodPass"); // Assert Assert.AreEqual("Home", result.RouteValues["controller"]); Assert.AreEqual("Index", result.RouteValues["action"]); }
private bool ValidateRegisterUserCommand(RegisterUserCommand registerUserCommand, string confirmPassword) { var userName = registerUserCommand.UserName; var password = registerUserCommand.Password; var email = registerUserCommand.Email; if (String.IsNullOrEmpty(userName)) { ModelState.AddModelError("username", "You must specify a username."); } if (String.IsNullOrEmpty(email)) { ModelState.AddModelError("email", "You must specify an email address."); } if (password == null || password.Length < membershipReadModel.MinPasswordLength) { ModelState.AddModelError("password", String.Format(CultureInfo.CurrentCulture, "You must specify a password of {0} or more characters.", membershipReadModel.MinPasswordLength)); } if (!String.Equals(password, confirmPassword, StringComparison.Ordinal)) { ModelState.AddModelError("_FORM", "The new password and confirmation password do not match."); } return ModelState.IsValid; }