api.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. import inspect
  2. import json
  3. import strawberry_django
  4. from django.conf import settings
  5. from django.contrib.auth import get_user_model
  6. from django.contrib.contenttypes.models import ContentType
  7. from django.urls import reverse
  8. from django.test import override_settings
  9. from rest_framework import status
  10. from rest_framework.test import APIClient
  11. from core.models import ObjectType
  12. from extras.choices import ObjectChangeActionChoices
  13. from extras.models import ObjectChange
  14. from users.models import ObjectPermission, Token
  15. from utilities.api import get_graphql_type_for_model
  16. from .base import ModelTestCase
  17. from .utils import disable_warnings
  18. from ipam.graphql.types import IPAddressFamilyType
  19. from strawberry.field import StrawberryField
  20. from strawberry.lazy_type import LazyType
  21. from strawberry.type import StrawberryList, StrawberryOptional
  22. from strawberry.union import StrawberryUnion
  23. __all__ = (
  24. 'APITestCase',
  25. 'APIViewTestCases',
  26. )
  27. User = get_user_model()
  28. #
  29. # REST/GraphQL API Tests
  30. #
  31. class APITestCase(ModelTestCase):
  32. """
  33. Base test case for API requests.
  34. client_class: Test client class
  35. view_namespace: Namespace for API views. If None, the model's app_label will be used.
  36. """
  37. client_class = APIClient
  38. view_namespace = None
  39. def setUp(self):
  40. """
  41. Create a user and token for API calls.
  42. """
  43. # Create the test user and assign permissions
  44. self.user = User.objects.create_user(username='testuser')
  45. self.add_permissions(*self.user_permissions)
  46. self.token = Token.objects.create(user=self.user)
  47. self.header = {'HTTP_AUTHORIZATION': f'Token {self.token.key}'}
  48. def _get_view_namespace(self):
  49. return f'{self.view_namespace or self.model._meta.app_label}-api'
  50. def _get_detail_url(self, instance):
  51. viewname = f'{self._get_view_namespace()}:{instance._meta.model_name}-detail'
  52. return reverse(viewname, kwargs={'pk': instance.pk})
  53. def _get_list_url(self):
  54. viewname = f'{self._get_view_namespace()}:{self.model._meta.model_name}-list'
  55. return reverse(viewname)
  56. class APIViewTestCases:
  57. class GetObjectViewTestCase(APITestCase):
  58. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'], LOGIN_REQUIRED=False)
  59. def test_get_object_anonymous(self):
  60. """
  61. GET a single object as an unauthenticated user.
  62. """
  63. url = self._get_detail_url(self._get_queryset().first())
  64. if (self.model._meta.app_label, self.model._meta.model_name) in settings.EXEMPT_EXCLUDE_MODELS:
  65. # Models listed in EXEMPT_EXCLUDE_MODELS should not be accessible to anonymous users
  66. with disable_warnings('django.request'):
  67. self.assertHttpStatus(self.client.get(url, **self.header), status.HTTP_403_FORBIDDEN)
  68. else:
  69. response = self.client.get(url, **self.header)
  70. self.assertHttpStatus(response, status.HTTP_200_OK)
  71. def test_get_object_without_permission(self):
  72. """
  73. GET a single object as an authenticated user without the required permission.
  74. """
  75. url = self._get_detail_url(self._get_queryset().first())
  76. # Try GET without permission
  77. with disable_warnings('django.request'):
  78. self.assertHttpStatus(self.client.get(url, **self.header), status.HTTP_403_FORBIDDEN)
  79. def test_get_object(self):
  80. """
  81. GET a single object as an authenticated user with permission to view the object.
  82. """
  83. self.assertGreaterEqual(self._get_queryset().count(), 2,
  84. f"Test requires the creation of at least two {self.model} instances")
  85. instance1, instance2 = self._get_queryset()[:2]
  86. # Add object-level permission
  87. obj_perm = ObjectPermission(
  88. name='Test permission',
  89. constraints={'pk': instance1.pk},
  90. actions=['view']
  91. )
  92. obj_perm.save()
  93. obj_perm.users.add(self.user)
  94. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  95. # Try GET to permitted object
  96. url = self._get_detail_url(instance1)
  97. self.assertHttpStatus(self.client.get(url, **self.header), status.HTTP_200_OK)
  98. # Try GET to non-permitted object
  99. url = self._get_detail_url(instance2)
  100. self.assertHttpStatus(self.client.get(url, **self.header), status.HTTP_404_NOT_FOUND)
  101. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
  102. def test_options_object(self):
  103. """
  104. Make an OPTIONS request for a single object.
  105. """
  106. url = self._get_detail_url(self._get_queryset().first())
  107. response = self.client.options(url, **self.header)
  108. self.assertHttpStatus(response, status.HTTP_200_OK)
  109. class ListObjectsViewTestCase(APITestCase):
  110. brief_fields = []
  111. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'], LOGIN_REQUIRED=False)
  112. def test_list_objects_anonymous(self):
  113. """
  114. GET a list of objects as an unauthenticated user.
  115. """
  116. url = self._get_list_url()
  117. if (self.model._meta.app_label, self.model._meta.model_name) in settings.EXEMPT_EXCLUDE_MODELS:
  118. # Models listed in EXEMPT_EXCLUDE_MODELS should not be accessible to anonymous users
  119. with disable_warnings('django.request'):
  120. self.assertHttpStatus(self.client.get(url, **self.header), status.HTTP_403_FORBIDDEN)
  121. else:
  122. response = self.client.get(url, **self.header)
  123. self.assertHttpStatus(response, status.HTTP_200_OK)
  124. self.assertEqual(len(response.data['results']), self._get_queryset().count())
  125. def test_list_objects_brief(self):
  126. """
  127. GET a list of objects using the "brief" parameter.
  128. """
  129. self.add_permissions(f'{self.model._meta.app_label}.view_{self.model._meta.model_name}')
  130. url = f'{self._get_list_url()}?brief=1'
  131. response = self.client.get(url, **self.header)
  132. self.assertEqual(len(response.data['results']), self._get_queryset().count())
  133. self.assertEqual(sorted(response.data['results'][0]), self.brief_fields)
  134. def test_list_objects_without_permission(self):
  135. """
  136. GET a list of objects as an authenticated user without the required permission.
  137. """
  138. url = self._get_list_url()
  139. # Try GET without permission
  140. with disable_warnings('django.request'):
  141. self.assertHttpStatus(self.client.get(url, **self.header), status.HTTP_403_FORBIDDEN)
  142. def test_list_objects(self):
  143. """
  144. GET a list of objects as an authenticated user with permission to view the objects.
  145. """
  146. self.assertGreaterEqual(self._get_queryset().count(), 3,
  147. f"Test requires the creation of at least three {self.model} instances")
  148. instance1, instance2 = self._get_queryset()[:2]
  149. # Add object-level permission
  150. obj_perm = ObjectPermission(
  151. name='Test permission',
  152. constraints={'pk__in': [instance1.pk, instance2.pk]},
  153. actions=['view']
  154. )
  155. obj_perm.save()
  156. obj_perm.users.add(self.user)
  157. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  158. # Try GET to permitted objects
  159. response = self.client.get(self._get_list_url(), **self.header)
  160. self.assertHttpStatus(response, status.HTTP_200_OK)
  161. self.assertEqual(len(response.data['results']), 2)
  162. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
  163. def test_options_objects(self):
  164. """
  165. Make an OPTIONS request for a list endpoint.
  166. """
  167. response = self.client.options(self._get_list_url(), **self.header)
  168. self.assertHttpStatus(response, status.HTTP_200_OK)
  169. class CreateObjectViewTestCase(APITestCase):
  170. create_data = []
  171. validation_excluded_fields = []
  172. def test_create_object_without_permission(self):
  173. """
  174. POST a single object without permission.
  175. """
  176. url = self._get_list_url()
  177. # Try POST without permission
  178. with disable_warnings('django.request'):
  179. response = self.client.post(url, self.create_data[0], format='json', **self.header)
  180. self.assertHttpStatus(response, status.HTTP_403_FORBIDDEN)
  181. def test_create_object(self):
  182. """
  183. POST a single object with permission.
  184. """
  185. # Add object-level permission
  186. obj_perm = ObjectPermission(
  187. name='Test permission',
  188. actions=['add']
  189. )
  190. obj_perm.save()
  191. obj_perm.users.add(self.user)
  192. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  193. initial_count = self._get_queryset().count()
  194. response = self.client.post(self._get_list_url(), self.create_data[0], format='json', **self.header)
  195. self.assertHttpStatus(response, status.HTTP_201_CREATED)
  196. self.assertEqual(self._get_queryset().count(), initial_count + 1)
  197. instance = self._get_queryset().get(pk=response.data['id'])
  198. self.assertInstanceEqual(
  199. instance,
  200. self.create_data[0],
  201. exclude=self.validation_excluded_fields,
  202. api=True
  203. )
  204. # Verify ObjectChange creation
  205. if hasattr(self.model, 'to_objectchange'):
  206. objectchanges = ObjectChange.objects.filter(
  207. changed_object_type=ContentType.objects.get_for_model(instance),
  208. changed_object_id=instance.pk
  209. )
  210. self.assertEqual(len(objectchanges), 1)
  211. self.assertEqual(objectchanges[0].action, ObjectChangeActionChoices.ACTION_CREATE)
  212. def test_bulk_create_objects(self):
  213. """
  214. POST a set of objects in a single request.
  215. """
  216. # Add object-level permission
  217. obj_perm = ObjectPermission(
  218. name='Test permission',
  219. actions=['add']
  220. )
  221. obj_perm.save()
  222. obj_perm.users.add(self.user)
  223. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  224. initial_count = self._get_queryset().count()
  225. response = self.client.post(self._get_list_url(), self.create_data, format='json', **self.header)
  226. self.assertHttpStatus(response, status.HTTP_201_CREATED)
  227. self.assertEqual(len(response.data), len(self.create_data))
  228. self.assertEqual(self._get_queryset().count(), initial_count + len(self.create_data))
  229. for i, obj in enumerate(response.data):
  230. for field in self.create_data[i]:
  231. if field not in self.validation_excluded_fields:
  232. self.assertIn(field, obj, f"Bulk create field '{field}' missing from object {i} in response")
  233. for i, obj in enumerate(response.data):
  234. self.assertInstanceEqual(
  235. self._get_queryset().get(pk=obj['id']),
  236. self.create_data[i],
  237. exclude=self.validation_excluded_fields,
  238. api=True
  239. )
  240. class UpdateObjectViewTestCase(APITestCase):
  241. update_data = {}
  242. bulk_update_data = None
  243. validation_excluded_fields = []
  244. def test_update_object_without_permission(self):
  245. """
  246. PATCH a single object without permission.
  247. """
  248. url = self._get_detail_url(self._get_queryset().first())
  249. update_data = self.update_data or getattr(self, 'create_data')[0]
  250. # Try PATCH without permission
  251. with disable_warnings('django.request'):
  252. response = self.client.patch(url, update_data, format='json', **self.header)
  253. self.assertHttpStatus(response, status.HTTP_403_FORBIDDEN)
  254. def test_update_object(self):
  255. """
  256. PATCH a single object identified by its numeric ID.
  257. """
  258. instance = self._get_queryset().first()
  259. url = self._get_detail_url(instance)
  260. update_data = self.update_data or getattr(self, 'create_data')[0]
  261. # Add object-level permission
  262. obj_perm = ObjectPermission(
  263. name='Test permission',
  264. actions=['change']
  265. )
  266. obj_perm.save()
  267. obj_perm.users.add(self.user)
  268. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  269. response = self.client.patch(url, update_data, format='json', **self.header)
  270. self.assertHttpStatus(response, status.HTTP_200_OK)
  271. instance.refresh_from_db()
  272. self.assertInstanceEqual(
  273. instance,
  274. update_data,
  275. exclude=self.validation_excluded_fields,
  276. api=True
  277. )
  278. # Verify ObjectChange creation
  279. if hasattr(self.model, 'to_objectchange'):
  280. objectchanges = ObjectChange.objects.filter(
  281. changed_object_type=ContentType.objects.get_for_model(instance),
  282. changed_object_id=instance.pk
  283. )
  284. self.assertEqual(len(objectchanges), 1)
  285. self.assertEqual(objectchanges[0].action, ObjectChangeActionChoices.ACTION_UPDATE)
  286. def test_bulk_update_objects(self):
  287. """
  288. PATCH a set of objects in a single request.
  289. """
  290. if self.bulk_update_data is None:
  291. self.skipTest("Bulk update data not set")
  292. # Add object-level permission
  293. obj_perm = ObjectPermission(
  294. name='Test permission',
  295. actions=['change']
  296. )
  297. obj_perm.save()
  298. obj_perm.users.add(self.user)
  299. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  300. id_list = list(self._get_queryset().values_list('id', flat=True)[:3])
  301. self.assertEqual(len(id_list), 3, "Insufficient number of objects to test bulk update")
  302. data = [
  303. {'id': id, **self.bulk_update_data} for id in id_list
  304. ]
  305. response = self.client.patch(self._get_list_url(), data, format='json', **self.header)
  306. self.assertHttpStatus(response, status.HTTP_200_OK)
  307. for i, obj in enumerate(response.data):
  308. for field in self.bulk_update_data:
  309. self.assertIn(field, obj, f"Bulk update field '{field}' missing from object {i} in response")
  310. for instance in self._get_queryset().filter(pk__in=id_list):
  311. self.assertInstanceEqual(instance, self.bulk_update_data, api=True)
  312. class DeleteObjectViewTestCase(APITestCase):
  313. def test_delete_object_without_permission(self):
  314. """
  315. DELETE a single object without permission.
  316. """
  317. url = self._get_detail_url(self._get_queryset().first())
  318. # Try DELETE without permission
  319. with disable_warnings('django.request'):
  320. response = self.client.delete(url, **self.header)
  321. self.assertHttpStatus(response, status.HTTP_403_FORBIDDEN)
  322. def test_delete_object(self):
  323. """
  324. DELETE a single object identified by its numeric ID.
  325. """
  326. instance = self._get_queryset().first()
  327. url = self._get_detail_url(instance)
  328. # Add object-level permission
  329. obj_perm = ObjectPermission(
  330. name='Test permission',
  331. actions=['delete']
  332. )
  333. obj_perm.save()
  334. obj_perm.users.add(self.user)
  335. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  336. response = self.client.delete(url, **self.header)
  337. self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT)
  338. self.assertFalse(self._get_queryset().filter(pk=instance.pk).exists())
  339. # Verify ObjectChange creation
  340. if hasattr(self.model, 'to_objectchange'):
  341. objectchanges = ObjectChange.objects.filter(
  342. changed_object_type=ContentType.objects.get_for_model(instance),
  343. changed_object_id=instance.pk
  344. )
  345. self.assertEqual(len(objectchanges), 1)
  346. self.assertEqual(objectchanges[0].action, ObjectChangeActionChoices.ACTION_DELETE)
  347. def test_bulk_delete_objects(self):
  348. """
  349. DELETE a set of objects in a single request.
  350. """
  351. # Add object-level permission
  352. obj_perm = ObjectPermission(
  353. name='Test permission',
  354. actions=['delete']
  355. )
  356. obj_perm.save()
  357. obj_perm.users.add(self.user)
  358. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  359. # Target the three most recently created objects to avoid triggering recursive deletions
  360. # (e.g. with MPTT objects)
  361. id_list = list(self._get_queryset().order_by('-id').values_list('id', flat=True)[:3])
  362. self.assertEqual(len(id_list), 3, "Insufficient number of objects to test bulk deletion")
  363. data = [{"id": id} for id in id_list]
  364. initial_count = self._get_queryset().count()
  365. response = self.client.delete(self._get_list_url(), data, format='json', **self.header)
  366. self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT)
  367. self.assertEqual(self._get_queryset().count(), initial_count - 3)
  368. class GraphQLTestCase(APITestCase):
  369. def _get_graphql_base_name(self):
  370. """
  371. Return graphql_base_name, if set. Otherwise, construct the base name for the query
  372. field from the model's verbose name.
  373. """
  374. base_name = self.model._meta.verbose_name.lower().replace(' ', '_')
  375. return getattr(self, 'graphql_base_name', base_name)
  376. def _build_query_with_filter(self, name, filter_string):
  377. """
  378. Called by either _build_query or _build_filtered_query - construct the actual
  379. query given a name and filter string
  380. """
  381. type_class = get_graphql_type_for_model(self.model)
  382. # Compile list of fields to include
  383. fields_string = ''
  384. file_fields = (strawberry_django.fields.types.DjangoFileType, strawberry_django.fields.types.DjangoImageType)
  385. for field in type_class.__strawberry_definition__.fields:
  386. if (
  387. field.type in file_fields or (
  388. type(field.type) is StrawberryOptional and field.type.of_type in file_fields
  389. )
  390. ):
  391. # image / file fields nullable or not...
  392. fields_string += f'{field.name} {{ name }}\n'
  393. elif type(field.type) is StrawberryList and type(field.type.of_type) is LazyType:
  394. # List of related objects (queryset)
  395. fields_string += f'{field.name} {{ id }}\n'
  396. elif type(field.type) is StrawberryList and type(field.type.of_type) is StrawberryUnion:
  397. # this would require a fragment query
  398. continue
  399. elif type(field.type) is StrawberryUnion:
  400. # this would require a fragment query
  401. continue
  402. elif type(field.type) is StrawberryOptional and type(field.type.of_type) is StrawberryUnion:
  403. # this would require a fragment query
  404. continue
  405. elif type(field.type) is StrawberryOptional and type(field.type.of_type) is LazyType:
  406. fields_string += f'{field.name} {{ id }}\n'
  407. elif hasattr(field, 'is_relation') and field.is_relation:
  408. # Note: StrawberryField types do not have is_relation
  409. fields_string += f'{field.name} {{ id }}\n'
  410. elif inspect.isclass(field.type) and issubclass(field.type, IPAddressFamilyType):
  411. fields_string += f'{field.name} {{ value, label }}\n'
  412. else:
  413. fields_string += f'{field.name}\n'
  414. query = f"""
  415. {{
  416. {name}{filter_string} {{
  417. {fields_string}
  418. }}
  419. }}
  420. """
  421. return query
  422. def _build_filtered_query(self, name, **filters):
  423. """
  424. Create a filtered query: i.e. device_list(filters: {name: {i_contains: "akron"}}){.
  425. """
  426. # TODO: This should be extended to support AND, OR multi-lookups
  427. if filters:
  428. for field_name, params in filters.items():
  429. lookup = params['lookup']
  430. value = params['value']
  431. if lookup:
  432. query = f'{{{lookup}: "{value}"}}'
  433. filter_string = f'{field_name}: {query}'
  434. else:
  435. filter_string = f'{field_name}: "{value}"'
  436. filter_string = f'(filters: {{{filter_string}}})'
  437. else:
  438. filter_string = ''
  439. return self._build_query_with_filter(name, filter_string)
  440. def _build_query(self, name, **filters):
  441. """
  442. Create a normal query - unfiltered or with a string query: i.e. site(name: "aaa"){.
  443. """
  444. if filters:
  445. filter_string = ', '.join(f'{k}:{v}' for k, v in filters.items())
  446. filter_string = f'({filter_string})'
  447. else:
  448. filter_string = ''
  449. return self._build_query_with_filter(name, filter_string)
  450. @override_settings(LOGIN_REQUIRED=True)
  451. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*', 'auth.user'])
  452. def test_graphql_get_object(self):
  453. url = reverse('graphql')
  454. field_name = self._get_graphql_base_name()
  455. object_id = self._get_queryset().first().pk
  456. query = self._build_query(field_name, id=object_id)
  457. # Non-authenticated requests should fail
  458. with disable_warnings('django.request'):
  459. header = {
  460. 'HTTP_ACCEPT': 'application/json',
  461. }
  462. self.assertHttpStatus(self.client.post(url, data={'query': query}, format="json", **header), status.HTTP_403_FORBIDDEN)
  463. # Add object-level permission
  464. obj_perm = ObjectPermission(
  465. name='Test permission',
  466. actions=['view']
  467. )
  468. obj_perm.save()
  469. obj_perm.users.add(self.user)
  470. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  471. response = self.client.post(url, data={'query': query}, format="json", **self.header)
  472. self.assertHttpStatus(response, status.HTTP_200_OK)
  473. data = json.loads(response.content)
  474. self.assertNotIn('errors', data)
  475. @override_settings(LOGIN_REQUIRED=True)
  476. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*', 'auth.user'])
  477. def test_graphql_list_objects(self):
  478. url = reverse('graphql')
  479. field_name = f'{self._get_graphql_base_name()}_list'
  480. query = self._build_query(field_name)
  481. # Non-authenticated requests should fail
  482. with disable_warnings('django.request'):
  483. header = {
  484. 'HTTP_ACCEPT': 'application/json',
  485. }
  486. self.assertHttpStatus(self.client.post(url, data={'query': query}, format="json", **header), status.HTTP_403_FORBIDDEN)
  487. # Add object-level permission
  488. obj_perm = ObjectPermission(
  489. name='Test permission',
  490. actions=['view']
  491. )
  492. obj_perm.save()
  493. obj_perm.users.add(self.user)
  494. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  495. response = self.client.post(url, data={'query': query}, format="json", **self.header)
  496. self.assertHttpStatus(response, status.HTTP_200_OK)
  497. data = json.loads(response.content)
  498. self.assertNotIn('errors', data)
  499. self.assertGreater(len(data['data'][field_name]), 0)
  500. @override_settings(LOGIN_REQUIRED=True)
  501. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*', 'auth.user'])
  502. def test_graphql_filter_objects(self):
  503. if not hasattr(self, 'graphql_filter'):
  504. return
  505. url = reverse('graphql')
  506. field_name = f'{self._get_graphql_base_name()}_list'
  507. query = self._build_filtered_query(field_name, **self.graphql_filter)
  508. # Add object-level permission
  509. obj_perm = ObjectPermission(
  510. name='Test permission',
  511. actions=['view']
  512. )
  513. obj_perm.save()
  514. obj_perm.users.add(self.user)
  515. obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
  516. response = self.client.post(url, data={'query': query}, format="json", **self.header)
  517. self.assertHttpStatus(response, status.HTTP_200_OK)
  518. data = json.loads(response.content)
  519. self.assertNotIn('errors', data)
  520. self.assertGreater(len(data['data'][field_name]), 0)
  521. class APIViewTestCase(
  522. GetObjectViewTestCase,
  523. ListObjectsViewTestCase,
  524. CreateObjectViewTestCase,
  525. UpdateObjectViewTestCase,
  526. DeleteObjectViewTestCase,
  527. GraphQLTestCase
  528. ):
  529. pass