diff --git a/src/tribler-core/tribler_core/components/libtorrent/restapi/tests/test_torrentinfo_endpoint.py b/src/tribler-core/tribler_core/components/libtorrent/restapi/tests/test_torrentinfo_endpoint.py index 99dce69fc51..ef5ee55adc5 100644 --- a/src/tribler-core/tribler_core/components/libtorrent/restapi/tests/test_torrentinfo_endpoint.py +++ b/src/tribler-core/tribler_core/components/libtorrent/restapi/tests/test_torrentinfo_endpoint.py @@ -1,18 +1,14 @@ import json import shutil -import urllib -from binascii import unhexlify from unittest.mock import Mock, patch from urllib.parse import quote_plus, unquote_plus +import pytest from aiohttp.web_app import Application +from binascii import unhexlify from ipv8.util import succeed - -import pytest - from tribler_common.simpledefs import NTFY - from tribler_core.components.libtorrent.restapi.torrentinfo_endpoint import TorrentInfoEndpoint from tribler_core.components.libtorrent.torrentdef import TorrentDef from tribler_core.components.metadata_store.db.orm_bindings.torrent_metadata import tdef_to_metadata_dict @@ -40,37 +36,33 @@ async def test_get_torrentinfo(mock_dlmgr, tmp_path, rest_api, endpoint): """ Testing whether the API returns a correct dictionary with torrent info. """ + + def _path(file): + return f'file:{TESTS_DATA_DIR / file}' + endpoint.download_manager = mock_dlmgr shutil.copyfile(TORRENT_UBUNTU_FILE, tmp_path / 'ubuntu.torrent') def verify_valid_dict(json_data): metainfo_dict = json.loads(unhexlify(json_data['metainfo'])) - # FIXME: This check is commented out because json.dump garbles pieces binary data during transfer. - # To fix it, we must switch to some encoding scheme that is able to encode and decode raw binary - # fields in the dicts. - # However, for this works fine at the moment because we never use pieces data in the GUI. - # assert TorrentDef.load_from_dict(metainfo_dict) assert 'info' in metainfo_dict - def path_to_url(path): - return urllib.request.pathname2url(str(path)) - mock_dlmgr.downloads = {} mock_dlmgr.metainfo_requests = {} mock_dlmgr.get_channel_downloads = lambda: [] mock_dlmgr.shutdown = lambda: succeed(None) mock_dlmgr.notifier = Mock() - await do_request(rest_api, 'torrentinfo', expected_code=400) - await do_request(rest_api, 'torrentinfo?uri=def', expected_code=400) + url = 'torrentinfo' + await do_request(rest_api, url, expected_code=400) + await do_request(rest_api, url, params={'uri': 'def'}, expected_code=400) - path = "file:" + path_to_url(TESTS_DATA_DIR / "bak_single.torrent") - verify_valid_dict(await do_request(rest_api, f'torrentinfo?uri={path}', expected_code=200)) + response = await do_request(rest_api, url, params={'uri': _path('bak_single.torrent')}, expected_code=200) + verify_valid_dict(response) # Corrupt file - path = "file:" + path_to_url(TESTS_DATA_DIR / "test_rss.xml") - await do_request(rest_api, f'torrentinfo?uri={path}', expected_code=500) + await do_request(rest_api, url, params={'uri': _path('test_rss.xml')}, expected_code=500) path = "http://localhost:1234/ubuntu.torrent" @@ -79,7 +71,7 @@ async def mock_http_query(*_): return f.read() with patch("tribler_core.components.libtorrent.restapi.torrentinfo_endpoint.query_http_uri", new=mock_http_query): - verify_valid_dict(await do_request(rest_api, f'torrentinfo?uri={quote_plus(path)}', expected_code=200)) + verify_valid_dict(await do_request(rest_api, url, params={'uri': quote_plus(path)}, expected_code=200)) path = quote_plus(f'magnet:?xt=urn:btih:{hexlify(UBUNTU_1504_INFOHASH)}' f'&dn=test torrent&tr=http://ubuntu.org/ann') @@ -146,6 +138,7 @@ async def test_on_got_invalid_metainfo(mock_dlmgr, rest_api): """ Test whether the right operations happen when we receive an invalid metainfo object """ + def get_metainfo(*_, **__): return succeed("abcd") diff --git a/src/tribler-core/tribler_core/components/libtorrent/restapi/torrentinfo_endpoint.py b/src/tribler-core/tribler_core/components/libtorrent/restapi/torrentinfo_endpoint.py index 6e9285ab179..6bae10eeb4a 100644 --- a/src/tribler-core/tribler_core/components/libtorrent/restapi/torrentinfo_endpoint.py +++ b/src/tribler-core/tribler_core/components/libtorrent/restapi/torrentinfo_endpoint.py @@ -71,9 +71,9 @@ def setup_routes(self): } ) async def get_torrent_info(self, request): - parameters = request.query - hops = parameters.get('hops', None) - uri = parameters.get('uri') + params = request.query + hops = params.get('hops', None) + uri = params.get('uri') if hops: try: diff --git a/src/tribler-core/tribler_core/components/restapi/rest/base_api_test.py b/src/tribler-core/tribler_core/components/restapi/rest/base_api_test.py index a5d2edba174..0174076aef2 100644 --- a/src/tribler-core/tribler_core/components/restapi/rest/base_api_test.py +++ b/src/tribler-core/tribler_core/components/restapi/rest/base_api_test.py @@ -1,5 +1,6 @@ import json from json import JSONDecodeError +from typing import Dict, Optional from aiohttp import ClientSession @@ -37,12 +38,13 @@ async def do_real_request(port, endpoint, expected_code=200, expected_json=None, async def do_request(test_client, url, expected_code=200, expected_json=None, - request_type='GET', post_data=None, headers=None, json_response=True): + request_type='GET', post_data=None, headers=None, json_response=True, + params: Optional[Dict] = None): post_data = post_data or {} data = json.dumps(path_to_str(post_data)) if isinstance(post_data, (dict, list)) else post_data headers = headers or {'User-Agent': 'Tribler ' + version_id} - async with test_client.request(request_type, url, data=data, headers=headers, ssl=False) as response: + async with test_client.request(request_type, url, data=data, headers=headers, ssl=False, params=params) as response: status = response.status try: response = (await response.json(content_type=None) diff --git a/src/tribler-core/tribler_core/tests/tools/common.py b/src/tribler-core/tribler_core/tests/tools/common.py index 26714c22711..d0716a4385a 100644 --- a/src/tribler-core/tribler_core/tests/tools/common.py +++ b/src/tribler-core/tribler_core/tests/tools/common.py @@ -1,7 +1,7 @@ import binascii -from pathlib import Path import tribler_common +from tribler_core.utilities.path_util import Path UBUNTU_1504_INFOHASH = binascii.unhexlify('FC8A15A2FAF2734DBB1DC5F7AFDC5C9BEAEB1F59') diff --git a/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py b/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py index 488fcfe9c10..4fdf319e00d 100644 --- a/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py +++ b/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py @@ -145,11 +145,11 @@ def perform_files_request(self): return direct = not self.dialog_widget.anon_download_checkbox.isChecked() - url_params = {'uri': self.download_uri} + params = {'uri': self.download_uri} if direct: - url_params['hops'] = 0 + params['hops'] = 0 self.rest_request = TriblerNetworkRequest('torrentinfo', self.on_received_metainfo, capture_core_errors=False, - url_params=url_params) + url_params=params) if self.metainfo_retries <= METAINFO_MAX_RETRIES: fetch_mode = tr("directly") if direct else tr("anonymously")