Ejemplo n.º 1
0
        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();
            LogOnModel model = new LogOnModel()
            {
                UserName = "******",
                Password = "******",
                RememberMe = false
            };

            // Act
            ActionResult result = controller.LogOn(model, null);

            // Assert
            Assert.That(result, Is.InstanceOf<ViewResult>());
            ViewResult viewResult = result as ViewResult;
            Assert.That(viewResult.ViewData.Model, Is.EqualTo(model));
            Assert.That(
                controller.ModelState[""].Errors[0].ErrorMessage,
                Is.EqualTo("The user name or password provided is incorrect.")
            );
        }
        public void LogOn_Post_ReturnsViewIfModelStateIsInvalid()
        {
            // Arrange
            AccountController controller = GetAccountController();
            LogOnModel model = new LogOnModel()
            {
                UserName = "******",
                Password = "******",
                RememberMe = false
            };
            controller.ModelState.AddModelError("", "Dummy error message.");

            // Act
            ActionResult result = controller.LogOn(model, null);

            // Assert
            Assert.That(result, Is.InstanceOf<ViewResult>());
            ViewResult viewResult = result as ViewResult;
            Assert.That(viewResult.ViewData.Model, Is.EqualTo(model));
        }
        public void LogOn_Post_ReturnsRedirectOnSuccess_WithReturnUrl()
        {
            // Arrange
            AccountController controller = GetAccountController();
            LogOnModel model = new LogOnModel()
            {
                UserName = "******",
                Password = "******",
                RememberMe = false
            };

            // Act
            ActionResult result = controller.LogOn(model, "/someUrl");

            // Assert
            Assert.That(result, Is.InstanceOf<RedirectResult>());
            RedirectResult redirectResult = result as RedirectResult;
            Assert.That(redirectResult.Url, Is.EqualTo("/someUrl"));
            Assert.That(
                ((MockFormsAuthenticationService)controller.FormsService).SignIn_WasCalled,
                Is.True
            );
        }