public async Task EditPostInvokesDeleteImageAsync_WhenCampaignHasAnImage()
        {
            const int organizationId = 1;
            const int campaignId     = 100;

            var mockMediator     = new Mock <IMediator>();
            var mockImageService = new Mock <IImageService>();

            var file = FormFile("image/jpeg");

            mockImageService.Setup(x => x.UploadCampaignImageAsync(It.IsAny <int>(), It.IsAny <int>(), file)).ReturnsAsync("newImageUrl");

            var sut = new CampaignController(mockMediator.Object, mockImageService.Object);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var campaignSummaryViewModel = new CampaignSummaryViewModel
            {
                OrganizationId = organizationId,
                ImageUrl       = "existingImageUrl",
                Id             = campaignId,
                StartDate      = new DateTimeOffset(new DateTime(2016, 2, 13)),
                EndDate        = new DateTimeOffset(new DateTime(2016, 2, 14)),
            };

            await sut.Edit(campaignSummaryViewModel, file);

            mockImageService.Verify(mock => mock.DeleteImageAsync(It.Is <string>(x => x == "existingImageUrl")), Times.Once);
        }
예제 #2
0
        public async Task EditPostInsertsCampaign()
        {
            const int organizationId = 99;
            const int newCampaignId  = 100;

            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(x => x.SendAsync(It.IsAny <EditCampaignCommand>())).ReturnsAsync(newCampaignId);

            var mockImageService = new Mock <IImageService>();
            var sut = new CampaignController(mockMediator.Object, mockImageService.Object);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var model = MassiveTrafficLightOutageModel;

            model.OrganizationId = organizationId;

            // verify the model is valid
            var validationContext = new ValidationContext(model, null, null);
            var validationResults = new List <ValidationResult>();

            Validator.TryValidateObject(model, validationContext, validationResults);
            Assert.Empty(validationResults);

            var file = FormFile("image/jpeg");
            var view = (RedirectToActionResult)await sut.Edit(model, file);

            // verify the edit(add) is called
            mockMediator.Verify(mock => mock.SendAsync(It.Is <EditCampaignCommand>(c => c.Campaign.OrganizationId == organizationId)));

            // verify that the next route
            Assert.Equal("Admin", view.RouteValues["area"]);
            Assert.Equal(view.RouteValues["id"], newCampaignId);
        }
예제 #3
0
        public async Task EditPostDoesNotInvokeDeleteImageAsync__WhenCampaignDoesNotHaveAnImage()
        {
            const int organizationId = 1;
            const int campaignId     = 100;

            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(false, true, false, false));
            var mockImageService = new Mock <IImageService>();

            var file = FormFile("image/jpeg");

            mockImageService.Setup(x => x.UploadCampaignImageAsync(It.IsAny <int>(), It.IsAny <int>(), file)).ReturnsAsync("newImageUrl");

            var sut = new CampaignController(mockMediator.Object, mockImageService.Object);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var campaignSummaryViewModel = new CampaignSummaryViewModel
            {
                OrganizationId = organizationId,
                Id             = campaignId,
                StartDate      = new DateTimeOffset(new DateTime(2016, 2, 13)),
                EndDate        = new DateTimeOffset(new DateTime(2016, 2, 14)),
            };

            await sut.Edit(campaignSummaryViewModel, file);

            mockImageService.Verify(mock => mock.DeleteImageAsync(It.IsAny <string>()), Times.Never);
        }
예제 #4
0
        public async Task DeleteCampaignImageReturnsJsonObjectWithStatusOfDateInvalidIfCampaignEndDateIsLessThanStartDate()
        {
            var mediatorMock = new Mock <IMediator>();

            var campaignSummaryViewModel = new CampaignSummaryViewModel
            {
                OrganizationId = 1,
                StartDate      = DateTimeOffset.Now.AddDays(10),
                EndDate        = DateTimeOffset.Now
            };

            mediatorMock.Setup(m => m.SendAsync(It.IsAny <CampaignSummaryQuery>())).ReturnsAsync(campaignSummaryViewModel);
            mediatorMock.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(false, true, false, false));

            var imageServiceMock = new Mock <IImageService>();

            var sut = new CampaignController(mediatorMock.Object, imageServiceMock.Object);

            sut.MakeUserAnOrgAdmin(campaignSummaryViewModel.OrganizationId.ToString());

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

            result.Value.GetType()
            .GetProperty("status")
            .GetValue(result.Value)
            .ShouldBe("DateInvalid");

            result.Value.GetType()
            .GetProperty("message")
            .GetValue(result.Value)
            .ShouldBe("The end date must fall on or after the start date.");
        }
