Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ichorid committed Dec 12, 2018
1 parent 63b6fc5 commit 964c0d6
Show file tree
Hide file tree
Showing 25 changed files with 222 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,6 @@ class ChannelsDiscoveredEndpoint(BaseChannelsEndpoint):
def getChild(self, path, request):
return ChannelsDiscoveredSpecificEndpoint(self.session, path)

@db_session
def render_GET(self, _):
"""
.. http:get:: /channels/discovered
A GET request to this endpoint returns all channels discovered in Tribler.
**Example request**:
.. sourcecode:: none
curl -X GET http://localhost:8085/channels/discovered
**Example response**:
.. sourcecode:: javascript
{
"channels": [{
"id": 3,
"dispersy_cid": "da69aaad39ccf468aba2ab9177d5f8d8160135e6",
"name": "My fancy channel",
"description": "A description of this fancy channel",
"subscribed": False,
"votes": 23,
"torrents": 3,
"spam": 5,
"modified": 14598395,
"can_edit": True
}, ...]
}
"""
all_channels_db = self.channel_db_handler.getAllChannels()

if self.session.config.get_chant_enabled():
chant_channels = list(self.session.lm.mds.ChannelMetadata.select())
for chant_channel in chant_channels:
all_channels_db.append(convert_channel_metadata_to_tuple(chant_channel))

results_json = []
for channel in all_channels_db:
channel_json = convert_db_channel_to_json(channel)
if self.session.config.get_family_filter_enabled() and \
self.session.lm.category.xxx_filter.isXXX(channel_json['name']):
continue

results_json.append(channel_json)

return json.dumps({"channels": results_json})

def render_PUT(self, request):
"""
.. http:put:: /channels/discovered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,36 +124,18 @@ def render_PUT(self, request):
"""
request.setHeader('Content-Type', 'text/json')

if len(self.cid) == 74:
with db_session:
channel = self.session.lm.mds.ChannelMetadata.get(public_key=buffer(self.cid))
if not channel:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": CHANNEL_NOT_FOUND})

if channel.subscribed:
request.setResponseCode(http.CONFLICT)
return json.dumps({"error": ALREADY_SUBSCRIBED_RESPONSE_MSG})
channel.subscribed = True

return json.dumps({"subscribed": True})

channel_info = self.get_channel_from_db(self.cid)

if channel_info is not None and channel_info[7] == VOTE_SUBSCRIBE:
request.setResponseCode(http.CONFLICT)
return json.dumps({"error": ALREADY_SUBSCRIBED_RESPONSE_MSG})

def on_vote_done(_):
request.write(json.dumps({"subscribed": True}))
request.finish()

def on_vote_error(failure):
request.processingFailed(failure)

self.vote_for_channel(self.cid, VOTE_SUBSCRIBE).addCallback(on_vote_done).addErrback(on_vote_error)

return NOT_DONE_YET
with db_session:
channel = self.session.lm.mds.ChannelMetadata.get(public_key=buffer(self.cid))
if not channel:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": CHANNEL_NOT_FOUND})

if channel.subscribed:
request.setResponseCode(http.CONFLICT)
return json.dumps({"error": ALREADY_SUBSCRIBED_RESPONSE_MSG})
channel.subscribed = True

return json.dumps({"subscribed": True})

