public void ValidationShouldFail_IfNoNameIsSupplied()
        {
            var model = new TaskSummaryViewModel();
            var result = ValidateModel(model);

            var nameError = result.Where(r => r.ErrorMessage == "The Name field is required.").FirstOrDefault();

            Assert.NotNull(nameError);
        }
Ejemplo n.º 2
0
        public void ReturnsCorrectErrorWhenModelsEndDateTimeIsGreaterThanParentEventStartDate()
        {
            var validator = GetValidator();

            var model = new TaskSummaryViewModel
            {
                StartDateTime = eventStartDate.AddDays(1),
                EndDateTime = eventEndDate.AddDays(10)
            };

            var errors = validator.Validate(model);

            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "EndDateTime").Value, "End date cannot be later than the event end date " + eventEndDate.ToString("d"));
        }
Ejemplo n.º 3
0
            public void ValidationShouldFail_IfNoNameIsSupplied()
            {
                //arrange
                using (new CultureContext(new CultureInfo("en")))
                {
                    var model = new TaskSummaryViewModel();
                    var tester = new PropertyValidationHelper<TaskSummaryViewModel>(model);

                    //act
                    var result = tester.ValidateProperty(m => m.Name);

                    //assert
                    Assert.Contains("The Name field is required.", result.Select(r => r.ErrorMessage));
                }
            }
Ejemplo n.º 4
0
        public void ReturnsCorrectErrorWhenItineraryTaskWithStartAndEndDatesNotOnSameDay()
        {
            var validator = GetValidator();

            var model = new TaskSummaryViewModel
            {
                StartDateTime = new DateTimeOffset(new DateTime(1999, 12, 1)),
                EndDateTime = new DateTimeOffset(new DateTime(2000, 12, 1))
            };

            var errors = validator.Validate(model);

            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "EndDateTime").Value, "For itinerary events the task end date must occur on the same day as the start date. Tasks cannot span multiple days");
        }
Ejemplo n.º 5
0
            public void ValidationShouldFail_IfNoNumberOfVolunteersRequiredIsSupplied()
            {
                //arrange
                using (new CultureContext(new CultureInfo("en")))
                {
                    var model = new TaskSummaryViewModel();
                    var tester = new PropertyValidationHelper<TaskSummaryViewModel>(model);

                    //act
                    var result = tester.ValidateProperty(m => m.NumberOfVolunteersRequired);

                    //assert
                    Assert.Contains("'Volunteers Required' must be greater than 0", result.Select(r => r.ErrorMessage));
                }
            }
Ejemplo n.º 6
0
        public List<KeyValuePair<string, string>> Validate(TaskSummaryViewModel model)
        {
            var result = new List<KeyValuePair<string, string>>();

            var campaignEvent = _mediator.Send(new EventByIdQuery { EventId = model.EventId });

            var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(campaignEvent.Campaign.TimeZoneId);

            // sgordon: Date time conversion seems overly complex and may be refactored per #710
            var startDateValue = model.StartDateTime;
            var startDateTimeOffset = timeZoneInfo.GetUtcOffset(startDateValue);
            var convertedStartDateTime = new DateTimeOffset(startDateValue.Year, startDateValue.Month, startDateValue.Day, startDateValue.Hour, startDateValue.Minute, 0, startDateTimeOffset);

            var endDateValue = model.EndDateTime;
            var endDateTimeOffset = timeZoneInfo.GetUtcOffset(endDateValue);
            var convertedEndDateTime = new DateTimeOffset(endDateValue.Year, endDateValue.Month, endDateValue.Day, endDateValue.Hour, endDateValue.Minute, 0, endDateTimeOffset);

            // Rule - End date cannot be earlier than start date
            if (convertedEndDateTime < convertedStartDateTime)
            {
                result.Add(new KeyValuePair<string, string>(nameof(model.EndDateTime), "End date cannot be earlier than the start date"));
            }

            // Rule - Start date cannot be out of range of parent event
            if (convertedStartDateTime < campaignEvent.StartDateTime)
            {
                result.Add(new KeyValuePair<string, string>(nameof(model.StartDateTime), "Start date cannot be earlier than the event start date " + campaignEvent.StartDateTime.ToString("d")));
            }

            // Rule - End date cannot be out of range of parent event
            if (convertedEndDateTime > campaignEvent.EndDateTime)
            {
                result.Add(new KeyValuePair<string, string>(nameof(model.EndDateTime), "End date cannot be later than the event end date " + campaignEvent.EndDateTime.ToString("d")));
            }

            // Rule - Itinerary tasks must start and end on same calendar day
            if (campaignEvent.EventType == EventType.Itinerary)
            {
                if (convertedStartDateTime.Date != convertedEndDateTime.Date)
                {
                    result.Add(new KeyValuePair<string, string>(nameof(model.EndDateTime), "For itinerary events the task end date must occur on the same day as the start date. Tasks cannot span multiple days"));
                }
            }

            return result;
        }
