From f7130ddbbf0482f66d62499a4bc7b083c112eb78 Mon Sep 17 00:00:00 2001 From: Martijn de Vos Date: Sat, 25 Jun 2016 16:23:09 +0200 Subject: [PATCH] Added tests for starting download from infohash --- .../RestApi/test_downloads_endpoint.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Tribler/Test/Core/Modules/RestApi/test_downloads_endpoint.py b/Tribler/Test/Core/Modules/RestApi/test_downloads_endpoint.py index 483c7d3698b..2dfc8879f3f 100644 --- a/Tribler/Test/Core/Modules/RestApi/test_downloads_endpoint.py +++ b/Tribler/Test/Core/Modules/RestApi/test_downloads_endpoint.py @@ -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 @@ -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): @@ -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')