test_engine_tineye.py 3.8 KB

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