test_rest_permissions.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from django.urls import reverse
  2. from ..models import Ban
  3. from ..testutils import UserTestCase
  4. class UnbannedOnlyTests(UserTestCase):
  5. def setUp(self):
  6. self.user = self.get_authenticated_user()
  7. def test_api_allows_guests(self):
  8. """policy allows guests"""
  9. response = self.client.post(
  10. reverse('misago:api:send-password-form'), data={
  11. 'email': self.user.email
  12. })
  13. self.assertEqual(response.status_code, 200)
  14. def test_api_allows_authenticated(self):
  15. """policy allows authenticated"""
  16. self.login_user(self.user)
  17. response = self.client.post(
  18. reverse('misago:api:send-password-form'), data={
  19. 'email': self.user.email
  20. })
  21. self.assertEqual(response.status_code, 200)
  22. def test_api_blocks_banned(self):
  23. """policy blocked banned ip"""
  24. Ban.objects.create(
  25. check_type=Ban.BAN_IP,
  26. banned_value='127.*',
  27. user_message='Ya got banned!'
  28. )
  29. response = self.client.post(
  30. reverse('misago:api:send-password-form'), data={
  31. 'email': self.user.email
  32. })
  33. self.assertEqual(response.status_code, 403)
  34. class UnbannedAnonOnlyTests(UserTestCase):
  35. def setUp(self):
  36. self.user = self.get_authenticated_user()
  37. def test_api_allows_guests(self):
  38. """policy allows guests"""
  39. self.user.requires_activation = 1
  40. self.user.save()
  41. response = self.client.post(
  42. reverse('misago:api:send-activation'), data={
  43. 'email': self.user.email
  44. })
  45. self.assertEqual(response.status_code, 200)
  46. def test_api_allows_authenticated(self):
  47. """policy blocks authenticated"""
  48. self.login_user(self.user)
  49. response = self.client.post(
  50. reverse('misago:api:send-activation'), data={
  51. 'email': self.user.email
  52. })
  53. self.assertEqual(response.status_code, 403)
  54. def test_api_blocks_banned(self):
  55. """policy blocked banned ip"""
  56. Ban.objects.create(
  57. check_type=Ban.BAN_IP,
  58. banned_value='127.*',
  59. user_message='Ya got banned!'
  60. )
  61. response = self.client.post(
  62. reverse('misago:api:send-activation'), data={
  63. 'email': self.user.email
  64. })
  65. self.assertEqual(response.status_code, 403)