test_webadapter.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. from searx.testing import SearxTestCase
  3. from searx.preferences import Preferences
  4. from searx.engines import engines
  5. import searx.search
  6. from searx.search import EngineRef
  7. from searx.webadapter import validate_engineref_list
  8. PRIVATE_ENGINE_NAME = 'general private offline'
  9. TEST_ENGINES = [
  10. {
  11. 'name': PRIVATE_ENGINE_NAME,
  12. 'engine': 'dummy-offline',
  13. 'categories': 'general',
  14. 'shortcut': 'do',
  15. 'timeout': 3.0,
  16. 'engine_type': 'offline',
  17. 'tokens': ['my-token'],
  18. },
  19. ]
  20. SEARCHQUERY = [EngineRef(PRIVATE_ENGINE_NAME, 'general')]
  21. class ValidateQueryCase(SearxTestCase):
  22. @classmethod
  23. def setUpClass(cls):
  24. searx.search.initialize(TEST_ENGINES)
  25. def test_query_private_engine_without_token(self):
  26. preferences = Preferences(['oscar'], ['general'], engines, [])
  27. valid, unknown, invalid_token = validate_engineref_list(SEARCHQUERY, preferences)
  28. self.assertEqual(len(valid), 0)
  29. self.assertEqual(len(unknown), 0)
  30. self.assertEqual(len(invalid_token), 1)
  31. def test_query_private_engine_with_incorrect_token(self):
  32. preferences_with_tokens = Preferences(['oscar'], ['general'], engines, [])
  33. preferences_with_tokens.parse_dict({'tokens': 'bad-token'})
  34. valid, unknown, invalid_token = validate_engineref_list(SEARCHQUERY, preferences_with_tokens)
  35. self.assertEqual(len(valid), 0)
  36. self.assertEqual(len(unknown), 0)
  37. self.assertEqual(len(invalid_token), 1)
  38. def test_query_private_engine_with_correct_token(self):
  39. preferences_with_tokens = Preferences(['oscar'], ['general'], engines, [])
  40. preferences_with_tokens.parse_dict({'tokens': 'my-token'})
  41. valid, unknown, invalid_token = validate_engineref_list(SEARCHQUERY, preferences_with_tokens)
  42. self.assertEqual(len(valid), 1)
  43. self.assertEqual(len(unknown), 0)
  44. self.assertEqual(len(invalid_token), 0)