private static Mock<IMediator> MockMediatorSkillDeleteQuery(out SkillController controller, SkillDeleteViewModel model = null)
        {
            if (model == null) model = new SkillDeleteViewModel {  HierarchicalName = "Name" };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<SkillDeleteQuery>())).Returns(() => Task.FromResult(model)).Verifiable();
            controller = new SkillController(mockMediator.Object);
            return mockMediator;
        }
        public async Task DeleteConfirmedPostReturnsRedirectToTheCorrectActionWithTheCorrectRouteValues()
        {
            // Arrange
            SkillController controller;
            MockMediatorSkillDeleteQuery(out controller);

            var viewModel = new SkillDeleteViewModel { SkillBelongsToSameOrgAsOrgAdmin = true };

            var routeValueDictionary = new RouteValueDictionary { ["area"] = "Admin" };

            var mockContext = MockControllerContextWithUser(SiteAdmin());
            controller.ControllerContext = mockContext.Object;

            // Act            
            var result = await controller.DeleteConfirmed(viewModel) as RedirectToActionResult;

            // Assert
            Assert.Equal(nameof(SkillController.Index), result.ActionName);
            Assert.Equal(routeValueDictionary, result.RouteValues);
        }
        public async Task DeleteConfirmedPostReturnsUnauthorizedResult_WhenSkillBelongsToSameOrgAsOrgAdminIsFalse()
        {
            // Arrange
            var viewModel = new SkillDeleteViewModel { SkillBelongsToSameOrgAsOrgAdmin = false };
            SkillController controller;
            MockMediatorSkillDeleteQueryNullModel(out controller);

            // Act
            var result = await controller.DeleteConfirmed(viewModel) as UnauthorizedResult;

            // Assert
            Assert.IsType<UnauthorizedResult>(result);
        }
        public async Task DeleteConfirmedPostSendsSkillDeleteCommandWithCorrectSkillId()
        {
            // Arrange
            const int skillId = 1;
            SkillController controller;
            var mockMediator = MockMediatorSkillDeleteQuery(out controller);

            var viewModel = new SkillDeleteViewModel { SkillBelongsToSameOrgAsOrgAdmin = true, SkillId = skillId };

            var mockContext = MockControllerContextWithUser(SiteAdmin());
            controller.ControllerContext = mockContext.Object;

            // Act            
            await controller.DeleteConfirmed(viewModel);

            // Assert
            mockMediator.Verify(mock => mock.SendAsync(It.Is<SkillDeleteCommand>(y => y.Id == skillId)), Times.Once);
        }
Exemplo n.º 5
0
        public async Task<IActionResult> DeleteConfirmed(SkillDeleteViewModel viewModel)
        {
            if (!viewModel.SkillBelongsToSameOrgAsOrgAdmin)
            {
                return new UnauthorizedResult();
            }

            await _mediator.SendAsync(new SkillDeleteCommand { Id = viewModel.SkillId });

            return RedirectToAction(nameof(Index), new { area = "Admin" });
        }