def test_public_projects_by_region(self): query = ''' query MyQuery ($projectFilter: RegionProjectFilterData) { publicProjectsByRegion (projectFilter: $projectFilter) { totalCount results { id projectsId centroid } } } ''' fake_centroid = Point(1, 2) region1 = RegionFactory.create(public=False, centroid=fake_centroid) region2 = RegionFactory.create(centroid=fake_centroid) region3 = RegionFactory.create(centroid=fake_centroid) region4 = RegionFactory.create(public=False, centroid=fake_centroid) RegionFactory.create() # No Centroid ( This will not show) project1 = ProjectFactory.create(is_private=False, regions=[region1, region2], title='Test Nepal') ProjectFactory.create(is_private=False, regions=[region3], title='Test Canada') project2 = ProjectFactory.create(is_private=True, regions=[region4], title='Test Brazil') def _query_check(project_filter): return self.query_check( query, variables={'projectFilter': project_filter}) content = self.query_check(query) self.assertEqual( content['data']['publicProjectsByRegion']['totalCount'], 3, content) # test for project filter content = _query_check({'ids': [project1.pk] })['data']['publicProjectsByRegion'] self.assertEqual(content['totalCount'], 2, content) content = _query_check({'ids': [project1.pk, project2.pk] })['data']['publicProjectsByRegion'] self.assertEqual(content['totalCount'], 2, content) content = _query_check({'search': 'Canada'})['data']['publicProjectsByRegion'] self.assertEqual(content['totalCount'], 1, content) content = _query_check({'search': 'Brazil'})['data']['publicProjectsByRegion'] self.assertEqual(content['totalCount'], 0, content) # Private projects are not shown
def test_projects_by_region(self): query = ''' query MyQuery ($projectFilter: RegionProjectFilterData) { projectsByRegion (projectFilter: $projectFilter) { totalCount results { id projectsId centroid } } } ''' user = UserFactory.create() region1 = RegionFactory.create() region2 = RegionFactory.create() project1 = ProjectFactory.create(regions=[region1], title='Test Nepal') project2 = ProjectFactory.create(is_private=True, regions=[region1, region2], title='Test USA') # This two projects willn't be shown ProjectFactory.create(is_private=True, regions=[region1, region2]) # private + no member access ProjectFactory.create() # no regions attached project2.add_member(user) self.force_login(user) content = self.query_check( query)['data']['projectsByRegion']['results'] self.assertEqual(content, [], content) # only save region2 centroid. region2.centroid = Point(1, 2) region2.save(update_fields=('centroid', )) content = self.query_check(query)['data']['projectsByRegion'] self.assertEqual(content['totalCount'], 1, content) self.assertEqual(content['results'], [{ 'id': str(region2.pk), 'centroid': { 'coordinates': [region2.centroid.x, region2.centroid.y], 'type': 'Point' }, 'projectsId': [str(project2.pk)] }], content) # Now save region1 centroid as well. region1.centroid = Point(2, 3) region1.save(update_fields=('centroid', )) content = self.query_check(query)['data']['projectsByRegion'] self.assertEqual(content['totalCount'], 2, content) self.assertEqual( content['results'], [{ 'id': str(region2.pk), 'centroid': { 'coordinates': [region2.centroid.x, region2.centroid.y], 'type': 'Point' }, 'projectsId': [str(project2.pk)] }, { 'id': str(region1.pk), 'centroid': { 'coordinates': [region1.centroid.x, region1.centroid.y], 'type': 'Point' }, 'projectsId': [str(project1.pk), str(project2.pk)] }], content) # Now using filters project_filter = {'search': 'USA'} content = self.query_check(query, variables={'projectFilter': project_filter })['data']['projectsByRegion'] self.assertEqual(content['totalCount'], 2, content) self.assertEqual(content['results'], [{ 'id': str(region2.pk), 'centroid': { 'coordinates': [region2.centroid.x, region2.centroid.y], 'type': 'Point' }, 'projectsId': [str(project2.pk)] }, { 'id': str(region1.pk), 'centroid': { 'coordinates': [region1.centroid.x, region1.centroid.y], 'type': 'Point' }, 'projectsId': [str(project2.pk)] }], content) project_filter = {'ids': [project1.pk]} content = self.query_check(query, variables={'projectFilter': project_filter })['data']['projectsByRegion'] self.assertEqual(content['totalCount'], 1, content) self.assertEqual(content['results'], [{ 'id': str(region1.pk), 'centroid': { 'coordinates': [region1.centroid.x, region1.centroid.y], 'type': 'Point' }, 'projectsId': [str(project1.pk)] }], content)
def test_project_geoareas(self): query = ''' query MyQuery( $projectID: ID!, $ids: [ID!], $ordering: [GeoAreaOrderingEnum!], $search: String, $titles: [String!] ) { project(id: $projectID) { geoAreas( ids: $ids, ordering: $ordering, search: $search, titles: $titles ) { totalCount results { id adminLevelTitle regionTitle title } } } } ''' user = UserFactory.create() region = RegionFactory.create(title='Nepal', is_published=True) project = ProjectFactory.create() project.add_member(user) project.regions.add(region) admin_level = AdminLevelFactory.create(title='District', region=region) geo1 = GeoAreaFactory.create(admin_level=admin_level, title='Kathmandu') geo2 = GeoAreaFactory.create(admin_level=admin_level, title='Lalitpur') GeoAreaFactory.create(admin_level=admin_level, title='Bhaktapur') geo1_data = dict( id=str(geo1.pk), adminLevelTitle=geo1.admin_level.title, regionTitle=geo1.admin_level.region.title, title=geo1.title, ) geo2_data = dict( id=str(geo2.pk), adminLevelTitle=geo2.admin_level.title, regionTitle=geo2.admin_level.region.title, title=geo2.title, ) def _query_check(variables={}, **kwargs): return self.query_check( query, variables={ 'projectID': project.id, **variables }, **kwargs, ) # -- Without login _query_check(assert_for_error=True) # -- With login self.force_login(user) content = _query_check()['data']['project']['geoAreas']['results'] self.assertEqual(len(content), 3, content) filters = { 'ids': [str(geo1.pk), str(geo2.pk)], 'ordering': self.genum(GeoAreaOrderingEnum.ASC_ID) } content = _query_check( variables=filters)['data']['project']['geoAreas']['results'] self.assertEqual(len(content), 2, content) self.assertEqual(content, [geo1_data, geo2_data], content) filters = { 'search': 'kathm', 'ordering': self.genum(GeoAreaOrderingEnum.ASC_ID) } content = _query_check( variables=filters)['data']['project']['geoAreas']['results'] self.assertEqual(len(content), 1, content) self.assertEqual(content, [geo1_data], content) filters = { 'titles': ['kathm', 'lalit'], 'ordering': self.genum(GeoAreaOrderingEnum.ASC_ID) } content = _query_check( variables=filters)['data']['project']['geoAreas']['results'] self.assertEqual(len(content), 2, content) self.assertEqual(content, [geo1_data, geo2_data], content)
def test_public_projects(self): query = ''' query MyQuery { publicProjects (ordering: "id") { page pageSize totalCount results { id title description createdAt analysisFrameworkTitle organizationsTitle regionsTitle numberOfEntries numberOfLeads numberOfUsers } } } ''' # Lets create some analysis_framework(private + publice) public_analysis_framework = AnalysisFrameworkFactory.create( is_private=False, title='Public Analysis Framework Title') private_analysis_framework = AnalysisFrameworkFactory.create( title='Private Analysis Framework Title', is_private=True) # lets create some regions(private + public) public_region = RegionFactory.create(public=True, title='Public Region') private_region = RegionFactory.create(public=False, title='Private Region') public_project1 = ProjectFactory.create( analysis_framework=public_analysis_framework, regions=[public_region]) public_project2 = ProjectFactory.create( analysis_framework=public_analysis_framework, regions=[private_region]) public_project3 = ProjectFactory.create( analysis_framework=private_analysis_framework, regions=[public_region]) public_project4 = ProjectFactory.create( analysis_framework=private_analysis_framework, regions=[public_region]) private_project = ProjectFactory.create(is_private=True) content = self.query_check(query) self.assertEqual(content['data']['publicProjects']['totalCount'], 4, content) self.assertListIds(content['data']['publicProjects']['results'], [ public_project1, public_project2, public_project3, public_project4 ], content) # some checks for analysis_framework private and public self.assertEqual( content['data']['publicProjects']['results'][0] ['analysisFrameworkTitle'], 'Public Analysis Framework Title', content) self.assertEqual( content['data']['publicProjects']['results'][2] ['analysisFrameworkTitle'], None, content) # some check for regions private and public self.assertEqual( content['data']['publicProjects']['results'][2]['regionsTitle'], 'Public Region', content) self.assertEqual( content['data']['publicProjects']['results'][1]['regionsTitle'], None, content) # make sure private projects are not visible here self.assertNotListIds(content['data']['publicProjects']['results'], [private_project], content)
def test_project_query(self): """ Test private + non-private project behaviour """ query = ''' query MyQuery ($id: ID!) { project(id: $id) { id title currentUserRole startDate status isVisualizationEnabled isPrivate endDate description data stats { entriesActivity { count date } numberOfLeads numberOfLeadsNotTagged numberOfLeadsInProgress numberOfLeadsTagged numberOfEntries numberOfEntriesVerified numberOfEntriesControlled numberOfUsers leadsActivity { count date } } membershipPending isRejected regions { id title } } } ''' user = UserFactory.create() analysis_framework = AnalysisFrameworkFactory.create() public_project, public_project2, public_project3, public_project4 = ProjectFactory.create_batch( 4, analysis_framework=analysis_framework) now = timezone.now() lead1_1 = self.update_obj(LeadFactory.create(project=public_project), created_at=now + relativedelta(months=-1)) lead1_2 = self.update_obj(LeadFactory.create(project=public_project), created_at=now + relativedelta(months=-2)) lead1_3 = self.update_obj(LeadFactory.create(project=public_project), created_at=now + relativedelta(months=-2)) lead1_4 = self.update_obj(LeadFactory.create(project=public_project), created_at=now + relativedelta(months=-1)) self.update_obj(LeadFactory.create(project=public_project), created_at=now + relativedelta(months=-1)) data = [ { "lead": lead1_1, "controlled": False, "months": -1, }, { "lead": lead1_1, "controlled": False, "months": -3, }, { "lead": lead1_2, "controlled": True, "months": -3, }, { "lead": lead1_2, "controlled": False, "months": -2, }, { "lead": lead1_2, "controlled": True, "months": -2, }, { "lead": lead1_3, "controlled": True, "months": -3, }, { "lead": lead1_3, "controlled": True, "months": -3, }, ] now = timezone.now() for item in data: self.update_obj( EntryFactory.create(lead=item['lead'], controlled=item['controlled'], project=public_project, analysis_framework=analysis_framework), created_at=now + relativedelta(months=item['months'])) EntryFactory.create(lead=lead1_3, project=public_project, controlled=True, analysis_framework=analysis_framework) EntryFactory.create(lead=lead1_4, project=public_project, controlled=True, analysis_framework=analysis_framework) lead2 = LeadFactory.create(project=public_project2) lead3 = LeadFactory.create(project=public_project3) EntryFactory.create(lead=lead2, project=public_project2, controlled=False, analysis_framework=analysis_framework) EntryFactory.create(lead=lead3, project=public_project3, controlled=False, analysis_framework=analysis_framework) user2, user3, request_user, non_member_user = UserFactory.create_batch( 4) public_project = ProjectFactory.create( analysis_framework=analysis_framework) private_project = ProjectFactory.create( is_private=True, analysis_framework=analysis_framework) ProjectJoinRequestFactory.create(project=public_project, requested_by=request_user, status='pending', role=self.project_role_admin) # create projectJoinRequest(status='rejected') ProjectJoinRequestFactory.create(project=public_project4, requested_by=request_user, status='rejected', role=self.project_role_admin) # add some project member public_project.add_member(user) public_project.add_member(user2) public_project.add_member(user3) # add some lead for the project lead = LeadFactory.create(project=public_project) lead2 = LeadFactory.create(project=public_project) LeadFactory.create_batch(3, project=public_project) LeadFactory.create(project=private_project) # add some entry for the project EntryFactory.create_batch(4, project=public_project, analysis_framework=analysis_framework, lead=lead) entry2_1 = EntryFactory.create(project=public_project, analysis_framework=analysis_framework, lead=lead2, controlled=True) entry2_2 = EntryFactory.create(project=public_project, analysis_framework=analysis_framework, lead=lead2) EntryFactory.create(project=private_project, analysis_framework=analysis_framework, lead=lead) # Verify entries entry2_1.verified_by.add(user) entry2_1.verified_by.add(user3) entry2_2.verified_by.add(user) # NOTE: Right noe only IN_PROGRESS status is set automatically # Control one lead lead2.status = Lead.Status.TAGGED lead2.save(update_fields=('status', )) # lets add some regions to project region1, region2, region3 = RegionFactory.create_batch(3) public_project.regions.add(region1) public_project.regions.add(region2) private_project.regions.add(region3) # Generate project cache _generate_project_stats_cache() # -- Without login self.query_check(query, assert_for_error=True, variables={'id': public_project.id}) self.query_check(query, assert_for_error=True, variables={'id': private_project.id}) # -- With login self.force_login(user) # --- non-member user content = self.query_check(query, variables={'id': public_project.id}) self.assertNotEqual(content['data']['project'], None, content) content = self.query_check(query, variables={'id': private_project.id}) self.assertEqual(content['data']['project'], None, content) # login with non_member self.force_login(non_member_user) content = self.query_check(query, variables={'id': public_project.id}) self.assertNotEqual(content['data']['project'], None, content) self.assertEqual(content['data']['project']['membershipPending'], False) # login with request_user self.force_login(request_user) content = self.query_check(query, variables={'id': public_project4.id}) self.assertNotEqual(content['data']['project'], None, content) self.assertEqual(content['data']['project']['isRejected'], True) # --- member user # ---- (public-project) self.force_login(user) content = self.query_check(query, variables={'id': public_project.id}) self.assertNotEqual(content['data']['project'], None, content) self.assertEqual(content['data']['project']['stats']['numberOfLeads'], 5, content) self.assertEqual( content['data']['project']['stats']['numberOfLeadsNotTagged'], 3, content) self.assertEqual( content['data']['project']['stats']['numberOfLeadsInProgress'], 1, content) self.assertEqual( content['data']['project']['stats']['numberOfLeadsTagged'], 1, content) self.assertEqual( content['data']['project']['stats']['numberOfEntries'], 6, content) self.assertEqual( content['data']['project']['stats']['numberOfEntriesVerified'], 2, content) self.assertEqual( content['data']['project']['stats']['numberOfEntriesControlled'], 1, content) self.assertEqual(content['data']['project']['stats']['numberOfUsers'], 3, content) self.assertEqual( len(content['data']['project']['stats']['leadsActivity']), 1, content) self.assertEqual( len(content['data']['project']['stats']['entriesActivity']), 1, content) self.assertEqual(len(content['data']['project']['regions']), 2, content) self.assertListIds(content['data']['project']['regions'], [region1, region2], content) # login with request user self.force_login(request_user) content = self.query_check(query, variables={'id': public_project.id}) self.assertNotEqual(content['data']['project'], None, content) self.assertEqual(content['data']['project']['membershipPending'], True) # ---- (private-project) self.force_login(user) private_project.add_member(user) content = self.query_check(query, variables={'id': private_project.id}) self.assertNotEqual(content['data']['project'], None, content) self.assertEqual(len(content['data']['project']['regions']), 1, content) self.assertListIds(content['data']['project']['regions'], [region3], content)
def test_project_region_action_mutation(self): query = ''' mutation MyMutation ($projectId: ID!, $regionsToAdd: [ID!], $regionsToRemove: [ID!]) { project(id: $projectId) { projectRegionBulk(regionsToAdd: $regionsToAdd, regionsToRemove: $regionsToRemove) { result { id title } deletedResult { id title } } } } ''' user = UserFactory.create() normal_user = UserFactory.create() another_user = UserFactory.create() af = AnalysisFrameworkFactory.create() project = ProjectFactory.create(title='Project 101', analysis_framework=af) project.add_member(user, role=self.project_role_owner) project.add_member(normal_user) region_public = RegionFactory.create(title='public-region') region_private = RegionFactory.create(title='private-region', public=False) region_private_owner = RegionFactory.create(title='private-region-owner', public=False, created_by=user) # Region with project membership # -- Normal region_private_with_membership = RegionFactory.create(title='private-region-with-membership', public=False) another_project_for_membership = ProjectFactory.create() another_project_for_membership.regions.add(region_private_with_membership) another_project_for_membership.add_member(user, role=self.project_role_admin) # -- Admin region_private_with_membership_admin = RegionFactory.create(title='private-region-with-membership', public=False) another_project_for_membership_admin = ProjectFactory.create() another_project_for_membership_admin.regions.add(region_private_with_membership_admin) another_project_for_membership_admin.add_member(user, role=self.project_role_admin) def _query_check(add, remove, **kwargs): return self.query_check( query, mnested=['project'], variables={ 'projectId': str(project.pk), 'regionsToAdd': add, 'regionsToRemove': remove, }, **kwargs, ) # ---------- Without login _query_check([], [], assert_for_error=True) # ---------- With login (non member) self.force_login(another_user) _query_check([], [], assert_for_error=True) # ---------- With login (member + low access) self.force_login(normal_user) _query_check([], [], assert_for_error=True) # ---------- With login (member with high access) self.force_login(user) # Simple checkup response = _query_check([], []) self.assertEqual(response['data']['project']['projectRegionBulk'], { 'deletedResult': [], 'result': [], }) # Add response = _query_check( [ str(region_public.pk), str(region_private.pk), str(region_private_owner.pk), str(region_private_with_membership.pk), ], [], ) self.assertEqual(response['data']['project']['projectRegionBulk'], { 'deletedResult': [], 'result': [ dict(id=str(region_public.pk), title=region_public.title), dict(id=str(region_private_owner.pk), title=region_private_owner.title), dict(id=str(region_private_with_membership.pk), title=region_private_with_membership.title), ], }) self.assertEqual( list(project.regions.values_list('id', flat=True).order_by('id')), [ region_public.pk, region_private_owner.pk, region_private_with_membership.pk, ] ) # Delete response = _query_check( [], [ str(region_public.pk), str(region_private.pk), str(region_private_owner.pk), str(region_private_with_membership.pk), ], ) self.assertEqual(response['data']['project']['projectRegionBulk'], { 'deletedResult': [ dict(id=str(region_public.pk), title=region_public.title), dict(id=str(region_private_owner.pk), title=region_private_owner.title), dict(id=str(region_private_with_membership.pk), title=region_private_with_membership.title), ], 'result': [], }) self.assertEqual(list(project.regions.values_list('id', flat=True).order_by('id')), [])
def setUp(self): super().setUp() self.entries_query = ''' query MyQuery ($projectId: ID! $filterableData: [EntryFilterDataType!]) { project(id: $projectId) { entries (filterableData: $filterableData) { results { id } } } } ''' # AnalysisFramework setup self.af = AnalysisFrameworkFactory.create() region = RegionFactory.create() # -- Admin levels admin_level1 = AdminLevelFactory.create(region=region, level=1) admin_level2 = AdminLevelFactory.create(region=region, level=2) admin_level3 = AdminLevelFactory.create(region=region, level=3) # -- GeoAreas (with parent relations) self.geo_area_1 = GeoAreaFactory.create(admin_level=admin_level1) self.geo_area_2_1 = GeoAreaFactory.create(admin_level=admin_level2, parent=self.geo_area_1) self.geo_area_2_2 = GeoAreaFactory.create(admin_level=admin_level2, parent=self.geo_area_1) self.geo_area_2_3 = GeoAreaFactory.create(admin_level=admin_level2, parent=self.geo_area_1) self.geo_area_3_1 = GeoAreaFactory.create(admin_level=admin_level3, parent=self.geo_area_2_1) self.geo_area_3_2 = GeoAreaFactory.create(admin_level=admin_level3, parent=self.geo_area_2_2) self.geo_area_3_3 = GeoAreaFactory.create(admin_level=admin_level3, parent=self.geo_area_2_3) # For LIST Filter self.widget_multiselect = WidgetFactory.create( analysis_framework=self.af, key='multiselect-widget-101', title='Multiselect Widget', widget_id=Widget.WidgetType.MULTISELECT, properties={ 'data': { 'options': [ { 'key': 'key-101', 'label': 'Key label 101' }, { 'key': 'key-102', 'label': 'Key label 102' }, { 'key': 'key-103', 'label': 'Key label 103' }, { 'key': 'key-104', 'label': 'Key label 104' }, ] }, }, ) # For Number Filter self.widget_number = WidgetFactory.create( analysis_framework=self.af, key='number-widget-101', title='Number Widget', widget_id=Widget.WidgetType.NUMBER, ) # For INTERSECTS Filter self.widget_date_range = WidgetFactory.create( analysis_framework=self.af, key='date-range-widget-101', title='DateRange Widget', widget_id=Widget.WidgetType.DATE_RANGE, ) # For TEXT Filter self.widget_text = WidgetFactory.create( analysis_framework=self.af, key='text-widget-101', title='Text Widget', widget_id=Widget.WidgetType.TEXT, ) self.widget_geo = WidgetFactory.create( analysis_framework=self.af, key='geo-widget-101', title='GEO Widget', widget_id=Widget.WidgetType.GEO, ) self.project = ProjectFactory.create(analysis_framework=self.af) self.project.regions.add(region) # User with role self.user = UserFactory.create() self.project.add_member(self.user, role=self.project_role_reader) self.lead1 = LeadFactory.create(project=self.project) self.lead2 = LeadFactory.create(project=self.project) self.lead3 = LeadFactory.create(project=self.project) self.entry_create_kwargs = dict(project=self.project, analysis_framework=self.af)