test_piratebay.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import piratebay
  5. from searx.testing import SearxTestCase
  6. class TestPiratebayEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. dicto['category'] = 'Toto'
  12. params = piratebay.request(query, dicto)
  13. self.assertIn('url', params)
  14. self.assertIn(query, params['url'])
  15. self.assertIn('piratebay.cr', params['url'])
  16. self.assertIn('0', params['url'])
  17. dicto['category'] = 'music'
  18. params = piratebay.request(query, dicto)
  19. self.assertIn('100', params['url'])
  20. def test_response(self):
  21. self.assertRaises(AttributeError, piratebay.response, None)
  22. self.assertRaises(AttributeError, piratebay.response, [])
  23. self.assertRaises(AttributeError, piratebay.response, '')
  24. self.assertRaises(AttributeError, piratebay.response, '[]')
  25. response = mock.Mock(text='<html></html>')
  26. self.assertEqual(piratebay.response(response), [])
  27. html = """
  28. <table id="searchResult">
  29. <tr>
  30. </tr>
  31. <tr>
  32. <td class="vertTh">
  33. <center>
  34. <a href="#" title="More from this category">Anime</a><br/>
  35. (<a href="#" title="More from this category">Anime</a>)
  36. </center>
  37. </td>
  38. <td>
  39. <div class="detName">
  40. <a href="/this.is.the.link" class="detLink" title="Title">
  41. This is the title
  42. </a>
  43. </div>
  44. <a href="magnet:?xt=urn:btih:MAGNETLINK" title="Download this torrent using magnet">
  45. <img src="/static/img/icon-magnet.gif" alt="Magnet link"/>
  46. </a>
  47. <a href="http://torcache.net/torrent/TORRENTFILE.torrent" title="Download this torrent">
  48. <img src="/static/img/dl.gif" class="dl" alt="Download"/>
  49. </a>
  50. <a href="/user/HorribleSubs">
  51. <img src="/static/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border='0'/>
  52. </a>
  53. <img src="/static/img/11x11p.png"/>
  54. <font class="detDesc">
  55. This is the content <span>and should be</span> OK
  56. </font>
  57. </td>
  58. <td align="right">13</td>
  59. <td align="right">334</td>
  60. </tr>
  61. </table>
  62. """
  63. response = mock.Mock(text=html)
  64. results = piratebay.response(response)
  65. self.assertEqual(type(results), list)
  66. self.assertEqual(len(results), 1)
  67. self.assertEqual(results[0]['title'], 'This is the title')
  68. self.assertEqual(results[0]['url'], 'https://thepiratebay.cr/this.is.the.link')
  69. self.assertEqual(results[0]['content'], 'This is the content and should be OK')
  70. self.assertEqual(results[0]['seed'], 13)
  71. self.assertEqual(results[0]['leech'], 334)
  72. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETLINK')
  73. self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/TORRENTFILE.torrent')
  74. html = """
  75. <table id="searchResult">
  76. <tr>
  77. </tr>
  78. <tr>
  79. <td class="vertTh">
  80. <center>
  81. <a href="#" title="More from this category">Anime</a><br/>
  82. (<a href="#" title="More from this category">Anime</a>)
  83. </center>
  84. </td>
  85. <td>
  86. <div class="detName">
  87. <a href="/this.is.the.link" class="detLink" title="Title">
  88. This is the title
  89. </a>
  90. </div>
  91. <a href="magnet:?xt=urn:btih:MAGNETLINK" title="Download this torrent using magnet">
  92. <img src="/static/img/icon-magnet.gif" alt="Magnet link"/>
  93. </a>
  94. <a href="http://torcache.net/torrent/TORRENTFILE.torrent" title="Download this torrent">
  95. <img src="/static/img/dl.gif" class="dl" alt="Download"/>
  96. </a>
  97. <a href="/user/HorribleSubs">
  98. <img src="/static/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border='0'/>
  99. </a>
  100. <img src="/static/img/11x11p.png"/>
  101. <font class="detDesc">
  102. This is the content <span>and should be</span> OK
  103. </font>
  104. </td>
  105. <td align="right">s</td>
  106. <td align="right">d</td>
  107. </tr>
  108. </table>
  109. """
  110. response = mock.Mock(text=html)
  111. results = piratebay.response(response)
  112. self.assertEqual(type(results), list)
  113. self.assertEqual(len(results), 1)
  114. self.assertEqual(results[0]['title'], 'This is the title')
  115. self.assertEqual(results[0]['url'], 'https://thepiratebay.cr/this.is.the.link')
  116. self.assertEqual(results[0]['content'], 'This is the content and should be OK')
  117. self.assertEqual(results[0]['seed'], 0)
  118. self.assertEqual(results[0]['leech'], 0)
  119. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETLINK')
  120. self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/TORRENTFILE.torrent')
  121. html = """
  122. <table id="searchResult">
  123. </table>
  124. """
  125. response = mock.Mock(text=html)
  126. results = piratebay.response(response)
  127. self.assertEqual(type(results), list)
  128. self.assertEqual(len(results), 0)