Beispiel #1
0
        public async Task EditPostInvokesValidateOnTaskSummaryModelValidatorWithCorrectParameters()
        {
            var model = new EditViewModel {
                EndDateTime = DateTimeOffset.Now.AddDays(-1), StartDateTime = DateTimeOffset.Now.AddDays(1), EventId = 1
            };

            var validator = new Mock <IValidateVolunteerTaskEditViewModelValidator>();

            validator.Setup(x => x.Validate(model)).ReturnsAsync(new List <KeyValuePair <string, string> >()).Verifiable();

            var urlHelper = new Mock <IUrlHelper>();

            urlHelper.Setup(x => x.Action(It.IsAny <UrlActionContext>())).Returns(It.IsAny <string>());

            var sut = new VolunteerTaskController(null, validator.Object)
            {
                Url = urlHelper.Object
            };

            sut.AddModelStateError();

            await sut.Edit(model);

            validator.Verify();
        }
Beispiel #2
0
        public async Task EditPostAddsCorrectModelStateErrorWhenValidatorReturnsError()
        {
            const string errorKey   = "ErrorKey";
            const string errorValue = "ErrorValue";

            var validator = new Mock <IValidateVolunteerTaskEditViewModelValidator>();

            validator.Setup(x => x.Validate(It.IsAny <EditViewModel>()))
            .ReturnsAsync(new List <KeyValuePair <string, string> > {
                new KeyValuePair <string, string>(errorKey, errorValue)
            });

            var urlHelper = new Mock <IUrlHelper>();

            urlHelper.Setup(x => x.Action(It.IsAny <UrlActionContext>())).Returns(It.IsAny <string>());

            var sut = new VolunteerTaskController(null, validator.Object)
            {
                Url = urlHelper.Object
            };
            await sut.Edit(new EditViewModel()
            {
                EventId = 1, Id = 1
            });

            var modelStateErrorCollection = sut.ModelState.GetErrorMessagesByKey(errorKey);

            Assert.Equal(modelStateErrorCollection.Single().ErrorMessage, errorValue);
        }
Beispiel #3
0
        public async Task EditGetReturnsCorrectViewModelAndView()
        {
            var editViewModel = new EditViewModel {
            };

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <EditVolunteerTaskQuery>())).ReturnsAsync(editViewModel);
            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableTaskQuery>())).ReturnsAsync(new FakeAuthorizableTask(true, false, false, false));

            var urlHelper = new Mock <IUrlHelper>();

            urlHelper.Setup(x => x.Action(It.IsAny <UrlActionContext>())).Returns(It.IsAny <string>());

            var sut = new VolunteerTaskController(mediator.Object, null)
            {
                Url = urlHelper.Object
            };

            var result = await sut.Edit(It.IsAny <int>()) as ViewResult;

            var modelResult = result.ViewData.Model as EditViewModel;

            Assert.IsType <ViewResult>(result);
            Assert.IsType <EditViewModel>(modelResult);
            Assert.Equal(modelResult, editViewModel);
        }
Beispiel #4
0
        public async Task CreateGetReturnsCorrectViewModel()
        {
            var model = new EditViewModel {
            };

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <CreateVolunteerTaskQuery>())).ReturnsAsync(model);
            mediator.Setup(x => x.SendAsync(It.IsAny <AllReady.Areas.Admin.Features.Events.AuthorizableEventQuery>()))
            .ReturnsAsync(new FakeAuthorizableEvent(false, false, false, true));

            var urlHelper = new Mock <IUrlHelper>();

            urlHelper.Setup(x => x.Action(It.IsAny <UrlActionContext>())).Returns(It.IsAny <string>());

            var sut = new VolunteerTaskController(mediator.Object, null)
            {
                Url = urlHelper.Object
            };

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

            var modelResult = result.ViewData.Model as EditViewModel;

            Assert.IsType <EditViewModel>(modelResult);
        }
Beispiel #5
0
        public async Task CreateGetSetCorrectToTitleOnViewBag()
        {
            var model = new EditViewModel {
                EventId = 1
            };

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <CreateVolunteerTaskQuery>())).ReturnsAsync(model);
            mediator.Setup(x => x.SendAsync(It.IsAny <AllReady.Areas.Admin.Features.Events.AuthorizableEventQuery>()))
            .ReturnsAsync(new FakeAuthorizableEvent(false, false, false, true));

            var urlHelper = new Mock <IUrlHelper>();

            urlHelper.Setup(x => x.Action(It.IsAny <UrlActionContext>())).Returns(It.IsAny <string>());

            var sut = new VolunteerTaskController(mediator.Object, null)
            {
                Url = urlHelper.Object
            };

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

            Assert.Equal(sut.ViewBag.Title, "Create Task");
        }
