Skip to content

Commit

Permalink
Update testcase of region mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Jun 4, 2024
1 parent e292ddc commit 1bdeb38
Show file tree
Hide file tree
Showing 5 changed files with 6,746 additions and 32 deletions.
9 changes: 4 additions & 5 deletions apps/geo/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def check_permissions(cls, info, **_):
class RemoveProjectRegion(PsDeleteMutation):
class Arguments:
id = graphene.ID(required=True)
projectid = graphene.ID(required=True)
project_id = graphene.ID(required=True)
errors = graphene.List(graphene.NonNull(CustomErrorType))
result = graphene.Field(RegionDetailType)
permissions = [PP.permission.UPDATE_PROJECT]
Expand All @@ -61,10 +61,9 @@ def check_permissions(cls, info, **_):

@staticmethod
def mutate(root, info, **kwargs):
project = Project.objects.get(id=kwargs['projectid'])
region = Region.objects.get(id=kwargs['id'])
print(project.regions.all())
if region not in project.regions.all():
project = Project.objects.get(id=kwargs['project_id'])
region = int(kwargs['id'])
if region not in [region.id for region in project.regions.all()]:
return RemoveProjectRegion(errors=[
dict(
field='nonFieldErrors',
Expand Down
5 changes: 2 additions & 3 deletions apps/geo/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from drf_dynamic_fields import DynamicFieldsMixin

from deep.serializers import (
ProjectPropertySerializerMixin,
RemoveNullFieldsMixin,
URLCachedFileField,
)
Expand Down Expand Up @@ -154,7 +153,7 @@ class Meta:
)


class RegionGqSerializer(ProjectPropertySerializerMixin, UserResourceSerializer):
class RegionGqSerializer(UserResourceSerializer):
project = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all())
client_id = serializers.CharField(required=False)

Expand Down Expand Up @@ -197,7 +196,7 @@ def create(self, validated_data):
admin_level = super().create(validated_data)

if not settings.TESTING:
transaction.on_commit(lambda: load_geo_areas.delay(region.id))
transaction.on_commit(lambda: load_geo_areas.delay(admin_level.region.id))

return admin_level

Expand Down
41 changes: 19 additions & 22 deletions apps/geo/tests/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class RemoveRegionFromProjectTestMutation(GraphQLTestCase):
def test_remove_region_from_project(self):
self.remove_region_query = '''
mutation MyMutation($projectId: ID!, $regionId: ID!) {
removeProjectRegion(id: $regionId, projectid: $projectId) {
removeProjectRegion(id: $regionId, projectId: $projectId) {
ok
errors
}
Expand All @@ -73,45 +73,42 @@ def test_remove_region_from_project(self):

user = UserFactory.create()
region = RegionFactory()
region2 = RegionFactory()
project = ProjectFactory(created_by=user)
project.add_member(user)
project.regions.add(region)

def _query_check(minput, **kwargs):
def _query_check(**kwargs):
return self.query_check(
self.remove_region_query,
variable={
'projectId': project.id,
'regionId': region.id
variables={
'projectId': project.id, 'regionId': region.id
},
**kwargs
)

# Without login
minput = {
'projectId': project.id,
'regionId': region.id
}
_query_check(minput, assert_for_error=True)
_query_check(assert_for_error=True)

# With login
self.force_login(user)
content = _query_check(minput)
content = _query_check()
self.assertTrue(content['data']['removeProjectRegion']['ok'])
self.assertIsNone(content['data']['removeProjectRegion']['errors'])

# Ensure region is removed from the project
project.refresh_from_db()
self.assertNotIn(region, project.regions.all())

# Login as a different user
user2 = UserFactory.create()
self.force_login(user2)
# Provide the required variables again
minput = {
'projectId': project.id,
'regionId': region.id
}
content = _query_check(minput)
self.assertFalse(content['data']['removeProjectRegion']['ok'])
self.assertEqual(content['data']['removeProjectRegion']['errors'][0]['messages'], "Permission Denied")
self.query_check(
self.remove_region_query, variables={
'projectId': project.id, 'regionId': region2.id
}
)

content = _query_check()
self.assertIsNotNone(content['data']['removeProjectRegion']['errors'])
self.assertEquals(
content['data']['removeProjectRegion']['errors'][0]['messages'],
'Region is not associated with Project'
)
2 changes: 0 additions & 2 deletions deep/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
from user import mutation as user_mutation, schema as user_schema
from user_group import mutation as user_group_mutation, schema as user_group_schema
from organization import schema as organization_schema, mutation as organization_mutation
from geo import schema as geo_schema
from organization import schema as organization_schema
from geo import schema as geo_schema, mutations as geo_mutation
from notification import schema as notification_schema, mutation as notification_mutation
from assisted_tagging import schema as assisted_tagging_schema
Expand Down
Loading

0 comments on commit 1bdeb38

Please sign in to comment.