public async Task DeleteConfirmedRedirectsToCorrectAction() { const int organizationId = 1; var deleteViewModel = new DeleteViewModel { OrganizationId = organizationId, EventId = 1, UserIsOrgAdmin = true }; var mediator = new Mock<IMediator>(); var sut = new TaskController(mediator.Object, null); sut.MakeUserAnOrgAdmin(organizationId.ToString()); var result = await sut.DeleteConfirmed(deleteViewModel) as RedirectToActionResult; var routeValues = new Dictionary<string, object> { ["id"] = deleteViewModel.EventId }; Assert.Equal(result.ActionName, nameof(EventController.Details)); Assert.Equal(result.ControllerName, "Event"); Assert.Equal(result.RouteValues, routeValues); }
public async Task DeleteReturnsCorrectViewModelAndView() { const int organizationId = 1; var deleteViewModel = new DeleteViewModel { OrganizationId = organizationId }; var mediator = new Mock<IMediator>(); mediator.Setup(x => x.SendAsync(It.IsAny<DeleteQueryAsync>())).ReturnsAsync(deleteViewModel); var sut = new TaskController(mediator.Object, null); sut.MakeUserAnOrgAdmin(organizationId.ToString()); var result = await sut.Delete(It.IsAny<int>()) as ViewResult; var modelResult = result.ViewData.Model as DeleteViewModel; Assert.IsType<ViewResult>(result); Assert.IsType<DeleteViewModel>(modelResult); Assert.Equal(modelResult, deleteViewModel); }
public async Task DeleteConfirmedReturnsUnauthorizedResultWhenUserIsNotOrgAdmin() { var model = new DeleteViewModel { UserIsOrgAdmin = false }; var sut = new TaskController(null, null); sut.SetDefaultHttpContext(); var result = await sut.DeleteConfirmed(model); Assert.IsType<UnauthorizedResult>(result); }
public async Task<IActionResult> DeleteConfirmed(DeleteViewModel viewModel) { if (!viewModel.UserIsOrgAdmin) { return Unauthorized(); } await _mediator.SendAsync(new DeleteTaskCommand { TaskId = viewModel.Id }); return RedirectToAction(nameof(EventController.Details), "Event", new { id = viewModel.EventId }); }