Beispiel #6
0
        public async Task EditPostReturnsCorrectViewAndViewModelWhenValidatorReturnsError()
        {
            var model = new EditViewModel();

            var validator = new Mock <IValidateVolunteerTaskEditViewModelValidator>();

            validator.Setup(x => x.Validate(It.IsAny <EditViewModel>()))
            .ReturnsAsync(new List <KeyValuePair <string, string> > {
                new KeyValuePair <string, string>("key", "value")
            });
            var urlHelper = new Mock <IUrlHelper>();

            urlHelper.Setup(x => x.Action(It.IsAny <UrlActionContext>())).Returns(It.IsAny <string>());

            var sut = new VolunteerTaskController(Mock.Of <IMediator>(), validator.Object)
            {
                Url = urlHelper.Object
            };
            var result = await sut.Edit(model) as ViewResult;

            var modelResult = result.ViewData.Model as EditViewModel;

            Assert.IsType <ViewResult>(result);
            Assert.Equal(modelResult, model);
        }
Beispiel #7
0
        public async Task CreateGetAssignsCancelUrlToModel()
        {
            const string cancelUrl = "url";
            var          model     = new EditViewModel {
                EventId = 1, OrganizationId = 1
            };

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <CreateVolunteerTaskQuery>())).ReturnsAsync(model);
            mediator.Setup(x => x.SendAsync(It.IsAny <AllReady.Areas.Admin.Features.Events.AuthorizableEventQuery>()))
            .ReturnsAsync(new FakeAuthorizableEvent(false, false, false, true));

            var urlHelper = new Mock <IUrlHelper>();

            urlHelper.Setup(x => x.Action(It.IsAny <UrlActionContext>())).Returns(cancelUrl);

            var sut = new VolunteerTaskController(mediator.Object, null)
            {
                Url = urlHelper.Object
            };

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

            var modelResult = result.Model as EditViewModel;

            Assert.Equal(modelResult.CancelUrl, cancelUrl);
        }
Beispiel #8
0
        public async Task DetailsReturnsHttpNotFoundResultWhenTaskIsNull()
        {
            var sut    = new VolunteerTaskController(Mock.Of <IMediator>(), null);
            var result = await sut.Details(It.IsAny <int>());

            Assert.IsType <NotFoundResult>(result);
        }
Beispiel #9
0
        public void DetailsHasHttpGetAttribute()
        {
            var sut       = new VolunteerTaskController(null, null);
            var attribute = sut.GetAttributesOn(x => x.Details(It.IsAny <int>())).OfType <HttpGetAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
        }
Beispiel #10
0
        public async Task CreateGetInvokesUrlActionWithTheCorrectParameters()
        {
            var model = new EditViewModel {
                EventId = 1, OrganizationId = 1
            };

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <CreateVolunteerTaskQuery>())).ReturnsAsync(model);
            mediator.Setup(x => x.SendAsync(It.IsAny <AllReady.Areas.Admin.Features.Events.AuthorizableEventQuery>()))
            .ReturnsAsync(new FakeAuthorizableEvent(false, false, false, true));

            var urlHelper = new Mock <IUrlHelper>();

            urlHelper.Setup(x => x.Action(It.IsAny <UrlActionContext>())).Returns(It.IsAny <string>());

            var sut = new VolunteerTaskController(mediator.Object, null)
            {
                Url = urlHelper.Object
            };

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

            urlHelper.Verify(x => x.Action(It.Is <UrlActionContext>(y =>
                                                                    y.Action == nameof(EventController.Details) &&
                                                                    y.Controller == "Event" &&
                                                                    y.Values.ToString() == $"{{ id = {model.EventId}, area = Admin }}")),
                             Times.Once);
        }
Beispiel #11
0
        public void AssignHasValidateAntiForgeryTokenAttribute()
        {
            var sut       = new VolunteerTaskController(null, null);
            var attribute = sut.GetAttributesOn(x => x.Assign(It.IsAny <int>(), It.IsAny <List <string> >())).OfType <ValidateAntiForgeryTokenAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
        }
Beispiel #12
0
        public void MessageAllVolunteersHasValidateAntiForgeryTokenAttribute()
        {
            var sut       = new VolunteerTaskController(null, null);
            var attribute = sut.GetAttributesOn(x => x.MessageAllVolunteers(It.IsAny <MessageTaskVolunteersViewModel>())).OfType <ValidateAntiForgeryTokenAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
        }
