test_settings.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from django.apps import apps
  2. from django.conf import settings as dj_settings
  3. from django.test import TestCase, override_settings
  4. from misago.conf import defaults
  5. from misago.conf.dbsettings import db_settings
  6. from misago.conf.gateway import settings as gateway
  7. from misago.conf.migrationutils import migrate_settings_group
  8. from misago.core import threadstore
  9. from misago.core.cache import cache
  10. class DBSettingsTests(TestCase):
  11. def test_get_existing_setting(self):
  12. """forum_name is defined"""
  13. self.assertEqual(db_settings.forum_name, 'Misago')
  14. with self.assertRaises(AttributeError):
  15. db_settings.MISAGO_THREADS_PER_PAGE
  16. class GatewaySettingsTests(TestCase):
  17. def tearDown(self):
  18. cache.clear()
  19. threadstore.clear()
  20. def test_get_existing_setting(self):
  21. """forum_name is defined"""
  22. self.assertEqual(gateway.forum_name, db_settings.forum_name)
  23. self.assertEqual(gateway.INSTALLED_APPS, dj_settings.INSTALLED_APPS)
  24. self.assertEqual(gateway.MISAGO_THREADS_PER_PAGE, defaults.MISAGO_THREADS_PER_PAGE)
  25. with self.assertRaises(AttributeError):
  26. gateway.LoremIpsum
  27. @override_settings(MISAGO_THREADS_PER_PAGE=1234)
  28. def test_override_file_setting(self):
  29. """file settings are overrideable"""
  30. self.assertEqual(gateway.MISAGO_THREADS_PER_PAGE, 1234)
  31. def test_setting_public(self):
  32. """get_public_settings returns public settings"""
  33. test_group = {
  34. 'key': 'test_group',
  35. 'name': "Test settings",
  36. 'description': "Those are test settings.",
  37. 'settings': [
  38. {
  39. 'setting': 'fish_name',
  40. 'name': "Fish's name",
  41. 'value': "Public Eric",
  42. 'field_extra': {
  43. 'min_length': 2,
  44. 'max_length': 255,
  45. },
  46. 'is_public': True,
  47. },
  48. {
  49. 'setting': 'private_fish_name',
  50. 'name': "Fish's name",
  51. 'value': "Private Eric",
  52. 'field_extra': {
  53. 'min_length': 2,
  54. 'max_length': 255,
  55. },
  56. 'is_public': False,
  57. },
  58. ],
  59. }
  60. migrate_settings_group(apps, test_group)
  61. self.assertEqual(gateway.fish_name, 'Public Eric')
  62. self.assertEqual(gateway.private_fish_name, 'Private Eric')
  63. public_settings = gateway.get_public_settings().keys()
  64. self.assertIn('fish_name', public_settings)
  65. self.assertNotIn('private_fish_name', public_settings)
  66. def test_setting_lazy(self):
  67. """lazy settings work"""
  68. test_group = {
  69. 'key': 'test_group',
  70. 'name': "Test settings",
  71. 'description': "Those are test settings.",
  72. 'settings': [
  73. {
  74. 'setting': 'fish_name',
  75. 'name': "Fish's name",
  76. 'value': "Greedy Eric",
  77. 'field_extra': {
  78. 'min_length': 2,
  79. 'max_length': 255,
  80. },
  81. 'is_lazy': False,
  82. },
  83. {
  84. 'setting': 'lazy_fish_name',
  85. 'name': "Fish's name",
  86. 'value': "Lazy Eric",
  87. 'field_extra': {
  88. 'min_length': 2,
  89. 'max_length': 255,
  90. },
  91. 'is_lazy': True,
  92. },
  93. {
  94. 'setting': 'lazy_empty_setting',
  95. 'name': "Fish's name",
  96. 'field_extra': {
  97. 'min_length': 2,
  98. 'max_length': 255,
  99. },
  100. 'is_lazy': True,
  101. },
  102. ],
  103. }
  104. migrate_settings_group(apps, test_group)
  105. self.assertTrue(gateway.lazy_fish_name)
  106. self.assertTrue(db_settings.lazy_fish_name)
  107. self.assertTrue(gateway.lazy_fish_name)
  108. self.assertEqual(gateway.get_lazy_setting('lazy_fish_name'), 'Lazy Eric')
  109. self.assertTrue(db_settings.lazy_fish_name)
  110. self.assertEqual(db_settings.get_lazy_setting('lazy_fish_name'), 'Lazy Eric')
  111. self.assertTrue(gateway.lazy_empty_setting is None)
  112. self.assertTrue(db_settings.lazy_empty_setting is None)
  113. with self.assertRaises(ValueError):
  114. db_settings.get_lazy_setting('fish_name')