Exemplo n.º 1
0
        public void DisplayItineraries_ReturnsTrue_WhenEventIsItineraryManaged()
        {
            var sut = new EventDetailModel();
            sut.EventType = AllReady.Models.EventType.Itinerary;

            Assert.True(sut.DisplayItineraries);
        }
Exemplo n.º 2
0
        public void DisplayItineraries_ReturnsFalse_WhenEventIsNotItineraryManaged()
        {
            var sut = new EventDetailModel();
            sut.EventType = AllReady.Models.EventType.Rally;

            Assert.False(sut.DisplayItineraries);
        }
        public async Task ReportErrorsWhenEndDateIsInvalid()
        {
            // arrage
            var mediator = new Mock<IMediator>();
            var validator = new EventDetailModelValidator(mediator.Object);
            var campaign = new CampaignSummaryModel();
            var model = new EventDetailModel
            {
                StartDateTime = new DateTimeOffset(new DateTime(2000, 1, 1)),
                EndDateTime = new DateTimeOffset(new DateTime(1999, 1, 1))
            };

            // act
            var errors = await validator.Validate(model, campaign);

            // assert
            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
        }
Exemplo n.º 4
0
        public async Task<IActionResult> Create(int campaignId, EventDetailModel campaignEvent, IFormFile fileUpload)
        {
            var campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = campaignId });
            if (campaign == null || !User.IsOrganizationAdmin(campaign.OrganizationId))
            {
                return HttpUnauthorized();
            }

            var validator = new EventDetailModelValidator(_mediator);
            var errors = await validator.Validate(campaignEvent, campaign);
            errors.ToList().ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            //TryValidateModel is called explictly because of MVC 6 behavior that supresses model state validation during model binding when binding to an IFormFile.
            //See #619.
            if (ModelState.IsValid && TryValidateModel(campaignEvent))
            {
                if (fileUpload != null)
                {
                    if (!fileUpload.IsAcceptableImageContentType())
                    {
                        ModelState.AddModelError("ImageUrl", "You must upload a valid image file for the logo (.jpg, .png, .gif)");
                        return View("Edit", campaignEvent);
                    }
                }

                campaignEvent.OrganizationId = campaign.OrganizationId;
                var id = await _mediator.SendAsync(new EditEventCommand { Event = campaignEvent });

                if (fileUpload != null)
                {
                    // resave now that event has the ImageUrl
                    campaignEvent.Id = id;
                    campaignEvent.ImageUrl = await _imageService.UploadEventImageAsync(campaign.OrganizationId, id, fileUpload);
                    await _mediator.SendAsync(new EditEventCommand { Event = campaignEvent });
                }

                return RedirectToAction(nameof(Details), new { area = "Admin", id = id });
            }

            return View("Edit", campaignEvent);
        }
Exemplo n.º 5
0
        public async Task<IActionResult> Create(int campaignId)
        {
            var campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = campaignId });
            if (campaign == null || !User.IsOrganizationAdmin(campaign.OrganizationId))
            {
                return HttpUnauthorized();
            }

            var campaignEvent = new EventDetailModel
            {
                CampaignId = campaign.Id,
                CampaignName = campaign.Name,
                TimeZoneId = campaign.TimeZoneId,
                OrganizationId = campaign.OrganizationId,
                OrganizationName = campaign.OrganizationName,
                StartDateTime = DateTime.Today.Date,
                EndDateTime = DateTime.Today.Date.AddMonths(1)
            };

            return View("Edit", campaignEvent);
        }
Exemplo n.º 6
0
        public async Task EventDoesNotExist()
        {
            var context = ServiceProvider.GetService<AllReadyContext>();

            var htb = new Organization
            {
                Id = 123,
                Name = "Humanitarian Toolbox",
                LogoUrl = "http://www.htbox.org/upload/home/ht-hero.png",
                WebUrl = "http://www.htbox.org",
                Campaigns = new List<Campaign>()
            };

            var firePrev = new Campaign
            {
                Id = 1,
                Name = "Neighborhood Fire Prevention Days",
                ManagingOrganization = htb,
                TimeZoneId = "Central Standard Time"
            };
            htb.Campaigns.Add(firePrev);

            context.Organizations.Add(htb);
            context.SaveChanges();

            var vm = new EventDetailModel
            {
                CampaignId = 1,
                TimeZoneId = "Central Standard Time"
            };

            var query = new EditEventCommand { Event = vm };
            var handler = new EditEventCommandHandler(context);
            var result = await handler.Handle(query);
            Assert.True(result > 0);

            var data = context.Events.Count(_ => _.Id == result);
            Assert.True(data == 1);
        }
        public async Task ReportErrorsWhenPostalCodeInvalid()
        {
            // arrage
            var mediator = new Mock<IMediator>();
            var validator = new EventDetailModelValidator(mediator.Object);
            var campaign = new CampaignSummaryModel();
            var model = new EventDetailModel
            {
                Location = new LocationEditModel()
                {
                    PostalCode = "12345",
                    State = "WA",
                    City = "Seattle"
                }
            };

            // act
            var errors = await validator.Validate(model, campaign);

            // assert
            Assert.True(errors.Exists(x => x.Key.Equals("PostalCode")));
        }
        public async Task CreatePostReturnsEditView_When_EventEndDateAfterCampaignEndDate()
        {
            var campaignStartDate = new DateTimeOffset(new DateTime(1900, 1, 1));
            var campaignEndDate = campaignStartDate.AddDays(4);
            var sut = GetEventController(campaignStartDate, campaignEndDate);
            var eventModel = new EventDetailModel
            {
                EndDateTime = campaignEndDate.AddDays(1),
                StartDateTime = campaignStartDate
            };

            var result = (ViewResult)await sut.Create(1, eventModel, null);

            Assert.Equal("Edit", result.ViewName);
            var errors = sut.ModelState.GetErrorMessages();
            Assert.Equal(1, errors.Count);
            Assert.Equal("End date cannot be later than the campaign end date " + campaignEndDate.ToString("d"), errors[0]);
        }
        public async Task CreatePostReturnsEditView_When_ModelStateNotValid()
        {
            var sut = GetEventController();
            sut.ModelState.AddModelError("test", "test");
            var eventModel = new EventDetailModel();
            var result = (ViewResult)await sut.Create(1, eventModel, null);

            Assert.Equal("Edit", result.ViewName);
        }
