public void Cannot_Login_With_Invalid_Credentials()
 {
     var mock = new Mock<IAuthProvider>();
     mock.Setup(m => m.Authenticate("badUser", "badPass"))
         .Returns(false);
     var model = new LoginViewModel
     {
         UserName = "******",
         Password = "******"
     };
     var target = new AccountController(mock.Object);
     ActionResult result = target.Login(model, "/MyURL");
     Assert.IsInstanceOfType(result, typeof(ViewResult));
     Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
 }
 public void Can_Login_With_Valid_Credentials()
 {
     var mock = new Mock<IAuthProvider>();
     mock.Setup(m => m.Authenticate("admin", "secret"))
         .Returns(true);
     var model = new LoginViewModel
     {
         UserName = "******",
         Password = "******"
     };
     var target = new AccountController(mock.Object);
     ActionResult result = target.Login(model, "/MyURL");
     Assert.IsInstanceOfType(result, typeof(RedirectResult));
     Assert.AreEqual("/MyURL", ((RedirectResult)result).Url);
 }
 public ActionResult Login(LoginViewModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         if (authProvider.Authenticate(model.Username, model.Password))
         {
             return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
         }
         else
         {
             ModelState.AddModelError("", "Incorrect username or password");
             return View();
         }
     }
     else
     {
         return View();
     }
 }
Example #4
0
 public void Cannot_Login_With_Invalid_Credentials()
 {
     // Arrange - create a mock authentication provider
     Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
     mock.Setup(m => m.Authenticate("badUser",
     "badPass")).Returns(false);
     // Arrange - create the view model
     LoginViewModel model = new LoginViewModel
     {
         UserName = "******",
         Password = "******"
     };
     // Arrange - create the controller
     AccountController target = new AccountController(mock.Object);
     // Act - authenticate using valid credentials
     ActionResult result = target.Login(model, "/MyURL");
     // Assert
     Assert.IsInstanceOfType(result, typeof(ViewResult));
     Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
 }
Example #5
0
        public void Can_Login_With_Valid_Credentials()
        {
            // Arrange - create a mock authentication provider
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("admin", "secret")).Returns(true);
            // Arrange - create the view model
            LoginViewModel model = new LoginViewModel
            {
                UserName = "******",
                Password = "******"
            };
            // Arrange - create the controller
            AccountController target = new AccountController(mock.Object);

            // Act - authenticate using valid credentials
            ActionResult result = target.Login(model, "/MyURL");

            // Assert
            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("/MyURL", ((RedirectResult)result).Url);
        }