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

        }
        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 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 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 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);
 }
        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.");
        }
        public async Task DetailsReturnsCorrectViewModelWhenViewModelIsNotNullAndUserIsOrgAdmin()
        {
            int CAMPAIGN_ID = 100;
            int 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 CampaignDetailModel { OrganizationId = ORGANIZATION_ID, Id = CAMPAIGN_ID }).Verifiable();

            // user is org admin
            CampaignController 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())
            });

            ViewResult view = (ViewResult)(await controller.Details(CAMPAIGN_ID));
            CampaignDetailModel viewModel = (CampaignDetailModel)view.ViewData.Model;
            Assert.Equal(viewModel.Id, CAMPAIGN_ID);
            Assert.Equal(viewModel.OrganizationId, ORGANIZATION_ID);
        }
        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 DetailConfirmedRedirectsToCorrectActionWithCorrectRouteValuesWhenUserIsOrgAdmin()
        {
            const int organizationId = 1;
            const int campaignId = 100;

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

            var sut = new CampaignController(mockMediator.Object, null);
            sut.SetClaims(new List<Claim>
            {
                new Claim(AllReady.Security.ClaimTypes.UserType, UserType.OrgAdmin.ToString()),
                new Claim(AllReady.Security.ClaimTypes.Organization, 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);
        }
        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 DetailConfirmedSendsDeleteCampaignCommandWithCorrectCampaignIdWhenUserIsOrgAdmin()
        {
            const int organizationId = 1;
            const int campaignId = 100;

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

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

            await sut.DeleteConfirmed(campaignId);

            mockMediator.Verify(mock => mock.SendAsync(It.Is<DeleteCampaignCommand>(i => i.CampaignId == campaignId)), Times.Once);
        }
 public async Task DeleteReturnsCorrectViewModelWhenUserIsOrgAdmin()
 {
     int ORGANIZATION_ID = 99;
     int CAMPAIGN_ID = 100;
     var mockMediator = new Mock<IMediator>();
     mockMediator.Setup(mock => mock.SendAsync(It.Is<CampaignSummaryQuery>(c => c.CampaignId == CAMPAIGN_ID))).ReturnsAsync(new CampaignSummaryModel { Id = CAMPAIGN_ID, OrganizationId = ORGANIZATION_ID });
     CampaignController 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())
     });
     ViewResult view = (ViewResult)(await controller.Delete(CAMPAIGN_ID));
     CampaignSummaryModel viewModel = (CampaignSummaryModel)view.ViewData.Model;
     Assert.Equal(viewModel.Id, CAMPAIGN_ID);
 }
        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.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);
        }
        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 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);

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