test_decorators.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from django.urls import reverse
  2. from misago.core.utils import encode_json_html
  3. from ..models import BAN_IP, Ban
  4. from ..testutils import UserTestCase
  5. class DenyAuthenticatedTests(UserTestCase):
  6. def test_success(self):
  7. """deny_authenticated decorator allowed guest request"""
  8. response = self.client.post(reverse('misago:request-activation'))
  9. self.assertEqual(response.status_code, 200)
  10. def test_fail(self):
  11. """deny_authenticated decorator denied authenticated request"""
  12. self.login_user(self.get_authenticated_user())
  13. response = self.client.post(reverse('misago:request-activation'))
  14. self.assertEqual(response.status_code, 403)
  15. class DenyGuestsTests(UserTestCase):
  16. def test_success(self):
  17. """deny_guests decorator allowed authenticated request"""
  18. self.login_user(self.get_authenticated_user())
  19. response = self.client.post(reverse('misago:options'))
  20. self.assertEqual(response.status_code, 200)
  21. def test_fail(self):
  22. """deny_guests decorator blocked guest request"""
  23. response = self.client.post(reverse('misago:options'))
  24. self.assertEqual(response.status_code, 403)
  25. class DenyBannedIPTests(UserTestCase):
  26. def test_success(self):
  27. """deny_banned_ips decorator allowed unbanned request"""
  28. Ban.objects.create(
  29. check_type=BAN_IP,
  30. banned_value='83.*',
  31. user_message="Ya got banned!")
  32. response = self.client.post(reverse('misago:request-activation'))
  33. self.assertEqual(response.status_code, 200)
  34. def test_fail(self):
  35. """deny_banned_ips decorator denied banned request"""
  36. Ban.objects.create(
  37. check_type=BAN_IP,
  38. banned_value='127.*',
  39. user_message="Ya got banned!")
  40. response = self.client.post(reverse('misago:request-activation'))
  41. self.assertContains(
  42. response, encode_json_html("<p>Ya got banned!</p>"), status_code=403)