test_rest_permissions.py 2.6 KB

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