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_SignUp_with_invalid_view_model_should_return_view_with_Errors() { var invalidSignUpModel = new SignUpViewModel() { UserName = "", Password = "******", ConfirmPassword = "******" }; using (var context = new HeliosDbContext()) { var mockFormsAuth = new Mock<IFormsAuthentication>(); var isAuthenticated = false; mockFormsAuth .Setup(f => f.SetAuthCookie(invalidSignUpModel.UserName, /* createPersistentCookie */ false)) .Callback(() => isAuthenticated = true); var controller = new UsersController(context, mockFormsAuth.Object); var validationResults = ModelTestHelper.ValidateModel<SignUpViewModel>(invalidSignUpModel); foreach (var validationResult in validationResults) { controller.ModelState.AddModelError("*", validationResult.ErrorMessage); } var result = controller.Create(invalidSignUpModel); Assert.IsType<ViewResult>(result); var viewResult = result as ViewResult; Assert.Equal("New", viewResult.ViewName); Assert.Equal(false, controller.ModelState.IsValid); Assert.Equal(false, isAuthenticated); } }