public static void AssertModel(this LoginModel model,
                                LoginModel source)
 {
     Assert.AreEqual(source.Username, model.Username);
     Assert.AreEqual(source.Password, model.Password);
     Assert.AreEqual(source.RememberMe, model.RememberMe);
 }
 public void Login_Should_Return_Error_If_Username_Is_Missing()
 {
     var inModel = new LoginModel
                       {
                           Password = password,
                           RememberMe = true,
                           Username = string.Empty
                       };
     var result = controller.Login(inModel);
     AssertLoginViewResultOnError(result,
                                  "username", "Username is required",
                                  inModel);
 }
 public void Login_Redirects_To_Index_Home_On_Success()
 {
     mocks.MembershipProvider
         .Expect(p => p.ValidateUser(username, password))
         .Returns(true);
     var inModel = new LoginModel
                       {
                           Password = password,
                           RememberMe = true,
                           Username = username
                       };
     var result = controller.Login(inModel);
     result.AssertRedirectToRouteResult("Index", "Home");
     mocks.MembershipProvider.VerifyAll();
 }
        public void Login_Should_Return_Error_For_Wrong_Credentials()
        {
            var aUsername = "******";
            var aPassword = "******";

            mocks.MembershipProvider
                .Expect(p => p.ValidateUser(aUsername, aPassword))
                .Returns(false);
            var inModel = new LoginModel
                              {
                                  Password = aPassword,
                                  RememberMe = true,
                                  Username = aUsername
                              };
            var result = controller.Login(inModel);
            AssertLoginViewResultOnError(result,
                                         "provider",
                                         "The username or password provided is incorrect.",
                                         inModel);
            mocks.MembershipProvider.VerifyAll();
        }
 //[Test]
 //public void Login_User_Can_Login()
 //{
 //    var controller = TestControllerFactory.GetControllerWithFakeContext<AccountController>("POST");
 //    for (var i = 0; i < 20; i++)
 //    {
 //        var aUsername = "******" + i;
 //        var aPassword = "******" + i;
 //        var aUserGuid = Guid.NewGuid();
 //        var aEmail = string.Format("{0}@dotnetfactory.com", aUsername);
 //        var rememberme = true;
 //        controller.SetMockMembershipUser(aUsername, aUserGuid, aEmail);
 //        controller.SetMockCurrentUser(mocks.MembershipUser.Object);
 //        mocks.FormsAuthentication
 //            .Expect(f => f.SetAuthCookie(aUsername, rememberme));
 //        mocks.MembershipProvider
 //            .Expect(p => p.ValidateUser(aUsername, aPassword))
 //            .Returns(true);
 //        mocks.MembershipProvider
 //            .Expect(p => p.GetUser(aUsername, true))
 //            .Returns(mocks.MembershipUser.Object);
 //        //todo we need to mock the call to CreateUserFromMembershipUser
 //        //mocks.AccountService
 //        //    .Expect(a => a.CreateUserFromMembershipUser(mocks.MembershipUser.Object))
 //        //    .Returns(mocks.CurrentUser.Object);
 //        //mocks.Session
 //        //    .Expect(s => s.Add("currentuser", mocks.CurrentUser.Object))
 //        //    ;
 //        //mocks.Session.Expect(s => s["currentuser"])
 //        //    .Callback(sc => Assert.AreEqual(mocks.CurrentUser.Object, sc));
 //        //mocks.Session.ExpectSet(s => s["currentuser"], mocks.CurrentUser.Object);
 //        var result = controller.Login(aUsername, aPassword, rememberme);
 //        MvcAssert.AssertRedirectToRouteResult(result, "Index", "Home");
 //        mocks.FormsAuthentication.VerifyAll();
 //        mocks.MembershipProvider.VerifyAll();
 //        mocks.MembershipUser.VerifyAll();
 //        //mocks.Session.VerifyAll();
 //        Assert.Inconclusive("Session is not verified");
 //        //todo figure out a way to mock and verify that the session is set
 //    }
 //}
 private void AssertLoginViewResultOnError(ActionResult result,
                                           string errorKey,
                                           string errorMessage,
                                           LoginModel model)
 {
     Assert.IsNotNull(result);
     Assert.IsInstanceOfType(typeof (ViewResult), result);
     controller.ViewData.ModelState.AssertErrorMessage(errorKey, errorMessage);
     Assert.IsInstanceOfType(typeof (LoginModel),
                             ((ViewResult) result).ViewData.Model);
     var outModel = (((ViewResult) result).ViewData.Model as LoginModel);
     outModel.AssertModel(model);
     Assert.AreEqual("Login", controller.ViewData["Title"], "Page title is wrong");
 }