예제 #5
0
        private static void IndexReturnsCorrectDataWhenUserIsOrgAdmin()
        {
            const int organizationId = 99;
            var       mockMediator   = new Mock <IMediator>();

            mockMediator.Setup(x => x.Send(It.Is <CampaignListQuery>(c => c.OrganizationId == organizationId)))
            .Returns((CampaignListQuery q) =>
            {
                var ret = new List <CampaignSummaryViewModel>
                {
                    new CampaignSummaryViewModel {
                        OrganizationId = organizationId
                    }
                };
                return(ret);
            }
                     );

            var controller = new CampaignController(mockMediator.Object, null);

            controller.MakeUserAnOrgAdmin(organizationId.ToString());

            var view = (ViewResult)controller.Index();

            mockMediator.Verify(mock => mock.Send(It.Is <CampaignListQuery>(c => c.OrganizationId == organizationId)));

            // Org admin should only see own campaigns
            var viewModel = (IEnumerable <CampaignSummaryViewModel>)view.ViewData.Model;

            Assert.NotNull(viewModel);
            Assert.Equal(viewModel.Count(), 1);
            Assert.Equal(viewModel.First().OrganizationId, organizationId);
        }
예제 #6
0
        public async Task DeleteCampaignImageReturnsJsonObjectWithStatusOfSuccessIfImageDeletedSuccessfully()
        {
            var campaignSummaryViewModel = new CampaignSummaryViewModel
            {
                OrganizationId = 1,
                ImageUrl       = "URL!"
            };

            var mediatorMock = new Mock <IMediator>();

            mediatorMock.Setup(m => m.SendAsync(It.IsAny <CampaignSummaryQuery>())).ReturnsAsync(campaignSummaryViewModel);
            mediatorMock.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(false, true, false, false));

            var imageServiceMock = new Mock <IImageService>();

            var sut = new CampaignController(mediatorMock.Object, imageServiceMock.Object);

            sut.MakeUserAnOrgAdmin(campaignSummaryViewModel.OrganizationId.ToString());

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

            result.Value.GetType()
            .GetProperty("status")
            .GetValue(result.Value)
            .ShouldBe("Success");
        }
예제 #7
0
        public async Task IndexSendsIndexQueryWithCorrectData_WhenUserIsOrgAdmin()
        {
            const int organizationId = 99;
            var       mockMediator   = new Mock <IMediator>();

            var sut = new CampaignController(mockMediator.Object, null);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());
            await sut.Index();

            mockMediator.Verify(mock => mock.SendAsync(It.Is <IndexQuery>(q => q.OrganizationId == organizationId)));
        }
예제 #8
0
        public void IndexSendsCampaignListQueryWithCorrectDataWhenUserIsOrgAdmin()
        {
            const int organizationId = 99;
            var       mockMediator   = new Mock <IMediator>();

            var controller = new CampaignController(mockMediator.Object, null);

            controller.MakeUserAnOrgAdmin(organizationId.ToString());
            controller.Index();

            mockMediator.Verify(mock => mock.Send(It.Is <CampaignListQuery>(q => q.OrganizationId == organizationId)));
        }
예제 #9
0
        public async Task DeleteReturnsCorrectViewWhenUserIsOrgAdmin()
        {
            const int organizationId = 1;

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <DeleteViewModelQuery>())).ReturnsAsync(new DeleteViewModel {
                OrganizationId = organizationId
            });

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

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            Assert.IsType <ViewResult>(await sut.Delete(It.IsAny <int>()));
        }
