test_settings_loader.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. from os.path import dirname, join, abspath
  3. from unittest.mock import patch
  4. from searx.testing import SearxTestCase
  5. from searx.exceptions import SearxSettingsException
  6. from searx import settings_loader
  7. test_dir = abspath(dirname(__file__))
  8. class TestLoad(SearxTestCase):
  9. def test_load_zero(self):
  10. with self.assertRaises(SearxSettingsException):
  11. settings_loader.load_yaml('/dev/zero')
  12. with self.assertRaises(SearxSettingsException):
  13. settings_loader.load_yaml(join(test_dir, '/settings/syntaxerror_settings.yml'))
  14. with self.assertRaises(SearxSettingsException):
  15. settings_loader.load_yaml(join(test_dir, '/settings/empty_settings.yml'))
  16. def test_check_settings_yml(self):
  17. self.assertIsNone(settings_loader.check_settings_yml('/dev/zero'))
  18. bad_settings_path = join(test_dir, 'settings/syntaxerror_settings.yml')
  19. self.assertEqual(settings_loader.check_settings_yml(bad_settings_path), bad_settings_path)
  20. class TestDefaultSettings(SearxTestCase):
  21. def test_load(self):
  22. settings, msg = settings_loader.load_settings(load_user_setttings=False)
  23. self.assertTrue(msg.startswith('load the default settings from'))
  24. self.assertFalse(settings['general']['debug'])
  25. self.assertTrue(isinstance(settings['general']['instance_name'], str))
  26. self.assertEqual(settings['server']['secret_key'], "ultrasecretkey")
  27. self.assertTrue(isinstance(settings['server']['port'], int))
  28. self.assertTrue(isinstance(settings['server']['bind_address'], str))
  29. self.assertTrue(isinstance(settings['engines'], list))
  30. self.assertTrue(isinstance(settings['locales'], dict))
  31. self.assertTrue(isinstance(settings['doi_resolvers'], dict))
  32. self.assertTrue(isinstance(settings['default_doi_resolver'], str))
  33. class TestUserSettings(SearxTestCase):
  34. def test_is_use_default_settings(self):
  35. self.assertFalse(settings_loader.is_use_default_settings({}))
  36. self.assertTrue(settings_loader.is_use_default_settings({'use_default_settings': True}))
  37. self.assertTrue(settings_loader.is_use_default_settings({'use_default_settings': {}}))
  38. with self.assertRaises(ValueError):
  39. self.assertFalse(settings_loader.is_use_default_settings({'use_default_settings': 1}))
  40. with self.assertRaises(ValueError):
  41. self.assertFalse(settings_loader.is_use_default_settings({'use_default_settings': 0}))
  42. def test_user_settings_not_found(self):
  43. with patch.dict(settings_loader.environ,
  44. {'SEARX_SETTINGS_PATH': '/dev/null'}):
  45. settings, msg = settings_loader.load_settings()
  46. self.assertTrue(msg.startswith('load the default settings from'))
  47. self.assertEqual(settings['server']['secret_key'], "ultrasecretkey")
  48. def test_user_settings(self):
  49. with patch.dict(settings_loader.environ,
  50. {'SEARX_SETTINGS_PATH': join(test_dir, 'settings/user_settings_simple.yml')}):
  51. settings, msg = settings_loader.load_settings()
  52. self.assertTrue(msg.startswith('merge the default settings'))
  53. self.assertEqual(settings['server']['secret_key'], "user_secret_key")
  54. self.assertEqual(settings['server']['default_http_headers']['Custom-Header'], "Custom-Value")
  55. def test_user_settings_remove(self):
  56. with patch.dict(settings_loader.environ,
  57. {'SEARX_SETTINGS_PATH': join(test_dir, 'settings/user_settings_remove.yml')}):
  58. settings, msg = settings_loader.load_settings()
  59. self.assertTrue(msg.startswith('merge the default settings'))
  60. self.assertEqual(settings['server']['secret_key'], "user_secret_key")
  61. self.assertEqual(settings['server']['default_http_headers']['Custom-Header'], "Custom-Value")
  62. engine_names = [engine['name'] for engine in settings['engines']]
  63. self.assertNotIn('wikinews', engine_names)
  64. self.assertNotIn('wikibooks', engine_names)
  65. self.assertIn('wikipedia', engine_names)
  66. def test_user_settings_remove2(self):
  67. with patch.dict(settings_loader.environ,
  68. {'SEARX_SETTINGS_PATH': join(test_dir, 'settings/user_settings_remove2.yml')}):
  69. settings, msg = settings_loader.load_settings()
  70. self.assertTrue(msg.startswith('merge the default settings'))
  71. self.assertEqual(settings['server']['secret_key'], "user_secret_key")
  72. self.assertEqual(settings['server']['default_http_headers']['Custom-Header'], "Custom-Value")
  73. engine_names = [engine['name'] for engine in settings['engines']]
  74. self.assertNotIn('wikinews', engine_names)
  75. self.assertNotIn('wikibooks', engine_names)
  76. self.assertIn('wikipedia', engine_names)
  77. wikipedia = list(filter(lambda engine: (engine.get('name')) == 'wikipedia', settings['engines']))
  78. self.assertEqual(wikipedia[0]['engine'], 'wikipedia')
  79. self.assertEqual(wikipedia[0]['tokens'], ['secret_token'])
  80. newengine = list(filter(lambda engine: (engine.get('name')) == 'newengine', settings['engines']))
  81. self.assertEqual(newengine[0]['engine'], 'dummy')
  82. def test_user_settings_keep_only(self):
  83. with patch.dict(settings_loader.environ,
  84. {'SEARX_SETTINGS_PATH': join(test_dir, 'settings/user_settings_keep_only.yml')}):
  85. settings, msg = settings_loader.load_settings()
  86. self.assertTrue(msg.startswith('merge the default settings'))
  87. engine_names = [engine['name'] for engine in settings['engines']]
  88. self.assertEqual(engine_names, ['wikibooks', 'wikinews', 'wikipedia', 'newengine'])
  89. # wikipedia has been removed, then added again with the "engine" section of user_settings_keep_only.yml
  90. self.assertEqual(len(settings['engines'][2]), 1)
  91. def test_custom_settings(self):
  92. with patch.dict(settings_loader.environ,
  93. {'SEARX_SETTINGS_PATH': join(test_dir, 'settings/user_settings.yml')}):
  94. settings, msg = settings_loader.load_settings()
  95. self.assertTrue(msg.startswith('load the user settings from'))
  96. self.assertEqual(settings['server']['port'], 9000)
  97. self.assertEqual(settings['server']['secret_key'], "user_settings_secret")
  98. engine_names = [engine['name'] for engine in settings['engines']]
  99. self.assertEqual(engine_names, ['wikidata', 'wikibooks', 'wikinews', 'wikiquote'])