test_decorators.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from django.urls import reverse
  2. from misago.core.utils import encode_json_html
  3. from misago.users.models import Ban
  4. from misago.users.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. )
  33. response = self.client.post(reverse('misago:request-activation'))
  34. self.assertEqual(response.status_code, 200)
  35. def test_fail(self):
  36. """deny_banned_ips decorator denied banned request"""
  37. Ban.objects.create(
  38. check_type=Ban.IP,
  39. banned_value='127.*',
  40. user_message="Ya got banned!"
  41. )
  42. response = self.client.post(reverse('misago:request-activation'))
  43. self.assertContains(
  44. response, encode_json_html("<p>Ya got banned!</p>"), status_code=403)