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 IndexReturnsCorrectDataWhenUserIsNotOrgAdmin()
        {
            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 
                    List<CampaignSummaryModel> ret = new List<CampaignSummaryModel>();
                    ret.Add(new CampaignSummaryModel { OrganizationId = OrganizationId });
                    ret.Add(new CampaignSummaryModel { OrganizationId = OrganizationId + 1 });
                    return ret;
                }
            );
            CampaignController controller = new CampaignController(mockMediator.Object, null);

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

            // All campaigns returned when not OrgAdmin
            ViewResult 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
            IEnumerable<CampaignSummaryModel> viewModel = (IEnumerable<CampaignSummaryModel>)view.ViewData.Model;
            Assert.NotNull(viewModel);
            Assert.Equal(viewModel.Count(), 2);
        }
예제 #3
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);
        }
예제 #4
0
        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)));
        }
예제 #5
0
        public void IndexSendsCampaignListQueryWithCorrectData_WhenUserIsNotOrgAdmin()
        {
            var mockMediator = new Mock <IMediator>();

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

            controller.MakeUserNotAnOrgAdmin();
            controller.Index();

            mockMediator.Verify(mock => mock.Send(It.Is <CampaignListQuery>(q => q.OrganizationId == null)));
        }
예제 #6
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)));
        }
        public void IndexSendsCampaignListQueryWithCorrectDataWhenUserIsNotOrgAdmin()
        {
            var mockMediator = new Mock<IMediator>();
            CampaignController controller = new CampaignController(mockMediator.Object, null);
            List<Claim> 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)));
        }
예제 #9
0
        public void CampaignsControllerTestMethod()
        {
            /// mock dla priorytetów
            Mock <IPriorityRepository> priorityMock = new Mock <IPriorityRepository>();
            // priorityMock.Setup(m => m.Priorities).Returns((MockData.Priorities).AsQueryable());

            /// mock dla kampanii
            Mock <ICampaignRepository> campaignMock = new Mock <ICampaignRepository>();
            // campaignMock.Setup(m => m.Campaigns).Returns((MockData.Campaigns).AsQueryable());

            /// mock dla użytkowników
            Mock <IUsersRepository> usersMock = new Mock <IUsersRepository>();
            //usersMock.Setup(m => m.Users).Returns((MockData.Users).AsQueryable());

            /// Utworzenie kontrolera kampanii
            CampaignController campaignController = new CampaignController(campaignMock.Object, priorityMock.Object, usersMock.Object);

            campaignController.ItemsPerPage = 6;

            /// Wywołanie akcji Index
            ViewResult view = (ViewResult)campaignController.Index(2, null, null);

            /// Weryfikacja wyników widoku
            /// Model nie może być null
            Assert.AreNotEqual(null, view.Model);
            CampaignListViewModel model = (CampaignListViewModel)view.Model;

            ///Lista kampani nie może być null
            Assert.AreNotEqual(null, model.Campaigns);

            ///Model musi być typu CampaignListViewModel
            Assert.IsInstanceOfType(model, typeof(CampaignListViewModel));

            ///Widok musi mieć nazwę Index
            Assert.IsTrue(view.ViewName == "Index");

            ///Bieżąca strona musi mieć numer 2
            Assert.AreEqual(2, model.CurrentPage);

            ///Liczba elementów na stronie musi być 6
            Assert.AreEqual(6, model.ItemsPerPage);

            ///Liczba wszystkich znalezionych kampanii musi być równa liczbie obiektów z mock-a
            Assert.AreEqual(campaignMock.Object.Campaigns.Count(), model.NumberOfResults);

            ///Liczba zwróconych użytkownikowi kampanii na bieżącej stronie musi być 6
            //Assert.AreEqual(6, model.Campaigns.Count);

            /// Wywołanie akcji listy kampanii
            //RedirectToRouteResult redirection = (RedirectToRouteResult)campaignController.List(new CampaignListViewModel
            //	{
            //		//Filters = new CampaignListViewModelFilter
            //		//{
            //		//	FilterActive = false
            //		//}
            //	});

            /// Weryfikacja wyników
            /// Przekierowanie nie może być null-em
            //Assert.IsNotNull(redirection);

            /////RouteValues nie mogą być null-em
            //Assert.IsNotNull(redirection.RouteValues);
            //Assert.IsTrue(redirection.RouteValues.Count > 0);

            /////RouteValues muszą zawierać akcję o nazwie Index
            //Assert.IsTrue(redirection.RouteValues["action"].ToString() == "Index");

            ///Wywołanie metody GET do pobierania listy kampanii
            view  = (ViewResult)campaignController.Index(null, null, null);
            model = (CampaignListViewModel)view.Model;

            ///Lista kampanii musi wynosić 4 (na stronie)
            //  Assert.AreEqual(4, model.Campaigns.Count);

            ///Wywołanie akcji z filtrowaniem
            //redirection = (RedirectToRouteResult)campaignController.List(new CampaignListViewModel
            //{
            //	//Filters = new CampaignListViewModelFilter
            //	//{
            //	//	FilterActive = true,
            //	//	FilterPriorityId = 1,
            //	//	FilterStartDateFrom = DateTime.Now.AddDays(-8).Date,
            //	//	FilterStartDateTo = DateTime.Now.AddDays(-4).Date
            //	//}
            //});
            //Assert.IsNotNull(redirection);
            //Assert.IsNotNull(redirection.RouteValues);
            //Assert.IsTrue(redirection.RouteValues.Count > 0);
            //Assert.IsTrue(redirection.RouteValues["action"].ToString() == "Index");

            ///Sprawdzenie wyników
            view  = (ViewResult)campaignController.Index(null, null, null);
            model = (CampaignListViewModel)view.Model;
            //    Assert.AreEqual(2, model.Campaigns.Count);

            //Sprawdzenie czy zwróciono spodziewane obiekty kampanii
            //Assert.IsTrue(model.Campaigns[0].Name == "Kampania 1");
            //Assert.IsTrue(model.Campaigns[1].Name == "Kampania 2");
        }