public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid && model.IsValid())
            {
                bool changePasswordSucceeded;
                try
                {
                    MembershipUser currentUser = _membershipTasks.GetUser(User.Identity.Name, true /* userIsOnline */);
                    changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
                }
                catch (Exception)
                {
                    changePasswordSucceeded = false;
                }

                if (changePasswordSucceeded)
                    return RedirectToAction("ChangePasswordSuccess");

                ModelState.AddModelError(string.Empty, "The current password is incorrect or the new password is invalid.");
            }

            model.MinRequiredPasswordLength = _membershipTasks.MinRequiredPasswordLength;
            return View(model);
        }
        public void ChangePassword_Fails_And_Forwards_To_ChangePassword()
        {
            //Arrange
            var routeData = new RouteData();
            var httpContext = MockRepository.GenerateMock<HttpContextBase>();
            httpContext.Expect(x => x.User).Return(new FakePrincipal(new FakeIdentity("Bruce"), null));
            var controllerContext = MockRepository.GenerateMock<ControllerContext>(httpContext, routeData, _controller);
            controllerContext.Expect(x => x.HttpContext).Return(httpContext);
            _controller.ControllerContext = controllerContext;
            _controller.ValueProvider = new FormCollection().ToValueProvider();
            _membershipTasks.Expect(x => x.MinRequiredPasswordLength).Return(6);
            var model = new ChangePasswordModel { OldPassword = "******", NewPassword = "******", ConfirmPassword = "******" };
            _membershipTasks.Expect(x => x.GetUser(Arg<string>.Is.Anything, Arg<bool>.Is.Anything)).Throw(new Exception());

            //Act
            var result = _controller.ChangePassword(model) as ViewResult;

            //Assert
            Assert.AreEqual("The current password is incorrect or the new password is invalid.", result.ViewData.ModelState[string.Empty].Errors[0].ErrorMessage);
            Assert.AreEqual(string.Empty, result.ViewName);
            Assert.IsInstanceOfType<ChangePasswordModel>(result.Model);
            Assert.AreEqual(6, (result.Model as ChangePasswordModel).MinRequiredPasswordLength);
            _membershipTasks.VerifyAllExpectations();
        }
        public void ChangePassword_Succeeds_And_Redirects()
        {
            //Arrange
            var routeData = new RouteData();
            var httpContext = MockRepository.GenerateMock<HttpContextBase>();
            httpContext.Expect(x => x.User).Return(new FakePrincipal(new FakeIdentity("Bruce"), null));
            var controllerContext = MockRepository.GenerateMock<ControllerContext>(httpContext, routeData, _controller);
            controllerContext.Expect(x => x.HttpContext).Return(httpContext);
            _controller.ControllerContext = controllerContext;
            _controller.ValueProvider = new FormCollection().ToValueProvider();
            var model = new ChangePasswordModel { OldPassword = "******", NewPassword = "******", ConfirmPassword = "******" };
            var user = MockRepository.GenerateMock<MembershipUser>();
            user.Expect(x => x.ChangePassword(Arg<string>.Is.Equal("peace"), Arg<string>.Is.Equal("ofMind"))).Return(true);
            _membershipTasks.Expect(x => x.GetUser(Arg<string>.Is.Equal("Bruce"), Arg<bool>.Is.Equal(true))).Return(user);

            //Act
            var result = _controller.ChangePassword(model) as RedirectToRouteResult;

            //Assert
            Assert.IsNull(result.RouteValues["controller"]);
            Assert.AreEqual("ChangePasswordSuccess", result.RouteValues["action"]);
            user.VerifyAllExpectations();
            httpContext.VerifyAllExpectations();
        }