Exemplo n.º 10
0
        public async Task ExistingEvent()
        {
            var context = ServiceProvider.GetService<AllReadyContext>();

            var htb = new Organization
            {
                Id = 123,
                Name = "Humanitarian Toolbox",
                LogoUrl = "http://www.htbox.org/upload/home/ht-hero.png",
                WebUrl = "http://www.htbox.org",
                Campaigns = new List<Campaign>()
            };

            var firePrev = new Campaign
            {
                Id = 1,
                Name = "Neighborhood Fire Prevention Days",
                ManagingOrganization = htb,
                TimeZoneId = "Central Standard Time"
            };
            htb.Campaigns.Add(firePrev);

            var queenAnne = new Event
            {
                Id = 100,
                Name = "Queen Anne Fire Prevention Day",
                Campaign = firePrev,
                CampaignId = firePrev.Id,
                StartDateTime = new DateTime(2015, 7, 4, 10, 0, 0).ToUniversalTime(),
                EndDateTime = new DateTime(2015, 12, 31, 15, 0, 0).ToUniversalTime(),
                Location = new Location { Id = 1 },
                RequiredSkills = new List<EventSkill>()
            };
            context.Organizations.Add(htb);
            context.Events.Add(queenAnne);
            context.SaveChanges();

            const string NEW_NAME = "Some new name value";

            var startDateTime = new DateTime(2015, 7, 12, 4, 15, 0);
            var endDateTime = new DateTime(2015, 12, 7, 15, 10, 0);
            var vm = new EventDetailModel
            {
                CampaignId = queenAnne.CampaignId,
                CampaignName = queenAnne.Campaign.Name,
                Description = queenAnne.Description,
                EndDateTime = endDateTime,
                Id = queenAnne.Id,
                ImageUrl = queenAnne.ImageUrl,
                Location = null,
                Name = NEW_NAME,
                RequiredSkills = queenAnne.RequiredSkills,
                TimeZoneId = "Central Standard Time",
                StartDateTime = startDateTime,
                Tasks = null,
                OrganizationId = queenAnne.Campaign.ManagingOrganizationId,
                OrganizationName = queenAnne.Campaign.ManagingOrganization.Name,
                Volunteers = null
            };
            var query = new EditEventCommand { Event = vm };
            var handler = new EditEventCommandHandler(context);
            var result = await handler.Handle(query);
            Assert.Equal(100, result); // should get back the event id

            var data = context.Events.Single(_ => _.Id == result);
            Assert.Equal(NEW_NAME, data.Name);

            Assert.Equal(2015, data.StartDateTime.Year);
            Assert.Equal(7, data.StartDateTime.Month);
            Assert.Equal(12, data.StartDateTime.Day);
            Assert.Equal(4, data.StartDateTime.Hour);
            Assert.Equal(15, data.StartDateTime.Minute);
            Assert.Equal(-5, data.StartDateTime.Offset.TotalHours);

            Assert.Equal(2015, data.EndDateTime.Year);
            Assert.Equal(12, data.EndDateTime.Month);
            Assert.Equal(7, data.EndDateTime.Day);
            Assert.Equal(15, data.EndDateTime.Hour);
            Assert.Equal(10, data.EndDateTime.Minute);
            Assert.Equal(-6, data.EndDateTime.Offset.TotalHours);
        }
