test_context_processors.py 2.4 KB

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