public void Test_POST_sessions_Edit_should_not_update_password_when_provide_not_match_password() { var changePasswordViewModel = new ChangePasswordViewModel() { Password = "******", ConfirmPassword = "******" }; var userName = "******"; var controllerContext = CreateControllerContext(userName); using (var context = new HeliosDbContext()) { SaveUserInDb(context, userName, password: "******"); var controller = new SessionsController(context, new Mock<IFormsAuthentication>().Object) { ControllerContext = controllerContext }; var validationResults = ModelTestHelper.ValidateModel<ChangePasswordViewModel>(changePasswordViewModel); foreach (var validationResult in validationResults) { controller.ModelState.AddModelError("*", validationResult.ErrorMessage); } var result = controller.Edit(changePasswordViewModel); // need to commit since we used UnitOfWorkAttribute in production context.Commit(); var dbUser = context.Users.FirstOrDefault(u => u.UserName == userName); Assert.NotNull(dbUser); Assert.Equal(true, dbUser.VerifyPassword("old_password")); Assert.IsType<ViewResult>(result); Assert.Equal(false, controller.ModelState.IsValid); } }
public void Test_POST_sessions_Edit_should_rediret_to_home_index() { var changePasswordViewModel = new ChangePasswordViewModel() { Password = "******", ConfirmPassword = "******" }; var userName = "******"; var controllerContext = CreateControllerContext(userName); using (var context = new HeliosDbContext()) { SaveUserInDb(context, userName, password: "******"); var mockFormsAuth = new Mock<IFormsAuthentication>(); var controller = new SessionsController(context, mockFormsAuth.Object) { ControllerContext = controllerContext }; var result = controller.Edit(changePasswordViewModel); // need to commit since we used UnitOfWorkAttribute in production context.Commit(); Assert.IsType<RedirectToRouteResult>(result); var redirectResult = result as RedirectToRouteResult; Assert.Equal("Home", (string)redirectResult.RouteValues["controller"]); Assert.Equal("Index", (string)redirectResult.RouteValues["action"]); } }
public void Test_should_set_UserPrinciple_if_User_is_authenticated() { var mockHttpContext = new Mock<HttpContextBase>(); var genericPrincipal = new GenericPrincipal(new GenericIdentity("username"), new string[]{}); mockHttpContext.SetupProperty(c => c.User, genericPrincipal); var controllerContext = new ControllerContext() { HttpContext = mockHttpContext.Object }; using (var dbContext = new HeliosDbContext()) { var dbUser = new User() { UserName = "******", IsAdmin = true }; dbUser.SetPassword("secret"); dbContext.Users.Add(dbUser); dbContext.Commit(); var stubController = new StubController(dbContext) { ControllerContext = controllerContext }; stubController.CallOnAuthorization(new AuthorizationContext() { HttpContext = controllerContext.HttpContext }); Assert.Equal(true, stubController.User is UserPrincipal); Assert.Equal("username", stubController.User.Identity.Name); Assert.Equal(true, stubController.User.IsInRole("admin")); } }
private static void InitDatabase() { Database.SetInitializer<HeliosDbContext>(new DropCreateDatabaseAlways<HeliosDbContext>()); using (var context = new HeliosDbContext()) { context.Database.Initialize(force: true); } }
public void Test_POST_SignUp_should_redirect_to_home_index() { var validSignUpModel = new SignUpViewModel() { UserName = "******", Password = "******", ConfirmPassword = "******" }; using (var context = new HeliosDbContext()) { var mockFormsAuth = new Mock<IFormsAuthentication>(); var controller = new UsersController(context, mockFormsAuth.Object); var result = controller.Create(validSignUpModel); Assert.IsType<RedirectToRouteResult>(result); var redirectResult = result as RedirectToRouteResult; Assert.Equal("Home", redirectResult.RouteValues["controller"] as string); Assert.Equal("Index", redirectResult.RouteValues["action"] as string); } }
public void Test_POST_SignUp_should_create_new_user_with_RequiredToChangePassword() { var validSignUpModel = new SignUpViewModel() { UserName = "******", Password = "******", ConfirmPassword = "******" }; using (var context = new HeliosDbContext()) { var mockFormsAuth = new Mock<IFormsAuthentication>(); var controller = new UsersController(context, mockFormsAuth.Object); controller.Create(validSignUpModel); // need to commit since we used UnitOfWorkAttribute in production context.Commit(); var dbUser = context.Users.FirstOrDefault(u => u.UserName == validSignUpModel.UserName); Assert.NotNull(dbUser); Assert.Equal(true, dbUser.RequiredToChangePassword); } }
public void Test_POST_sessions_Edit_should_update_password_and_set_false_to_RequiredToChangePassword() { var changePasswordViewModel = new ChangePasswordViewModel() { Password = "******", ConfirmPassword = "******" }; var userName = "******"; var controllerContext = CreateControllerContext(userName); using (var context = new HeliosDbContext()) { SaveUserInDb(context, userName, password: "******", requiredToChangePassword: true); var mockFormsAuth = new Mock<IFormsAuthentication>(); var controller = new SessionsController(context, mockFormsAuth.Object) { ControllerContext = controllerContext }; controller.Edit(changePasswordViewModel); // need to commit since we used UnitOfWorkAttribute in production context.Commit(); var dbUser = context.Users.FirstOrDefault(u => u.UserName == userName); Assert.NotNull(dbUser); Assert.Equal(true, dbUser.VerifyPassword(changePasswordViewModel.Password)); Assert.Equal(false, dbUser.RequiredToChangePassword); } }
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); } }
private static void SaveUserInDb(HeliosDbContext context, string userName, string password, bool requiredToChangePassword = false) { var dbUser = new User() { UserName = userName, RequiredToChangePassword = requiredToChangePassword }; dbUser.SetPassword(password); context.Users.Add(dbUser); context.Commit(); }
public void Test_sessions_new_should_return_ViewResult() { using (var context = new HeliosDbContext()) { var controller = new SessionsController(context, new Mock<IFormsAuthentication>().Object); var result = controller.New(); Assert.IsType<ViewResult>(result); } }
public void Test_sessions_destroy_should_sign_out_and_redirect_to_home_index() { var mockFormsAuth = new Mock<IFormsAuthentication>(); mockFormsAuth.Setup(f => f.SignOut()).Verifiable(); using (var context = new HeliosDbContext()) { var controller = new SessionsController(context, mockFormsAuth.Object); var result = controller.Destroy(); Assert.IsType<RedirectToRouteResult>(result); var redirectResult = result as RedirectToRouteResult; Assert.Equal("Home", redirectResult.RouteValues["controller"] as string); Assert.Equal("Index", redirectResult.RouteValues["action"] as string); mockFormsAuth.Verify(); } }
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(); } }
public ApplicationController(IUnitOfWork unitOfWork) { _dbContext = (HeliosDbContext)unitOfWork; }