Ejemplo n.º 1
0
        public void ValidationShouldFail_IfNoNameIsSupplied()
        {
            var model = new TaskSummaryModel();
            var result = ValidateModel(model);

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

            Assert.NotNull(nameError);
        }
        public void ReturnsCorrectErrorWhenModelsEndDateTimeIsGreaterThanParentEventStartDate()
        {
            var validator = GetValidator();

            var model = new TaskSummaryModel
            {
                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"));
        }
        public void ReturnsCorrectErrorWhenItineraryTaskWithStartAndEndDatesNotOnSameDay()
        {
            var validator = GetValidator();

            var model = new TaskSummaryModel
            {
                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");
        }
        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 TaskSummaryModel
            {
                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.º 5
0
        public async Task<IActionResult> Edit(TaskSummaryModel 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 HttpUnauthorized();
                }
                
                await _mediator.SendAsync(new EditTaskCommandAsync { Task = model });

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

            return View(model);
        }
Ejemplo n.º 6
0
        public async Task<IActionResult> Create(int eventId, TaskSummaryModel 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 HttpUnauthorized();
                }
                
                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.º 7
0
        public IActionResult Create(int eventId)
        {
            var campaignEvent = GetEventBy(eventId);
            if (campaignEvent == null || !User.IsOrganizationAdmin(campaignEvent.Campaign.ManagingOrganizationId))
            {
                return HttpUnauthorized();
            }
            
            var viewModel = new TaskSummaryModel
            {
                EventId = campaignEvent.Id,
                EventName = campaignEvent.Name,                
                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.º 8
0
        public async Task DetailsReturnsCorrectViewModelAndView()
        {
            var taskSummaryModel = new TaskSummaryModel();

            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 TaskSummaryModel;

            Assert.IsType<ViewResult>(result);
            Assert.IsType<TaskSummaryModel>(modelResult);
            Assert.Equal(modelResult, taskSummaryModel);
        }
        public void ReturnsNoErrorForItineraryTaskWhenModelsDatesAreValid()
        {
            var validator = GetValidator();

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

            var errors = validator.Validate(model);

            Assert.True(errors.Count == 0);
        }
Ejemplo n.º 10
0
        public async Task AssignRedirectsToRoute()
        {
            const int organizationId = 1;
            const int taskId = 1;
            var taskModelSummary = new TaskSummaryModel { 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.º 11
0
        public async Task AssignSendsAssignTaskCommandAsync()
        {
            const int organizationId = 1;
            const int taskId = 1;
            var taskModelSummary = new TaskSummaryModel { 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.º 12
0
        public async Task AssignReturnsHttpUnauthorizedResultWhenUserIsNotOrgAdmin()
        {
            var taskModelSummary = new TaskSummaryModel { 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<HttpUnauthorizedResult>(result);
        }
Ejemplo n.º 13
0
        public async Task AssignSendsTaskQueryAsyncWithCorrectTaskId()
        {
            const int taskId = 1;
            var taskModelSummary = new TaskSummaryModel { 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.º 14
0
        public async Task DeleteReturnsCorrectViewModelAndView()
        {
            const int organizationId = 1;
            var taskSummaryModel = new TaskSummaryModel { 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 TaskSummaryModel;

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

            var model = new TaskSummaryModel { 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<TaskSummaryModel>())).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 async Task EditPostReturnsHttpUnauthorizedResultWhenModelStateIsValidAndUserIsNotAnOrganizationAdminUser()
        {
            var startDateTime = DateTimeOffset.Now.AddDays(-1);
            var endDateTime = DateTimeOffset.Now.AddDays(1);
            var model = new TaskSummaryModel { 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<TaskSummaryModel>())).Returns(new List<KeyValuePair<string, string>>());

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

            var result = await sut.Edit(model);

            Assert.IsType<HttpUnauthorizedResult>(result);
        }
Ejemplo n.º 18
0
        public async Task EditPostReturnsCorrectViewAndViewModelWhenValidatorReturnsError()
        {
            var model = new TaskSummaryModel { 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<TaskSummaryModel>())).Returns(new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>(nameof(TaskSummaryModel.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 TaskSummaryModel;

            Assert.IsType<ViewResult>(result);
            Assert.Equal(modelResult, model);
        }
        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 TaskSummaryModel
            {
                StartDateTime = eventStartDate.AddDays(1),
                EndDateTime = eventEndDate.AddDays(-1)
            };

            var errors = validator.Validate(model);

            Assert.True(errors.Count == 0);
        }
Ejemplo n.º 20
0
        public async Task DeleteConfirmedRedirectsToCorrectAction()
        {
            const int organizationId = 1;
            const int taskId = 1;
            var taskSummaryModel = new TaskSummaryModel { 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);
        }