예제 #10
0
        public async Task EditPostAddsCorrectKeyAndErrorMessageToModelStateWhenCampaignEndDateIsLessThanCampainStartDate_WhenCampaignIdIsZero()
        {
            var campaignSummaryModel = new CampaignSummaryViewModel {
                OrganizationId = 1, StartDate = DateTime.Now.AddDays(1), EndDate = DateTime.Now.AddDays(-1)
            };

            var sut = new CampaignController(null, null);

            sut.MakeUserAnOrgAdmin(campaignSummaryModel.OrganizationId.ToString());

            await sut.Edit(campaignSummaryModel, null);

            var modelStateErrorCollection = sut.ModelState.GetErrorMessagesByKey(nameof(CampaignSummaryViewModel.EndDate));

            Assert.Equal("The end date must fall on or after the start date.", modelStateErrorCollection.Single().ErrorMessage);
        }
        public async Task DeleteConfirmedSendsDeleteCampaignCommandWithCorrectCampaignId()
        {
            var viewModel = new DeleteViewModel {
                Id = 1, OrganizationId = 1
            };

            var mediator = new Mock <IMediator>();

            mediator.Setup(a => a.SendAsync(It.IsAny <DeleteCampaignCommand>())).Verifiable();
            var sut = new CampaignController(mediator.Object, null);

            sut.MakeUserAnOrgAdmin(viewModel.OrganizationId.ToString());
            await sut.DeleteConfirmed(viewModel);

            mediator.Verify(mock => mock.SendAsync(It.Is <DeleteCampaignCommand>(i => i.CampaignId == viewModel.Id)), Times.Once);
        }
        public async Task EditPostRedirectsToCorrectActionWithCorrectRouteValuesWhenModelStateIsValid()
        {
            const int organizationId = 99;
            const int campaignId     = 100;

            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(x => x.SendAsync(It.IsAny <EditCampaignCommand>())).ReturnsAsync(campaignId);
            var sut = new CampaignController(mockMediator.Object, new Mock <IImageService>().Object);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var result = (RedirectToActionResult)await sut.Edit(new CampaignSummaryViewModel { Name = "Foo", OrganizationId = organizationId, Id = campaignId }, null);

            Assert.Equal(result.ActionName, "Details");
            Assert.Equal(result.RouteValues["area"], "Admin");
            Assert.Equal(result.RouteValues["id"], campaignId);
        }
예제 #13
0
        public async Task DetailConfirmedSendsDeleteCampaignCommandWithCorrectCampaignIdWhenUserIsOrgAdmin()
        {
            const int organizationId = 1;

            var mockMediator = new Mock <IMediator>();

            var viewModel = new DeleteViewModel {
                Id = 100, UserIsOrgAdmin = true
            };

            var sut = new CampaignController(mockMediator.Object, null);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            await sut.DeleteConfirmed(viewModel);

            mockMediator.Verify(mock => mock.SendAsync(It.Is <DeleteCampaignCommand>(i => i.CampaignId == viewModel.Id)), Times.Once);
        }
예제 #14
0
        public async Task DeleteSendsDeleteQueryWithCorrectCampaignId()
        {
            const int organizationId = 99;
            const int campaignId     = 100;

            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(mock => mock.SendAsync(It.Is <DeleteViewModelQuery>(c => c.CampaignId == campaignId))).ReturnsAsync(new DeleteViewModel {
                Id = campaignId, OrganizationId = organizationId
            });

            var sut = new CampaignController(mockMediator.Object, null);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());
            await sut.Delete(campaignId);

            mockMediator.Verify(mock => mock.SendAsync(It.Is <DeleteViewModelQuery>(c => c.CampaignId == campaignId)), Times.Once);
        }
예제 #15
0
        public async Task DetailConfirmedSendsDeleteCampaignCommandWithCorrectCampaignIdWhenUserIsOrgAdmin()
        {
            const int organizationId = 1;
            const int campaignId     = 100;

            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(mock => mock.SendAsync(It.IsAny <CampaignSummaryQuery>())).ReturnsAsync(new CampaignSummaryViewModel {
                OrganizationId = organizationId
            });

            var sut = new CampaignController(mockMediator.Object, null);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            await sut.DeleteConfirmed(campaignId);

            mockMediator.Verify(mock => mock.SendAsync(It.Is <DeleteCampaignCommand>(i => i.CampaignId == campaignId)), Times.Once);
        }
예제 #16
0
        public async Task PublishSendsPublishQueryWithCorrectCampaignId()
        {
            const int organizationId = 99;
            const int campaignId     = 100;

            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(mock => mock.SendAsync(It.Is <PublishViewModelQuery>(c => c.CampaignId == campaignId))).ReturnsAsync(new PublishViewModel {
                Id = campaignId, OrganizationId = organizationId
            });
            mockMediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(true, false, false, false));

            var sut = new CampaignController(mockMediator.Object, null);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());
            await sut.Publish(campaignId);

            mockMediator.Verify(mock => mock.SendAsync(It.Is <PublishViewModelQuery>(c => c.CampaignId == campaignId)), Times.Once);
        }
