123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import swisscows
  4. from searx.testing import SearxTestCase
  5. class TestSwisscowsEngine(SearxTestCase):
  6. def test_request(self):
  7. swisscows.supported_languages = ['de-AT', 'de-DE']
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. dicto['language'] = 'de-DE'
  12. params = swisscows.request(query, dicto)
  13. self.assertTrue('url' in params)
  14. self.assertTrue(query in params['url'])
  15. self.assertTrue('swisscows.ch' in params['url'])
  16. self.assertTrue('uiLanguage=de' in params['url'])
  17. self.assertTrue('region=de-DE' in params['url'])
  18. dicto['category'] = 'images'
  19. params = swisscows.request(query, dicto)
  20. self.assertIn('image', params['url'])
  21. def test_response(self):
  22. self.assertRaises(AttributeError, swisscows.response, None)
  23. self.assertRaises(AttributeError, swisscows.response, [])
  24. self.assertRaises(AttributeError, swisscows.response, '')
  25. self.assertRaises(AttributeError, swisscows.response, '[]')
  26. response = mock.Mock(text=b'<html></html>')
  27. self.assertEqual(swisscows.response(response), [])
  28. response = mock.Mock(text=b'<html></html>')
  29. self.assertEqual(swisscows.response(response), [])
  30. html = b"""
  31. <script>
  32. App.Dispatcher.dispatch("initialize", {
  33. html5history: true,
  34. initialData: {"Request":
  35. {"Page":1,
  36. "ItemsCount":1,
  37. "Query":"This should ",
  38. "NormalizedQuery":"This should ",
  39. "Region":"de-AT",
  40. "UILanguage":"de"},
  41. "Results":{"items":[
  42. {"Title":"\uE000This should\uE001 be the title",
  43. "Description":"\uE000This should\uE001 be the content.",
  44. "Url":"http://this.should.be.the.link/",
  45. "DisplayUrl":"www.\uE000this.should.be.the\uE001.link",
  46. "Id":"782ef287-e439-451c-b380-6ebc14ba033d"},
  47. {"Title":"Datei:This should1.svg",
  48. "Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should1.png",
  49. "SourceUrl":"http://de.wikipedia.org/wiki/Datei:This should1.svg",
  50. "DisplayUrl":"de.wikipedia.org/wiki/Datei:This should1.svg",
  51. "Width":950,
  52. "Height":534,
  53. "FileSize":92100,
  54. "ContentType":"image/jpeg",
  55. "Thumbnail":{
  56. "Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should1.png",
  57. "ContentType":"image/jpeg",
  58. "Width":300,
  59. "Height":168,
  60. "FileSize":9134},
  61. "Id":"6a97a542-8f65-425f-b7f6-1178c3aba7be"
  62. }
  63. ],"TotalCount":55300,
  64. "Query":"This should "
  65. },
  66. "Images":[{"Title":"Datei:This should.svg",
  67. "Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should.png",
  68. "SourceUrl":"http://de.wikipedia.org/wiki/Datei:This should.svg",
  69. "DisplayUrl":"de.wikipedia.org/wiki/Datei:This should.svg",
  70. "Width":1280,
  71. "Height":677,
  72. "FileSize":50053,
  73. "ContentType":"image/png",
  74. "Thumbnail":{"Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should.png",
  75. "ContentType":"image/png",
  76. "Width":300,
  77. "Height":158,
  78. "FileSize":8023},
  79. "Id":"ae230fd8-a06a-47d6-99d5-e74766d8143a"}]},
  80. environment: "production"
  81. }).then(function (options) {
  82. $('#Search_Form').on('submit', function (e) {
  83. if (!Modernizr.history) return;
  84. e.preventDefault();
  85. var $form = $(this),
  86. $query = $('#Query'),
  87. query = $.trim($query.val()),
  88. path = App.Router.makePath($form.attr('action'), null, $form.serializeObject())
  89. if (query.length) {
  90. options.html5history ?
  91. ReactRouter.HistoryLocation.push(path) :
  92. ReactRouter.RefreshLocation.push(path);
  93. }
  94. else $('#Query').trigger('blur');
  95. });
  96. });
  97. </script>
  98. """
  99. response = mock.Mock(text=html)
  100. results = swisscows.response(response)
  101. self.assertEqual(type(results), list)
  102. self.assertEqual(len(results), 3)
  103. self.assertEqual(results[0]['title'], 'This should be the title')
  104. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  105. self.assertEqual(results[0]['content'], 'This should be the content.')
  106. self.assertEqual(results[1]['title'], 'Datei:This should1.svg')
  107. self.assertEqual(results[1]['url'], 'http://de.wikipedia.org/wiki/Datei:This should1.svg')
  108. self.assertEqual(results[1]['img_src'], 'http://ts2.mm.This/should1.png')
  109. self.assertEqual(results[1]['template'], 'images.html')
  110. self.assertEqual(results[2]['title'], 'Datei:This should.svg')
  111. self.assertEqual(results[2]['url'], 'http://de.wikipedia.org/wiki/Datei:This should.svg')
  112. self.assertEqual(results[2]['img_src'], 'http://ts2.mm.This/should.png')
  113. self.assertEqual(results[2]['template'], 'images.html')
  114. def test_fetch_supported_languages(self):
  115. html = """<html></html>"""
  116. response = mock.Mock(text=html)
  117. languages = swisscows._fetch_supported_languages(response)
  118. self.assertEqual(type(languages), list)
  119. self.assertEqual(len(languages), 0)
  120. html = """
  121. <html>
  122. <div id="regions-popup">
  123. <div>
  124. <ul>
  125. <li><a data-search-language="browser"></a></li>
  126. <li><a data-search-language="de-CH"></a></li>
  127. <li><a data-search-language="fr-CH"></a></li>
  128. </ul>
  129. </div>
  130. </div>
  131. </html>
  132. """
  133. response = mock.Mock(text=html)
  134. languages = swisscows._fetch_supported_languages(response)
  135. self.assertEqual(type(languages), list)
  136. self.assertEqual(len(languages), 3)
  137. self.assertIn('de-CH', languages)
  138. self.assertIn('fr-CH', languages)