Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        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"]);
            }
        }
Beispiel #3
0
        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);
            }
        }
Beispiel #4
0
        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);
            }
        }
Beispiel #5
0
        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);
            }
        }
Beispiel #6
0
        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();
            }
        }
Beispiel #7
0
        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);
            }
        }
Beispiel #8
0
        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();
            }
        }