test_decorators.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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(check_type=Ban.IP, banned_value='83.*', user_message="Ya got banned!")
  29. response = self.client.post(reverse('misago:request-activation'))
  30. self.assertEqual(response.status_code, 200)
  31. def test_fail(self):
  32. """deny_banned_ips decorator denied banned request"""
  33. Ban.objects.create(check_type=Ban.IP, banned_value='127.*', user_message="Ya got banned!")
  34. response = self.client.post(reverse('misago:request-activation'))
  35. self.assertContains(response, encode_json_html("<p>Ya got banned!</p>"), status_code=403)