Skip to content

Commit

Permalink
Testcase updated of create region
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Apr 9, 2024
1 parent df9b4fa commit 80c9696
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 13 deletions.
270 changes: 270 additions & 0 deletions \
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
import json

from deep.tests import TestCase
from geo.models import Region, AdminLevel, GeoArea
from project.models import Project


class RegionTests(TestCase):
def test_create_region(self):
region_count = Region.objects.count()

project = self.create(Project, role=self.admin_role)
url = '/api/v1/regions/'
data = {
'code': 'NLP',
'title': 'Nepal',
'data': {'testfield': 'testfile'},
'public': True,
'project': project.id,
}

self.authenticate()
response = self.client.post(url, data)
self.assert_201(response)

self.assertEqual(Region.objects.count(), region_count + 1)
self.assertEqual(response.data['code'], data['code'])
self.assertIn(Region.objects.get(id=response.data['id']),
project.regions.all())

def test_region_published_status(self):
"""
Once published is set to True for region don't allow it to modify
"""
project = self.create(Project, role=self.admin_role)
region = self.create(Region, is_published=True)
project.regions.add(region)

data = {
'is_published': False
}
url = f'/api/v1/regions/{region.id}/'
self.authenticate()
response = self.client.patch(url, data)
self.assert_403(response)

def test_publish_region(self):
user = self.create_user()
user1 = self.create_user()
project = self.create(Project, role=self.admin_role)
region = self.create(Region, created_by=user)
project.regions.add(region)

url = f'/api/v1/regions/{region.id}/publish/'
data = {}

# authenticated with user that has not created region
self.authenticate(user1)
response = self.client.post(url, data)
self.assert_400(response)

self.authenticate(user)
response = self.client.post(url, data)
self.assert_200(response)
self.assertEqual(response.data['is_published'], True)

def test_clone_region(self):
project = self.create(Project, role=self.admin_role)
region = self.create(Region)
project.regions.add(region)

url = '/api/v1/clone-region/{}/'.format(region.id)
data = {
'project': project.id,
}

self.authenticate()
response = self.client.post(url, data)
self.assert_201(response)

self.assertNotEqual(response.data['id'], region.id)
self.assertFalse(response.data['public'])
self.assertFalse(region in project.regions.all())

new_region = Region.objects.get(id=response.data['id'])
self.assertTrue(new_region in project.regions.all())

def test_region_filter_not_in_project(self):
project_1 = self.create(Project, role=self.admin_role)
project_2 = self.create(Project, role=self.admin_role)
region_1 = self.create(Region)
region_2 = self.create(Region)
region_3 = self.create(Region)
project_1.regions.add(region_1)
project_1.regions.add(region_2)
project_2.regions.add(region_3)

# filter regions in project
url = f'/api/v1/regions/?project={project_1.id}'
self.authenticate()
response = self.client.get(url)
self.assert_200(response)
self.assertEqual(len(response.data['results']), 2)
self.assertEqual(
set(rg['id'] for rg in response.data['results']),
set([region_1.id, region_2.id])
)

# filter the region that are not in project
url = f'/api/v1/regions/?exclude_project={project_1.id}'
self.authenticate()
response = self.client.get(url)
self.assert_200(response)
self.assertEqual(len(response.data['results']), 1)
self.assertEqual(response.data['results'][0]['id'], region_3.id)

def test_trigger_api(self):
region = self.create(Region)
url = '/api/v1/geo-areas-load-trigger/{}/'.format(region.id)

self.authenticate()
response = self.client.get(url)
self.assert_200(response)


class AdminLevelTests(TestCase):
def test_create_admin_level(self):
admin_level_count = AdminLevel.objects.count()

region = self.create(Region)
url = '/api/v1/admin-levels/'
data = {
'region': region.pk,
'title': 'test',
'name_prop': 'test',
'pcode_prop': 'test',
'parent_name_prop': 'test',
'parent_pcode_prop': 'test',
}

self.authenticate()
response = self.client.post(url, data)
self.assert_201(response)

self.assertEqual(AdminLevel.objects.count(), admin_level_count + 1)
self.assertEqual(response.data['title'], data['title'])


