public void ShouldLogOnUserAndRedirectToUrl()
        {
            var controller = CreateController();

               membershipService.ValidateUser("test", "password").Returns(true);
            urlValidationService.IsRedirectable(controller, "testurl").Returns(true);

            var model = new LogOnModel();
            model.UserName = "******";
            model.Password = "******";
            var url = "testurl";

            var actionResult = controller.LogOn(model, url);
            Expect(controller.ModelState.IsValid, Is.True);
            Expect(actionResult.AssertHttpRedirect().Url, Is.EqualTo(url));
            actionResult.AssertHttpRedirect();
        }
        public void ShouldNotLogOnForIncorrectCredentials()
        {
            var controller = CreateController();

            membershipService.ValidateUser("test", "password").Returns(false);

            var model = new LogOnModel();
            model.UserName = "******";
            model.Password = "******";
            var url = string.Empty;

            var actionResult = controller.LogOn(model, url);
            Expect(controller.ModelState.IsValid, Is.False);
            Expect(controller.ModelState[string.Empty].Errors.Count, Is.EqualTo(1));
            Expect(controller.ModelState[string.Empty].Errors[0].ErrorMessage,Is.EqualTo("The user name or password provided is incorrect."));
            actionResult.AssertViewRendered().WithViewData<LogOnModel>();
        }
        public void ShouldLogOnUserAndRedirectToHome()
        {
            var controller = CreateController();
            membershipService.ValidateUser("test","password").Returns(true);
            urlValidationService.IsRedirectable(controller,string.Empty).Returns(false);

            var model = new LogOnModel();
            model.UserName = "******";
            model.Password = "******";
            var url = string.Empty;

            var actionResult = controller.LogOn(model, url);
            Expect(controller.ModelState.IsValid, Is.True);
            actionResult.AssertActionRedirect();
        }
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {

                if(membershipService.ValidateUser(model.UserName,model.Password))
                {
                    authenticationService.SetAuthorizationCredentials(model.UserName,model.RememberMe);

                    if(urlValidationService.IsRedirectable(this,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);
        }