From 7fb14c73ae8691a4f168654ff2e2356e525adcdd Mon Sep 17 00:00:00 2001 From: "V.G. Bulavintsev" Date: Thu, 11 Jul 2019 16:04:31 +0200 Subject: [PATCH] Add substitute for null pk in REST URLs --- .../Modules/MetadataStore/OrmBindings/torrent_metadata.py | 7 +++++-- .../Test/Core/Modules/RestApi/test_metadata_endpoint.py | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Tribler/Core/Modules/MetadataStore/OrmBindings/torrent_metadata.py b/Tribler/Core/Modules/MetadataStore/OrmBindings/torrent_metadata.py index b819d99442d..2ab8aa66bf1 100644 --- a/Tribler/Core/Modules/MetadataStore/OrmBindings/torrent_metadata.py +++ b/Tribler/Core/Modules/MetadataStore/OrmBindings/torrent_metadata.py @@ -19,6 +19,8 @@ from Tribler.Core.Utilities.tracker_utils import get_uniformed_tracker_url from Tribler.Core.Utilities.utilities import is_channel_public_key, is_hex_string, is_infohash +NULL_KEY_SUBST = b"\00" + # This function is used to devise id_ from infohash in deterministic way. Used in FFA channels. def infohash_to_id(infohash): @@ -187,7 +189,8 @@ def get_entries_query(cls, metadata_type=None, channel_pk=None, metadata_type=metadata_type if metadata_type is not None else cls._discriminator_) # Note that origin_id and channel_pk can be 0 and "" respectively, for, say, root channel and FFA entry - pony_query = pony_query.where(public_key=channel_pk) if channel_pk is not None else pony_query + pony_query = pony_query.where(public_key=(b"" if channel_pk == NULL_KEY_SUBST else channel_pk))\ + if channel_pk is not None else pony_query pony_query = pony_query.where(origin_id=origin_id) if origin_id is not None else pony_query pony_query = pony_query.where(lambda g: g.status != TODELETE) if exclude_deleted else pony_query pony_query = pony_query.where(lambda g: g.xxx == 0) if hide_xxx else pony_query @@ -229,7 +232,7 @@ def to_simple_dict(self, include_trackers=False): epoch = datetime.utcfromtimestamp(0) simple_dict = { "id": self.id_, - "public_key": hexlify(self.public_key), + "public_key": hexlify(self.public_key or NULL_KEY_SUBST), "name": self.title, "infohash": hexlify(self.infohash), "size": self.size, diff --git a/Tribler/Test/Core/Modules/RestApi/test_metadata_endpoint.py b/Tribler/Test/Core/Modules/RestApi/test_metadata_endpoint.py index 0b7677aca0f..4c7f8737f7c 100644 --- a/Tribler/Test/Core/Modules/RestApi/test_metadata_endpoint.py +++ b/Tribler/Test/Core/Modules/RestApi/test_metadata_endpoint.py @@ -186,6 +186,7 @@ def on_response(response): return self.do_request('metadata/channels/%s/123/torrents' % channel_pk).addCallback( on_response) + @inlineCallbacks def test_get_torrents_ffa_channel(self): """ Test whether we can query channel contents for unsigned (legacy/FFA) channels @@ -201,7 +202,9 @@ def on_response(response): self.assertEqual(len(json_dict['results']), 1) self.should_check_equality = False - return self.do_request('metadata/channels//123/torrents').addCallback(on_response) + # We test for both forms of querying null-key channels + yield self.do_request('metadata/channels//123/torrents').addCallback(on_response) + yield self.do_request('metadata/channels/00/123/torrents').addCallback(on_response) class TestSpecificChannelTorrentsCountEndpoint(BaseTestMetadataEndpoint):