public ActionResult JsonRegister(UserFormModel form) { if (ModelState.IsValid) { var command = Mapper.Map<UserFormModel, UserRegisterCommand>(form); command. Activated = true; command.RoleId = (Int32)UserRoles.User; IEnumerable<ValidationResult> errors = commandBus.Validate(command); ModelState.AddModelErrors(errors); if (ModelState.IsValid) { var result = commandBus.Submit(command); if (result.Success) { User user = userRepository.Get(u => u.Email == form.Email); formAuthentication.SetAuthCookie(this.HttpContext, UserAuthenticationTicketBuilder.CreateAuthenticationTicket( user)); return Json(new { success = true }); } else { ModelState.AddModelError("", "An unknown error occurred."); } } // If we got this far, something failed return Json(new { errors = GetErrorsFromModelState() }); } // If we got this far, something failed return Json(new { errors = GetErrorsFromModelState() }); }
public ActionResult Register(UserFormModel form) { if (ModelState.IsValid) { var command = new UserRegisterCommand { FirstName = form.FirstName, LastName = form.LastName, Email = form.Email, Password = form.Password, Activated = true, RoleId = (Int32)UserRoles.User }; IEnumerable<ValidationResult> errors = commandBus.Validate(command); ModelState.AddModelErrors(errors); if (ModelState.IsValid) { var result = commandBus.Submit(command); if (result.Success) { User user = userRepository.Get(u => u.Email == form.Email); formAuthentication.SetAuthCookie(this.HttpContext, UserAuthenticationTicketBuilder.CreateAuthenticationTicket( user)); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", "An unknown error occurred."); } } // If we got this far, something failed, redisplay form return View(form); } // If we got this far, something failed return Json(new { errors = GetErrorsFromModelState() }); }
public void User_Register_Redirects_To_Home() { //Arrange User user = new User() { Email = "*****@*****.**", UserId = 1, FirstName="Shiju", LastName="Var", DateCreated=DateTime.Now, Password = "******", RoleId = 2 }; commandBus.Setup(c => c.Submit(It.IsAny<UserRegisterCommand>())).Returns(new CommandResult(true)); userRepository.Setup(x => x.Get(It.IsAny<Expression<Func<User, bool>>>())).Returns(user); Mapper.CreateMap<UserFormModel, UserRegisterCommand>(); UserFormModel userForm = new UserFormModel { Email = "*****@*****.**", FirstName = "Shiju", LastName = "Var", Password = "******", ConfirmPassword = "******" }; AccountController controller = new AccountController(commandBus.Object, userRepository.Object, formsAuthentication.Object); // Act var result= controller.Register(userForm) as RedirectToRouteResult; // Assert Assert.AreEqual("Home", result.RouteValues["controller"]); Assert.AreEqual("Index", result.RouteValues["action"]); }