test_piratebay.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.se', 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. <tr>
  62. <td class="vertTh">
  63. <center>
  64. <a href="#" title="More from this category">Anime</a><br/>
  65. (<a href="#" title="More from this category">Anime</a>)
  66. </center>
  67. </td>
  68. <td>
  69. <div class="detName">
  70. <a href="/this.is.the.link" class="detLink" title="Title">
  71. This is the title
  72. </a>
  73. </div>
  74. <a href="magnet:?xt=urn:btih:MAGNETLINK" title="Download this torrent using magnet">
  75. <img src="/static/img/icon-magnet.gif" alt="Magnet link"/>
  76. </a>
  77. <a href="/user/HorribleSubs">
  78. <img src="/static/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border='0'/>
  79. </a>
  80. <img src="/static/img/11x11p.png"/>
  81. <font class="detDesc">
  82. This is the content <span>and should be</span> OK
  83. </font>
  84. </td>
  85. <td align="right">13</td>
  86. <td align="right">334</td>
  87. </tr>
  88. </table>
  89. """
  90. response = mock.Mock(text=html)
  91. results = piratebay.response(response)
  92. self.assertEqual(type(results), list)
  93. self.assertEqual(len(results), 2)
  94. self.assertEqual(results[0]['title'], 'This is the title')
  95. self.assertEqual(results[0]['url'], 'https://thepiratebay.se/this.is.the.link')
  96. self.assertEqual(results[0]['content'], 'This is the content and should be OK')
  97. self.assertEqual(results[0]['seed'], 13)
  98. self.assertEqual(results[0]['leech'], 334)
  99. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETLINK')
  100. self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/TORRENTFILE.torrent')
  101. self.assertEqual(results[1]['torrentfile'], None)
  102. html = """
  103. <table id="searchResult">
  104. <tr>
  105. </tr>
  106. <tr>
  107. <td class="vertTh">
  108. <center>
  109. <a href="#" title="More from this category">Anime</a><br/>
  110. (<a href="#" title="More from this category">Anime</a>)
  111. </center>
  112. </td>
  113. <td>
  114. <div class="detName">
  115. <a href="/this.is.the.link" class="detLink" title="Title">
  116. This is the title
  117. </a>
  118. </div>
  119. <a href="magnet:?xt=urn:btih:MAGNETLINK" title="Download this torrent using magnet">
  120. <img src="/static/img/icon-magnet.gif" alt="Magnet link"/>
  121. </a>
  122. <a href="http://torcache.net/torrent/TORRENTFILE.torrent" title="Download this torrent">
  123. <img src="/static/img/dl.gif" class="dl" alt="Download"/>
  124. </a>
  125. <a href="/user/HorribleSubs">
  126. <img src="/static/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border='0'/>
  127. </a>
  128. <img src="/static/img/11x11p.png"/>
  129. <font class="detDesc">
  130. This is the content <span>and should be</span> OK
  131. </font>
  132. </td>
  133. <td align="right">s</td>
  134. <td align="right">d</td>
  135. </tr>
  136. </table>
  137. """
  138. response = mock.Mock(text=html)
  139. results = piratebay.response(response)
  140. self.assertEqual(type(results), list)
  141. self.assertEqual(len(results), 1)
  142. self.assertEqual(results[0]['title'], 'This is the title')
  143. self.assertEqual(results[0]['url'], 'https://thepiratebay.se/this.is.the.link')
  144. self.assertEqual(results[0]['content'], 'This is the content and should be OK')
  145. self.assertEqual(results[0]['seed'], 0)
  146. self.assertEqual(results[0]['leech'], 0)
  147. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETLINK')
  148. self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/TORRENTFILE.torrent')
  149. html = """
  150. <table id="searchResult">
  151. </table>
  152. """
  153. response = mock.Mock(text=html)
  154. results = piratebay.response(response)
  155. self.assertEqual(type(results), list)
  156. self.assertEqual(len(results), 0)