Ejemplo n.º 7
0
            public void ValidationShouldSucceed_IfNameIsSupplied()
            {
                //arrange
                using (new CultureContext(new CultureInfo("en")))
                {
                    var model = new TaskSummaryViewModel
                    {
                        Name = "name"
                    };
                    var tester = new PropertyValidationHelper<TaskSummaryViewModel>(model);

                    //act
                    var result = tester.ValidateProperty(m => m.Name);

                    //assert
                    Assert.Empty(result);
                }
            }
Ejemplo n.º 8
0
        public void ReturnsCorrectErrorWhenEndDateTimeIsLessThanStartDateTime()
        {
            var mockMediator = new Mock<IMediator>();

            mockMediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event { Id = 1, Campaign = new Campaign { TimeZoneId = "UTC" } });

            var validator = new TaskSummaryModelValidator(mockMediator.Object);

            var model = new TaskSummaryViewModel
            {
                StartDateTime = new DateTimeOffset(new DateTime(2000, 1, 1)),
                EndDateTime = new DateTimeOffset(new DateTime(1999, 1, 1))
            };

            var errors = validator.Validate(model);

            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "EndDateTime").Value, "End date cannot be earlier than the start date");
        }
Ejemplo n.º 9
0
        public async Task<IActionResult> Edit(TaskSummaryViewModel model)
        {
            var errors = _taskDetailModelValidator.Validate(model);

            errors.ToList().ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            if (ModelState.IsValid)
            {
                if (!User.IsOrganizationAdmin(model.OrganizationId))
                {
                    return Unauthorized();
                }
                
                await _mediator.SendAsync(new EditTaskCommandAsync { Task = model });

                return RedirectToAction(nameof(Details), "Task", new { id = model.Id });
            }

            return View(model);
        }
Ejemplo n.º 10
0
        public async Task<IActionResult> Create(int eventId, TaskSummaryViewModel model)
        {
            var errors = _taskDetailModelValidator.Validate(model);

            errors.ToList().ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            if (ModelState.IsValid)
            {
                if (!User.IsOrganizationAdmin(model.OrganizationId))
                {
                    return Unauthorized();
                }
                
                await _mediator.SendAsync(new EditTaskCommandAsync { Task = model });

                //mgmccarthy: I'm assuming this is EventController in the Admin area
                return RedirectToAction(nameof(Details), "Event", new { id = eventId });
            }

            return View("Edit", model);
        }
Ejemplo n.º 11
0
        public IActionResult Create(int eventId)
        {
            var campaignEvent = GetEventBy(eventId);
            if (campaignEvent == null || !User.IsOrganizationAdmin(campaignEvent.Campaign.ManagingOrganizationId))
            {
                return Unauthorized();
            }
            
            var viewModel = new TaskSummaryViewModel
            {
                EventId = campaignEvent.Id,
                EventName = campaignEvent.Name,
                EventStartDateTime = campaignEvent.StartDateTime,
                EventEndDateTime = campaignEvent.EndDateTime,
                CampaignId = campaignEvent.CampaignId,
                CampaignName = campaignEvent.Campaign.Name,
                OrganizationId = campaignEvent.Campaign.ManagingOrganizationId,
                TimeZoneId = campaignEvent.Campaign.TimeZoneId,
                StartDateTime = campaignEvent.StartDateTime,
                EndDateTime = campaignEvent.EndDateTime
            };

            return View("Edit", viewModel);
        }
