test_decorators.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. def test_ref_login(self):
  26. """deny_guests decorator redirected guest request to homepage if ref=login"""
  27. response = self.client.post('{}?ref=login'.format(reverse('misago:options')))
  28. self.assertEqual(response.status_code, 302)
  29. self.assertEqual(response['location'], reverse('misago:index'))
  30. class DenyBannedIPTests(UserTestCase):
  31. def test_success(self):
  32. """deny_banned_ips decorator allowed unbanned request"""
  33. Ban.objects.create(
  34. check_type=Ban.IP,
  35. banned_value='83.*',
  36. user_message="Ya got banned!",
  37. )
  38. response = self.client.post(reverse('misago:request-activation'))
  39. self.assertEqual(response.status_code, 200)
  40. def test_fail(self):
  41. """deny_banned_ips decorator denied banned request"""
  42. Ban.objects.create(
  43. check_type=Ban.IP,
  44. banned_value='127.*',
  45. user_message="Ya got banned!",
  46. )
  47. response = self.client.post(reverse('misago:request-activation'))
  48. self.assertContains(response, encode_json_html("<p>Ya got banned!</p>"), status_code=403)