2015-09-04 05:35:12 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import unittest
|
2015-09-15 05:21:13 +02:00
|
|
|
from unittest import mock
|
2015-09-20 23:14:00 +02:00
|
|
|
from unittest.mock import patch, MagicMock
|
2015-09-15 05:21:13 +02:00
|
|
|
import io
|
|
|
|
import urllib
|
2020-05-21 14:21:55 +02:00
|
|
|
import json
|
2015-09-04 05:35:12 +02:00
|
|
|
|
2015-09-20 23:14:00 +02:00
|
|
|
import pirate.torrent
|
|
|
|
import pirate.data
|
|
|
|
from pirate.print import Printer
|
2015-09-04 05:35:12 +02:00
|
|
|
from tests import util
|
|
|
|
|
2020-05-21 14:21:55 +02:00
|
|
|
|
2015-09-04 05:35:12 +02:00
|
|
|
class TestTorrent(unittest.TestCase):
|
|
|
|
|
2015-09-04 06:44:02 +02:00
|
|
|
def test_no_hits(self):
|
2015-09-04 07:18:38 +02:00
|
|
|
expected = []
|
2020-05-21 14:21:55 +02:00
|
|
|
with util.open_data('no_hits.json') as res:
|
|
|
|
actual = pirate.torrent.parse_page(res)
|
2015-09-04 06:44:02 +02:00
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
|
|
|
def test_blocked_mirror(self):
|
2020-05-21 14:21:55 +02:00
|
|
|
with util.open_data('blocked.html') as res:
|
|
|
|
with self.assertRaises(IOError):
|
|
|
|
pirate.torrent.parse_page(res)
|
2015-09-04 06:44:02 +02:00
|
|
|
|
|
|
|
def test_search_results(self):
|
2020-05-21 14:21:55 +02:00
|
|
|
with util.open_data('result.json') as file:
|
|
|
|
expected = json.load(file)
|
|
|
|
with util.open_data('debian_iso.json') as res:
|
|
|
|
actual = pirate.torrent.parse_page(res)
|
2015-09-04 05:35:12 +02:00
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
2015-09-15 05:21:13 +02:00
|
|
|
def test_parse_category(self):
|
2015-09-20 23:14:00 +02:00
|
|
|
category = pirate.torrent.parse_category(MagicMock(Printer), 'Audio')
|
2015-09-15 05:21:13 +02:00
|
|
|
self.assertEqual(100, category)
|
2015-09-20 23:14:00 +02:00
|
|
|
category = pirate.torrent.parse_category(MagicMock(Printer), 'Video')
|
2015-09-15 05:21:13 +02:00
|
|
|
self.assertEqual(200, category)
|
2015-09-20 23:14:00 +02:00
|
|
|
category = pirate.torrent.parse_category(MagicMock(Printer), '100')
|
2015-09-15 05:21:13 +02:00
|
|
|
self.assertEqual(100, category)
|
2015-09-20 23:14:00 +02:00
|
|
|
category = pirate.torrent.parse_category(MagicMock(Printer), 'asdf')
|
2015-09-15 05:21:13 +02:00
|
|
|
self.assertEqual(0, category)
|
2015-09-20 23:14:00 +02:00
|
|
|
category = pirate.torrent.parse_category(MagicMock(Printer), '9001')
|
2015-09-15 05:21:13 +02:00
|
|
|
self.assertEqual(0, category)
|
|
|
|
|
|
|
|
def test_parse_sort(self):
|
2015-09-20 23:14:00 +02:00
|
|
|
sort = pirate.torrent.parse_sort(MagicMock(Printer), 'SeedersDsc')
|
2015-09-15 05:21:13 +02:00
|
|
|
self.assertEqual(7, sort)
|
2015-09-20 23:14:00 +02:00
|
|
|
sort = pirate.torrent.parse_sort(MagicMock(Printer), '7')
|
2015-09-15 05:21:13 +02:00
|
|
|
self.assertEqual(7, sort)
|
2015-09-20 23:14:00 +02:00
|
|
|
sort = pirate.torrent.parse_sort(MagicMock(Printer), 'asdf')
|
2015-09-15 05:21:13 +02:00
|
|
|
self.assertEqual(99, sort)
|
2015-09-20 23:14:00 +02:00
|
|
|
sort = pirate.torrent.parse_sort(MagicMock(Printer), '7000')
|
2015-09-15 05:21:13 +02:00
|
|
|
self.assertEqual(99, sort)
|
|
|
|
|
|
|
|
def test_request_path(self):
|
|
|
|
# the args are (page, category, sort, mode, terms)
|
|
|
|
tests = [
|
|
|
|
((0, 100, 10, 'browse', []), '/browse/100/0/10'),
|
|
|
|
((0, 0, 10, 'browse', []), '/browse/100/0/10'),
|
|
|
|
((0, 0, 10, 'recent', []), '/top/48hall'),
|
|
|
|
((0, 100, 10, 'recent', []), '/top/48h100'),
|
|
|
|
((0, 100, 10, 'top', []), '/top/100'),
|
|
|
|
((0, 0, 10, 'top', []), '/top/all'),
|
|
|
|
((0, 100, 10, 'search', ['abc']), '/search/abc/0/10/100'),
|
|
|
|
((0, 100, 10, 'search', ['abc', 'def']), '/search/abc+def/0/10/100'),
|
|
|
|
((0, 100, 10, 'search', [u'\u1234']), '/search/%E1%88%B4/0/10/100'),
|
|
|
|
((0, 100, 10, 'asdf', []), Exception),
|
|
|
|
]
|
|
|
|
for test in tests:
|
|
|
|
if test[1] != Exception:
|
|
|
|
path = pirate.torrent.build_request_path(*test[0])
|
|
|
|
self.assertEqual(test[1], path)
|
|
|
|
else:
|
|
|
|
with self.assertRaises(test[1]):
|
|
|
|
pirate.torrent.build_request_path(test[0])
|
|
|
|
|
|
|
|
@patch('pirate.torrent.get_torrent')
|
|
|
|
def test_save_torrents(self, get_torrent):
|
2020-05-21 14:21:55 +02:00
|
|
|
with patch('pirate.torrent.open',
|
|
|
|
mock.mock_open(), create=True) as open_:
|
|
|
|
pirate.torrent.save_torrents(
|
|
|
|
MagicMock(Printer), [0],
|
|
|
|
[{'name': 'cool torrent',
|
|
|
|
'info_hash': 3735928559,
|
|
|
|
'magnet': 'magnet:?xt=urn:btih:deadbeef'}], 'path', 9)
|
|
|
|
get_torrent.assert_called_once_with(3735928559, 9)
|
|
|
|
open_.assert_called_once_with('path/cool torrent.torrent', 'wb')
|
2015-09-15 05:21:13 +02:00
|
|
|
|
2020-05-21 14:21:55 +02:00
|
|
|
@patch('pirate.torrent.get_torrent',
|
|
|
|
side_effect=urllib.error.HTTPError('', '', '', '', io.StringIO()))
|
2015-09-15 05:21:13 +02:00
|
|
|
def test_save_torrents_fail(self, get_torrent):
|
2020-05-21 14:21:55 +02:00
|
|
|
pirate.torrent.save_torrents(
|
|
|
|
MagicMock(Printer), [0],
|
|
|
|
[{'name': 'cool torrent',
|
|
|
|
'info_hash': 3735928559,
|
|
|
|
'magnet': 'magnet:?xt=urn:btih:deadbeef'}], 'path', 9)
|
2015-09-15 05:21:13 +02:00
|
|
|
|
|
|
|
def test_save_magnets(self):
|
2020-05-21 14:21:55 +02:00
|
|
|
with patch('pirate.torrent.open',
|
|
|
|
mock.mock_open(), create=True) as open_:
|
|
|
|
pirate.torrent.save_magnets(
|
|
|
|
MagicMock(Printer), [0],
|
|
|
|
[{'name': 'cool torrent',
|
|
|
|
'info_hash': 3735928559,
|
|
|
|
'magnet': 'magnet:?xt=urn:btih:deadbeef'}], 'path')
|
|
|
|
open_.assert_called_once_with('path/cool torrent.magnet', 'w')
|
2015-09-15 05:21:13 +02:00
|
|
|
|
2015-09-20 06:15:26 +02:00
|
|
|
@patch('urllib.request.urlopen')
|
|
|
|
def test_get_torrent(self, urlopen):
|
|
|
|
class MockRequest():
|
|
|
|
add_header = mock.MagicMock()
|
|
|
|
request_obj = MockRequest()
|
2020-05-21 14:21:55 +02:00
|
|
|
with patch('urllib.request.Request', return_value=request_obj) as req:
|
|
|
|
pirate.torrent.get_torrent(100000000000000, 9)
|
|
|
|
req.assert_called_once_with(
|
|
|
|
'http://itorrents.org/torrent/5AF3107A4000.torrent',
|
|
|
|
headers=pirate.data.default_headers)
|
|
|
|
urlopen.assert_called_once_with(
|
|
|
|
request_obj,
|
|
|
|
timeout=9)
|
2015-09-20 06:15:26 +02:00
|
|
|
|
|
|
|
def test_remote(self):
|
|
|
|
class MockRequest():
|
|
|
|
add_header = mock.MagicMock()
|
2020-05-21 14:21:55 +02:00
|
|
|
req_obj = MockRequest()
|
|
|
|
|
|
|
|
class MockInfo():
|
|
|
|
get_content_type = mock.MagicMock(return_value='application/json')
|
|
|
|
get = mock.MagicMock()
|
|
|
|
|
2015-09-20 06:15:26 +02:00
|
|
|
class MockResponse():
|
2020-05-21 14:21:55 +02:00
|
|
|
read = mock.MagicMock(return_value=b'[]')
|
|
|
|
info = mock.MagicMock(return_value=MockInfo())
|
|
|
|
res_obj = MockResponse()
|
|
|
|
|
|
|
|
with patch('urllib.request.Request', return_value=req_obj) as req:
|
|
|
|
with patch('urllib.request.urlopen', return_value=res_obj) as res:
|
|
|
|
results = pirate.torrent.remote(
|
|
|
|
MagicMock(Printer), 100, 10, 'browse',
|
|
|
|
[], 'http://example.com', 9)
|
|
|
|
req.assert_called_once_with(
|
|
|
|
'http://example.com/precompiled/data_top100_100.json',
|
|
|
|
headers=pirate.data.default_headers)
|
|
|
|
res.assert_called_once_with(req_obj, timeout=9)
|
|
|
|
self.assertEqual(results, [])
|
|
|
|
|
2015-09-15 05:21:13 +02:00
|
|
|
|
2015-09-04 05:35:12 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|