public void Should_Delete_Item() { // Arrange var LabelToGet = new Label() { Id = 10, Color = "#ffffff", Title = "testItem", UserId = "testID" }; var repository = new Mock <IRepository <Label> >(); repository.Setup(u => u.Delete(10)); repository.Setup(u => u.GetItem(10)).Returns(LabelToGet); repository.Setup(u => u.SaveChanges()); // Act var service = new LabelService(_mapper, repository.Object); service.Delete(10, "testID"); // Assert repository.Verify(u => u.Delete(10)); repository.Verify(u => u.GetItem(10)); repository.Verify(u => u.SaveChanges()); repository.VerifyNoOtherCalls(); }
public async Task <IActionResult> OnPostAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } RequirePassword = await _userManager.HasPasswordAsync(user); if (RequirePassword) { if (!await _userManager.CheckPasswordAsync(user, Input.Password)) { ModelState.AddModelError(string.Empty, "Password not correct."); return(Page()); } } var labels = _labelService.GetAll(user.Id); foreach (var label in labels) { _labelService.Delete(label.Id, user.Id); } var result = await _userManager.DeleteAsync(user); var userId = await _userManager.GetUserIdAsync(user); if (!result.Succeeded) { throw new InvalidOperationException($"Unexpected error occurred deleteing user with ID '{userId}'."); } await _signInManager.SignOutAsync(); _logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId); return(Redirect("~/")); }
public void Should_UnauthorizedAccessException_When_Delete_Item() { // Arrange var newLabel = new Label() { Id = 10, Color = "#ffffff", Title = "testItem", UserId = "testID" }; var repository = new Mock <IRepository <Label> >(); repository.Setup(u => u.Delete(newLabel.Id)); repository.Setup(u => u.GetItem(10)).Returns(newLabel); // Act var service = new LabelService(_mapper, repository.Object); //Assert Assert.Throws <UnauthorizedAccessException>(() => service.Delete(newLabel.Id, "none")); repository.Verify(u => u.GetItem(newLabel.Id)); repository.VerifyNoOtherCalls(); }
public IActionResult Delete(int id) { _labelService.Delete(id, _userId); return(RedirectToAction(nameof(List))); }