test_engine_tineye.py 3.5 KB

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