public void Login_InvalidCredentials_ErrorMessage()
        {
            // arrange
            var model = new LoginViewModel { Password = "******", Email = "*****@*****.**", RememberMe = false };
            var returnUrl = "/home/index";
            var mockProvider = new MyMvcApplication.Models.Fakes.StubIAuthProvider
            {
                ValidateUserStringString = (usr, pwd) =>
                {
                    return usr == "*****@*****.**" && pwd == "123";
                },
                SetAuthCookieStringBoolean = (email, rememberMe) =>
                {

                }
            };

            // act
            var controller = new AccountDController(mockProvider);
            var viewResult = controller.Login(model, returnUrl) as ViewResult;

            // assert
            Assert.AreEqual(viewResult.ViewName, string.Empty);
            Assert.AreEqual(controller.ModelState["auth"].Errors[0].ErrorMessage, "The user name or password incorrect.", "Incorrect error message");
        }
        public void Login_ValidCredentials_AuthenticateAndRedirect()
        {
            // arrange
            var model = new LoginViewModel { Password = "******", Email = "*****@*****.**", RememberMe = true };
            var returnUrl = "/home/index";
            bool calledValidateUser = false;
            bool calledSetCookie = false;
            using(ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.ValidateUserStringString = (usr, pwd) =>
                {
                    calledValidateUser = true;
                    return usr == "*****@*****.**" && pwd == "123";
                };

                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (email, remeberMe) =>
                {
                    calledSetCookie = true;
                };

                // act
                AccountController controller = new AccountController();
                var redirectResult = controller.Login(model, returnUrl) as RedirectResult;

                // assert
                Assert.IsTrue(calledValidateUser, "Membership.ValidateUser not invoked");
                Assert.IsTrue(calledSetCookie, "FormsAuthentication.SetAuthCookie not invoked");
                Assert.AreEqual(returnUrl, redirectResult.Url);
            }
        }
        public void Login_ValidCredentials_AuthenticateAndRedirect()
        {
            // arrange
            var model = new LoginViewModel { Password = "******", Email = "*****@*****.**", RememberMe = true };
            var returnUrl = "/home/index";
            bool calledValidateUser = false;
            bool calledSetCookie = false;
            var mockProvider = new MyMvcApplication.Models.Fakes.StubIAuthProvider
            {
                ValidateUserStringString = (usr, pwd) =>
                {
                    calledValidateUser = true;
                    return usr == "*****@*****.**" && pwd == "123";
                },
                SetAuthCookieStringBoolean = (email, rememberMe) =>
                {
                    calledSetCookie = true;
                }                
            };

            // act
            var controller = new AccountDController(mockProvider);
            var redirectResult = controller.Login(model, returnUrl) as RedirectResult;

            // assert
            Assert.IsTrue(calledValidateUser, "Membership.ValidateUser not invoked");
            Assert.IsTrue(calledSetCookie, "FormsAuthentication.SetAuthCookie not invoked");
            Assert.AreEqual(returnUrl, redirectResult.Url);
        }
 public ActionResult Login(LoginViewModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         if (Membership.ValidateUser(model.Email, model.Password))
         {
             FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
             return Redirect(returnUrl);
         }
         ModelState.AddModelError("auth", "The user name or password incorrect.");
     }
     return View(model);
 }
        public void Login_InvalidCredentials_ErrorMessage()
        {
            // arrange
            var model = new LoginViewModel { Password = "******", Email = "*****@*****.**", RememberMe = false };
            var returnUrl = "/home/index";
            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.ValidateUserStringString = (usr, pwd) =>
                {
                    return usr == "*****@*****.**" && pwd == "123";
                };

                // act
                AccountController controller = new AccountController();
                var viewResult = controller.Login(model, returnUrl) as ViewResult;
                
                // assert
                Assert.AreEqual(viewResult.ViewName, string.Empty);
                Assert.AreEqual(controller.ModelState["auth"].Errors[0].ErrorMessage, "The user name or password incorrect.", "Incorrect error message");
            }
        }