public async Task Edit_Should_Find_Goal_By_Id(int goalId, string expectedTitle) { var result = await goalController.Edit(goalId); var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsAssignableFrom <ToDoGoal>( viewResult.ViewData.Model); Assert.Equal(goalId, model.Id); Assert.Equal(expectedTitle, model.Title); }
public async void GoalEditPostForOrgAdminWithIncorrectIdInModelStateReturnsBadRequest() { const int campaignId = 123; const int goalId = 456; // Arrange var mockMediator = new Mock <IMediator>(); mockMediator.Setup(mock => mock.SendAsync(It.IsAny <GoalEditQuery>())) .ReturnsAsync(new GoalEditViewModel { Id = goalId, CampaignId = campaignId, OrganizationId = OrgAdminOrgId }) .Verifiable(); var goalController = new GoalController(mockMediator.Object); var mockContext = MockControllerContextWithUser(OrgAdmin(OrgAdminOrgId)); goalController.ControllerContext = mockContext.Object; var vm = new GoalEditViewModel { Id = goalId, GoalType = GoalType.Text, TextualGoal = "Aim to please" }; // Act var result = await goalController.Edit(987, vm); // Assert Assert.IsType <BadRequestResult>(result); }
public async void TestGoalEditPostForWrongOrgAdminReturns401() { // Arrange var mockMediator = new Mock <IMediator>(); mockMediator.Setup(mock => mock.SendAsync(It.IsAny <GoalEditQuery>())) .ReturnsAsync(new GoalEditViewModel()) .Verifiable(); mockMediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(false, false, false, false)); var goalController = new GoalController(mockMediator.Object); var mockContext = MockControllerContextWithUser(OrgAdmin(654)); goalController.ControllerContext = mockContext.Object; // Act var result = await goalController.Edit(456, new GoalEditViewModel() { Id = 456 }); // Assert Assert.IsType <UnauthorizedResult>(result); }
public void CannotEditWithMissingValues() { Mock <IGenericService <Goal> > goalServiceMock = new Mock <IGenericService <Goal> >(); Goal goal = new Goal() { Id = 100, Bloom = null, Description = @"Je begrijpt de basismechanismen voor access control, zodanig dat je in eigen woorden kunt uitleggen wat de betekenis is in de context van software security. " }; ClaimsPrincipal identity = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "123") })); goalServiceMock.Setup(m => m.Update(It.IsAny <Goal>())).Returns((Goal model) => { if (!string.IsNullOrWhiteSpace(model.Bloom) && !string.IsNullOrWhiteSpace(model.Description)) { return(1); } return(0); }); GoalController controller = new GoalController(goalServiceMock.Object) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = identity } } }; ViewResult result = controller.Edit(goal) as ViewResult; Assert.NotNull(result); Assert.NotNull(result.Model); Assert.True(string.IsNullOrEmpty(result.ViewName) || result.ViewName == "Edit"); }
public void ShouldEditCorrectly() { Mock <IGenericService <Goal> > goalServiceMock = new Mock <IGenericService <Goal> >(); Goal goal = new Goal() { Id = 100, Bloom = "Understand", Description = @"Je begrijpt de basismechanismen voor access control, zodanig dat je in eigen woorden kunt uitleggen wat de betekenis is in de context van software security. " }; ClaimsPrincipal identity = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "123") })); goalServiceMock.Setup(m => m.Update(It.IsAny <Goal>())).Returns((Goal model) => { if (!string.IsNullOrWhiteSpace(model.Bloom) && !string.IsNullOrWhiteSpace(model.Description)) { return(1); } return(0); }); GoalController controller = new GoalController(goalServiceMock.Object) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = identity } } }; RedirectToActionResult result = controller.Edit(goal) as RedirectToActionResult; Assert.Equal("Index", result?.ActionName); }
public async void TestGoalEditForOrgAdminForNonexistantGoalReturnUnauthorizedResult() { // Arrange var mockMediator = new Mock <IMediator>(); mockMediator.Setup(mock => mock.SendAsync(It.IsAny <GoalEditQuery>())) .ReturnsAsync(null) .Verifiable(); var goalController = new GoalController(mockMediator.Object); var mockContext = MockControllerContextWithUser(OrgAdmin(OrgAdminOrgId)); goalController.ControllerContext = mockContext.Object; // Act var result = await goalController.Edit(987); // Assert Assert.IsType <UnauthorizedResult>(result); }
public async void TestGoalEditForWrongOrgAdminReturns401() { // Arrange var mockMediator = new Mock <IMediator>(); mockMediator.Setup(mock => mock.SendAsync(It.IsAny <CampaignSummaryQuery>())) .ReturnsAsync(new CampaignSummaryViewModel()) .Verifiable(); var goalController = new GoalController(mockMediator.Object); var mockContext = MockControllerContextWithUser(OrgAdmin(654)); goalController.ControllerContext = mockContext.Object; // Act var result = await goalController.Edit(456); // Assert Assert.IsType <UnauthorizedResult>(result); }
public async void TestGoalEditPostForSiteAdminWithValidModelStateReturnsRedirectToAction() { const int campaignId = 123; const int goalId = 456; // Arrange var mockMediator = new Mock <IMediator>(); mockMediator.Setup(mock => mock.SendAsync(It.IsAny <GoalEditQuery>())) .ReturnsAsync(new GoalEditViewModel { Id = goalId, CampaignId = campaignId, OrganizationId = OrgAdminOrgId }) .Verifiable(); mockMediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(false, true, false, false)); var goalController = new GoalController(mockMediator.Object); var mockContext = MockControllerContextWithUser(SiteAdmin()); goalController.ControllerContext = mockContext.Object; var vm = new GoalEditViewModel { Id = goalId, GoalType = GoalType.Text, TextualGoal = "Aim to please" }; // Act var result = await goalController.Edit(goalId, vm) as RedirectToActionResult; // Assert Assert.NotNull(result); Assert.Equal("Details", result.ActionName); Assert.Equal("Campaign", result.ControllerName); Assert.Equal(AreaNames.Admin, result.RouteValues["area"]); Assert.Equal(campaignId, result.RouteValues["id"]); mockMediator.Verify(mock => mock.SendAsync(It.IsAny <GoalEditCommand>()), Times.Once); }
public async void TestGoalEditForSiteAdmin() { const int campaignId = 123; const int goalId = 456; // Arrange var mockMediator = new Mock <IMediator>(); mockMediator.Setup(mock => mock.SendAsync(It.IsAny <GoalEditQuery>())) .ReturnsAsync(new GoalEditViewModel { Id = goalId, CampaignId = campaignId, OrganizationId = OrgAdminOrgId }) .Verifiable(); mockMediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(false, true, false, false)); var goalController = new GoalController(mockMediator.Object); var mockContext = MockControllerContextWithUser(SiteAdmin()); goalController.ControllerContext = mockContext.Object; // Act var actionResult = await goalController.Edit(goalId); // Assert Assert.IsType <ViewResult>(actionResult); var viewResult = (ViewResult)actionResult; Assert.Equal("Edit", viewResult.ViewName); var vm = viewResult.ViewData.Model as GoalEditViewModel; Assert.NotNull(vm); Assert.Equal(campaignId, vm.CampaignId); Assert.Equal(OrgAdminOrgId, vm.OrganizationId); }