Beispiel #1
0
        public async Task AccountController_ChangePassword_Unsuccessful()
        {
            var mockLogger         = Mock.Of <ILogger <AccountController> >();
            var mockUserProvider   = Mock.Of <IUserProvider>();
            var mockAccountService = new MockAccountService().MockChangeUserPasswordAsync(false);

            var accountController = new AccountController(mockUserProvider, mockAccountService.Object, mockLogger);

            var changePasswordViewModel = new ViewModel.ChangePassword
            {
                CurrentPassword = "******",
                Password        = "******",
                ConfirmPassword = "******"
            };

            var result = await accountController.ChangePassword(changePasswordViewModel);

            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsType <ViewModel.ChangePassword>(viewResult.ViewData.Model);

            string messages = string.Join("; ", accountController.ModelState.Values
                                          .SelectMany(x => x.Errors)
                                          .Select(x => x.ErrorMessage));

            Assert.IsType <ViewModel.ChangePassword>(model);
            Assert.Equal(1, accountController.ModelState.ErrorCount);
            Assert.Equal("Incorrect password", messages);
            Assert.Equal("", model.SuccessMessage);
        }
        public async Task <IActionResult> ChangePassword(ViewModel.ChangePassword model)
        {
            model.ClearMessages();

            if (ModelState.IsValid)
            {
                try
                {
                    var result = await _service.ChangeUserPasswordAsync(model.CurrentPassword, model.Password);

                    if (result)
                    {
                        model.SuccessMessage = "Password changed successfully";
                    }
                    else
                    {
                        ModelState.AddModelError(nameof(model.CurrentPassword), "Incorrect password");
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error changing password");
                    model.ErrorMessage = "Error changing password";
                }
            }

            return(View(model));
        }
Beispiel #3
0
        public async Task AccountController_ChangePassword_Successful()
        {
            var mockLogger         = Mock.Of <ILogger <AccountController> >();
            var mockUserProvider   = Mock.Of <IUserProvider>();
            var mockAccountService = new MockAccountService().MockChangeUserPasswordAsync(true);

            var accountController = new AccountController(mockUserProvider, mockAccountService.Object, mockLogger);

            var changePasswordViewModel = new ViewModel.ChangePassword
            {
                CurrentPassword = "******",
                Password        = "******",
                ConfirmPassword = "******"
            };

            var result = await accountController.ChangePassword(changePasswordViewModel);

            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsType <ViewModel.ChangePassword>(viewResult.ViewData.Model);

            Assert.IsType <ViewModel.ChangePassword>(model);
            Assert.Equal("Password changed successfully", model.SuccessMessage);
            Assert.Equal("", model.ErrorMessage);
        }
        public IActionResult ChangePassword()
        {
            var model = new ViewModel.ChangePassword();

            return(View(model));
        }