Beispiel #1
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);
        }
Beispiel #2
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);
        }
        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);
        }
        public async Task EditPostUploadsImageToImageService()
        {
            const int organizationId = 1;
            const int campaignId     = 100;

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

            mockImageService.Setup(mock => mock.UploadCampaignImageAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <IFormFile>())).Returns(() => Task.FromResult("")).Verifiable();

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

            sut.SetClaims(new List <Claim>
            {
                new Claim(AllReady.Security.ClaimTypes.UserType, UserType.OrgAdmin.ToString()),
                new Claim(AllReady.Security.ClaimTypes.Organization, organizationId.ToString())
            });

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

            await sut.Edit(new CampaignSummaryModel { 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);
        }
Beispiel #5
0
        public async Task EditPostReturnsBadRequestWhenCampaignIsNull()
        {
            var sut = new CampaignController(null, null);

            var result = await sut.Edit(null, null);

            Assert.IsType <BadRequestResult>(result);
        }
Beispiel #6
0
        public async Task EditGetSendsCampaignSummaryQueryWithCorrectCampaignId()
        {
            const int campaignId   = 100;
            var       mockMediator = new Mock <IMediator>();

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

            mockMediator.Verify(mock => mock.SendAsync(It.Is <CampaignSummaryQuery>(c => c.CampaignId == campaignId)));
        }
        public async Task EditGetSendsCampaignSummaryQueryWithCorrectCampaignId()
        {
            int CAMPAIGN_ID = 100;
            var mockMediator = new Mock<IMediator>();

            // model is not null
            mockMediator.Setup(mock => mock.SendAsync(It.Is<CampaignSummaryQuery>(c => c.CampaignId == CAMPAIGN_ID))).ReturnsAsync(new CampaignSummaryModel { Id = CAMPAIGN_ID });

            CampaignController controller = new CampaignController(mockMediator.Object, null);
            controller.SetClaims(new List<Claim>()); // create a User for the controller
            var view = await controller.Edit(CAMPAIGN_ID);
            mockMediator.Verify(mock => mock.SendAsync(It.Is<CampaignSummaryQuery>(c => c.CampaignId == CAMPAIGN_ID)));
        }
Beispiel #8
0
        public async Task EditGetReturnsCorrectViewModelWhenUserIsAuthorized()
        {
            var mediator = new Mock <IMediator>();

            mediator.Setup(mock => mock.SendAsync(It.IsAny <CampaignSummaryQuery>())).ReturnsAsync(new CampaignSummaryViewModel()).Verifiable();
            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(true, false, false, false));

            var mockImageService = new Mock <IImageService>();

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

            Assert.IsType <ViewResult>(await sut.Edit(It.IsAny <int>()));
        }
Beispiel #9
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 EditPostAddsCorrectKeyAndErrorMessageToModelStateWhenCampaignEndDateIsLessThanCampainStartDate()
        {
            var campaignSummaryModel = new CampaignSummaryModel { OrganizationId = 1, StartDate = DateTime.Now.AddDays(1), EndDate = DateTime.Now.AddDays(-1)};

            var sut = new CampaignController(null, null);
            sut.SetClaims(new List<Claim>
            {
                new Claim(AllReady.Security.ClaimTypes.UserType, UserType.OrgAdmin.ToString()),
                new Claim(AllReady.Security.ClaimTypes.Organization, campaignSummaryModel.OrganizationId.ToString())
            });

            await sut.Edit(campaignSummaryModel, null);
            var modelStateErrorCollection = sut.ModelState.GetErrorMessagesByKey(nameof(CampaignSummaryModel.EndDate));

            Assert.Equal(modelStateErrorCollection.Single().ErrorMessage, "The end date must fall on or after the start date.");
        }
Beispiel #11
0
        public async Task EditPostReturnsForbidResultResultWhenUserIsNotAuthorized()
        {
            const int campaignId = 100;
            var       viewModel  = new CampaignSummaryViewModel {
                Id = campaignId
            };

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(false, false, false, false));

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

            var result = await sut.Edit(viewModel, null);

            Assert.IsType <ForbidResult>(result);
        }
        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);
        }
