You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.2 KiB

2 years ago
2 years ago
2 years ago
  1. from typing import Any, Dict
  2. from rest_framework import status
  3. from rest_framework.test import APITestCase
  4. class CRUDMixin(APITestCase):
  5. url = ""
  6. data: Dict[str, Any] = {}
  7. def assert_fetch(self, user=None, expected=status.HTTP_403_FORBIDDEN):
  8. if user:
  9. self.client.force_login(user)
  10. response = self.client.get(self.url)
  11. self.assertEqual(response.status_code, expected)
  12. return response
  13. def assert_create(self, user=None, expected=status.HTTP_403_FORBIDDEN):
  14. if user:
  15. self.client.force_login(user)
  16. response = self.client.post(self.url, data=self.data, format="json")
  17. self.assertEqual(response.status_code, expected)
  18. return response
  19. def assert_update(self, user=None, expected=status.HTTP_403_FORBIDDEN):
  20. if user:
  21. self.client.force_login(user)
  22. response = self.client.patch(self.url, data=self.data, format="json")
  23. self.assertEqual(response.status_code, expected)
  24. return response
  25. def assert_delete(self, user=None, expected=status.HTTP_403_FORBIDDEN):
  26. if user:
  27. self.client.force_login(user)
  28. response = self.client.delete(self.url)
  29. self.assertEqual(response.status_code, expected)