예제 #17
0
        public async Task EditPostReturnsCorrectViewModelWhenInvalidImageFormatIsSupplied()
        {
            const int organizationId   = 100;
            var       mockMediator     = new Mock <IMediator>();
            var       mockImageService = new Mock <IImageService>();

            var sut = new CampaignController(mockMediator.Object, mockImageService.Object);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var file  = FormFile("audio/mpeg3");
            var model = MassiveTrafficLightOutage_model;

            model.OrganizationId = organizationId;

            var view      = (ViewResult)(await sut.Edit(model, file));
            var viewModel = (CampaignSummaryViewModel)view.ViewData.Model;

            Assert.True(Object.ReferenceEquals(model, viewModel));
        }
예제 #18
0
        public async Task EditPostUploadsImageToImageService()
        {
            const int organizationId = 1;
            const int campaignId     = 100;

            var mockMediator     = new Mock <IMediator>();
            var mockImageService = new Mock <IImageService>();

            var sut = new CampaignController(mockMediator.Object, mockImageService.Object);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var file = FormFile("image/jpeg");

            await sut.Edit(new CampaignSummaryViewModel { Name = "Foo", OrganizationId = organizationId, Id = campaignId }, file);

            mockImageService.Verify(mock => mock.UploadCampaignImageAsync(
                                        It.Is <int>(i => i == organizationId),
                                        It.Is <int>(i => i == campaignId),
                                        It.Is <IFormFile>(i => i == file)), Times.Once);
        }
예제 #19
0
        public async Task DetailConfirmedRedirectsToCorrectActionWithCorrectRouteValuesWhenUserIsOrgAdmin()
        {
            const int organizationId = 1;

            var viewModel = new DeleteViewModel {
                Id = 100, UserIsOrgAdmin = true
            };

            var sut = new CampaignController(Mock.Of <IMediator>(), null);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var routeValues = new Dictionary <string, object> {
                ["area"] = "Admin"
            };

            var result = await sut.DeleteConfirmed(viewModel) as RedirectToActionResult;

            Assert.Equal(result.ActionName, nameof(CampaignController.Index));
            Assert.Equal(result.RouteValues, routeValues);
        }
예제 #20
0
        public async Task DeleteReturnsCorrectViewModelWhenUserIsOrgAdmin()
        {
            const int organizationId = 99;
            const int campaignId     = 100;

            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(mock => mock.SendAsync(It.Is <DeleteViewModelQuery>(c => c.CampaignId == campaignId))).ReturnsAsync(new DeleteViewModel {
                Id = campaignId, OrganizationId = organizationId
            });

            var sut = new CampaignController(mockMediator.Object, null);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var view = (ViewResult)await sut.Delete(campaignId);

            var viewModel = (DeleteViewModel)view.ViewData.Model;

            Assert.Equal(viewModel.Id, campaignId);
        }
예제 #21
0
        public async Task EditPostInvokesUploadCampaignImageAsyncWithTheCorrectParameters_WhenFileUploadIsNotNull_AndFileIsAcceptableContentType()
        {
            const int organizationId = 1;
            const int campaignId     = 100;

            var mockMediator     = new Mock <IMediator>();
            var mockImageService = new Mock <IImageService>();

            var sut = new CampaignController(mockMediator.Object, mockImageService.Object);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var file = FormFile("image/jpeg");

            await sut.Edit(new CampaignSummaryViewModel { Name = "Foo", OrganizationId = organizationId, Id = campaignId }, file);

            mockImageService.Verify(mock => mock.UploadCampaignImageAsync(
                                        It.Is <int>(i => i == organizationId),
                                        It.Is <int>(i => i == campaignId),
                                        It.Is <IFormFile>(i => i == file)), Times.Once);
        }