Beispiel #13
0
        public async Task EditPostAddsCorrectKeyAndErrorMessageToModelStateWhenCampaignEndDateIsLessThanCampainStartDate_WhenCampaignIdIsNotZero()
        {
            const int campaignId           = 33;
            var       campaignSummaryModel = new CampaignSummaryViewModel {
                Id = campaignId, OrganizationId = 1, StartDate = DateTime.Now.AddDays(1), EndDate = DateTime.Now.AddDays(-1)
            };

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(false, true, false, false));

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

            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);
        }
Beispiel #14
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));
        }
Beispiel #15
0
        public async Task EditPostAddsErrorToModelStateWhenInvalidImageFormatIsSupplied()
        {
            const int campaignId = 100;
            var       viewModel  = new CampaignSummaryViewModel {
                Name = "Foo", Id = campaignId
            };

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <AuthorizableCampaignQuery>())).ReturnsAsync(new FakeAuthorizableCampaign(false, true, false, false));

            var mockImageService = new Mock <IImageService>();

            var file = FormFile("");

            var sut = new CampaignController(mediator.Object, mockImageService.Object);
            await sut.Edit(viewModel, file);

            Assert.False(sut.ModelState.IsValid);
            Assert.Equal("You must upload a valid image file for the logo (.jpg, .png, .gif)", sut.ModelState["ImageUrl"].Errors.Single().ErrorMessage);
        }
Beispiel #16
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);
        }
Beispiel #17
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);
        }
        public async Task EditPostReturnsCorrectViewModelWhenInvalidImageFormatIsSupplied()
        {
            int ORGANIZATION_ID = 100;
            var mockMediator = new Mock<IMediator>();
            var mockImageService = new Mock<IImageService>();

            var sut = new CampaignController(mockMediator.Object, mockImageService.Object);
            sut.SetClaims(new List<Claim>
            {
                new Claim(AllReady.Security.ClaimTypes.UserType, UserType.OrgAdmin.ToString()),
                new Claim(AllReady.Security.ClaimTypes.Organization, ORGANIZATION_ID.ToString())
            });

            var file = FormFile("audio/mpeg3");
            CampaignSummaryModel model = MassiveTrafficLightOutage_model;
            model.OrganizationId = ORGANIZATION_ID;

            ViewResult view = (ViewResult)(await sut.Edit(model, file));
            CampaignSummaryModel viewModel = (CampaignSummaryModel)view.ViewData.Model;
            Assert.True(Object.ReferenceEquals(model, viewModel));
        }
        public async void CampaignEditPostUploadsImageToImageService()
        {
            const int organizationId   = 1;
            const int campaignId       = 100;
            var       mockMediator     = new Mock <IMediator>();
            var       mockImageService = new Mock <IImageService>();

            mockImageService
            .Setup(mock => mock.UploadCampaignImageAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <IFormFile>()))
            .Returns(() => Task.FromResult(""))
            .Verifiable();
            var controller = new CampaignController(
                mockMediator.Object,
                mockImageService.Object);

            var mockHttpContext = new Mock <HttpContext>();

            mockHttpContext.Setup(mock => mock.User)
            .Returns(() => GetClaimsPrincipal(UserType.OrgAdmin.ToString(), organizationId));
            var mockContext = new Mock <ActionContext>();

            mockContext.Object.HttpContext = mockHttpContext.Object;
            controller.ActionContext       = mockContext.Object;

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

            await controller.Edit(new CampaignSummaryModel
            {
                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);
        }
        public async Task EditPostInsertsCampaign()
        {
            int OrganizationId = 99;
            int NewCampaignId  = 100;
            var mockMediator   = new Mock <IMediator>();

            mockMediator.Setup(x => x.SendAsync(It.IsAny <EditCampaignCommand>()))
            .Returns((EditCampaignCommand q) => Task.FromResult <int>(NewCampaignId));

            var mockImageService          = new Mock <IImageService>();
            CampaignController controller = new CampaignController(mockMediator.Object, mockImageService.Object);
            List <Claim>       claims     = new List <Claim>
            {
                new Claim(AllReady.Security.ClaimTypes.UserType, UserType.OrgAdmin.ToString()),
                new Claim(AllReady.Security.ClaimTypes.Organization, OrganizationId.ToString())
            };

            controller.SetClaims(claims);

            var model = MassiveTrafficLightOutage_model;

            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.Equal(0, validationResults.Count());

            var file = FormFile("image/jpeg");
            RedirectToActionResult view = (RedirectToActionResult)await controller.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(view.RouteValues["area"], "Admin");
            Assert.Equal(view.RouteValues["id"], NewCampaignId);
        }