test_context_processors.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from django.test import TestCase
  2. from django.utils import translation
  3. from misago.core import context_processors
  4. class MockRequest(object):
  5. path = "/"
  6. def __init__(self, secure, host):
  7. self.secure = secure
  8. self.host = host
  9. def is_secure(self):
  10. return self.secure
  11. def get_host(self):
  12. return self.host
  13. class MetaMockRequest(object):
  14. def __init__(self, meta):
  15. self.META = meta
  16. class MomentjsLocaleTests(TestCase):
  17. def test_momentjs_locale(self):
  18. """momentjs_locale adds MOMENTJS_LOCALE_URL to context"""
  19. with translation.override("no-no"):
  20. self.assertEqual(
  21. context_processors.momentjs_locale(True), {"MOMENTJS_LOCALE_URL": None}
  22. )
  23. with translation.override("en-us"):
  24. self.assertEqual(
  25. context_processors.momentjs_locale(True), {"MOMENTJS_LOCALE_URL": None}
  26. )
  27. with translation.override("de"):
  28. self.assertEqual(
  29. context_processors.momentjs_locale(True),
  30. {"MOMENTJS_LOCALE_URL": "misago/momentjs/de.js"},
  31. )
  32. with translation.override("pl-de"):
  33. self.assertEqual(
  34. context_processors.momentjs_locale(True),
  35. {"MOMENTJS_LOCALE_URL": "misago/momentjs/pl.js"},
  36. )
  37. class SiteAddressTests(TestCase):
  38. def test_site_address_for_http(self):
  39. """Correct SITE_ADDRESS set for HTTP request"""
  40. mock_request = MockRequest(False, "somewhere.com")
  41. self.assertEqual(
  42. context_processors.site_address(mock_request),
  43. {
  44. "REQUEST_PATH": "/",
  45. "SITE_ADDRESS": "http://somewhere.com",
  46. "SITE_HOST": "somewhere.com",
  47. "SITE_PROTOCOL": "http",
  48. },
  49. )
  50. def test_site_address_for_https(self):
  51. """Correct SITE_ADDRESS set for HTTPS request"""
  52. mock_request = MockRequest(True, "somewhere.com")
  53. self.assertEqual(
  54. context_processors.site_address(mock_request),
  55. {
  56. "REQUEST_PATH": "/",
  57. "SITE_ADDRESS": "https://somewhere.com",
  58. "SITE_HOST": "somewhere.com",
  59. "SITE_PROTOCOL": "https",
  60. },
  61. )
  62. class FrontendContextTests(TestCase):
  63. def test_frontend_context(self):
  64. """frontend_context is available in templates"""
  65. mock_request = MockRequest(False, "somewhere.com")
  66. mock_request.include_frontend_context = True
  67. mock_request.frontend_context = {"someValue": "Something"}
  68. self.assertEqual(
  69. context_processors.frontend_context(mock_request),
  70. {"frontend_context": {"someValue": "Something"}},
  71. )
  72. mock_request.include_frontend_context = False
  73. self.assertEqual(context_processors.frontend_context(mock_request), {})