public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { object providerUserKey; // Attempt to register the user MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Members, out providerUserKey ); if (createStatus == MembershipCreateStatus.Success) { FormsService.SignIn((int)providerUserKey, model.UserName, false /* createPersistentCookie */); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", ErrorCodeToString(createStatus)); } } // If we got this far, something failed, redisplay form return View(model); }
public void Register_Post_ReturnsViewIfModelStateIsInvalid() { // Arrange AccountController controller = GetAccountController(); RegisterModel model = new RegisterModel() { UserName = "******", Password = "******", ConfirmPassword = "******" }; controller.ModelState.AddModelError("", "Dummy error message."); // Act ActionResult result = controller.Register(model); // Assert Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult viewResult = (ViewResult)result; Assert.AreEqual(model, viewResult.ViewData.Model); }
public void Register_Post_ReturnsViewIfRegistrationFails() { // Arrange AccountController controller = GetAccountController(); RegisterModel model = new RegisterModel() { UserName = "******", Password = "******", ConfirmPassword = "******" }; // Act ActionResult result = controller.Register(model); // Assert Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult viewResult = (ViewResult)result; Assert.AreEqual(model, viewResult.ViewData.Model); Assert.AreEqual("Username already exists. Please enter a different user name.", controller.ModelState[""].Errors[0].ErrorMessage); }
public void Register_Post_ReturnsRedirectOnSuccess() { // Arrange AccountController controller = GetAccountController(); RegisterModel model = new RegisterModel() { UserName = "******", Password = "******", ConfirmPassword = "******" }; // Act ActionResult result = controller.Register(model); // Assert Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); RedirectToRouteResult redirectResult = (RedirectToRouteResult)result; Assert.AreEqual("Home", redirectResult.RouteValues["controller"]); Assert.AreEqual("Index", redirectResult.RouteValues["action"]); }