api.py 18 KB

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