test_context_processors.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from django.test import TestCase
  2. from django.utils import translation
  3. from .. import context_processors
  4. class MockRequest(object):
  5. path_info = '/'
  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(context_processors.momentjs_locale(True), {
  21. 'MOMENTJS_LOCALE_URL': None,
  22. })
  23. with translation.override('en-us'):
  24. self.assertEqual(context_processors.momentjs_locale(True), {
  25. 'MOMENTJS_LOCALE_URL': None,
  26. })
  27. with translation.override('de'):
  28. self.assertEqual(context_processors.momentjs_locale(True), {
  29. 'MOMENTJS_LOCALE_URL': 'misago/momentjs/de.js',
  30. })
  31. with translation.override('pl-de'):
  32. self.assertEqual(context_processors.momentjs_locale(True), {
  33. 'MOMENTJS_LOCALE_URL': 'misago/momentjs/pl.js',
  34. })
  35. class SiteAddressTests(TestCase):
  36. def test_site_address_for_http(self):
  37. """Correct SITE_ADDRESS set for HTTP request"""
  38. mock_request = MockRequest(False, 'somewhere.com')
  39. self.assertEqual(context_processors.site_address(mock_request), {
  40. 'REQUEST_PATH': '/',
  41. 'SITE_ADDRESS': 'http://somewhere.com',
  42. 'SITE_HOST': 'somewhere.com',
  43. 'SITE_PROTOCOL': 'http',
  44. })
  45. def test_site_address_for_https(self):
  46. """Correct SITE_ADDRESS set for HTTPS request"""
  47. mock_request = MockRequest(True, 'somewhere.com')
  48. self.assertEqual(context_processors.site_address(mock_request), {
  49. 'REQUEST_PATH': '/',
  50. 'SITE_ADDRESS': 'https://somewhere.com',
  51. 'SITE_HOST': 'somewhere.com',
  52. 'SITE_PROTOCOL': 'https',
  53. })
  54. class FrontendContextTests(TestCase):
  55. def test_frontend_context(self):
  56. """frontend_context is available in templates"""
  57. mock_request = MockRequest(False, 'somewhere.com')
  58. mock_request.include_frontend_context = True
  59. mock_request.frontend_context = {'someValue': 'Something'}
  60. self.assertEqual(context_processors.frontend_context(mock_request), {
  61. 'frontend_context': {
  62. 'someValue': 'Something'
  63. }
  64. })
  65. mock_request.include_frontend_context = False
  66. self.assertEqual(context_processors.frontend_context(mock_request), {})