Пример #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);
            }
        }
Пример #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"]);
            }
        }
Пример #3
0
        public ActionResult Edit(ChangePasswordViewModel changePasswordViewModel)
        {
            if (this.ModelState.IsValid) {
                var dbUser = Users.FirstOrDefault(u => u.UserName.Equals(this.User.Identity.Name));
                dbUser.RequiredToChangePassword = false;
                dbUser.SetPassword(changePasswordViewModel.Password);

                return RedirectToRoute(new { controller = "Home", action = "Index" });
            }

            return View();
        }
Пример #4
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);
            }
        }