def render_DELETE(self, request):
"""
Expand Down
4 changes: 2 additions & 2 deletions Tribler/Core/Modules/restapi/search_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@ def render_GET(self, request):
results.extend(pony_query_results)

# Legacy query for subscribed channels
skip_dispersy = channel and not is_dispersy_channel
skip_dispersy = not txt_search_query or (channel and not is_dispersy_channel)
if subscribed:
skip_dispersy = True
subscribed_channels_db = self.channel_db_handler.getMySubscribedChannels(include_dispersy=True)
results.extend([channel_to_torrent_adapter(c) for c in subscribed_channels_db])

# Legacy query for channels
previous_query_size = pony_query_size
if not skip_dispersy:
# Legacy query for channels
if item_type not in metadata_type_conversion_dict or item_type == u'channel':
first2 = shift_and_clamp(first, previous_query_size)
last2 = shift_and_clamp(last, previous_query_size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_channel_update_and_download(self):
# There should be 4 torrents + 1 channel torrent
channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(payload.public_key)
self.assertEqual(5, len(list(self.session.lm.mds.TorrentMetadata.select())))
self.assertEqual(4, channel.local_version)
self.assertEqual(6, channel.local_version)

def test_wrong_signature_exception_on_channel_update(self):
# Test wrong signature exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pony.orm import db_session
from six.moves import xrange
from twisted.internet.defer import inlineCallbacks
from typing import Optional, Any

from Tribler.Core.Modules.MetadataStore.OrmBindings.channel_metadata import entries_to_chunk
from Tribler.Core.Modules.MetadataStore.serialization import ChannelMetadataPayload
Expand Down Expand Up @@ -136,9 +137,9 @@ def test_process_channel_metadata_payload(self):

# Check that we always take the latest version
channel_metadata.version -= 1
self.assertEqual(channel_metadata.version, 2)
self.assertEqual(channel_metadata.version, 4)
channel_metadata = self.mds.ChannelMetadata.process_channel_metadata_payload(payload)
self.assertEqual(channel_metadata.version, 3)
self.assertEqual(channel_metadata.version, 5)
self.assertEqual(len(self.mds.ChannelMetadata.select()), 1)

@db_session
Expand All @@ -156,8 +157,8 @@ def test_get_channel_with_dirname(self):
sample_channel_dict = TestChannelMetadata.get_sample_channel_dict(self.my_key)
channel_metadata = self.mds.ChannelMetadata.from_dict(sample_channel_dict)
dirname = channel_metadata.dir_name
channels_list = list(self.mds.ChannelMetadata.get_channel_with_dirname(dirname))
self.assertEqual(len(channels_list), 1)
channel_result = self.mds.ChannelMetadata.get_channel_with_dirname(dirname) # type: Optional[Any]
self.assertEqual(channel_metadata, channel_result)

@db_session
def test_get_channel_with_id(self):
Expand Down
4 changes: 2 additions & 2 deletions Tribler/Test/Core/Modules/MetadataStore/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TestMetadataStore(TriblerCoreTest):
"""
DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), '..', '..', 'data')
CHANNEL_DIR = os.path.join(DATA_DIR, 'sample_channel',
'd24941643ff471e40d7761c71f4e3a4c21a4a5e89b0281430d01e78a4e46')
'4c69624e61434c504b3a6268ef448cf1476392eaf8856b89008e70af5eb6')
CHANNEL_METADATA = os.path.join(DATA_DIR, 'sample_channel', 'channel.mdblob')

@inlineCallbacks
Expand Down Expand Up @@ -114,4 +114,4 @@ def test_process_channel_dir(self):
self.assertFalse(channel.contents_list)
self.mds.process_channel_dir(self.CHANNEL_DIR, channel.public_key)
self.assertEqual(len(channel.contents_list), 3)
self.assertEqual(channel.local_version, 3)
self.assertEqual(channel.local_version, 5)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from binascii import hexlify
import json
import os
import random

import six
from pony.orm import db_session

from Tribler.Core.Modules.MetadataStore.serialization import ChannelMetadataPayload
from Tribler.Core.Modules.restapi.channels.base_channels_endpoint import UNKNOWN_CHANNEL_RESPONSE_MSG
from Tribler.Core.Modules.restapi.channels.channels_subscription_endpoint import ALREADY_SUBSCRIBED_RESPONSE_MSG
from Tribler.Test.Core.Modules.RestApi.Channels.test_channels_endpoint import AbstractTestChannelsEndpoint, \
AbstractTestChantEndpoint
from Tribler.Test.test_as_server import TESTS_DATA_DIR
Expand Down Expand Up @@ -42,20 +44,6 @@ def test_get_channel_info(self):

class TestChannelsDiscoveredChantEndpoints(AbstractTestChantEndpoint):

@trial_timeout(10)
def test_get_discovered_chant_channel(self):
"""
Test whether we successfully retrieve a discovered chant channel
"""

