test_user_requestdatadownload_api.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from django.test.utils import override_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_settings(MISAGO_ENABLE_DOWNLOAD_OWN_DATA=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": "You can't have more than one data download request at single time."
  41. },
  42. )
  43. def test_request_download(self):
  44. """request to api succeeds"""
  45. response = self.client.post(self.link)
  46. self.assertEqual(response.status_code, 200)
  47. self.assertEqual(response.json(), {"detail": "ok"})
  48. self.assertTrue(self.user.datadownload_set.exists())