test_tineye.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. from datetime import datetime
  4. from unittest.mock import Mock
  5. from requests import HTTPError
  6. from searx.engines import load_engines, tineye
  7. from tests import SearxTestCase
  8. class TinEyeTests(SearxTestCase): # pylint: disable=missing-class-docstring
  9. def setUp(self):
  10. load_engines([{'name': 'tineye', 'engine': 'tineye', 'shortcut': 'tin', 'timeout': 9.0, 'disabled': True}])
  11. def tearDown(self):
  12. load_engines([])
  13. def test_status_code_raises(self):
  14. response = Mock()
  15. response.status_code = 401
  16. response.raise_for_status.side_effect = HTTPError()
  17. self.assertRaises(HTTPError, lambda: tineye.response(response))
  18. def test_returns_empty_list_for_422(self):
  19. response = Mock()
  20. response.json.return_value = {}
  21. response.status_code = 422
  22. response.raise_for_status.side_effect = HTTPError()
  23. with self.assertLogs(tineye.logger) as _dev_null:
  24. results = tineye.response(response)
  25. self.assertEqual(0, len(results))
  26. def test_logs_format_for_422(self):
  27. response = Mock()
  28. response.json.return_value = {"suggestions": {"key": "Invalid image URL"}}
  29. response.status_code = 422
  30. response.raise_for_status.side_effect = HTTPError()
  31. with self.assertLogs(tineye.logger) as assert_logs_context:
  32. tineye.response(response)
  33. self.assertIn(tineye.FORMAT_NOT_SUPPORTED, ','.join(assert_logs_context.output))
  34. def test_logs_signature_for_422(self):
  35. response = Mock()
  36. response.json.return_value = {"suggestions": {"key": "NO_SIGNATURE_ERROR"}}
  37. response.status_code = 422
  38. response.raise_for_status.side_effect = HTTPError()
  39. with self.assertLogs(tineye.logger) as assert_logs_context:
  40. tineye.response(response)
  41. self.assertIn(tineye.NO_SIGNATURE_ERROR, ','.join(assert_logs_context.output))
  42. def test_logs_download_for_422(self):
  43. response = Mock()
  44. response.json.return_value = {"suggestions": {"key": "Download Error"}}
  45. response.status_code = 422
  46. response.raise_for_status.side_effect = HTTPError()
  47. with self.assertLogs(tineye.logger) as assert_logs_context:
  48. tineye.response(response)
  49. self.assertIn(tineye.DOWNLOAD_ERROR, ','.join(assert_logs_context.output))
  50. def test_empty_list_for_400(self):
  51. response = Mock()
  52. response.json.return_value = {}
  53. response.status_code = 400
  54. response.raise_for_status.side_effect = HTTPError()
  55. with self.assertLogs(tineye.logger) as _dev_null:
  56. results = tineye.response(response)
  57. self.assertEqual(0, len(results))
  58. def test_logs_description_for_400(self):
  59. description = 'There was a problem with that request. Error ID: ad5fc955-a934-43c1-8187-f9a61d301645'
  60. response = Mock()
  61. response.json.return_value = {"suggestions": {"description": [description], "title": "Oops! We're sorry!"}}
  62. response.status_code = 400
  63. response.raise_for_status.side_effect = HTTPError()
  64. with self.assertLogs(tineye.logger) as assert_logs_context:
  65. tineye.response(response)
  66. self.assertIn(description, ','.join(assert_logs_context.output))
  67. def test_crawl_date_parses(self):
  68. date_str = '2020-05-25'
  69. date = datetime.strptime(date_str, '%Y-%m-%d')
  70. response = Mock()
  71. response.json.return_value = {
  72. 'matches': [
  73. {
  74. 'backlinks': [
  75. {
  76. 'crawl_date': date_str,
  77. }
  78. ]
  79. }
  80. ]
  81. }
  82. response.status_code = 200
  83. results = tineye.response(response)
  84. self.assertEqual(date, results[0]['publishedDate'])