Ejemplo n.º 12
0
        public async Task DeleteConfirmedRedirectsToCorrectAction()
        {
            const int organizationId = 1;
            const int taskId = 1;
            var taskSummaryModel = new TaskSummaryViewModel { OrganizationId = organizationId, EventId = 1 };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<TaskQueryAsync>())).ReturnsAsync(taskSummaryModel);

            var sut = new TaskController(mediator.Object, Mock.Of<ITaskSummaryModelValidator>());
            MakeUserOrganizationAdminUser(sut, organizationId.ToString());
            var result = await sut.DeleteConfirmed(taskId) as RedirectToActionResult;

            var routeValues = new Dictionary<string, object> { ["id"] = taskSummaryModel.EventId };

            Assert.Equal(result.ActionName, nameof(EventController.Details));
            Assert.Equal(result.ControllerName, "Event");
            Assert.Equal(result.RouteValues, routeValues);
        }
Ejemplo n.º 13
0
        public async Task DetailsReturnsCorrectViewModelAndView()
        {
            var taskSummaryModel = new TaskSummaryViewModel();

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<TaskQueryAsync>())).ReturnsAsync(taskSummaryModel);

            var sut = new TaskController(mediator.Object, Mock.Of<ITaskSummaryModelValidator>());
            var result = await sut.Details(It.IsAny<int>()) as ViewResult;
            var modelResult = result.ViewData.Model as TaskSummaryViewModel;

            Assert.IsType<ViewResult>(result);
            Assert.IsType<TaskSummaryViewModel>(modelResult);
            Assert.Equal(modelResult, taskSummaryModel);
        }
Ejemplo n.º 14
0
        public async Task DeleteReturnsCorrectViewModelAndView()
        {
            const int organizationId = 1;
            var taskSummaryModel = new TaskSummaryViewModel { OrganizationId = organizationId };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<TaskQueryAsync>())).ReturnsAsync(taskSummaryModel);

            var sut = new TaskController(mediator.Object, Mock.Of<ITaskSummaryModelValidator>());
            MakeUserOrganizationAdminUser(sut, organizationId.ToString());

            var result = await sut.Delete(It.IsAny<int>()) as ViewResult;
            var modelResult = result.ViewData.Model as TaskSummaryViewModel;

            Assert.IsType<ViewResult>(result);
            Assert.IsType<TaskSummaryViewModel>(modelResult);
            Assert.Equal(modelResult, taskSummaryModel);
        }
Ejemplo n.º 15
0
        public async Task EditPostRedirectsToCorrectAction()
        {
            const int organizationId = 1;
            var taskSummaryModel = new TaskSummaryViewModel { OrganizationId = organizationId };
            var startDateTime = DateTimeOffset.Now.AddDays(-1);
            var endDateTime = DateTimeOffset.Now.AddDays(1);
            var model = new TaskSummaryViewModel { Id = 1, StartDateTime = startDateTime, EndDateTime = endDateTime, OrganizationId = organizationId };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event
            {
                Campaign = new Campaign { TimeZoneId = "Eastern Standard Time" },
                StartDateTime = startDateTime.AddDays(-1),
                EndDateTime = endDateTime.AddDays(1)
            });
            mediator.Setup(x => x.SendAsync(It.IsAny<TaskQueryAsync>())).ReturnsAsync(taskSummaryModel);

            var validator = new Mock<ITaskSummaryModelValidator>();
            validator.Setup(x => x.Validate(It.IsAny<TaskSummaryViewModel>())).Returns(new List<KeyValuePair<string, string>>());

            var sut = new TaskController(mediator.Object, validator.Object);
            MakeUserOrganizationAdminUser(sut, organizationId.ToString());

            var result = await sut.Edit(model) as RedirectToActionResult;

            var routeValues = new Dictionary<string, object> { ["id"] = model.Id };

            Assert.Equal(result.ControllerName, "Task");
            Assert.Equal(result.ActionName, nameof(TaskController.Details));
            Assert.Equal(result.RouteValues, routeValues);
        }