Exemplo n.º 11
0
        public async Task ExistingEventUpdateLocation()
        {
            var seattlePostalCode = new PostalCodeGeo { City = "Seattle", PostalCode = "98117", State = "WA" };

            var seattle = new Location
            {
                Id = 1,
                Address1 = "123 Main Street",
                Address2 = "Unit 2",
                City = "Seattle",
                PostalCode = seattlePostalCode,
                Country = "USA",
                State = "WA",
                Name = "Organizer name",
                PhoneNumber = "555-555-5555"
            };

            var context = ServiceProvider.GetService<AllReadyContext>();

            var htb = new Organization
            {
                Id = 123,
                Name = "Humanitarian Toolbox",
                LogoUrl = "http://www.htbox.org/upload/home/ht-hero.png",
                WebUrl = "http://www.htbox.org",
                Campaigns = new List<Campaign>()
            };

            var firePrev = new Campaign
            {
                Id = 1,
                Name = "Neighborhood Fire Prevention Days",
                ManagingOrganization = htb,
                TimeZoneId = "Central Standard Time"
            };
            htb.Campaigns.Add(firePrev);

            var queenAnne = new Event
            {
                Id = 100,
                Name = "Queen Anne Fire Prevention Day",
                Campaign = firePrev,
                CampaignId = firePrev.Id,
                StartDateTime = new DateTime(2015, 7, 4, 10, 0, 0).ToUniversalTime(),
                EndDateTime = new DateTime(2015, 12, 31, 15, 0, 0).ToUniversalTime(),
                Location = seattle,
                RequiredSkills = new List<EventSkill>()
            };
            context.Locations.Add(seattle);
            context.Organizations.Add(htb);
            context.Events.Add(queenAnne);
            context.SaveChanges();

            var NEW_LOCATION = LocationExtensions.ToEditModel(new Location
            {
                Address1 = "123 new address",
                Address2 = "new suite",
                PostalCode = new PostalCodeGeo { City = "Bellevue", PostalCode = "98004", State = "WA" },
                City = "Bellevue",
                State = "WA",
                Country = "USA",
                Name = "New name",
                PhoneNumber = "New number"
            });

            var locationEdit = new EventDetailModel
            {
                CampaignId = queenAnne.CampaignId,
                CampaignName = queenAnne.Campaign.Name,
                Description = queenAnne.Description,
                EndDateTime = queenAnne.EndDateTime,
                Id = queenAnne.Id,
                ImageUrl = queenAnne.ImageUrl,
                Location = NEW_LOCATION,
                Name = queenAnne.Name,
                RequiredSkills = queenAnne.RequiredSkills,
                TimeZoneId = "Central Standard Time",
                StartDateTime = queenAnne.StartDateTime,
                Tasks = null,
                OrganizationId = queenAnne.Campaign.ManagingOrganizationId,
                OrganizationName = queenAnne.Campaign.ManagingOrganization.Name,
                Volunteers = null
            };

            var query = new EditEventCommand { Event = locationEdit };
            var handler = new EditEventCommandHandler(context);
            var result = await handler.Handle(query);
            Assert.Equal(100, result); // should get back the event id

            var data = context.Events.Single(a => a.Id == result);
            Assert.Equal(data.Location.Address1, NEW_LOCATION.Address1);
            Assert.Equal(data.Location.Address2, NEW_LOCATION.Address2);
            Assert.Equal(data.Location.City, NEW_LOCATION.City);
            Assert.Equal(data.Location.PostalCode?.PostalCode, NEW_LOCATION.PostalCode);
            Assert.Equal(data.Location.State, NEW_LOCATION.State);
            Assert.Equal(data.Location.Country, NEW_LOCATION.Country);
            Assert.Equal(data.Location.PhoneNumber, NEW_LOCATION.PhoneNumber);
            Assert.Equal(data.Location.Name, NEW_LOCATION.Name);
        }
Exemplo n.º 12
0
        public async Task<IActionResult> Edit(EventDetailModel campaignEvent, IFormFile fileUpload)
        {
            if (campaignEvent == null)
            {
                return HttpBadRequest();
            }
            
            var organizationId = _mediator.Send(new ManagingOrganizationIdByEventIdQuery { EventId = campaignEvent.Id });
            if (!User.IsOrganizationAdmin(organizationId))
            {
                return HttpUnauthorized();
            }

            var campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = campaignEvent.CampaignId });

            var validator = new EventDetailModelValidator(_mediator);
            var errors = await validator.Validate(campaignEvent, campaign);
            errors.ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            if (ModelState.IsValid)
            {
                if (fileUpload != null)
                {
                    if (fileUpload.IsAcceptableImageContentType())
                    {
                        campaignEvent.ImageUrl = await _imageService.UploadEventImageAsync(campaign.OrganizationId, campaignEvent.Id, fileUpload);
                    }
                    else
                    {
                        ModelState.AddModelError("ImageUrl", "You must upload a valid image file for the logo (.jpg, .png, .gif)");
                        return View(campaignEvent);
                    }
                }
                
                var id = await _mediator.SendAsync(new EditEventCommand { Event = campaignEvent });

                return RedirectToAction(nameof(Details), new { area = "Admin", id = id });
            }

            return View(campaignEvent);
        }