public void CanLoginWithValidCredentials() { Mock<IAuthProvider> mock = new Mock<IAuthProvider>(); mock.Setup(m => m.Authenticate("admin", "123456")).Returns(true); LoginViewModel model = new LoginViewModel { Name = "admin", Password = "******" }; AccountController target = new AccountController(mock.Object); ActionResult result = target.Login(model, "/MyUrl"); Assert.IsInstanceOfType(result, typeof(RedirectResult)); Assert.AreEqual("/MyUrl", ((RedirectResult)result).Url); }
public void CanNotLoginWithInvalidCredentials() { Mock<IAuthProvider> mock = new Mock<IAuthProvider>(); mock.Setup(m => m.Authenticate("badUser", "badPass")).Returns(false); LoginViewModel model = new LoginViewModel { Name = "badUser", Password = "******" }; AccountController target = new AccountController(mock.Object); ActionResult result = target.Login(model, "/MyURL"); Assert.IsInstanceOfType(result, typeof(ViewResult)); Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid); }
public ActionResult Login(LoginViewModel model, string returnUrl) { if(ModelState.IsValid) { if(_authProvider.Authenticate(model.Name, model.Password)) { return Redirect(returnUrl ?? Url.Action("Index", "Admin")); } else { ModelState.AddModelError("", "User name or password are not correct"); return View(); } } else { return View(); } }