public ActionResult Create(SignInViewModel signInModel) { if (!this.ModelState.IsValid) { return View("New"); } var dbUser = Users.FirstOrDefault(u => u.UserName.Equals(signInModel.UserName)); if (dbUser == null || !dbUser.VerifyPassword(signInModel.Password)) { this.ModelState.AddModelError("*", "User Name or Password is wrong."); return View("New"); } _formsAuthentication.SetAuthCookie(signInModel.UserName, createPersistentCookie: signInModel.RememberMe); var routeValues = dbUser.RequiredToChangePassword ? new { controller = "Sessions", action = "Edit" } : new { controller = "Home", action = "Index" }; return RedirectToRoute(routeValues); }
private static void Sessions_create_AssertReturnViewWithError(SignInViewModel invalidSignInModel) { using (var context = new HeliosDbContext()) { SaveUserInDb(context, userName: "******", password: "******"); var mockFormsAuth = new Mock<IFormsAuthentication>(); var isAuthenticated = false; mockFormsAuth .Setup(f => f.SetAuthCookie(invalidSignInModel.UserName, /* createPersistentCookie */ false)) .Callback(() => isAuthenticated = true); var controller = new SessionsController(context, mockFormsAuth.Object); var result = controller.Create(invalidSignInModel); Assert.IsType<ViewResult>(result); var viewResult = result as ViewResult; Assert.Equal("New", viewResult.ViewName); Assert.Equal(false, controller.ModelState.IsValid); Assert.Equal(false, isAuthenticated); } }
public void Test_sessions_create_with_wrong_username_should_return_view_with_error() { var invalidSignInModel = new SignInViewModel() { UserName = "******", Password = "******" }; Sessions_create_AssertReturnViewWithError(invalidSignInModel); }
public void Test_sessions_create_with_invalid_username_should_return_view_with_error() { var invalidSignInModel = new SignInViewModel() { UserName = "", Password = "******" }; using (var context = new HeliosDbContext()) { var mockFormsAuth = new Mock<IFormsAuthentication>(); var isAuthenticated = false; mockFormsAuth .Setup(f => f.SetAuthCookie(invalidSignInModel.UserName, /* createPersistentCookie */ false)) .Callback(() => isAuthenticated = true); var controller = new SessionsController(context, mockFormsAuth.Object); var validationResults = ModelTestHelper.ValidateModel<SignInViewModel>(invalidSignInModel); foreach (var validationResult in validationResults) { controller.ModelState.AddModelError("*", validationResult.ErrorMessage); } var result = controller.Create(invalidSignInModel); Assert.IsType<ViewResult>(result); var viewResult = result as ViewResult; Assert.Equal("New", viewResult.ViewName); Assert.Equal(false, controller.ModelState.IsValid); Assert.Equal(false, isAuthenticated); } }
public void Test_sessions_create_should_sign_in_and_redirect_to_sessions_reset_for_first_time_user() { var validSignInModel = new SignInViewModel() { UserName = "******", Password = "******", RememberMe = true }; using (var context = new HeliosDbContext()) { SaveUserInDb(context, validSignInModel.UserName, validSignInModel.Password, requiredToChangePassword: true); var mockFormsAuth = new Mock<IFormsAuthentication>(); mockFormsAuth .Setup(f => f.SetAuthCookie(validSignInModel.UserName, /* createPersistentCookie */ validSignInModel.RememberMe)) .Verifiable(); var controller = new SessionsController(context, mockFormsAuth.Object); var result = controller.Create(validSignInModel); Assert.IsType<RedirectToRouteResult>(result); var redirectResult = result as RedirectToRouteResult; Assert.Equal("Sessions", redirectResult.RouteValues["controller"] as string); Assert.Equal("Edit", redirectResult.RouteValues["action"] as string); mockFormsAuth.Verify(); } }