api.py 16 KB

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