def verify_response(response):
json_response = json.loads(response)
self.assertTrue(json_response['channels'])

self.create_my_channel('test', 'test')
self.should_check_equality = False
return self.do_request('channels/discovered', expected_code=200).addCallback(verify_response)

@trial_timeout(10)
def test_create_my_channel(self):
"""
Expand Down Expand Up @@ -112,3 +100,118 @@ def test_export_channel_mdblob_notfound(self):
self.should_check_equality = False
return self.do_request('channels/discovered/%s/mdblob' % hexlify(payload.public_key),
expected_code=404, request_type='GET')

@trial_timeout(10)
def test_subscribe_channel_already_subscribed(self):
"""
Testing whether the API returns error 409 when subscribing to an already subscribed channel
"""
with db_session:
channel = self.add_random_channel()
channel.subscribed = True
channel_public_key = channel.public_key
expected_json = {"error": ALREADY_SUBSCRIBED_RESPONSE_MSG}

return self.do_request('channels/subscribed/%s' % str(channel_public_key).encode('hex'),
expected_code=409, expected_json=expected_json, request_type='PUT')

@trial_timeout(10)
def test_remove_single_torrent(self):
"""
Testing whether the API can remove a single selected torrent from a channel
"""
with db_session:
channel = self.create_my_channel("bla","bla")
channel_public_key = channel.public_key
torrent = self.add_random_torrent_to_my_channel()
torrent_infohash = torrent.infohash

def verify_torrent_removed(response):
json_response = json.loads(response)
self.assertTrue(json_response["removed"], "Removing selected torrents failed")
with db_session:
self.assertEqual(len(channel.contents[:]), 0)

self.should_check_equality = False
url = 'channels/discovered/%s/torrents/%s' % (str(channel_public_key).encode('hex'), str(torrent_infohash).encode('hex'))

return self.do_request(url, expected_code=200, request_type='DELETE').addCallback(verify_torrent_removed)

@trial_timeout(10)
def test_remove_multiple_torrents(self):
"""
Testing whether the API can remove multiple selected torrents from a channel
"""
with db_session:
channel = self.create_my_channel("bla","bla")
channel_public_key = channel.public_key
torrent1 = self.add_random_torrent_to_my_channel()
torrent2 = self.add_random_torrent_to_my_channel()
torrent1_infohash = torrent1.infohash
torrent2_infohash = torrent2.infohash

def verify_torrent_removed(response):
json_response = json.loads(response)
self.assertTrue(json_response["removed"], "Removing selected torrents failed")
with db_session:
self.assertEqual(len(channel.contents[:]), 0)

self.should_check_equality = False
url = 'channels/discovered/%s/torrents/%s' % (str(channel_public_key).encode('hex'), str(torrent1_infohash).encode('hex')+','+str(torrent2_infohash).encode('hex'))

return self.do_request(url, expected_code=200, request_type='DELETE').addCallback(verify_torrent_removed)

@trial_timeout(10)
def test_remove_wrong_channel(self):
"""
Testing whether the API returns correct error message in case the channel public key is wrong
"""
with db_session:
channel = self.create_my_channel("bla","bla")
url = 'channels/discovered/%s/torrents/%s' % (str('123').encode('hex'), str('123').encode('hex'))
return self.do_request(url, expected_code=405, request_type='DELETE')

@trial_timeout(10)
def test_remove_wrong_channel(self):
"""
Testing whether the API returns correct error message in case the personal channel is not created yet
"""
with db_session:
channel = self.create_my_channel("bla","bla")
channel_pubkey = channel.public_key
channel.delete()
url = 'channels/discovered/%s/torrents/%s' % (str(channel_pubkey).encode('hex'), str('123').encode('hex'))
return self.do_request(url, expected_code=404, request_type='DELETE')

@trial_timeout(10)
def test_remove_wrong_channel(self):
"""
Testing whether the API returns correct error message in case the personal channel is not created yet
"""
with db_session:
channel = self.create_my_channel("bla","bla")
channel_pubkey = channel.public_key
channel.delete()
url = 'channels/discovered/%s/torrents/%s' % (str(channel_pubkey).encode('hex'), str('123').encode('hex'))
return self.do_request(url, expected_code=404, request_type='DELETE')

@trial_timeout(10)
def test_remove_unknown_infohash(self):
"""
Testing whether the API returns {"removed": False, "failed_torrents":[ infohash ]} if an unknown torrent is
removed from a channel
"""
with db_session:
channel = self.create_my_channel("bla","bla")
channel_public_key = channel.public_key
unknown_torrent_infohash = buffer(bytearray(random.getrandbits(8) for _ in xrange(20)))

def verify_torrent_removed(response):
json_response = json.loads(response)
self.assertFalse(json_response["removed"], "Tribler removed an unknown torrent")
self.assertTrue(str(unknown_torrent_infohash).encode('hex') in json_response["failed_torrents"])

self.should_check_equality = False
url = 'channels/discovered/%s/torrents/%s' % (str(channel_public_key).encode('hex'), str(unknown_torrent_infohash).encode('hex'))

return self.do_request(url, expected_code=200, request_type='DELETE').addCallback(verify_torrent_removed)
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from __future__ import absolute_import
import random

from pony.orm import db_session
from six.moves import xrange
from twisted.internet.defer import inlineCallbacks
Expand Down Expand Up @@ -57,7 +59,7 @@ def add_random_torrent_to_my_channel(self, name=None):
Add a random torrent to your channel.
"""
return self.session.lm.mds.TorrentMetadata(title='test' if not name else name,
infohash='a' * 20)
infohash=database_blob(bytearray(random.getrandbits(8) for _ in xrange(20))))

