public async Task ReportErrorsWhenDatesAreInvalid() { // arrage var mediator = new Mock <IMediator>(); var validator = new CampaignSummaryModelValidator(mediator.Object); var model = new CampaignSummaryModel { StartDate = new DateTimeOffset(new DateTime(2000, 1, 1)), EndDate = new DateTimeOffset(new DateTime(1999, 1, 1)) }; // act var errors = await validator.Validate(model); // assert Assert.True(errors.ContainsKey("EndDate")); }
public async Task <IActionResult> Edit(CampaignSummaryModel campaign, IFormFile fileUpload) { if (campaign == null) { return(HttpBadRequest()); } if (!User.IsOrganizationAdmin(campaign.OrganizationId)) { return(HttpUnauthorized()); } var validator = new CampaignSummaryModelValidator(_mediator); var errors = await validator.Validate(campaign); errors.ToList().ForEach(e => ModelState.AddModelError(e.Key, e.Value)); if (ModelState.IsValid) { if (fileUpload != null) { if (fileUpload.IsAcceptableImageContentType()) { campaign.ImageUrl = await _imageService.UploadCampaignImageAsync(campaign.OrganizationId, campaign.Id, fileUpload); } else { ModelState.AddModelError("ImageUrl", "You must upload a valid image file for the logo (.jpg, .png, .gif)"); return(View(campaign)); } } var id = _mediator.SendAsync(new EditCampaignCommand { Campaign = campaign }); return(RedirectToAction(nameof(Details), new { area = "Admin", id = id })); } return(View(campaign)); }
public async Task ReportErrorsWhenPostalCodeIsInvalid() { // arrange var mediator = new Mock <IMediator>(); mediator.Setup(m => m.SendAsync(It.IsAny <CheckValidPostcodeQueryAsync>())).ReturnsAsync(false); var validator = new CampaignSummaryModelValidator(mediator.Object); var model = new CampaignSummaryModel { Location = new LocationEditModel { PostalCode = "90210" } }; // act var errors = await validator.Validate(model); // assert mediator.Verify(m => m.SendAsync(It.IsAny <CheckValidPostcodeQueryAsync>())); Assert.True(errors.ContainsKey("PostalCode")); }