public ActionResult LogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (MembershipService.ValidateUser(model.UserName, model.Password)) { FormsService.SignIn(model.UserName, model.RememberMe); 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_ReturnsViewIfValidateUserFails() { // Arrange AccountController controller = GetAccountController(); var model = new LogOnModel { UserName = "******", Password = "******", RememberMe = false }; // Act ActionResult result = controller.LogOn(model, null); // Assert var 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_ReturnsViewIfModelStateIsInvalid() { // Arrange AccountController controller = GetAccountController(); var model = new LogOnModel { UserName = "******", Password = "******", RememberMe = false }; controller.ModelState.AddModelError("", "Dummy error message."); // Act ActionResult result = controller.LogOn(model, null); // Assert var viewResult = (ViewResult) result; Assert.AreEqual(model, viewResult.ViewData.Model); }
public void LogOn_Post_ReturnsRedirectOnSuccess_WithoutReturnUrl() { // Arrange AccountController controller = GetAccountController(); var model = new LogOnModel { UserName = "******", Password = "******", RememberMe = false }; // Act ActionResult result = controller.LogOn(model, null); // Assert var redirectResult = (RedirectToRouteResult) result; Assert.AreEqual("Home", redirectResult.RouteValues["controller"]); Assert.AreEqual("Index", redirectResult.RouteValues["action"]); Assert.IsTrue(((MockFormsAuthenticationService) controller.FormsService).SignIn_WasCalled); }