Skip to content

Commit

Permalink
Added tests for starting download from infohash
Browse files Browse the repository at this point in the history
  • Loading branch information
devos50 committed Jun 26, 2016
1 parent 6cf7018 commit f7130dd
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Tribler/Test/Core/Modules/RestApi/test_downloads_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from Tribler.Core.DownloadConfig import DownloadStartupConfig
from Tribler.Core.Utilities.twisted_thread import deferred
from Tribler.Core.simpledefs import NTFY_TORRENTS
from Tribler.Test.Core.Modules.RestApi.base_api_test import AbstractApiTest
from Tribler.Test.test_as_server import TESTS_DATA_DIR

Expand All @@ -13,6 +14,7 @@ class TestDownloadsEndpoint(AbstractApiTest):
def setUpPreSession(self):
super(TestDownloadsEndpoint, self).setUpPreSession()
self.config.set_libtorrent(True)
self.config.set_megacache(True)

@deferred(timeout=10)
def test_get_downloads_no_downloads(self):
Expand Down Expand Up @@ -167,3 +169,44 @@ def test_download_unknown_state(self):
self.should_check_equality = False
return self.do_request('downloads/%s' % video_tdef.get_infohash().encode('hex'), expected_code=400,
post_data={"state": "abc"}, request_type='PATCH')

@deferred(timeout=20)
def test_start_download_hash(self):
"""
Testing whether starting a download from an infohash works
"""
self.session.get_collected_torrent = lambda _: None
torrent_db = self.session.open_dbhandler(NTFY_TORRENTS)
torrent_db.getTorrent = lambda infohash, keys: {"name": "test", "infohash": infohash, "keys": keys}

def verify_download(_):
self.assertEqual(len(self.session.get_downloads()), 1)

return self.do_request('downloads/%s' % ('a' * 40), expected_code=200,
expected_json={"started": True}, request_type='PUT').addCallback(verify_download)

@deferred(timeout=20)
def test_start_download_hash_cache(self):
"""
Testing whether starting a download from an infohash present in the megacache works
"""
with open(os.path.join(TESTS_DATA_DIR, 'bak_single.torrent')) as torrent_file:
raw_data = torrent_file.read()
self.session.get_collected_torrent = lambda _: raw_data

def verify_download(_):
self.assertEqual(len(self.session.get_downloads()), 1)

return self.do_request('downloads/%s' % ('a' * 40), expected_code=200,
expected_json={"started": True}, request_type='PUT').addCallback(verify_download)

@deferred(timeout=20)
def test_start_download_hash_twice(self):
"""
Testing whether starting a download from an infohash twice raises error 409
"""
video_tdef, _ = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi'))
self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig())

return self.do_request('downloads/%s' % video_tdef.get_infohash().encode('hex'), expected_code=409,
request_type='PUT')

0 comments on commit f7130dd

Please sign in to comment.