public async void GoalCreatePostForOrgAdminWithValidModelStateReturnsRedirectToAction() { const int campaignId = 123; // Arrange var mockMediator = new Mock <IMediator>(); mockMediator.Setup(mock => mock.SendAsync(It.IsAny <CampaignSummaryQuery>())) .ReturnsAsync(new CampaignSummaryViewModel { Id = campaignId, OrganizationId = OrgAdminOrgId }) .Verifiable(); var goalController = new GoalController(mockMediator.Object); var mockContext = MockControllerContextWithUser(OrgAdmin(OrgAdminOrgId)); goalController.ControllerContext = mockContext.Object; var vm = new GoalEditViewModel { GoalType = GoalType.Text, TextualGoal = "Aim to please" }; // Act var result = await goalController.Create(campaignId, 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 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 Task <IActionResult> Create(int campaignId, GoalEditViewModel model) { var campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = campaignId }); if (campaign == null) { return(NotFound()); } var authorizableCampaign = await _mediator.SendAsync(new AuthorizableCampaignQuery(campaign.Id)); if (!await authorizableCampaign.UserCanEdit()) { return(Unauthorized()); } ValidateGoalEditViewModel(model); if (ModelState.IsValid) { await _mediator.SendAsync(new GoalEditCommand { Goal = model }); return(RedirectToAction(nameof(CampaignController.Details), "Campaign", new { area = AreaNames.Admin, id = campaignId })); } return(View("Edit", model)); }
public async Task <IActionResult> Create(int campaignId) { var campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = campaignId }); if (campaign == null) { return(NotFound()); } var authorizableCampaign = await _mediator.SendAsync(new AuthorizableCampaignQuery(campaign.Id)); if (!await authorizableCampaign.UserCanEdit()) { return(Unauthorized()); } var viewModel = new GoalEditViewModel() { CampaignId = campaign.Id, CampaignName = campaign.Name, OrganizationId = campaign.OrganizationId, Display = true, GoalType = GoalType.Numeric }; return(View("Edit", viewModel)); }
public async Task <IActionResult> Edit(int id, GoalEditViewModel model) { var goal = await _mediator.SendAsync(new GoalEditQuery { GoalId = id }); if (goal == null) { return(NotFound()); } if (id != model.Id) { return(BadRequest()); } var authorizableCampaign = await _mediator.SendAsync(new AuthorizableCampaignQuery(goal.CampaignId)); if (!await authorizableCampaign.UserCanEdit()) { return(Unauthorized()); } ValidateGoalEditViewModel(model); if (ModelState.IsValid) { await _mediator.SendAsync(new GoalEditCommand { Goal = model }); return(RedirectToAction(nameof(CampaignController.Details), "Campaign", new { area = AreaNames.Admin, id = goal.CampaignId })); } return(View("Edit", model)); }
public async Task <IActionResult> Edit(int id) { var goal = await _mediator.SendAsync(new GoalEditQuery { GoalId = id }); if (goal == null) { return(NotFound()); } var authorizableCampaign = await _mediator.SendAsync(new AuthorizableCampaignQuery(goal.CampaignId)); if (!await authorizableCampaign.UserCanEdit()) { return(Unauthorized()); } var viewModel = new GoalEditViewModel { CampaignId = goal.CampaignId, CampaignName = goal.CampaignName, Id = id, CurrentGoalLevel = goal.CurrentGoalLevel, Display = goal.Display, GoalType = goal.GoalType, TextualGoal = goal.TextualGoal, NumericGoal = goal.NumericGoal, OrganizationId = goal.OrganizationId }; return(View("Edit", viewModel)); }
public async Task AddNewGoal() { // Arrange var handler = new GoalEditCommandHandler(Context); var newGoal = new GoalEditViewModel { GoalType = GoalType.Numeric, CurrentGoalLevel = 123, NumericGoal = 456, CampaignId = 98, TextualGoal = "We aim to please", Display = true }; // Act var result = await handler.Handle(new GoalEditCommand { Goal = newGoal }); // Assert Assert.Equal(8, Context.CampaignGoals.Count()); Assert.Equal(8, result); }
public async Task UpdatingExistingGoal() { // Arrange var handler = new GoalEditCommandHandler(Context); var newGoal = new GoalEditViewModel { Id = 2, GoalType = GoalType.Numeric, CurrentGoalLevel= 123, NumericGoal = 456, CampaignId = 98, TextualGoal = "We aim to please", Display = true}; // Act var result = await handler.Handle(new GoalEditCommand { Goal = newGoal }); var savedGoal = Context.CampaignGoals.SingleOrDefault(s => s.Id == 2); // Assert Assert.Equal(7, Context.CampaignGoals.Count()); Assert.Equal(2, result); Assert.Equal("We aim to please", savedGoal.TextualGoal); }
public ActionResult Edit(int id) { Goal goal = _context.Goals.SingleOrDefault(item => item.Id == id); if (goal == null) { return(RedirectToAction("Index", "Goal")); } var model = new GoalEditViewModel { Goal = goal }; return(View(model)); }
public async Task <IActionResult> Create(int campaignId, GoalEditViewModel model) { var campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = campaignId }); if (campaign == null || !User.IsOrganizationAdmin(campaign.OrganizationId)) { return(Unauthorized()); } ValidateGoalEditViewModel(model); if (ModelState.IsValid) { await _mediator.SendAsync(new GoalEditCommand { Goal = model }); return(RedirectToAction("Details", "Campaign", new { area = "admin", id = campaignId })); } return(View("Edit", model)); }
public ActionResult Edit(GoalEditViewModel model) { if (!ModelState.IsValid) { return(View("Edit", model)); } Goal goalInDb = _context.Goals.SingleOrDefault(item => item.Id == model.Goal.Id); if (goalInDb == null) { return(RedirectToAction("Index", "Goal")); } goalInDb.Name = model.Goal.Name; goalInDb.DaysToComplete = model.Goal.DaysToComplete; goalInDb.Reward = model.Goal.Reward; _context.SaveChanges(); return(RedirectToAction("Index", "Goal")); }
private void ValidateGoalEditViewModel(GoalEditViewModel model) { if (model.GoalType == GoalType.Numeric) { if (model.NumericGoal <= 0) { ModelState.AddModelError(nameof(model.NumericGoal), "The field Numeric Goal must be greater than 0"); } if (model.CurrentGoalLevel < 0) { ModelState.AddModelError(nameof(model.CurrentGoalLevel), "The field Current Goal Level must be greater than or equal to 0"); } } else { if (string.IsNullOrEmpty(model.TextualGoal)) { ModelState.AddModelError(nameof(model.TextualGoal), "The Campaign Goal field is required for a Textual Goal"); } } }
public async Task <IActionResult> Edit(int id, GoalEditViewModel model) { var goal = await _mediator.SendAsync(new GoalEditQuery { GoalId = id }); if (goal == null || !User.IsOrganizationAdmin(goal.OrganizationId)) { return(Unauthorized()); } if (id != model.Id) { return(BadRequest()); } ValidateGoalEditViewModel(model); if (ModelState.IsValid) { await _mediator.SendAsync(new GoalEditCommand { Goal = model }); return(RedirectToAction("Details", "Campaign", new { area = "admin", id = goal.CampaignId })); } return(View("Edit", model)); }