Beispiel #13
0
        public void EditPostHasHttpPostAttribute()
        {
            var sut       = new VolunteerTaskController(null, null);
            var attribute = sut.GetAttributesOn(x => x.Edit(It.IsAny <EditViewModel>())).OfType <HttpPostAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
        }
Beispiel #14
0
        public async Task EditPostRedirectsToCorrectAction_WhenUpdatingTask()
        {
            var model = new EditViewModel {
                Id = 1
            };

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <EditVolunteerTaskCommand>())).ReturnsAsync(1);
            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableTaskQuery>())).ReturnsAsync(new FakeAuthorizableTask(false, true, false, false));

            var validator = new Mock <IValidateVolunteerTaskEditViewModelValidator>();

            validator.Setup(x => x.Validate(It.IsAny <EditViewModel>())).ReturnsAsync(new List <KeyValuePair <string, string> >());

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

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

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

            Assert.Equal("VolunteerTask", result.ControllerName);
            Assert.Equal(nameof(VolunteerTaskController.Details), result.ActionName);
            Assert.Equal(result.RouteValues, routeValues);
        }
Beispiel #15
0
        public void DeleteConfirmedHasValidateAntiForgeryTokenAttribute()
        {
            var sut       = new VolunteerTaskController(null, null);
            var attribute = sut.GetAttributesOn(x => x.DeleteConfirmed(It.IsAny <DeleteViewModel>())).OfType <ValidateAntiForgeryTokenAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
        }
        public void CreateGetHasRouteAttributeWithCorrectTemplate()
        {
            var sut       = new VolunteerTaskController(null, null);
            var attribute = sut.GetAttributesOn(x => x.Create(It.IsAny <int>())).OfType <RouteAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
            Assert.Equal(attribute.Template, "Admin/VolunteerTask/Create/{eventId}");
        }
Beispiel #17
0
        public void DetailsHasRouteAttributeWithCorrectTemplate()
        {
            var sut       = new VolunteerTaskController(null, null);
            var attribute = sut.GetAttributesOn(x => x.Details(It.IsAny <int>())).OfType <RouteAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
            Assert.Equal("Admin/VolunteerTask/Details/{id}", attribute.Template);
        }
Beispiel #18
0
        public void ControllerHasAuthorizeAtttributeWithTheCorrectPolicy()
        {
            var sut       = new VolunteerTaskController(null, null);
            var attribute = sut.GetAttributes().OfType <AuthorizeAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
            Assert.Null(attribute.Policy);
        }
Beispiel #19
0
        public void DeleteConfirmedHasActionNameAttributeWithCorrectActionName()
        {
            var sut       = new VolunteerTaskController(null, null);
            var attribute = sut.GetAttributesOn(x => x.DeleteConfirmed(It.IsAny <DeleteViewModel>())).OfType <ActionNameAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
            Assert.Equal("Delete", attribute.Name);
        }
Beispiel #20
0
        public async Task MessageAllVolunteersReturnsBadRequestObjectResultWhenModelStateIsInvalid()
        {
            var sut = new VolunteerTaskController(null, null);

            sut.AddModelStateError();
            var result = await sut.MessageAllVolunteers(It.IsAny <MessageTaskVolunteersViewModel>());

            Assert.IsType <BadRequestObjectResult>(result);
        }
Beispiel #21
0
        public async Task DetailsSendsDetailsQueryWithCorrectTaskId()
        {
            const int volunteerTaskId = 1;
            var       mediator        = new Mock <IMediator>();
            var       sut             = new VolunteerTaskController(mediator.Object, null);
            await sut.Details(volunteerTaskId);

            mediator.Verify(x => x.SendAsync(It.Is <DetailsQuery>(y => y.VolunteerTaskId == volunteerTaskId)), Times.Once);
        }
Beispiel #22
0
        public void ControllerHasAreaAtttributeWithTheCorrectAreaName()
        {
            var sut = new VolunteerTaskController(null, null);

            sut.SetDefaultHttpContext();
            var attribute = sut.GetAttributes().OfType <AreaAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
            Assert.Equal(AreaNames.Admin, attribute.RouteValue);
        }
