test_user_requestdatadownload_api.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from ...conf.test import override_dynamic_settings
  2. from ..datadownloads import request_user_data_download
  3. from ..test import AuthenticatedUserTestCase
  4. class UserRequestDataDownload(AuthenticatedUserTestCase):
  5. def setUp(self):
  6. super().setUp()
  7. self.link = "/api/users/%s/request-data-download/" % self.user.pk
  8. def test_request_other_user_download_anonymous(self):
  9. """requests to api fails if user is anonymous"""
  10. self.logout_user()
  11. response = self.client.post(self.link)
  12. self.assertEqual(response.status_code, 403)
  13. self.assertEqual(
  14. response.json(), {"detail": "This action is not available to guests."}
  15. )
  16. def test_request_other_user_download(self):
  17. """requests to api fails if user tries to access other user"""
  18. other_user = self.get_superuser()
  19. link = "/api/users/%s/request-data-download/" % other_user.pk
  20. response = self.client.post(link)
  21. self.assertEqual(response.status_code, 403)
  22. self.assertEqual(
  23. response.json(),
  24. {"detail": "You can't request data downloads for other users."},
  25. )
  26. @override_dynamic_settings(allow_data_downloads=False)
  27. def test_request_download_disabled(self):
  28. """request to api fails if own data downloads are disabled"""
  29. response = self.client.post(self.link)
  30. self.assertEqual(response.status_code, 403)
  31. self.assertEqual(response.json(), {"detail": "You can't download your data."})
  32. def test_request_download_in_progress(self):
  33. """request to api fails if user has already requested data download"""
  34. request_user_data_download(self.user)
  35. response = self.client.post(self.link)
  36. self.assertEqual(response.status_code, 403)
  37. self.assertEqual(
  38. response.json(),
  39. {
  40. "detail": (
  41. "You can't have more than one data download request at a single time."
  42. )
  43. },
  44. )
  45. def test_request_download(self):
  46. """request to api succeeds"""
  47. response = self.client.post(self.link)
  48. self.assertEqual(response.status_code, 200)
  49. self.assertEqual(response.json(), {"detail": "ok"})
  50. self.assertTrue(self.user.datadownload_set.exists())