Ejemplo n.º 16
0
        public async Task EditPostSendsEditTaskCommandAsyncWhenModelStateIsValidAndUserIsOrganizationAdmin()
        {
            const int organizationId = 1;
            var taskSummaryModel = new TaskSummaryViewModel { OrganizationId = organizationId };
            var startDateTime = DateTimeOffset.Now.AddDays(-1);
            var endDateTime = DateTimeOffset.Now.AddDays(1);

            var model = new TaskSummaryViewModel { StartDateTime = startDateTime, EndDateTime = endDateTime, OrganizationId = organizationId };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event
            {
                Campaign = new Campaign { TimeZoneId = "Eastern Standard Time" },
                StartDateTime = startDateTime.AddDays(-1),
                EndDateTime = endDateTime.AddDays(1)
            });
            mediator.Setup(x => x.SendAsync(It.IsAny<TaskQueryAsync>())).ReturnsAsync(taskSummaryModel);

            var validator = new Mock<ITaskSummaryModelValidator>();
            validator.Setup(x => x.Validate(It.IsAny<TaskSummaryViewModel>())).Returns(new List<KeyValuePair<string, string>>());

            var sut = new TaskController(mediator.Object, validator.Object);
            MakeUserOrganizationAdminUser(sut, organizationId.ToString());

            await sut.Edit(model);

            mediator.Verify(x => x.SendAsync(It.Is<EditTaskCommandAsync>(y => y.Task == model)));
        }
Ejemplo n.º 17
0
        public void ReturnsNoErrorForNonItineraryTaskWhenModelsDatesAreValid()
        {
            var mockMediator = new Mock<IMediator>();

            mockMediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event
            {
                Id = 1,
                Campaign = new Campaign
                {
                    TimeZoneId = "UTC",
                },
                StartDateTime = eventStartDate,
                EndDateTime = eventEndDate,
                EventType = EventType.Rally
            });

            var validator = new TaskSummaryModelValidator(mockMediator.Object);

            var model = new TaskSummaryViewModel
            {
                StartDateTime = eventStartDate.AddDays(1),
                EndDateTime = eventEndDate.AddDays(-1)
            };

            var errors = validator.Validate(model);

            Assert.True(errors.Count == 0);
        }
Ejemplo n.º 18
0
        public async Task AssignSendsAssignTaskCommandAsync()
        {
            const int organizationId = 1;
            const int taskId = 1;
            var taskModelSummary = new TaskSummaryViewModel { EventId = 1 };
            var userIds = new List<string> { "1", "2" };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<TaskQueryAsync>())).ReturnsAsync(taskModelSummary);
            mediator.Setup(x => x.Send(It.Is<EventByIdQuery>(y => y.EventId == taskModelSummary.EventId))).Returns(new Event { Campaign = new Campaign { ManagingOrganizationId = organizationId } });

            var sut = new TaskController(mediator.Object, Mock.Of<ITaskSummaryModelValidator>());
            MakeUserOrganizationAdminUser(sut, organizationId.ToString());
            await sut.Assign(taskId, userIds);

            mediator.Verify(x => x.SendAsync(It.Is<AssignTaskCommandAsync>(y => y.TaskId == taskId && y.UserIds == userIds)), Times.Once);
        }
Ejemplo n.º 19
0
            public void ValidationShouldSucceed_IfNumberOfVolunteersRequiredIs1()
            {
                //arrange
                using (new CultureContext(new CultureInfo("en")))
                {
                    var model = new TaskSummaryViewModel
                    {
                        NumberOfVolunteersRequired = 1
                    };
                    var tester = new PropertyValidationHelper<TaskSummaryViewModel>(model);

                    //act
                    var result = tester.ValidateProperty(m => m.NumberOfVolunteersRequired);

                    //assert
                    Assert.Empty(result);
                }
            }
Ejemplo n.º 20
0
        public void ReturnsNoErrorForItineraryTaskWhenModelsDatesAreValid()
        {
            var validator = GetValidator();

            var model = new TaskSummaryViewModel
            {
                StartDateTime = eventStartDate.AddDays(1),
                EndDateTime = eventStartDate.AddDays(1).AddHours(2),
            };

            var errors = validator.Validate(model);

            Assert.True(errors.Count == 0);
        }
