api.py 22 KB

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