class GeoOptionsApi(TestCase):
def test_geo_options(self):
region1 = self.create(Region, title='Region 1')
region2 = self.create(Region, title='Region 2')
region3 = self.create(Region, title='Region 3')

project = self.create_project()
project.regions.add(region1, region2)
admin_level1_1 = self.create(AdminLevel, title='AdminLevel1', region=region1, level=0)
admin_level1_2 = self.create(AdminLevel, title='AdminLevel2', region=region2, level=1)
admin_level2_1 = self.create(AdminLevel, title='AdminLevel1', region=region1, level=0)
self.create(AdminLevel, title='AdminLevel1', region=region2, level=0)
self.create(AdminLevel, title='AdminLevel2', region=region2, level=1)
self.create(AdminLevel, title='AdminLevel1', region=region3, level=0)
self.create(AdminLevel, title='AdminLevel2', region=region3, level=1)
geo_area1_1 = self.create(GeoArea, title='GeoArea1', admin_level=admin_level1_1)
geo_area1_2 = self.create(GeoArea, title='GeoArea2', admin_level=admin_level1_2, parent=geo_area1_1)
self.create(GeoArea, title='GeoArea2', admin_level=admin_level2_1)

url = f'/api/v1/geo-options/?project={project.pk}'

self.authenticate()
response = self.client.get(url, follow=True)
self.assert_200(response)
cached_file_url = response.data['geo_options_cached_file']

data = json.loads(b''.join(list(self.client.get(cached_file_url).streaming_content)))
self.assertEqual(
data[str(region1.id)][1].get('label'),
'{} / {}'.format(admin_level1_1.title, geo_area1_2.title)
)

# check if parent is present in geo options
for _, options in data.items():
for option in options:
assert 'parent' in option

# URL should be same for future request
response = self.client.get(url, follow=True)
self.assert_200(response)
assert cached_file_url == response.data['geo_options_cached_file']

# URL should be changed if region data is changed
region1.refresh_from_db()
region1.cache_index += 1
region1.save(update_fields=('cache_index',))
response = self.client.get(url, follow=True)
self.assert_200(response)
assert cached_file_url != response.data['geo_options_cached_file']
cached_file_url = response.data['geo_options_cached_file']

# URL should be same again for future request
response = self.client.get(url, follow=True)
self.assert_200(response)
assert cached_file_url == response.data['geo_options_cached_file']

# URL shouldn't be changed if non assigned region data is changed
region3.refresh_from_db()
region3.cache_index += 1
region3.save(update_fields=('cache_index',))
response = self.client.get(url, follow=True)
self.assert_200(response)
assert cached_file_url == response.data['geo_options_cached_file']


class TestGeoAreaApi(TestCase):
def test_geo_area(self):
region = self.create(Region, is_published=True)
region1 = self.create(Region, is_published=False)
region2 = self.create(Region, is_published=True)
user1 = self.create_user()
user2 = self.create_user()
project = self.create(Project)
project.add_member(user1)
project.regions.set([region, region1])
project2 = self.create(Project)
project2.add_member(user2)
project2.regions.add(region2)

admin_level1 = self.create(AdminLevel, region=region, title='test')
admin_level2 = self.create(AdminLevel, region=region)
admin_level3 = self.create(AdminLevel, region=region1)
admin_level4 = self.create(AdminLevel, region=region2)
geo_area1 = self.create(GeoArea, admin_level=admin_level1, title='me')
self.create(GeoArea, admin_level=admin_level2, parent=geo_area1)
self.create(GeoArea, admin_level=admin_level4)
self.create(GeoArea, admin_level=admin_level3)

url = f'/api/v1/projects/{project.id}/geo-area/'

self.authenticate(user1)
response = self.client.get(url)
self.assert_200(response)
self.assertEqual(response.data['count'], 2) # geo area with region `published=True`
# test for the label
self.assertEqual(response.data['results'][0]['label'], '{}/{}'.format(admin_level1.title, geo_area1.title))

# test for the not project member
self.authenticate(user2)
response = self.client.get(url)
self.assert_403(response)

# test for the pagination
url = f'/api/v1/projects/{project.id}/geo-area/?limit=1'
self.authenticate(user1)
response = self.client.get(url)
self.assert_200(response)
self.assertEqual(len(response.data['results']), 1)