@db_session
def add_random_channel(self):
Expand Down Expand Up @@ -118,30 +120,3 @@ def test_channels_unknown_endpoint(self):
self.should_check_equality = False
return self.do_request('channels/thisendpointdoesnotexist123', expected_code=404)

@trial_timeout(10)
def test_get_discovered_channels_no_channels(self):
"""
Testing whether the API returns no channels when fetching discovered channels
and there are no channels in the database
"""
expected_json = {u'channels': []}
return self.do_request('channels/discovered', expected_code=200, expected_json=expected_json)

@trial_timeout(10)
def test_get_discovered_channels(self):
"""
Testing whether the API returns inserted channels when fetching discovered channels
"""
self.should_check_equality = False
for i in xrange(0, 10):
self.insert_channel_in_db('rand%d' % i, 42 + i, 'Test channel %d' % i, 'Test description %d' % i)
self.insert_channel_in_db('randbad', 100, 'badterm', 'Test description bad')

def verify_channels(channels):
channels_json = json.loads(channels)['channels']
self.assertEqual(len(channels_json), 10)
channels_json = sorted(channels_json, key=lambda channel: channel['name'])
for ind in xrange(len(channels_json)):
self.assertEqual(channels_json[ind]['name'], 'Test channel %d' % ind)

return self.do_request('channels/discovered', expected_code=200).addCallback(verify_channels)
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from twisted.internet.defer import succeed, fail, inlineCallbacks
from twisted.python.failure import Failure

from Tribler.Core.Modules.restapi import VOTE_SUBSCRIBE, VOTE_UNSUBSCRIBE
from Tribler.Core.Modules.restapi import VOTE_UNSUBSCRIBE
from Tribler.Core.Modules.restapi.channels.base_channels_endpoint import UNKNOWN_CHANNEL_RESPONSE_MSG
from Tribler.Core.Modules.restapi.channels.channels_subscription_endpoint import ALREADY_SUBSCRIBED_RESPONSE_MSG, \
NOT_SUBSCRIBED_RESPONSE_MSG, ChannelsModifySubscriptionEndpoint
from Tribler.Core.Modules.restapi.channels.channels_subscription_endpoint import NOT_SUBSCRIBED_RESPONSE_MSG, \
ChannelsModifySubscriptionEndpoint
from Tribler.Test.Core.Modules.RestApi.Channels.test_channels_endpoint import AbstractTestChannelsEndpoint, \
AbstractTestChantEndpoint
from Tribler.Test.tools import trial_timeout
Expand Down
Loading

0 comments on commit 964c0d6

Please sign in to comment.