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("", "指定されたユーザー名またはパスワードが正しくありません。"); } } // ここで問題が発生した場合はフォームを再表示します return View(model); }
public void LogOn_Post_ReturnsViewIfModelStateIsInvalid() { // 準備 AccountController controller = GetAccountController(); LogOnModel model = new LogOnModel() { UserName = "******", Password = "******", RememberMe = false }; controller.ModelState.AddModelError("", "ダミー エラー メッセージ。"); // 実行 ActionResult result = controller.LogOn(model, null); // アサート Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult viewResult = (ViewResult)result; Assert.AreEqual(model, viewResult.ViewData.Model); }
public void LogOn_Post_ReturnsViewIfValidateUserFails() { // 準備 AccountController controller = GetAccountController(); LogOnModel model = new LogOnModel() { UserName = "******", Password = "******", RememberMe = false }; // 実行 ActionResult result = controller.LogOn(model, null); // アサート Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult viewResult = (ViewResult)result; Assert.AreEqual(model, viewResult.ViewData.Model); Assert.AreEqual("指定されたユーザー名またはパスワードが正しくありません。", controller.ModelState[""].Errors[0].ErrorMessage); }
public void LogOn_Post_ReturnsRedirectOnSuccess_WithReturnUrl() { // 準備 AccountController controller = GetAccountController(); LogOnModel model = new LogOnModel() { UserName = "******", Password = "******", RememberMe = false }; // 実行 ActionResult result = controller.LogOn(model, "/someUrl"); // アサート Assert.IsInstanceOfType(result, typeof(RedirectResult)); RedirectResult redirectResult = (RedirectResult)result; Assert.AreEqual("/someUrl", redirectResult.Url); Assert.IsTrue(((MockFormsAuthenticationService)controller.FormsService).SignIn_WasCalled); }