예제 #1
0
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {

                // ChangePassword will throw an exception rather
                // than return false in certain failure scenarios.
                bool changePasswordSucceeded;
                try
                {
                    string username = User == null ? string.Empty : User.Identity.Name;
                    changePasswordSucceeded = membershipService.ChangePassword(username, model.OldPassword, model.NewPassword);
                }
                catch (Exception)
                {
                    changePasswordSucceeded = false;
                }

                if (changePasswordSucceeded)
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public void ShouldChangePassword()
        {
            var model = new ChangePasswordModel
                            {
                                NewPassword = "******",
                                OldPassword = "******",
                                ConfirmPassword = "******"
                            };

            membershipService.ChangePassword("userName", "password","Slartibartfast").Returns(true);

            var controller = CreateController();

            var actionResult = controller.ChangePassword(model);
            Expect(controller.ModelState.IsValid,Is.True);
            actionResult.AssertActionRedirect();
        }
        public void ShouldNotChangePasswordInvalidModel()
        {
            var model = new ChangePasswordModel
            {
                NewPassword = "******",
                OldPassword = "******",
                ConfirmPassword = "******"
            };

            var controller = CreateController();
            controller.ModelState.AddModelError("NewPassword","The new password and confirmation password do not match.");

            var actionResult = controller.ChangePassword(model);
            Expect(controller.ViewData.ModelState.IsValid, Is.False);
            actionResult.AssertViewRendered().WithViewData<ChangePasswordModel>();
        }
        public void ShouldNotChangePasswordIFailForChangePassword()
        {
            var model = new ChangePasswordModel
            {
                NewPassword = "******",
                OldPassword = "******",
                ConfirmPassword = "******"
            };

            membershipService.ChangePassword("userName", "password", "Slartibartfast").Returns(false);
            var controller = CreateController();

            var actionResult = controller.ChangePassword(model);
            Expect(controller.ViewData.ModelState.IsValid, Is.False);
            Expect(controller.ModelState[string.Empty].Errors.Count,Is.EqualTo(1));
            Expect(controller.ModelState[string.Empty].Errors[0].ErrorMessage, Is.EqualTo("The current password is incorrect or the new password is invalid."));
            actionResult.AssertViewRendered().WithViewData<ChangePasswordModel>();
        }