public void SignInPost_ValidUser_Redirects() { var model = new SignInViewModel { Username = "******", Password = "******", RememberMe = false, }; var fakeVolunteer = new Volunteer { Username = model.Username, Password = model.Password, Active = true, }; var formsAuthenticationServiceMock = new Mock<IFormsAuthenticationService>(); formsAuthenticationServiceMock.Setup(f => f.SetAuthCookie(It.IsAny<Volunteer>(), It.IsAny<bool>())) .Verifiable(); var volunteerDataServiceMock = new Mock<IDataService<Volunteer>>(); volunteerDataServiceMock.Setup(v => v.SelectOne(It.IsAny<Expression<Func<Volunteer, bool>>>())) .Returns(fakeVolunteer); volunteerDataServiceMock.Setup(v => v.HashPassword(It.IsAny<string>())) .Returns(model.Password); volunteerDataServiceMock.Setup(v => v.VerifyPassword(It.IsAny<string>(), It.IsAny<string>())) .Returns(true); var accountController = new AccountController( formsAuthenticationServiceMock.Object, volunteerDataServiceMock.Object); ActionResult result = accountController.SignIn(model, "home/index"); Assert.IsInstanceOf<RedirectResult>(result); }
public ActionResult SignIn(SignInViewModel model, string returnUrl) { if (!ModelState.IsValid) { return View(); } var volunteer = volunteerDataService.SelectOne(v => v.Username == model.Username); if (volunteer != null && !volunteer.Active) { ModelState.AddModelError("Username", "You account is inactive. Please contact us to activate your account."); return View(); } if (volunteer != null && volunteer.Password == volunteerDataService.HashPassword(model.Password, volunteer.Id)) { formsAuthenticationService.SetAuthCookie(volunteer, model.RememberMe); return Redirect(returnUrl ?? "~/"); } ModelState.AddModelError("Username", "Invalid username or password."); return View(); }
public void SignInPost_InvalidModelState_ReturnsView() { var accountController = new AccountController( new Mock<IFormsAuthenticationService>().Object, new Mock<IDataService<Volunteer>>().Object); accountController.ModelState.AddModelError("test", "error"); var model = new SignInViewModel(); ActionResult result = accountController.SignIn(model, "home/index"); Assert.IsInstanceOf<ViewResult>(result); }