test_settings_loader.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. from pathlib import Path
  4. import os
  5. from unittest.mock import patch
  6. from parameterized import parameterized
  7. from searx.exceptions import SearxSettingsException
  8. from searx import settings_loader
  9. from tests import SearxTestCase
  10. def _settings(f_name):
  11. return str(Path(__file__).parent.absolute() / "settings" / f_name)
  12. class TestLoad(SearxTestCase): # pylint: disable=missing-class-docstring
  13. def test_load_zero(self):
  14. with self.assertRaises(SearxSettingsException):
  15. settings_loader.load_yaml('/dev/zero')
  16. with self.assertRaises(SearxSettingsException):
  17. settings_loader.load_yaml(_settings("syntaxerror_settings.yml"))
  18. self.assertEqual(settings_loader.load_yaml(_settings("empty_settings.yml")), {})
  19. class TestDefaultSettings(SearxTestCase): # pylint: disable=missing-class-docstring
  20. def test_load(self):
  21. settings, msg = settings_loader.load_settings(load_user_settings=False)
  22. self.assertTrue(msg.startswith('load the default settings from'))
  23. self.assertFalse(settings['general']['debug'])
  24. self.assertIsInstance(settings['general']['instance_name'], str)
  25. self.assertEqual(settings['server']['secret_key'], "ultrasecretkey")
  26. self.assertIsInstance(settings['server']['port'], int)
  27. self.assertIsInstance(settings['server']['bind_address'], str)
  28. self.assertIsInstance(settings['engines'], list)
  29. self.assertIsInstance(settings['doi_resolvers'], dict)
  30. self.assertIsInstance(settings['default_doi_resolver'], str)
  31. class TestUserSettings(SearxTestCase): # pylint: disable=missing-class-docstring
  32. def test_is_use_default_settings(self):
  33. self.assertFalse(settings_loader.is_use_default_settings({}))
  34. self.assertTrue(settings_loader.is_use_default_settings({'use_default_settings': True}))
  35. self.assertTrue(settings_loader.is_use_default_settings({'use_default_settings': {}}))
  36. with self.assertRaises(ValueError):
  37. self.assertFalse(settings_loader.is_use_default_settings({'use_default_settings': 1}))
  38. with self.assertRaises(ValueError):
  39. self.assertFalse(settings_loader.is_use_default_settings({'use_default_settings': 0}))
  40. @parameterized.expand(
  41. [
  42. _settings("not_exists.yml"),
  43. "/folder/not/exists",
  44. ]
  45. )
  46. def test_user_settings_not_found(self, path: str):
  47. with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': path}):
  48. with self.assertRaises(EnvironmentError):
  49. _s, _m = settings_loader.load_settings()
  50. def test_user_settings(self):
  51. with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("user_settings_simple.yml")}):
  52. settings, msg = settings_loader.load_settings()
  53. self.assertTrue(msg.startswith('merge the default settings'))
  54. self.assertEqual(settings['server']['secret_key'], "user_secret_key")
  55. self.assertEqual(settings['server']['default_http_headers']['Custom-Header'], "Custom-Value")
  56. def test_user_settings_remove(self):
  57. with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _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(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("user_settings_remove2.yml")}):
  68. settings, msg = settings_loader.load_settings()
  69. self.assertTrue(msg.startswith('merge the default settings'))
  70. self.assertEqual(settings['server']['secret_key'], "user_secret_key")
  71. self.assertEqual(settings['server']['default_http_headers']['Custom-Header'], "Custom-Value")
  72. engine_names = [engine['name'] for engine in settings['engines']]
  73. self.assertNotIn('wikinews', engine_names)
  74. self.assertNotIn('wikibooks', engine_names)
  75. self.assertIn('wikipedia', engine_names)
  76. wikipedia = list(filter(lambda engine: (engine.get('name')) == 'wikipedia', settings['engines']))
  77. self.assertEqual(wikipedia[0]['engine'], 'wikipedia')
  78. self.assertEqual(wikipedia[0]['tokens'], ['secret_token'])
  79. newengine = list(filter(lambda engine: (engine.get('name')) == 'newengine', settings['engines']))
  80. self.assertEqual(newengine[0]['engine'], 'dummy')
  81. def test_user_settings_keep_only(self):
  82. with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("user_settings_keep_only.yml")}):
  83. settings, msg = settings_loader.load_settings()
  84. self.assertTrue(msg.startswith('merge the default settings'))
  85. engine_names = [engine['name'] for engine in settings['engines']]
  86. self.assertEqual(engine_names, ['wikibooks', 'wikinews', 'wikipedia', 'newengine'])
  87. # wikipedia has been removed, then added again with the "engine" section of user_settings_keep_only.yml
  88. self.assertEqual(len(settings['engines'][2]), 1)
  89. def test_custom_settings(self):
  90. with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("user_settings.yml")}):
  91. settings, msg = settings_loader.load_settings()
  92. self.assertTrue(msg.startswith('load the user settings from'))
  93. self.assertEqual(settings['server']['port'], 9000)
  94. self.assertEqual(settings['server']['secret_key'], "user_settings_secret")
  95. engine_names = [engine['name'] for engine in settings['engines']]
  96. self.assertEqual(engine_names, ['wikidata', 'wikibooks', 'wikinews', 'wikiquote'])