test_floodprotection.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from django.urls import reverse
  2. from misago.acl.test import patch_user_acl
  3. from misago.categories.models import Category
  4. from misago.threads import testutils
  5. from misago.users.testutils import AuthenticatedUserTestCase
  6. class FloodProtectionTests(AuthenticatedUserTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.category = Category.objects.get(slug='first-category')
  10. self.thread = testutils.post_thread(category=self.category)
  11. self.post_link = reverse(
  12. 'misago:api:thread-post-list', kwargs={
  13. 'thread_pk': self.thread.pk,
  14. }
  15. )
  16. def test_flood_has_no_showstoppers(self):
  17. """endpoint handles posting interruption"""
  18. response = self.client.post(
  19. self.post_link, data={
  20. 'post': "This is test response!",
  21. }
  22. )
  23. self.assertEqual(response.status_code, 200)
  24. response = self.client.post(
  25. self.post_link, data={
  26. 'post': "This is test response!",
  27. }
  28. )
  29. self.assertEqual(response.status_code, 403)
  30. self.assertEqual(response.json(), {
  31. "detail": "You can't post message so quickly after previous one."
  32. })
  33. @patch_user_acl({"can_omit_flood_protection": True})
  34. def test_user_with_permission_omits_flood_protection(self):
  35. response = self.client.post(
  36. self.post_link, data={
  37. 'post': "This is test response!",
  38. }
  39. )
  40. self.assertEqual(response.status_code, 200)
  41. response = self.client.post(
  42. self.post_link, data={
  43. 'post': "This is test response!",
  44. }
  45. )
  46. self.assertEqual(response.status_code, 200)