# test for the search field
url = f'/api/v1/projects/{project.id}/geo-area/?label=test'
self.authenticate(user1)
response = self.client.get(url)
self.assert_200(response)
self.assertEqual(response.data['count'], 1)

# Passing the label that is not either region or geoarea title
url = f'/api/v1/projects/{project.id}/geo-area/?label=acd'
self.authenticate(user1)
response = self.client.get(url)
self.assert_200(response)
self.assertEqual(response.data['count'], 0)
6 changes: 2 additions & 4 deletions apps/geo/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from geo.schema import RegionType
from geo.serializers import RegionGqSerializer

from deep.permissions import ProjectPermissions as PP
from utils.graphene.mutation import GrapheneMutation, generate_input_type_for_serializer

RegionInputType = generate_input_type_for_serializer(
Expand All @@ -21,9 +20,8 @@ class Arguments:
result = graphene.Field(RegionType)

@classmethod
def check_permissions(cls, *args, **_):
if PP.Permission.UPDATE_PROJECT:
return True
def check_permissions(cls, info, **_):
return True # global permission is always true


class Mutation():
Expand Down
13 changes: 9 additions & 4 deletions apps/geo/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
from django.db import transaction
from drf_dynamic_fields import DynamicFieldsMixin

from deep.serializers import ProjectPropertySerializerMixin, RemoveNullFieldsMixin, URLCachedFileField
from deep.serializers import (
ProjectPropertySerializerMixin,
RemoveNullFieldsMixin,
URLCachedFileField,
IntegerIDField,
)
from rest_framework import serializers
from user_resource.serializers import UserResourceSerializer
from geo.models import (
Expand Down Expand Up @@ -151,11 +156,11 @@ class Meta:


class RegionGqSerializer(ProjectPropertySerializerMixin, UserResourceSerializer):
project = serializers.IntegerField()
project = IntegerIDField()

class Meta:
model = Region
fields = '__all__'
exclude = ('geo_options', 'is_published')

def validate_project(self, project):
try:
Expand All @@ -166,7 +171,7 @@ def validate_project(self, project):
)

if not project.can_modify(self.context['request'].user):
raise serializers.ValidationError('Invalid project')
raise serializers.ValidationError('Permission Denied')
return project.id

def validate(self, data):
Expand Down
16 changes: 14 additions & 2 deletions apps/geo/tests/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from project.factories import ProjectFactory
from utils.graphene.tests import GraphQLTestCase

from geo.models import Region


class CreateTestMutation(GraphQLTestCase):
def test_create_region_in_project(self):
Expand All @@ -18,6 +20,7 @@ def test_create_region_in_project(self):
}
'''
user = UserFactory.create()
user2 = UserFactory.create()
project = ProjectFactory.create(
title="dummy project 1",
created_by=user
Expand All @@ -30,7 +33,6 @@ def _query_check(minput, **kwargs):
minput=minput,
**kwargs
)
# -- Without login

minput = dict(
project=project.id,
Expand All @@ -40,9 +42,19 @@ def _query_check(minput, **kwargs):
# without login
_query_check(minput, assert_for_error=True)

# with login
self.force_login(user)

_query_check(minput)
content = _query_check(minput)
self.assertIn(user, project.members.all())
self.assertEqual(content['data']['createRegion']['errors'], None)
self.assertEqual(content['data']['createRegion']['result']['title'], "Test")
self.assertIn(
Region.objects.get(id=content['data']['createRegion']['result']['id']), project.regions.all()
)

# login normal user
self.force_login(user2)
content = _query_check(minput)
self.assertNotIn(user2, project.members.all())
self.assertEqual(content['data']['createRegion']['errors'][0]['messages'], "Permission Denied")
4 changes: 1 addition & 3 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5124,18 +5124,16 @@ type RegionDetailType {

input RegionInputType {
clientId: String
project: Int!
project: ID!
code: String!
title: String!
public: Boolean
isPublished: Boolean
regionalGroups: GenericScalar
keyFigures: GenericScalar
populationData: GenericScalar
mediaSources: GenericScalar
cacheIndex: Int
centroid: String
geoOptions: GenericScalar
createdBy: ID
}

Expand Down

0 comments on commit 80c9696

Please sign in to comment.