Skip to content

Commit

Permalink
Add substitute for null pk in REST URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
ichorid committed Jul 11, 2019
1 parent 5b5c2a8 commit 7fb14c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion Tribler/Test/Core/Modules/RestApi/test_metadata_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 7fb14c7

Please sign in to comment.