mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-07 16:18:16 -06:00
Clean up test case
This commit is contained in:
parent
39ac70b036
commit
ee030f8c1d
@ -2,11 +2,11 @@ import json
|
|||||||
|
|
||||||
from django.test import override_settings
|
from django.test import override_settings
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from rest_framework import status
|
||||||
|
|
||||||
from core.models import ObjectType
|
from core.models import ObjectType
|
||||||
from rest_framework import status
|
from dcim.models import Site, Location
|
||||||
from users.models import ObjectPermission, Token
|
from users.models import ObjectPermission
|
||||||
from users.models import User
|
|
||||||
from utilities.testing import disable_warnings, APITestCase, TestCase
|
from utilities.testing import disable_warnings, APITestCase, TestCase
|
||||||
|
|
||||||
|
|
||||||
@ -47,14 +47,16 @@ class GraphQLAPITestCase(APITestCase):
|
|||||||
@override_settings(LOGIN_REQUIRED=True)
|
@override_settings(LOGIN_REQUIRED=True)
|
||||||
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*', 'auth.user'])
|
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*', 'auth.user'])
|
||||||
def test_graphql_filter_objects(self):
|
def test_graphql_filter_objects(self):
|
||||||
from dcim.models import Site, Location
|
"""
|
||||||
|
Test the operation of filters for GraphQL API requests.
|
||||||
site = Site.objects.create(name='Site A', slug='site-a')
|
"""
|
||||||
location = Location.objects.create(site=site, name='Location A1', slug='location-a1')
|
sites = (
|
||||||
|
Site(name='Site 1', slug='site-1'),
|
||||||
url = reverse('graphql')
|
Site(name='Site 2', slug='site-2'),
|
||||||
field_name = 'location_list'
|
)
|
||||||
query = '{location_list(filters: {site_id: "' + str(site.id) + '"}) {id site {id}}}'
|
Site.objects.bulk_create(sites)
|
||||||
|
Location.objects.create(site=sites[0], name='Location 1', slug='location-1'),
|
||||||
|
Location.objects.create(site=sites[1], name='Location 2', slug='location-2'),
|
||||||
|
|
||||||
# Add object-level permission
|
# Add object-level permission
|
||||||
obj_perm = ObjectPermission(
|
obj_perm = ObjectPermission(
|
||||||
@ -65,15 +67,18 @@ class GraphQLAPITestCase(APITestCase):
|
|||||||
obj_perm.users.add(self.user)
|
obj_perm.users.add(self.user)
|
||||||
obj_perm.object_types.add(ObjectType.objects.get_for_model(Location))
|
obj_perm.object_types.add(ObjectType.objects.get_for_model(Location))
|
||||||
|
|
||||||
|
# A valid request should return the filtered list
|
||||||
|
url = reverse('graphql')
|
||||||
|
query = '{location_list(filters: {site_id: "' + str(sites[0].pk) + '"}) {id site {id}}}'
|
||||||
response = self.client.post(url, data={'query': query}, format="json", **self.header)
|
response = self.client.post(url, data={'query': query}, format="json", **self.header)
|
||||||
self.assertHttpStatus(response, status.HTTP_200_OK)
|
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||||
data = json.loads(response.content)
|
data = json.loads(response.content)
|
||||||
self.assertNotIn('errors', data)
|
self.assertNotIn('errors', data)
|
||||||
self.assertGreater(len(data['data']['location_list']), 0)
|
self.assertEqual(len(data['data']['location_list']), 1)
|
||||||
|
|
||||||
query = '{location_list(filters: {site_id: "' + str(site.id + 1234) + '"}) {id site {id}}}'
|
# An invalid request should return an empty list
|
||||||
|
query = '{location_list(filters: {site_id: "99999"}) {id site {id}}}' # Invalid site ID
|
||||||
response = self.client.post(url, data={'query': query}, format="json", **self.header)
|
response = self.client.post(url, data={'query': query}, format="json", **self.header)
|
||||||
|
|
||||||
self.assertHttpStatus(response, status.HTTP_200_OK)
|
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||||
data = json.loads(response.content)
|
data = json.loads(response.content)
|
||||||
self.assertEqual(len(data['data']['location_list']), 0)
|
self.assertEqual(len(data['data']['location_list']), 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user