public ActionResult LogOn( LogOnModel model, string returnUrl )
        {
            if (ModelState.IsValid)
            {
                if (
                AllowDemo(model.UserName) ||
                    MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);

                    //CreateUserIfNotFound(model.UserName);

                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public void LogOn_Post_CreatesANewUser_IfQueryUserDoesNotFindOne_ForSuccessfullLogin()
        {
            // Arrange
            AccountController controller = GetAccountController();
            LogOnModel model = new LogOnModel()
            {
                UserName = "******",
                Password = "******",
                RememberMe = false
            };
            queryServiceStub.Expect(m => m.Query(null)).IgnoreArguments().Return( new List<User>() .AsQueryable());

            saveOrUpdateStub.Expect(m => m.Execute(null)).Repeat.Once();
            // Act
            ActionResult result = controller.LogOn(model, null);
            queryServiceStub.VerifyAllExpectations();
            saveOrUpdateStub.VerifyAllExpectations();
            // Assert
            Assert.IsInstanceOf<RedirectToRouteResult>(result);

            Assert.IsTrue(((MockFormsAuthenticationService)controller.FormsService).SignIn_WasCalled);
        }
        public void LogOn_Post_ReturnsViewIfModelStateIsInvalid()
        {
            // Arrange
            AccountController controller = GetAccountController();
            LogOnModel model = new LogOnModel()
            {
                UserName = "******",
                Password = "******",
                RememberMe = false
            };
            controller.ModelState.AddModelError("", "Dummy error message.");

            // Act
            ActionResult result = controller.LogOn(model, null);

            // Assert
            Assert.IsInstanceOf<ViewResult>(result);
            ViewResult viewResult = (ViewResult)result;
            Assert.AreEqual(model, viewResult.ViewData.Model);
        }
        public void LogOn_Post_ReturnsViewIfValidateUserFails()
        {
            // Arrange
            AccountController controller = GetAccountController();
            LogOnModel model = new LogOnModel()
            {
                UserName = "******",
                Password = "******",
                RememberMe = false
            };

            // Act
            ActionResult result = controller.LogOn(model, null);

            // Assert
            Assert.IsInstanceOf<ViewResult>(result);
            ViewResult viewResult = (ViewResult)result;
            Assert.AreEqual(model, viewResult.ViewData.Model);
            Assert.AreEqual("The user name or password provided is incorrect.", controller.ModelState[""].Errors[0].ErrorMessage);
        }
        public void LogOn_Post_ReturnsRedirectOnSuccess_WithReturnUrl()
        {
            // Arrange
            AccountController controller = GetAccountController();
            LogOnModel model = new LogOnModel()
            {
                UserName = "******",
                Password = "******",
                RememberMe = false
            };

            queryServiceStub.Expect(m => m.Query(null)).IgnoreArguments().Return( new List<User>(){ new User{UserName=model.UserName}}.AsQueryable());
            // Act
            ActionResult result = controller.LogOn(model, "/someUrl");
            queryServiceStub.VerifyAllExpectations();

            // Assert
            Assert.IsInstanceOf<RedirectResult>(result);
            RedirectResult redirectResult = (RedirectResult)result;
            Assert.AreEqual("/someUrl", redirectResult.Url);
            Assert.IsTrue(((MockFormsAuthenticationService)controller.FormsService).SignIn_WasCalled);
        }