public virtual ActionResult Login(LoginModel loginModel, string ReturnUrl)
        {
            this.ViewData["ReturnUrl"] = ReturnUrl;

            if (!this.ModelState.IsValid)
            {
                loginModel.Password = string.Empty;
                return View(loginModel);
            }

            var authenticated = this.accountRepository.Authenticate(loginModel.Username, loginModel.Password);

            if (!authenticated)
            {
                loginModel.Password = string.Empty;
                this.ModelState.AddModelError("invalidUser", "The credentials provided are not valid");
                return View(loginModel);
            }

            if (!string.IsNullOrWhiteSpace(ReturnUrl))
            {
                return Redirect(ReturnUrl);
            }

            return RedirectToAction(MVC.Product.List());
        }
            public void when_the_user_enters_valid_credentials_and_the_returnUrl_parameter_is_not_empty_it_should_redirect_to_that_url()
            {
                var sut = AccountControllerTests.GetAccountController();
                var loginModel = new LoginModel { Username = "******", Password = "******" };

                var res = sut.Login(loginModel, "de") as RedirectResult;

                res.Url.Should().Be("de");
            }
            public void when_the_user_enters_invalid_credentials_it_should_render_the_default_view_adding_an_error_message_to_the_view()
            {
                var sut = AccountControllerTests.GetAccountController();
                var loginModel = new LoginModel { Password = "******", Username = "******" };

                var res = sut.Login(loginModel, "de") as ViewResult;
                var model = res.Model as LoginModel;

                model.Should().Be(loginModel);
                sut.ModelState["invalidUser"].Should().NotBeNull();
                sut.ModelState["invalidUser"].Errors.Count.Should().Be(1);
                sut.ModelState["invalidUser"].Errors[0].ErrorMessage.Should().Be("The credentials provided are not valid");
                res.ViewData["ReturnUrl"].ToString().Should().Be("de");
            }
            public void when_the_model_contains_validation_errors_it_should_render_the_default_view_and_pass_the_current_model_back_to_the_view()
            {
                var sut = AccountControllerTests.GetAccountController();
                var loginModel = new LoginModel();

                sut.ModelState.AddModelError("error", "error");

                var res = sut.Login(loginModel, "return") as ViewResult;
                var model = res.Model as LoginModel;

                model.Should().Be(loginModel);
                res.ViewName.Should().BeEmpty();
                res.ViewData["ReturnUrl"].ToString().Should().Be("return");
            }
            public void when_the_user_enters_valid_credentials_and_the_returnUrl_parameter_is_empty_it_should_redirect_to_the_default_store_url()
            {
                var sut = AccountControllerTests.GetAccountController();
                var loginModel = new LoginModel { Username = "******", Password = "******" };

                var res = sut.Login(loginModel, "") as RedirectToRouteResult;

                res.RouteValues["controller"].ToString().Should().Be("Product");
                res.RouteValues["action"].ToString().Should().Be("List");
                res.RouteValues["area"].ToString().Should().Be("");
                res.RouteValues["page"].ToString().Should().Be("1");
            }