1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Added initial GraphQL tests

This commit is contained in:
jeremystretch
2021-06-21 13:35:52 -04:00
parent 930ca8d9a1
commit 91d39cc0c0
4 changed files with 78 additions and 6 deletions

View File

@ -1,3 +1,5 @@
import json
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
@ -421,6 +423,49 @@ class APIViewTestCases:
self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT)
self.assertEqual(self._get_queryset().count(), initial_count - 3)
class GraphQLTestCase(APITestCase):
def test_graphql_get_object(self):
url = reverse('graphql')
object_type = self.model._meta.verbose_name.replace(' ', '_')
object_id = self._get_queryset().first().pk
query = f"""
{{
{object_type}(id:{object_id}) {{
id
}}
}}
"""
# Non-authenticated requests should fail
with disable_warnings('django.request'):
self.assertHttpStatus(self.client.post(url, data={'query': query}), status.HTTP_403_FORBIDDEN)
response = self.client.post(url, data={'query': query}, **self.header)
self.assertHttpStatus(response, status.HTTP_200_OK)
data = json.loads(response.content)
self.assertNotIn('errors', data)
def test_graphql_list_objects(self):
url = reverse('graphql')
object_type = self.model._meta.verbose_name_plural.replace(' ', '_')
query = f"""
{{
{object_type} {{
id
}}
}}
"""
# Non-authenticated requests should fail
with disable_warnings('django.request'):
self.assertHttpStatus(self.client.post(url, data={'query': query}), status.HTTP_403_FORBIDDEN)
response = self.client.post(url, data={'query': query}, **self.header)
self.assertHttpStatus(response, status.HTTP_200_OK)
data = json.loads(response.content)
self.assertNotIn('errors', data)
class APIViewTestCase(
GetObjectViewTestCase,
ListObjectsViewTestCase,