public ActionResult Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            if (model.Password != model.ConfirmPassword)
            {
                ModelState.AddModelError("Error", "Your passwords must match");
                return View(model);
            }

            var user = Mapper.Map<User>(model);
            try
            {
                _userStore.AddUser(user, model.Password);
            }
            catch (ArgumentException e)
            {
                ModelState.AddModelError("Error", e.Message);
                return View(model);
            }

            return RedirectToAction("Index", "Home");
        }
        public void Cant_Register_Same_User_Twice()
        {
            var userStore = new Mock<IUserStore>();
            var authService = new Mock<IFormsAuthenticationService>();
            userStore.Setup(u => u.AddUser(It.IsAny<User>(), "abc")).Throws(new ArgumentException("Username already taken"));
            var controller = new UserController(userStore.Object, authService.Object);
            var model = new RegisterViewModel
            {
                ConfirmPassword = "******",
                Password = "******",
                Email = "*****@*****.**",
                Username = "******"
            };

            var result = controller.Register(model);

            userStore.Verify(u => u.AddUser(It.IsAny<User>(), "abc"), Times.Once);
            Assert.IsInstanceOf(typeof(ViewResult), result);
            var view = (RegisterViewModel)((ViewResult)result).Model;
            Assert.AreEqual("abc", view.Password);
            Assert.AreEqual("abc", view.ConfirmPassword);
            Assert.AreEqual("abc", view.Username);
            Assert.AreEqual("*****@*****.**", view.Email);
        }
        public void Register_Passwords_Must_Match()
        {
            var userStore = new Mock<IUserStore>();
            var authService = new Mock<IFormsAuthenticationService>();
            var controller = new UserController(userStore.Object, authService.Object);

            var model = new RegisterViewModel
            {
                ConfirmPassword = "******",
                Password = "******",
                Username = "******"
            };

            var result = controller.Register(model);

            Assert.IsInstanceOf(typeof(ViewResult), result);
            var view = (RegisterViewModel)((ViewResult)result).Model;
            Assert.AreEqual("abc", view.Password);
            Assert.AreEqual("def", view.ConfirmPassword);
            Assert.AreEqual("abc", view.Username);
        }
        public void Register_User()
        {
            var userStore = new Mock<IUserStore>();
            var authService = new Mock<IFormsAuthenticationService>();
            userStore.Setup(u => u.AddUser(It.IsAny<User>(), "abc"));
            var controller = new UserController(userStore.Object, authService.Object);
            var model = new RegisterViewModel
            {
                ConfirmPassword = "******",
                Password = "******",
                Email = "*****@*****.**",
                Username = "******"
            };

            var result = controller.Register(model);

            userStore.Verify(u => u.AddUser(It.IsAny<User>(), "abc"), Times.Once);
            Assert.IsInstanceOf(typeof(RedirectToRouteResult), result);
            var redirect = (RedirectToRouteResult) result;
            Assert.AreEqual("Index", redirect.RouteValues["action"]);
            Assert.AreEqual("Home", redirect.RouteValues["controller"]);
        }