public void Login_InvalidModel_DoesntCallApi()
        {
            var model = new Login ();
            _controller.ModelState.AddModelError("key", "error");

            var result = _controller.Login(model, null);

            _apiClient.Verify(x => x.ValidateAccount(model.Email, model.LoginPassword), Times.Never());
            Assert.That(((ViewResult)result).ViewName, Is.EqualTo("Index"));
        }
        public void Login_ValidCredentials_RedirectsToAccount_WhenNoReturnUrl()
        {
            var model = new Login { Email = Email, LoginPassword = Password };
            _apiClient.Setup(c => c.ValidateAccount(model.Email, model.LoginPassword)).Returns(true);

            var result = _controller.Login(model, null);

            Assert.That(result, Is.TypeOf<RedirectToRouteResult>());
            Assert.That(((RedirectToRouteResult)result).RouteValues["action"], Is.EqualTo("Account"));
            Assert.That(((RedirectToRouteResult)result).RouteValues["controller"], Is.EqualTo("Account"));
        }
        public void Login_IncorrectPassword_RedirectsLoginPage()
        {
            var model = new Login { Email = Email, LoginPassword = Password };
            _apiClient.Setup(c => c.ValidateAccount(model.Email, model.LoginPassword)).Returns(false);

            var result = _controller.Login(model, null);

            Assert.That(_session.Authenticated, Is.False);
            Assert.That(result, Is.TypeOf<RedirectToRouteResult>());
            Assert.That(((RedirectToRouteResult)result).RouteValues["action"], Is.EqualTo("Login"));
            Assert.That(_controller.TempData["errorMessage"], Is.EqualTo("Login unsuccessful."));
        }
        public void Login_ValidCredentials_PopulatesSession()
        {
            var model = new Login { Email = Email, LoginPassword = Password };
            _apiClient.Setup(c => c.ValidateAccount(model.Email, model.LoginPassword)).Returns(true);

            _controller.Login(model, null);

            Assert.That(_session.Authenticated, Is.True);
            Assert.That(_session.EmailAddress, Is.EqualTo(Email));
            Assert.That(_session.JustGivingPassword, Is.EqualTo(Password));
            Assert.That(_session.Pages, Is.Not.Null);
            _apiClient.Verify(s => s.GetPagesAsync(_session.EmailAddress, _session.UpdatePages), Times.Once());
        }
        public ActionResult Login(Login model, string returnUrl)
        {
            if (!ModelState.IsValid) return View("Index", new Account { Login = model } );

            if (_apiClient.ValidateAccount(model.Email, model.LoginPassword))
            {
                _currentUserSession.Login(model.Email, model.LoginPassword);

                //TODO: go and populate the fundraising pages async - this is a fill in solution until we can request paginated result sets from the API
                _apiClient.GetPagesAsync(_currentUserSession.EmailAddress, _currentUserSession.UpdatePages);

                if (returnUrl != null)
                {
                    return Redirect(returnUrl);
                }
                return RedirectToAction("Account", "Account");
            }

            TempData["errorMessage"] = "Login unsuccessful.";

            return RedirectToAction("Login");
        }
        public void Login_ValidCredentials_RedirectsToReturnUrl()
        {
            var model = new Login { Email = Email, LoginPassword = Password };
            _apiClient.Setup(c => c.ValidateAccount(model.Email, model.LoginPassword)).Returns(true);

            var result = _controller.Login(model, "/aReturnUrl");

            Assert.That(result, Is.TypeOf<RedirectResult>());
            Assert.That(((RedirectResult)result).Url, Is.EqualTo("/aReturnUrl"));
        }