public async Task AccountController_DeleteAccount_Unsuccessful_IncorrectPassword() { var mockLogger = Mock.Of <ILogger <AccountController> >(); var mockUserProvider = Mock.Of <IUserProvider>(); var mockAccountService = new MockAccountService().MockDeleteUserAccountAsync("Incorrect password"); var accountController = new AccountController(mockUserProvider, mockAccountService.Object, mockLogger); var deleteAccountViewModel = new ViewModel.DeleteAccount { Password = "******" }; var result = await accountController.DeleteAccount(deleteAccountViewModel); var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsType <ViewModel.DeleteAccount>(viewResult.ViewData.Model); string messages = string.Join("; ", accountController.ModelState.Values .SelectMany(x => x.Errors) .Select(x => x.ErrorMessage)); Assert.IsType <ViewModel.DeleteAccount>(model); Assert.Equal(1, accountController.ModelState.ErrorCount); Assert.Equal("Incorrect password", messages); Assert.Equal("", model.SuccessMessage); }
public async Task <IActionResult> DeleteAccount(ViewModel.DeleteAccount model) { model.ClearMessages(); if (ModelState.IsValid) { try { var errorMessage = await _service.DeleteUserAccountAsync(model.Password); switch (errorMessage) { case "Incorrect password": ModelState.AddModelError(nameof(model.Password), "Incorrect password"); return(View(model)); case "Error deleting account": model.ErrorMessage = "Error deleting account"; return(View(model)); default: return(RedirectToAction("Index", "Home")); } } catch (Exception ex) { model.ErrorMessage = "Error deleting account"; _logger.LogError(ex, "Error Deleting Account"); } } return(View(model)); }
public async Task AccountController_DeleteAccount_Successful() { var mockLogger = Mock.Of <ILogger <AccountController> >(); var mockUserProvider = Mock.Of <IUserProvider>(); var mockAccountService = new MockAccountService().MockDeleteUserAccountAsync(""); var accountController = new AccountController(mockUserProvider, mockAccountService.Object, mockLogger); var deleteAccountViewModel = new ViewModel.DeleteAccount { Password = "******" }; var result = (RedirectToActionResult)await accountController.DeleteAccount(deleteAccountViewModel); Assert.Equal("Home", result.ControllerName); Assert.Equal("Index", result.ActionName); }
public async Task AccountController_DeleteAccount_Unsuccessful_Error() { var mockLogger = Mock.Of <ILogger <AccountController> >(); var mockUserProvider = Mock.Of <IUserProvider>(); var mockAccountService = new MockAccountService().MockDeleteUserAccountAsync("Error deleting account"); var accountController = new AccountController(mockUserProvider, mockAccountService.Object, mockLogger); var deleteAccountViewModel = new ViewModel.DeleteAccount { Password = "******" }; var result = await accountController.DeleteAccount(deleteAccountViewModel); var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsType <ViewModel.DeleteAccount>(viewResult.ViewData.Model); Assert.IsType <ViewModel.DeleteAccount>(model); Assert.Equal("Error deleting account", model.ErrorMessage); Assert.Equal("", model.SuccessMessage); }
public IActionResult DeleteAccount() { var model = new ViewModel.DeleteAccount(); return(View(model)); }