public async Task CreateCallsValidatorWithCorrectItineraryEditModelAndEventSummaryModel() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var mediator = new Mock <IMediator>(); var eventSummaryModel = new EventSummaryModel { Id = 1, Name = "Event", OrganizationId = 1, StartDateTime = new DateTimeOffset(new DateTime(2016, 01, 01)), EndDateTime = new DateTimeOffset(new DateTime(2016, 12, 31)) }; mediator.Setup(x => x.SendAsync(It.IsAny <EventSummaryQuery>())).ReturnsAsync(eventSummaryModel); mediator.Setup(x => x.SendAsync(It.IsAny <EditItineraryCommand>())).ReturnsAsync(1); var mockValidator = new Mock <IItineraryEditModelValidator>(); mockValidator.Setup(mock => mock.Validate(model, eventSummaryModel)).Returns(new List <KeyValuePair <string, string> >()).Verifiable(); var sut = new ItineraryController(mediator.Object, mockValidator.Object); sut.SetClaims(new List <Claim> { new Claim(AllReady.Security.ClaimTypes.UserType, UserType.SiteAdmin.ToString()), new Claim(AllReady.Security.ClaimTypes.Organization, "1") }); await sut.Create(model); mockValidator.Verify(x => x.Validate(model, eventSummaryModel), Times.Once); }
public async Task CreateReturnsHttpBadRequestResultWhenEditItineraryReturnsZero() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var mediator = new Mock <IMediator>(); mediator.Setup(x => x.SendAsync(It.IsAny <EventSummaryQuery>())).ReturnsAsync(new EventSummaryModel { Id = 1, Name = "Event", OrganizationId = 1, StartDateTime = new DateTimeOffset(new DateTime(2016, 01, 01)), EndDateTime = new DateTimeOffset(new DateTime(2016, 12, 31)) }); mediator.Setup(x => x.SendAsync(It.IsAny <EditItineraryCommand>())).ReturnsAsync(0); var mockValidator = new Mock <IItineraryEditModelValidator>(); mockValidator.Setup(mock => mock.Validate(It.IsAny <ItineraryEditModel>(), It.IsAny <EventSummaryModel>())).Returns(new List <KeyValuePair <string, string> >()).Verifiable(); var sut = new ItineraryController(mediator.Object, mockValidator.Object); sut.SetClaims(new List <Claim> { new Claim(AllReady.Security.ClaimTypes.UserType, UserType.SiteAdmin.ToString()), new Claim(AllReady.Security.ClaimTypes.Organization, "1") }); var result = await sut.Create(model); Assert.IsType <BadRequestResult>(result); }
public async Task CreateReturnsHttpUnauthorizedResultWhenUserIsNotOrgAdminForEventOrg() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var sut = GetItineraryControllerWithEventSummaryQuery(UserType.OrgAdmin.ToString(), 5); Assert.IsType <HttpUnauthorizedResult>(await sut.Create(model)); }
public async Task CreateReturnsOkResultWhenUserIsSiteAdmin_AndModelIsValid_AndSuccessfulAdd() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var sut = GetItineraryControllerWithEventSummaryQuery(UserType.SiteAdmin.ToString(), 0); Assert.IsType <HttpOkObjectResult>(await sut.Create(model)); }
public async Task CreateReturnsOkResultWhenUserIsOrgAdmin() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var sut = GetItineraryControllerWithEventSummaryQuery(UserType.OrgAdmin.ToString(), 1); Assert.IsType <OkObjectResult>(await sut.Create(model)); }
public void ReturnsNoErrorForWhenDatesIsBetweenStartAndEndOfParentEvent() { var sut = new ItineraryEditModelValidator(); var model = new ItineraryEditModel { EventId = 1, Date = eventStartDate.AddDays(1).DateTime }; var errors = sut.Validate(model, TestEvent); Assert.True(errors.Count == 0); }
public void ReturnsNoErrorForWhenDatesIsBetweenStartAndEndOfParentEvent() { var sut = new ItineraryEditModelValidator(); var model = new ItineraryEditModel { EventId = 1, Date = eventStartDate.AddDays(1).DateTime }; var errors = sut.Validate(model, TestEvent); Assert.True(errors.Count == 0); }
public void ReturnsCorrectErrorWhenModelsDateIsGreaterThanParentEventEndDate() { var sut = new ItineraryEditModelValidator(); var model = new ItineraryEditModel { EventId = 1, Date = eventEndDate.AddDays(1).DateTime }; var errors = sut.Validate(model, TestEvent); Assert.True(errors.Exists(x => x.Key.Equals("Date"))); Assert.Equal(errors.Find(x => x.Key == "Date").Value, "Date cannot be later than the event end date " + eventEndDate.Date.ToString("d")); }
public void ReturnsCorrectErrorWhenModelsDateIsGreaterThanParentEventEndDate() { var sut = new ItineraryEditModelValidator(); var model = new ItineraryEditModel { EventId = 1, Date = eventEndDate.AddDays(1).DateTime }; var errors = sut.Validate(model, TestEvent); Assert.True(errors.Exists(x => x.Key.Equals("Date"))); Assert.Equal(errors.Find(x => x.Key == "Date").Value, "Date cannot be later than the event end date " + eventEndDate.Date.ToString("d")); }
public async Task CreateReturnsHttpBadRequestResultWhenModelStateHasError() { var model = new ItineraryEditModel { EventId = 1, Date = new DateTime(2016, 06, 01) }; var sut = GetItineraryControllerWithEventSummaryQuery(UserType.SiteAdmin.ToString(), 0); sut.ModelState.AddModelError("key", "Error"); var result = await sut.Create(model); Assert.IsType <BadRequestObjectResult>(result); }
public List <KeyValuePair <string, string> > Validate(ItineraryEditModel model, EventSummaryModel eventSummary) { var result = new List <KeyValuePair <string, string> >(); if (model.Date < eventSummary.StartDateTime.Date) { result.Add(new KeyValuePair <string, string>(nameof(model.Date), "Date cannot be earlier than the event start date " + eventSummary.StartDateTime.Date.ToString("d"))); } if (model.Date > eventSummary.EndDateTime.Date) { result.Add(new KeyValuePair <string, string>(nameof(model.Date), "Date cannot be later than the event end date " + eventSummary.EndDateTime.Date.ToString("d"))); } return(result); }
public List<KeyValuePair<string, string>> Validate(ItineraryEditModel model, EventSummaryModel eventSummary) { var result = new List<KeyValuePair<string, string>>(); if (model.Date < eventSummary.StartDateTime.Date) { result.Add(new KeyValuePair<string, string>(nameof(model.Date), "Date cannot be earlier than the event start date " + eventSummary.StartDateTime.Date.ToString("d"))); } if (model.Date > eventSummary.EndDateTime.Date) { result.Add(new KeyValuePair<string, string>(nameof(model.Date), "Date cannot be later than the event end date " + eventSummary.EndDateTime.Date.ToString("d"))); } return result; }
public async Task CreateSendsEventSummaryQuery() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var mockMediator = new Mock <IMediator>(); mockMediator.Setup(x => x.SendAsync(It.IsAny <EventSummaryQuery>())) .ReturnsAsync(It.IsAny <EventSummaryModel>()).Verifiable(); var sut = new ItineraryController(mockMediator.Object, MockSuccessValidation().Object); await sut.Create(model); mockMediator.Verify(x => x.SendAsync(It.IsAny <EventSummaryQuery>()), Times.Once); }
public async Task <IActionResult> Create(ItineraryEditModel model) { if (model == null) { return(HttpBadRequest()); } var campaignEvent = await _mediator.SendAsync(new EventSummaryQuery { EventId = model.EventId }); if (campaignEvent == null) { return(HttpBadRequest()); } if (!User.IsOrganizationAdmin(campaignEvent.OrganizationId)) { return(HttpUnauthorized()); } var errors = _itineraryValidator.Validate(model, campaignEvent); errors.ToList().ForEach(e => ModelState.AddModelError(e.Key, e.Value)); if (!ModelState.IsValid) { return(HttpBadRequest(ModelState)); } var result = await _mediator.SendAsync(new EditItineraryCommand() { Itinerary = model }); if (result > 0) { return(Ok(result)); } return(HttpBadRequest()); }
public async Task<IActionResult> Create(ItineraryEditModel model) { if (model == null) { return BadRequest(); } var campaignEvent = await _mediator.SendAsync(new EventSummaryQuery { EventId = model.EventId }); if (campaignEvent == null) { return BadRequest(); } if (!User.IsOrganizationAdmin(campaignEvent.OrganizationId)) { return Unauthorized(); } var errors = _itineraryValidator.Validate(model, campaignEvent); errors.ToList().ForEach(e => ModelState.AddModelError(e.Key, e.Value)); if (!ModelState.IsValid) { return BadRequest(ModelState); } var result = await _mediator.SendAsync(new EditItineraryCommand { Itinerary = model }); if (result > 0) { return Ok(result); } return BadRequest(); }
public async Task CreateReturnsOkResultWhenUserIsOrgAdmin() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var sut = GetItineraryControllerWithEventSummaryQuery(UserType.OrgAdmin.ToString(), 1); Assert.IsType<OkObjectResult>(await sut.Create(model)); }
public async Task CreateReturnsOkResultWhenUserIsSiteAdmin_AndModelIsValid_AndSuccessfulAdd() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var sut = GetItineraryControllerWithEventSummaryQuery(UserType.SiteAdmin.ToString(), 0); Assert.IsType<HttpOkObjectResult>(await sut.Create(model)); }
public async Task CreateSendsEventSummaryQueryWithCorrectEventId() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var mockMediator = new Mock<IMediator>(); mockMediator.Setup(x => x.SendAsync(It.IsAny<EventSummaryQuery>())) .ReturnsAsync(It.IsAny<EventSummaryModel>()).Verifiable(); var sut = new ItineraryController(mockMediator.Object, MockSuccessValidation().Object); await sut.Create(model); mockMediator.Verify(x => x.SendAsync(It.Is<EventSummaryQuery>(y => y.EventId == model.EventId)), Times.Once); }
public async Task CreateReturnsHttpUnauthorizedResultWhenUserIsNotOrgAdminForEventOrg() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var sut = GetItineraryControllerWithEventSummaryQuery(UserType.OrgAdmin.ToString(), 5); Assert.IsType<HttpUnauthorizedResult>(await sut.Create(model)); }
public async Task CreateReturnsHttpBadRequestResultWhenModelStateHasError() { var model = new ItineraryEditModel { EventId = 1, Date = new DateTime(2016, 06, 01) }; var sut = GetItineraryControllerWithEventSummaryQuery(UserType.SiteAdmin.ToString(), 0); sut.ModelState.AddModelError("key", "Error"); var result = await sut.Create(model); Assert.IsType<BadRequestObjectResult>(result); }
public async Task CreateCallsValidatorWithCorrectItineraryEditModelAndEventSummaryModel() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var mediator = new Mock<IMediator>(); var eventSummaryModel = new EventSummaryModel { Id = 1, Name = "Event", OrganizationId = 1, StartDateTime = new DateTimeOffset(new DateTime(2016, 01, 01)), EndDateTime = new DateTimeOffset(new DateTime(2016, 12, 31)) }; mediator.Setup(x => x.SendAsync(It.IsAny<EventSummaryQuery>())).ReturnsAsync(eventSummaryModel); mediator.Setup(x => x.SendAsync(It.IsAny<EditItineraryCommand>())).ReturnsAsync(1); var mockValidator = new Mock<IItineraryEditModelValidator>(); mockValidator.Setup(mock => mock.Validate(model, eventSummaryModel)).Returns(new List<KeyValuePair<string, string>>()).Verifiable(); var sut = new ItineraryController(mediator.Object, mockValidator.Object); sut.SetClaims(new List<Claim> { new Claim(AllReady.Security.ClaimTypes.UserType, UserType.SiteAdmin.ToString()), new Claim(AllReady.Security.ClaimTypes.Organization, "1") }); await sut.Create(model); mockValidator.Verify(x => x.Validate(model, eventSummaryModel), Times.Once); }
public async Task CreateReturnsHttpBadRequestResultWhenEditItineraryReturnsZero() { var model = new ItineraryEditModel { EventId = 1, Name = "Test", Date = new DateTime(2016, 06, 01) }; var mediator = new Mock<IMediator>(); mediator.Setup(x => x.SendAsync(It.IsAny<EventSummaryQuery>())).ReturnsAsync(new EventSummaryModel { Id = 1, Name = "Event", OrganizationId = 1, StartDateTime = new DateTimeOffset(new DateTime(2016, 01, 01)), EndDateTime = new DateTimeOffset(new DateTime(2016, 12, 31)) }); mediator.Setup(x => x.SendAsync(It.IsAny<EditItineraryCommand>())).ReturnsAsync(0); var mockValidator = new Mock<IItineraryEditModelValidator>(); mockValidator.Setup(mock => mock.Validate(It.IsAny<ItineraryEditModel>(), It.IsAny<EventSummaryModel>())).Returns(new List<KeyValuePair<string, string>>()).Verifiable(); var sut = new ItineraryController(mediator.Object, mockValidator.Object); sut.SetClaims(new List<Claim> { new Claim(AllReady.Security.ClaimTypes.UserType, UserType.SiteAdmin.ToString()), new Claim(AllReady.Security.ClaimTypes.Organization, "1") }); var result = await sut.Create(model); Assert.IsType<BadRequestResult>(result); }