public List<KeyValuePair<string, string>> Validate(ItineraryEditViewModel model, EventSummaryViewModel 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(ItineraryEditViewModel model, EventSummaryViewModel eventSummary)
        {
            var result = new List<KeyValuePair<string, string>>();

            var itineraryDateConverted = ConvertIntineraryDateToEventsTimeZone(model.Date, eventSummary.TimeZoneId);

            if (itineraryDateConverted.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:d}"));
            }

            if (itineraryDateConverted.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:d}"));
            }

            return result;
        }
        public async Task CreateCallsValidatorWithCorrectItineraryEditModelAndEventSummaryModel()
        {
            var model = new ItineraryEditViewModel
            {
                EventId = 1,
                Name = "Test",
                Date = new DateTime(2016, 06, 01)
            };

            var mediator = new Mock<IMediator>();

            var eventSummaryModel = new EventSummaryViewModel
            {
                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 EditPost_ReturnsRedirectToAction_WhenEventIsNotNullAndUserIsOrgAdmin()
        {
            const int eventId = 1;
            const int orgId = 1;
            var viewModel = new EventSummaryViewModel { Id = eventId, OrganizationId = orgId };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<EventSummaryQuery>())).ReturnsAsync(viewModel);

            mockMediator.Setup(x => x.SendAsync(It.IsAny<EditRequestCommand>())).ReturnsAsync(Guid.NewGuid());

            var sut = new RequestController(mockMediator.Object);
            sut.MakeUserAnOrgAdmin(orgId.ToString());

            var result = await sut.Edit(new EditRequestViewModel { EventId = eventId });

            var objResult = Assert.IsType<RedirectToActionResult>(result);
            objResult.ActionName.ShouldBe("Requests");
            objResult.ControllerName.ShouldBe("Event");
        }
        public async Task EditPost_SendsEditRequestCommand_WhenEventIsNotNullAndUserIsOrgAdmin()
        {
            const int eventId = 1;
            const int orgId = 1;
            var viewModel = new EventSummaryViewModel { Id = eventId, OrganizationId = orgId };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<EventSummaryQuery>())).ReturnsAsync(viewModel);

            EditRequestCommand editRequestCommand = null; // will be assigned from the Moq callback
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EditRequestCommand>())).ReturnsAsync(Guid.NewGuid()).Callback<EditRequestCommand>(cmd => editRequestCommand = cmd).Verifiable();

            var sut = new RequestController(mockMediator.Object);
            sut.MakeUserAnOrgAdmin(orgId.ToString());

            await sut.Edit(new EditRequestViewModel { EventId = eventId });

            mockMediator.Verify(x => x.SendAsync(It.IsAny<EditRequestCommand>()), Times.Once);

            editRequestCommand.RequestModel.EventId.ShouldBe(eventId);
        }
        public async Task EditPost_ReturnsViewResult_WhenModelStateIsNotValid()
        {
            const int eventId = 1;
            const int orgId = 1;
            var viewModel = new EventSummaryViewModel { Id = eventId, OrganizationId = orgId };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<EventSummaryQuery>())).ReturnsAsync(viewModel);

            var sut = new RequestController(mockMediator.Object);
            sut.MakeUserAnOrgAdmin(orgId.ToString());
            sut.ModelState.AddModelError("test", "test error");

            var result = await sut.Edit(new EditRequestViewModel { EventId = eventId });

            var objResult = Assert.IsType<ViewResult>(result);
            objResult.ViewName.ShouldBe("Edit");
        }
        public async Task Create_ReturnsCorrectViewAndViewModel_WhenEventIsNotNullAndUserIsOrgAdmin()
        {
            const int eventId = 1;
            const int orgId = 1;
            var viewModel = new EventSummaryViewModel { Id = eventId, OrganizationId = orgId };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<EventSummaryQuery>())).ReturnsAsync(viewModel);

            var sut = new RequestController(mockMediator.Object);
            sut.MakeUserAnOrgAdmin(orgId.ToString());

            var result = await sut.Create(It.IsAny<int>());

            var objResult = Assert.IsType<ViewResult>(result);

            objResult.ViewName.ShouldBe("Edit");

            var resultViewModel = Assert.IsType<EditRequestViewModel>(objResult.ViewData.Model);
            resultViewModel.EventId.ShouldBe(viewModel.Id);
            resultViewModel.OrganizationId.ShouldBe(viewModel.OrganizationId);

            var viewBagTitle = objResult.ViewData["Title"];
            viewBagTitle.ShouldNotBeNull();
            viewBagTitle.ShouldBe(RequestController.CreateRequestTitle);
        }