Beispiel #23
0
        public async Task DeleteConfirmedSendsDeleteTaskCommandWithCorrectTaskId()
        {
            const int volunteerTaskId = 1;
            var       mediator        = new Mock <IMediator>();

            var sut = new VolunteerTaskController(mediator.Object, null);

            await sut.DeleteConfirmed(new DeleteViewModel { Id = volunteerTaskId, UserIsOrgAdmin = true });

            mediator.Verify(x => x.SendAsync(It.Is <DeleteVolunteerTaskCommand>(y => y.VolunteerTaskId == volunteerTaskId)), Times.Once);
        }
Beispiel #24
0
        public async Task MessageAllVolunteersReturnsHttpOkResult()
        {
            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <OrganizationIdByVolunteerTaskIdQuery>())).ReturnsAsync(It.IsAny <int>());
            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableTaskQuery>())).ReturnsAsync(new FakeAuthorizableTask(false, true, false, false));

            var sut    = new VolunteerTaskController(mediator.Object, null);
            var result = await sut.MessageAllVolunteers(new MessageTaskVolunteersViewModel());

            Assert.IsType <OkResult>(result);
        }
Beispiel #25
0
        public async Task MessageAllVolunteersSendsMessageTaskVolunteersCommandWithCorrectData()
        {
            var model = new MessageTaskVolunteersViewModel();

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <OrganizationIdByVolunteerTaskIdQuery>())).ReturnsAsync(It.IsAny <int>());
            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableTaskQuery>())).ReturnsAsync(new FakeAuthorizableTask(false, true, false, false));

            var sut = new VolunteerTaskController(mediator.Object, null);
            await sut.MessageAllVolunteers(model);

            mediator.Verify(x => x.SendAsync(It.Is <MessageTaskVolunteersCommand>(y => y.Model == model)), Times.Once);
        }
Beispiel #26
0
        public async Task AssignReturnsForbidResultWhenUserIsNotAuthorized()
        {
            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <OrganizationIdByVolunteerTaskIdQuery>())).ReturnsAsync(It.IsAny <int>());
            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableTaskQuery>())).ReturnsAsync(new FakeAuthorizableTask(false, false, false, false));

            var sut = new VolunteerTaskController(mediator.Object, null);

            sut.SetDefaultHttpContext();
            var result = await sut.Assign(1, null);

            Assert.IsType <ForbidResult>(result);
        }
Beispiel #27
0
        public async Task DeleteConfirmedReturnsForbidResultWhenUserIsNotAuthorized()
        {
            var model = new DeleteViewModel {
                UserIsOrgAdmin = false
            };

            var sut = new VolunteerTaskController(null, null);

            sut.SetDefaultHttpContext();

            var result = await sut.DeleteConfirmed(model);

            Assert.IsType <ForbidResult>(result);
        }
Beispiel #28
0
        public async Task DeleteSendsDeleteQueryWithCorrectTaskId()
        {
            const int volunteerTaskId = 1;

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <DeleteQuery>())).ReturnsAsync(new DeleteViewModel {
            });
            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableTaskQuery>())).ReturnsAsync(new FakeAuthorizableTask(false, true, false, false));

            var sut = new VolunteerTaskController(mediator.Object, null);
            await sut.Delete(volunteerTaskId);

            mediator.Verify(x => x.SendAsync(It.Is <DeleteQuery>(y => y.VolunteerTaskId == volunteerTaskId)), Times.Once);
        }
Beispiel #29
0
        public async Task CreateGetReturnsForbidResultWhenUserIsNotAuthorized()
        {
            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <CreateVolunteerTaskQuery>())).ReturnsAsync(new EditViewModel());
            mediator.Setup(x => x.SendAsync(It.IsAny <AllReady.Areas.Admin.Features.Events.AuthorizableEventQuery>()))
            .ReturnsAsync(new FakeAuthorizableEvent(false, false, false, false));

            var sut = new VolunteerTaskController(mediator.Object, null);

            sut.SetDefaultHttpContext();

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

            Assert.IsType <ForbidResult>(result);
        }
Beispiel #30
0
        public async Task AssignSendsOrganizationIdByTaskIdQueryWithCorrectTaskId()
        {
            const int volunteerTaskId = 1;

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <OrganizationIdByVolunteerTaskIdQuery>())).ReturnsAsync(It.IsAny <int>());
            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableTaskQuery>())).ReturnsAsync(new FakeAuthorizableTask(false, true, false, false));

            var sut = new VolunteerTaskController(mediator.Object, null);

            sut.SetDefaultHttpContext();
            await sut.Assign(volunteerTaskId, null);

            mediator.Verify(x => x.SendAsync(It.Is <OrganizationIdByVolunteerTaskIdQuery>(y => y.VolunteerTaskId == volunteerTaskId)), Times.Once);
        }