예제 #22
0
        public async Task DeleteCampaignImageCallsEditCampaignCommandWithCorrectData()
        {
            var mediatorMock = new Mock <IMediator>();

            var campaignSummaryViewModel = new CampaignSummaryViewModel
            {
                OrganizationId = 1,
                ImageUrl       = "URL!"
            };

            mediatorMock.Setup(m => m.SendAsync(It.IsAny <CampaignSummaryQuery>())).ReturnsAsync(campaignSummaryViewModel);

            var imageServiceMock = new Mock <IImageService>();

            var sut = new CampaignController(mediatorMock.Object, imageServiceMock.Object);

            sut.MakeUserAnOrgAdmin(campaignSummaryViewModel.OrganizationId.ToString());

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

            mediatorMock.Verify(m => m.SendAsync(It.Is <EditCampaignCommand>(s => s.Campaign == campaignSummaryViewModel)), Times.Once);
        }
예제 #23
0
        public async Task DetailsReturnsCorrectViewModelWhenViewModelIsNotNullAndUserIsOrgAdmin()
        {
            const int campaignId     = 100;
            const int organizationId = 99;
            var       mockMediator   = new Mock <IMediator>();

            // model is not null
            mockMediator.Setup(mock => mock.SendAsync(It.Is <CampaignDetailQuery>(c => c.CampaignId == campaignId))).ReturnsAsync(new CampaignDetailViewModel {
                OrganizationId = organizationId, Id = campaignId
            }).Verifiable();

            // user is org admin
            var sut = new CampaignController(mockMediator.Object, null);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var view      = (ViewResult)(await sut.Details(campaignId));
            var viewModel = (CampaignDetailViewModel)view.ViewData.Model;

            Assert.Equal(viewModel.Id, campaignId);
            Assert.Equal(viewModel.OrganizationId, organizationId);
        }
예제 #24
0
        public async Task DeleteCampaignImageCallsDeleteImageAsyncWithCorrectData()
        {
            var campaignSummaryViewModel = new CampaignSummaryViewModel
            {
                OrganizationId = 1,
                ImageUrl       = "URL!"
            };

            var mediatorMock = new Mock <IMediator>();

            mediatorMock.Setup(m => m.SendAsync(It.IsAny <CampaignSummaryQuery>())).ReturnsAsync(campaignSummaryViewModel);
            mediatorMock.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(false, true, false, false));

            var imageServiceMock = new Mock <IImageService>();

            var sut = new CampaignController(mediatorMock.Object, imageServiceMock.Object);

            sut.MakeUserAnOrgAdmin(campaignSummaryViewModel.OrganizationId.ToString());

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

            imageServiceMock.Verify(i => i.DeleteImageAsync(It.Is <string>(f => f == "URL!")), Times.Once);
        }
예제 #25
0
        public async Task DetailConfirmedRedirectsToCorrectActionWithCorrectRouteValuesWhenUserIsOrgAdmin()
        {
            const int organizationId = 1;
            const int campaignId     = 100;

            var mockMediator = new Mock <IMediator>();

            mockMediator.Setup(mock => mock.SendAsync(It.IsAny <CampaignSummaryQuery>())).ReturnsAsync(new CampaignSummaryViewModel {
                OrganizationId = organizationId
            });

            var sut = new CampaignController(mockMediator.Object, null);

            sut.MakeUserAnOrgAdmin(organizationId.ToString());

            var routeValues = new Dictionary <string, object> {
                ["area"] = "Admin"
            };

            var result = await sut.DeleteConfirmed(campaignId) as RedirectToActionResult;

            Assert.Equal(result.ActionName, nameof(CampaignController.Index));
            Assert.Equal(result.RouteValues, routeValues);
        }
예제 #26
0
        public async Task DeleteCampaignImageReturnsJsonObjectWithStatusOfNothingToDeleteIfThereWasNoExistingImage()
        {
            var mediatorMock = new Mock <IMediator>();

            var campaignSummaryViewModel = new CampaignSummaryViewModel
            {
                OrganizationId = 1
            };

            mediatorMock.Setup(m => m.SendAsync(It.IsAny <CampaignSummaryQuery>())).ReturnsAsync(campaignSummaryViewModel);

            var imageServiceMock = new Mock <IImageService>();

            var sut = new CampaignController(mediatorMock.Object, imageServiceMock.Object);

            sut.MakeUserAnOrgAdmin(campaignSummaryViewModel.OrganizationId.ToString());

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

            result.Value.GetType()
            .GetProperty("status")
            .GetValue(result.Value)
            .ShouldBe("NothingToDelete");
        }