test_context_processors.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from django.test import TestCase
  2. from django.utils import translation
  3. from .. import context_processors
  4. class MockRequest:
  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:
  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 FrontendContextTests(TestCase):
  38. def test_frontend_context(self):
  39. """frontend_context is available in templates"""
  40. mock_request = MockRequest(False, "somewhere.com")
  41. mock_request.include_frontend_context = True
  42. mock_request.frontend_context = {"someValue": "Something"}
  43. self.assertEqual(
  44. context_processors.frontend_context(mock_request),
  45. {"frontend_context": {"someValue": "Something"}},
  46. )
  47. mock_request.include_frontend_context = False
  48. self.assertEqual(context_processors.frontend_context(mock_request), {})