Ejemplo n.º 21
0
        public async Task AssignSendsTaskQueryAsyncWithCorrectTaskId()
        {
            const int taskId = 1;
            var taskModelSummary = new TaskSummaryViewModel { EventId = 1 };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event { Campaign = new Campaign { ManagingOrganizationId = 1 } });
            mediator.Setup(x => x.SendAsync(It.IsAny<TaskQueryAsync>())).ReturnsAsync(taskModelSummary);

            var sut = new TaskController(mediator.Object, Mock.Of<ITaskSummaryModelValidator>());
            sut.SetDefaultHttpContext();
            await sut.Assign(taskId, null);

            mediator.Verify(x => x.SendAsync(It.Is<TaskQueryAsync>(y => y.TaskId == taskId)), Times.Once);
        }
Ejemplo n.º 22
0
        public async Task AssignReturnsHttpUnauthorizedResultWhenUserIsNotOrgAdmin()
        {
            var taskModelSummary = new TaskSummaryViewModel { EventId = 1 };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<TaskQueryAsync>())).ReturnsAsync(taskModelSummary);
            mediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event { Campaign = new Campaign { ManagingOrganizationId = 1 } });

            var sut = new TaskController(mediator.Object, Mock.Of<ITaskSummaryModelValidator>());
            sut.SetDefaultHttpContext();
            var result = await sut.Assign(1, null);

            Assert.IsType<UnauthorizedResult>(result);
        }
Ejemplo n.º 23
0
        public async Task EditPostReturnsCorrectViewAndViewModelWhenValidatorReturnsError()
        {
            var model = new TaskSummaryViewModel { EndDateTime = DateTimeOffset.Now.AddDays(-1), StartDateTime = DateTimeOffset.Now.AddDays(1) };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event { Campaign = new Campaign { TimeZoneId = "Eastern Standard Time" } });

            var validator = new Mock<ITaskSummaryModelValidator>();
            validator.Setup(x => x.Validate(It.IsAny<TaskSummaryViewModel>())).Returns(new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>(nameof(TaskSummaryViewModel.EndDateTime), "Ending time cannot be earlier than the starting time") });

            var sut = new TaskController(mediator.Object, validator.Object);
            var result = await sut.Edit(model) as ViewResult;
            var modelResult = result.ViewData.Model as TaskSummaryViewModel;

            Assert.IsType<ViewResult>(result);
            Assert.Equal(modelResult, model);
        }
Ejemplo n.º 24
0
        public async Task AssignRedirectsToRoute()
        {
            const int organizationId = 1;
            const int taskId = 1;
            var taskModelSummary = new TaskSummaryViewModel { EventId = 1 };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<TaskQueryAsync>())).ReturnsAsync(taskModelSummary);
            mediator.Setup(x => x.Send(It.Is<EventByIdQuery>(y => y.EventId == taskModelSummary.EventId))).Returns(new Event { Campaign = new Campaign { ManagingOrganizationId = organizationId } });

            var sut = new TaskController(mediator.Object, Mock.Of<ITaskSummaryModelValidator>());
            MakeUserOrganizationAdminUser(sut, organizationId.ToString());

            var result = await sut.Assign(taskId, null) as RedirectToRouteResult;

            Assert.Equal(result.RouteValues["controller"], "Task");
            Assert.Equal(result.RouteValues["Area"], "Admin");
            Assert.Equal(result.RouteValues["action"], nameof(TaskController.Details));
            Assert.Equal(result.RouteValues["id"], taskId);
        }
Ejemplo n.º 25
0
        public async Task EditPostReturnsHttpUnauthorizedResultWhenModelStateIsValidAndUserIsNotAnOrganizationAdminUser()
        {
            var startDateTime = DateTimeOffset.Now.AddDays(-1);
            var endDateTime = DateTimeOffset.Now.AddDays(1);
            var model = new TaskSummaryViewModel { StartDateTime = startDateTime, EndDateTime = endDateTime, OrganizationId = 1 };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event
            {
                Campaign = new Campaign { TimeZoneId = "Eastern Standard Time" },
                StartDateTime = startDateTime.AddDays(-1),
                EndDateTime = endDateTime.AddDays(1)
            });

            var validator = new Mock<ITaskSummaryModelValidator>();
            validator.Setup(x => x.Validate(It.IsAny<TaskSummaryViewModel>())).Returns(new List<KeyValuePair<string, string>>());

            var sut = new TaskController(mediator.Object, validator.Object);
            sut.SetDefaultHttpContext();

            var result = await sut.Edit(model);

            Assert.IsType<UnauthorizedResult>(result);
        }