Inheritance: Microsoft.AspNet.Mvc.Controller
        public void IndexReturnsCorrectDataWhenUserIsOrgAdmin()
        {
            int OrganizationId = 99;
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.Send(It.Is<CampaignListQuery>(c => c.OrganizationId == OrganizationId)))
                .Returns((CampaignListQuery q) => {
                    List<CampaignSummaryModel> ret = new List<CampaignSummaryModel>();
                    ret.Add(new CampaignSummaryModel { OrganizationId = OrganizationId });
                    return ret;
                }
            );
            CampaignController controller = new CampaignController(mockMediator.Object, null);

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

            ViewResult view = (ViewResult)controller.Index();
            mockMediator.Verify(mock => mock.Send(It.Is<CampaignListQuery>(c => c.OrganizationId == OrganizationId)));

            // Org admin should only see own campaigns
            IEnumerable<CampaignSummaryModel> viewModel = (IEnumerable<CampaignSummaryModel>)view.ViewData.Model;
            Assert.NotNull(viewModel);
            Assert.Equal(viewModel.Count(), 1);
            Assert.Equal(viewModel.First().OrganizationId, OrganizationId);
        }
        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);
        }
        public async Task DetailsSendsCampaignDetailQueryWithCorrectCampaignId()
        {
            const int campaignId = 100;
            var mockMediator = new Mock<IMediator>();

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

            mockMediator.Verify(mock => mock.SendAsync(It.Is<CampaignDetailQuery>(c => c.CampaignId == campaignId)));
        }
        public async Task IndexSendsIndexQueryWithCorrectData_WhenUserIsNotOrgAdmin()
        {
            var mockMediator = new Mock<IMediator>();

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

            mockMediator.Verify(mock => mock.SendAsync(It.Is<IndexQuery>(q => q.OrganizationId == null)));
        }
        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)));
        }
 public void CampaignDetailsNoCampaignReturns404()
 {
     var mockMediator = new Mock<IMediator>();
     mockMediator.Setup(mock => mock.Send(It.IsAny<CampaignDetailQuery>())).Returns(() => null).Verifiable();
     var mockImageService = new Mock<IImageService>();
     var mockDataAccess = new Mock<IAllReadyDataAccess>();
     var controller = new CampaignController(
         mockMediator.Object,
         mockImageService.Object,
         mockDataAccess.Object );
     var actionResult = controller.Details(0);
     Assert.IsType<HttpNotFoundResult>(actionResult);
     mockMediator.Verify(mock => mock.Send(It.IsAny<CampaignDetailQuery>()), Times.Once);
 }
        public void IndexSendsCampaignListQueryWithCorrectDataWhenUserIsNotOrgAdmin()
        {
            var mockMediator = new Mock<IMediator>();
            var controller = new CampaignController(mockMediator.Object, null);
            var claims = new List<Claim>
            {
                new Claim(AllReady.Security.ClaimTypes.UserType, UserType.OrgAdmin.ToString()),
            };
            controller.SetClaims(claims);

            controller.Index();

            mockMediator.Verify(mock => mock.Send(It.Is<CampaignListQuery>(q => q.OrganizationId == null)));
        }
        public void IndexSendsCampaignListQueryWithCorrectDataWhenUserIsOrgAdmin()
        {
            int OrganizationId = 99;
            var mockMediator = new Mock<IMediator>();
            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);

            controller.Index();

            mockMediator.Verify(mock => mock.Send(It.Is<CampaignListQuery>(q => q.OrganizationId == OrganizationId)));
        }
        public async Task LockUnlockSendsLockUnlockCampaignCommandWithCorrectCampaignIdWhenUserIsSiteAdmin()
        {
            var CAMPAIGN_ID = 99;
            var mockMediator = new Mock<IMediator>();
            var controller = new CampaignController(mockMediator.Object, null);
            var claims = new List<Claim> { new Claim(AllReady.Security.ClaimTypes.UserType, UserType.SiteAdmin.ToString()) };
            controller.SetClaims(claims);

            await controller.LockUnlock(CAMPAIGN_ID);

            mockMediator.Verify(mock => mock.SendAsync(It.Is<LockUnlockCampaignCommand>(q => q.CampaignId == CAMPAIGN_ID)), Times.Once);
        }
        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);
        }
 public async Task LockUnlockReturnsHttpUnauthorizedResultWhenUserIsNotSiteAdmin()
 {
     var controller = new CampaignController(null, null);
     controller.SetClaims(new List<Claim> { new Claim(AllReady.Security.ClaimTypes.UserType, UserType.OrgAdmin.ToString()) });
     Assert.IsType<UnauthorizedResult>(await controller.LockUnlock(100));
 }
        public async Task DeleteConfirmedSendsCampaignSummaryQueryWithCorrectCampaignId()
        {
            const int campaignId = 1;

            var mediator = new Mock<IMediator>();
            var sut = new CampaignController(mediator.Object, null);
            await sut.DeleteConfirmed(campaignId);

            mediator.Verify(mock => mock.SendAsync(It.Is<CampaignSummaryQuery>(i => i.CampaignId == campaignId)), Times.Once);
        }
        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);
        }
 public void CreateReturnsCorrectViewWithCorrectViewModel()
 {
     var mockMediator = new Mock<IMediator>();
     var controller = new CampaignController(mockMediator.Object, null);
     var view = (ViewResult) controller.Create();
     var viewModel = (CampaignSummaryViewModel)view.ViewData.Model;
     Assert.Equal(view.ViewName, "Edit");
     Assert.NotNull(viewModel);
 }
        public async Task DeleteReturnsCorrectViewModelWhenUserIsOrgAdmin()
        {
            const int organizationId = 99;
            const int campaignId = 100;

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.Is<CampaignSummaryQuery>(c => c.CampaignId == campaignId))).ReturnsAsync(new CampaignSummaryViewModel { Id = campaignId, OrganizationId = organizationId });

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

            var view = (ViewResult)await controller.Delete(campaignId);
            var viewModel = (CampaignSummaryViewModel)view.ViewData.Model;

            Assert.Equal(viewModel.Id, campaignId);
        }
        public async Task LockUnlockRedirectsToCorrectActionWithCorrectRouteValuesWhenUserIsSiteAdmin()
        {
            const int campaignId = 100;
            var mockMediator = new Mock<IMediator>();

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

            var view = (RedirectToActionResult)await sut.LockUnlock(campaignId);

            // verify the next route
            Assert.Equal(view.ActionName, nameof(CampaignController.Details));
            Assert.Equal(view.RouteValues["area"], "Admin");
            Assert.Equal(view.RouteValues["id"], campaignId);
        }
 public async Task DeleteSendsCampaignSummaryQueryWithCorrectCampaignId()
 {
     var ORGANIZATION_ID = 99;
     var CAMPAIGN_ID = 100;
     var mockMediator = new Mock<IMediator>();
     mockMediator.Setup(mock => mock.SendAsync(It.Is<CampaignSummaryQuery>(c => c.CampaignId == CAMPAIGN_ID))).ReturnsAsync(new CampaignSummaryViewModel { Id = CAMPAIGN_ID, OrganizationId = ORGANIZATION_ID });
     var controller = new CampaignController(mockMediator.Object, null);
     controller.SetClaims(new List<Claim>
     {
         new Claim(AllReady.Security.ClaimTypes.UserType, UserType.OrgAdmin.ToString()),
         new Claim(AllReady.Security.ClaimTypes.Organization, ORGANIZATION_ID.ToString())
     });
     var view = (ViewResult)(await controller.Delete(CAMPAIGN_ID));
     mockMediator.Verify(mock => mock.SendAsync(It.Is<CampaignSummaryQuery>(c => c.CampaignId == CAMPAIGN_ID)), Times.Once);
 }
        private static CampaignController CampaignControllerWithDetailQuery(string userType, int organizationId)
        {
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<CampaignDetailQuery>())).ReturnsAsync(new CampaignDetailViewModel { OrganizationId = organizationId }).Verifiable();

            var controller = new CampaignController(mockMediator.Object, null);
            controller.SetClaims(new List<Claim>
            {
                new Claim(AllReady.Security.ClaimTypes.UserType, userType),
                new Claim(AllReady.Security.ClaimTypes.Organization, organizationId.ToString())
            });

            return controller;
        }
        public async Task DetailsSendsCampaignDetailQueryWithCorrectCampaignId()
        {
            var CAMPAIGN_ID = 100;
            var ORGANIZATION_ID = 99;
            var mockMediator = new Mock<IMediator>();

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

            var controller = new CampaignController(mockMediator.Object, null);
            controller.SetClaims(new List<Claim>()); // create a User for the controller
            var view = await controller.Details(CAMPAIGN_ID);
            mockMediator.Verify(mock => mock.SendAsync(It.Is<CampaignDetailQuery>(c => c.CampaignId == CAMPAIGN_ID)));
        }
        public async Task EditPostInsertsCampaign()
        {
            var OrganizationId = 99;
            var 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>();
            var controller = new CampaignController(mockMediator.Object, mockImageService.Object);
            controller.MakeUserAnOrgAdmin(OrganizationId.ToString());

            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");
            var 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);

        }
        public async Task EditPostAddsCorrectKeyAndErrorMessageToModelStateWhenCampaignEndDateIsLessThanCampainStartDate()
        {
            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(modelStateErrorCollection.Single().ErrorMessage, "The end date must fall on or after the start date.");
        }
        public async Task EditGetSendsCampaignSummaryQueryWithCorrectCampaignId()
        {
            var 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 CampaignSummaryViewModel { Id = CAMPAIGN_ID });

            var 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)));
        }
        public async Task LockUnlockRedirectsToCorrectActionWithCorrectRouteValuesWhenUserIsSiteAdmin()
        {
            var CAMPAIGN_ID = 100;
            var mockMediator = new Mock<IMediator>();

            var controller = new CampaignController(mockMediator.Object, null);
            var claims = new List<Claim>
            {
                new Claim(AllReady.Security.ClaimTypes.UserType, UserType.SiteAdmin.ToString()),
            };
            controller.SetClaims(claims);

            var view = (RedirectToActionResult)await controller.LockUnlock(CAMPAIGN_ID);

            // verify the next route
            Assert.Equal(view.ActionName, nameof(CampaignController.Details));
            Assert.Equal(view.RouteValues["area"], "Admin");
            Assert.Equal(view.RouteValues["id"], CAMPAIGN_ID);

        }
        public async Task LockUnlockSendsLockUnlockCampaignCommandWithCorrectCampaignIdWhenUserIsSiteAdmin()
        {
            const int campaignId = 99;
            var mockMediator = new Mock<IMediator>();

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

            await sut.LockUnlock(campaignId);

            mockMediator.Verify(mock => mock.SendAsync(It.Is<LockUnlockCampaignCommand>(q => q.CampaignId == campaignId)), Times.Once);
        }
        private static Mock<IMediator> MockMediatorCampaignSummaryQuery(out CampaignController controller)
        {
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<CampaignSummaryQuery>())).ReturnsAsync(null).Verifiable();

            controller = new CampaignController(mockMediator.Object, null);
            return mockMediator;
        }
        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 controller = new CampaignController(mockMediator.Object, null);
            controller.MakeUserAnOrgAdmin(organizationId.ToString());

            var view = (ViewResult)(await controller.Details(campaignId));
            var viewModel = (CampaignDetailViewModel)view.ViewData.Model;
            Assert.Equal(viewModel.Id, campaignId);
            Assert.Equal(viewModel.OrganizationId, organizationId);
        }
        private static CampaignController CampaignControllerWithSummaryQuery(string userType, int organizationId)
        {
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<CampaignSummaryQuery>()))
                .ReturnsAsync(new CampaignSummaryViewModel { OrganizationId = organizationId, Location = new LocationEditViewModel() }).Verifiable();

            var mockImageService = new Mock<IImageService>();

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

            return controller;
        }
        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));
        }
        private void IndexReturnsCorrectDataWhenUserIsNotOrgAdmin()
        {
            const int organizationId = 99;
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.Send(It.Is<CampaignListQuery>(c => c.OrganizationId == null)))
                .Returns((CampaignListQuery q) => 
                {
                    // return some models 
                    var ret = new List<CampaignSummaryViewModel>
                    {
                        new CampaignSummaryViewModel { OrganizationId = organizationId },
                        new CampaignSummaryViewModel { OrganizationId = organizationId + 1 }
                    };
                    return ret;
                }
            );

            var controller = new CampaignController(mockMediator.Object, null);
            var claims = new List<Claim>
            {
                new Claim(AllReady.Security.ClaimTypes.UserType, UserType.SiteAdmin.ToString()),
            };
            controller.SetClaims(claims);

            // All campaigns returned when not OrgAdmin
            var view = (ViewResult)controller.Index();

            // verify the fetch was called
            mockMediator.Verify(mock => mock.Send(It.Is<CampaignListQuery>(c => c.OrganizationId == null)));

            // Site admin should only see all campaigns
            var viewModel = (IEnumerable<CampaignSummaryViewModel>)view.ViewData.Model;
            Assert.NotNull(viewModel);
            Assert.Equal(viewModel.Count(), 2);
        }
        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);
        }