test_user_dataexport_api.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from misago.users.dataexport import start_data_export_for_user
  2. from misago.users.testutils import AuthenticatedUserTestCase
  3. class UserStartDataExportApiTests(AuthenticatedUserTestCase):
  4. def setUp(self):
  5. super(UserStartDataExportApiTests, self).setUp()
  6. self.link = '/api/users/%s/start-data-export/' % self.user.pk
  7. def test_start_other_user_export_anonymous(self):
  8. """requests to api fails if user is anonymous"""
  9. self.logout_user()
  10. response = self.client.post(self.link)
  11. self.assertEqual(response.status_code, 403)
  12. self.assertEqual(response.json(), {
  13. 'detail': "This action is not available to guests.",
  14. })
  15. def test_start_other_user_export(self):
  16. """requests to api fails if user tries to access other user"""
  17. other_user = self.get_superuser()
  18. link = '/api/users/%s/start-data-export/' % other_user.pk
  19. response = self.client.post(link)
  20. self.assertEqual(response.status_code, 403)
  21. self.assertEqual(response.json(), {
  22. 'detail': "You can\'t request data export for other users.",
  23. })
  24. def test_start_export_in_progress(self):
  25. """request to api fails if user already has export in progress"""
  26. start_data_export_for_user(self.user)
  27. response = self.client.post(self.link)
  28. self.assertEqual(response.status_code, 403)
  29. self.assertEqual(response.json(), {
  30. 'detail': "You already have an data export in progress.",
  31. })
  32. def test_start_export(self):
  33. """request to api fails if user already has export in progress"""
  34. response = self.client.post(self.link)
  35. self.assertEqual(response.status_code, 200)
  36. self.assertEqual(response.json(), {
  37. 'detail': "ok",
  38. })
  39. self.assertTrue(self.user.dataexport_set.exists())