desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Test whether the conversion from db torrent tuple to json works'
def test_convert_torrent_to_json_tuple(self):
input_tuple = (1, '2', 'abc', 4, 5, 6, 7, 8, 0, 0.123) output = {'id': 1, 'infohash': '2'.encode('hex'), 'name': 'abc', 'size': 4, 'category': 5, 'num_seeders': 6, 'num_leechers': 7, 'last_tracker_check': 8, 'relevance_score': 0.123} self.assertEqual(convert_search_torrent_to_json(input_tuple), output) ...
'Testing the get_parameters method in REST API util class'
def test_get_parameter(self):
self.assertEqual(42, get_parameter({'test': [42]}, 'test')) self.assertEqual(None, get_parameter({}, 'test'))
'Test whether the conversion from a db channel tuple to json works'
def test_convert_db_channel_to_json(self):
input_tuple = (1, 'aaaa'.decode('hex'), 'test', 'desc', 42, 43, 44, 2, 1234, 0.123) output = {'id': 1, 'dispersy_cid': 'aaaa', 'name': 'test', 'description': 'desc', 'torrents': 42, 'votes': 43, 'spam': 44, 'subscribed': True, 'modified': 1234, 'relevance_score': 0.123} self.assertEqual(convert_db_channel_t...
'Testing whether we can edit a channel.'
def test_can_edit_channel(self):
self.session.config.get_dispersy_enabled = (lambda : False) self.assertFalse(can_edit_channel('abcd', 0)) self.session.config.get_dispersy_enabled = (lambda : True) mocked_dispersy = MockObject() mocked_community = MockObject() mocked_community.get_channel_mode = (lambda : (ChannelCommunity.CHAN...
'Testing the fix of a unicode array'
def test_fix_unicode_array(self):
arr1 = ['a', 'b', 'c', u'd'] self.assertListEqual(fix_unicode_array(arr1), ['a', 'b', 'c', 'd']) arr2 = ['a', '\xa1'] self.assertListEqual(fix_unicode_array(arr2), ['a', '']) arr3 = [1, 2, 3, '4'] self.assertListEqual(fix_unicode_array(arr3), [1, 2, 3, '4']) arr4 = [{'a': 'b'}] self.asse...
'Testing the fix of a unicode dictionary'
def test_fix_unicode_dict(self):
dict1 = {'a': 'b', 'c': 'd'} self.assertDictEqual(fix_unicode_dict(dict1), {'a': 'b', 'c': 'd'}) dict2 = {'a': '\xa2'} self.assertDictEqual(fix_unicode_dict(dict2), {'a': ''}) dict3 = {'a': [1, 2], 'b': ['1', '2']} self.assertDictEqual(fix_unicode_dict(dict3), {'a': [1, 2], 'b': ['1', '2']}) ...
'Testing whether the event endpoint returns search results when we have search results available'
@deferred(timeout=20) def test_search_results(self):
def verify_search_results(results): self.assertEqual(len(results), 2) self.messages_to_wait_for = 2 def send_notifications(_): self.session.lm.api_manager.root_endpoint.events_endpoint.start_new_query() results_dict = {'keywords': ['test'], 'result_list': [(('a',) * 10)]} sel...
'Testing whether various events are coming through the events endpoints'
@deferred(timeout=20) def test_events(self):
self.messages_to_wait_for = 20 def send_notifications(_): self.session.lm.api_manager.root_endpoint.events_endpoint.start_new_query() results_dict = {'keywords': ['test'], 'result_list': [(('a',) * 10)]} self.session.notifier.notify(SIGNAL_TORRENT, SIGNAL_ON_SEARCH_RESULTS, None, results...
'Testing the family filter when searching for torrents and channels'
@deferred(timeout=20) def test_family_filter_search(self):
self.messages_to_wait_for = 2 def send_searches(_): events_endpoint = self.session.lm.api_manager.root_endpoint.events_endpoint channels = [(['a'] * 10), (['a'] * 10)] channels[0][2] = 'badterm' events_endpoint.on_search_results_channels(None, None, None, {'keywords': ['test'], '...
'Testing whether the API returns a correct Tribler statistics dictionary when requested'
@deferred(timeout=10) def test_get_tribler_statistics(self):
def verify_dict(data): self.assertTrue(json.loads(data)['tribler_statistics']) self.should_check_equality = False return self.do_request('statistics/tribler', expected_code=200).addCallback(verify_dict)
'Testing whether the API returns a correct Dispersy statistics dictionary when requested'
@deferred(timeout=10) def test_get_dispersy_statistics(self):
def verify_dict(data): self.assertTrue(json.loads(data)['dispersy_statistics']) self.should_check_equality = False return self.do_request('statistics/dispersy', expected_code=200).addCallback(verify_dict)
'Testing whether the API returns a correct community statistics dictionary when requested'
@deferred(timeout=10) def test_get_community_statistics(self):
def verify_dict(data): self.assertTrue(json.loads(data)['community_statistics']) self.should_check_equality = False return self.do_request('statistics/communities', expected_code=200).addCallback(verify_dict)
'Testing whether the API returns a formatted 500 error if an unhandled Exception is raised'
@deferred(10) def test_unhandled_exception(self):
def verify_error_message(body): error_response = json.loads(body) expected_response = {u'error': {u'handled': False, u'code': u'TriblerException', u'message': u'Oops! Something went wrong. Please restart Tribler'}} self.assertDictContainsSubset(expected_response[u'error'], ...
'Testing whether the API returns error 404 if no trustchain community is loaded'
@deferred(timeout=10) def test_get_statistics_no_community(self):
dummy_master_member = DummyMember(self.dispersy, 1, ('b' * 20)) Community.__abstractmethods__ = frozenset() self.dispersy.get_communities = (lambda : [Community(self.dispersy, dummy_master_member, self.member), Community(self.dispersy, dummy_master_member, self.member)]) return self.do_request('trustcha...
'Testing whether the API returns the correct statistics'
@deferred(timeout=10) def test_get_statistics(self):
block = TrustChainBlock() block.public_key = self.member.public_key block.link_public_key = 'deadbeef'.decode('HEX') block.link_sequence_number = 21 block.transaction = {'up': 42, 'down': 8, 'total_up': 1024, 'total_down': 2048} block.sequence_number = 3 block.previous_hash = 'babecafe'.deco...
'Testing whether the API returns the correct statistics'
@deferred(timeout=10) def test_get_statistics_no_data(self):
def verify_response(response): response_json = json.loads(response) self.assertTrue(('statistics' in response_json)) stats = response_json['statistics'] self.assertEqual(stats['id'], self.member.public_key.encode('HEX')) self.assertEqual(stats['total_blocks'], 0) self...
'Testing whether the API returns error 404 if no trustchain community is loaded when requesting blocks'
@deferred(timeout=10) def test_get_blocks_no_community(self):
self.dispersy.get_communities = (lambda : []) return self.do_request('trustchain/blocks/aaaaa', expected_code=404)
'Testing whether the API returns the correct blocks'
@deferred(timeout=10) def test_get_blocks(self):
def verify_response(response): response_json = json.loads(response) self.assertEqual(len(response_json['blocks']), 1) test_block = TestBlock() self.tc_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request(('trustchain/blocks/%s?limit=10...
'Testing whether the API takes large values for the limit'
@deferred(timeout=10) def test_get_blocks_bad_limit_too_many(self):
self.should_check_equality = False return self.do_request(('trustchain/blocks/%s?limit=10000000' % TestBlock().public_key.encode('HEX')), expected_code=400)
'Testing whether the API takes negative values for the limit'
@deferred(timeout=10) def test_get_blocks_bad_limit_negative(self):
self.should_check_equality = False return self.do_request(('trustchain/blocks/%s?limit=-10000000' % TestBlock().public_key.encode('HEX')), expected_code=400)
'Testing whether the API takes odd values for the limit'
@deferred(timeout=10) def test_get_blocks_bad_limit_nan(self):
self.should_check_equality = False return self.do_request(('trustchain/blocks/%s?limit=bla' % TestBlock().public_key.encode('HEX')), expected_code=400)
'Testing whether the API takes no values for the limit'
@deferred(timeout=10) def test_get_blocks_bad_limit_nothing(self):
self.should_check_equality = False return self.do_request(('trustchain/blocks/%s?limit=' % TestBlock().public_key.encode('HEX')), expected_code=400)
'Testing whether the API takes no limit argument'
@deferred(timeout=10) def test_get_blocks_unlimited(self):
self.should_check_equality = False return self.do_request(('trustchain/blocks/%s' % TestBlock().public_key.encode('HEX')), expected_code=200)
'Testing whether random torrents are returned if random torrents are fetched'
@deferred(timeout=10) def test_get_random_torrents(self):
def verify_torrents(results): json_results = json.loads(results) self.assertEqual(len(json_results['torrents']), 2) channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST) channel_db_handler._get_my_dispersy_cid = (lambda : 'myfakedispersyid') channel_id = channel_db_handler.o...
'Testing whether error 400 is returned when a negative limit is passed to the request to fetch random torrents'
@deferred(timeout=10) def test_random_torrents_negative(self):
expected_json = {'error': 'the limit parameter must be a positive number'} return self.do_request('torrents/random?limit=-5', expected_code=400, expected_json=expected_json)
'Testing whether we get an error 404 if we are fetching the trackers of a non-existent torrent'
@deferred(timeout=10) def test_get_torrent_trackers_404(self):
self.should_check_equality = False return self.do_request(('torrents/%s/trackers' % ('a' * 40)), expected_code=404)
'Testing whether fetching the trackers of a non-existent torrent is successful'
@deferred(timeout=10) def test_get_torrent_trackers(self):
torrent_db = self.session.open_dbhandler(NTFY_TORRENTS) torrent_db.addExternalTorrentNoDef(('a' * 20), 'ubuntu-torrent.iso', [['file1.txt', 42]], ('udp://trackerurl.com:1234/announce', 'http://trackerurl.com:4567/announce'), time.time()) def verify_trackers(trackers): self.assertIn('DHT', trackers) ...
'Test the endpoint to fetch the health of a torrent'
@deferred(timeout=20) @inlineCallbacks def test_check_torrent_health(self):
torrent_db = self.session.open_dbhandler(NTFY_TORRENTS) torrent_db.addExternalTorrentNoDef(('a' * 20), 'ubuntu-torrent.iso', [['file1.txt', 42]], (('udp://localhost:%s/announce' % self.udp_port), ('http://localhost:%s/announce' % self.http_port)), time.time()) url = ('torrents/%s/health?timeout=10&refresh=1...
'Testing whether the API triggers a Tribler shutdown'
@deferred(timeout=10) def test_shutdown(self):
self.orig_shutdown = self.session.shutdown self.shutdown_called = False def verify_shutdown_called(_): self.assertTrue(self.shutdown_called) def fake_shutdown(): self.shutdown_called = True self.session.shutdown = self.orig_shutdown return defer.Deferred() self.sessio...
'Testing whether the API returns a proper base64 encoded torrent'
@deferred(timeout=10) def test_create_torrent(self):
torrent_path = os.path.join(self.files_path, 'video.avi.torrent') expected_tdef = TorrentDef.load(torrent_path) def verify_torrent(body): response = json.loads(body) torrent = base64.b64decode(response['torrent']) tdef = TorrentDef.load_from_memory(torrent) creation_date = td...
'Testing whether the API returns a formatted 500 error if IOError is raised'
@deferred(timeout=10) def test_create_torrent_io_error(self):
def verify_error_message(body): error_response = json.loads(body) expected_response = {u'error': {u'handled': True, u'code': u'IOError', u'message': (u'Path does not exist: %s' % post_data['files[]'])}} self.assertDictContainsSubset(expected_response[u'error'], error_response[u'e...
'Test the get_parameters method'
def test_get_parameters(self):
parameters = {'abc': [3]} self.assertIsNone(get_param(parameters, 'abcd')) self.assertIsNotNone(get_param(parameters, 'abc'))
'Testing whether the API returns an empty list when downloads are fetched but no downloads are active'
@deferred(timeout=10) def test_get_downloads_no_downloads(self):
return self.do_request('downloads?get_peers=1&get_pieces=1', expected_code=200, expected_json={'downloads': []})
'Testing whether the API returns the right download when a download is added'
@deferred(timeout=20) def test_get_downloads(self):
def verify_download(downloads): downloads_json = json.loads(downloads) self.assertEqual(len(downloads_json['downloads']), 2) (video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) s...
'Testing whether an error is returned when we start a torrent download and do not pass any URI'
@deferred(timeout=10) def test_start_download_no_uri(self):
self.should_check_equality = False return self.do_request('downloads', expected_code=400, request_type='PUT')
'Testing whether an error is returned when we start a torrent download and pass wrong data'
@deferred(timeout=10) def test_start_download_bad_params(self):
self.should_check_equality = False post_data = {'anon_hops': 1, 'safe_seeding': 0, 'uri': 'abcd'} return self.do_request('downloads', expected_code=400, request_type='PUT', post_data=post_data)
'Testing whether an error is returned when we start a download from a bad URI'
@deferred(timeout=10) def test_start_download_bad_uri(self):
post_data = {'uri': 'abcd', 'destination': 'a/b/c', 'selected_files[]': '1'} return self.do_request('downloads', expected_code=500, request_type='PUT', post_data=post_data, expected_json={'error': 'invalid uri'})
'Testing whether we can start a download from a file'
@deferred(timeout=10) def test_start_download_from_file(self):
def verify_download(_): self.assertGreaterEqual(len(self.session.get_downloads()), 1) post_data = {'uri': ('file:%s' % os.path.join(TESTS_DATA_DIR, 'video.avi.torrent'))} expected_json = {'started': True, 'infohash': '42bb0a78d8a10bef4a5aee3a7d9f1edf9941cee4'} return self.do_request('downloads',...
'Testing whether we can start a download from a magnet'
@deferred(timeout=10) def test_start_download_from_magnet(self):
def verify_download(_): self.assertGreaterEqual(len(self.session.get_downloads()), 1) self.assertEqual(self.session.get_downloads()[0].get_def().get_name(), 'Unknown name') post_data = {'uri': ('magnet:?xt=urn:btih:%s' % hexlify(UBUNTU_1504_INFOHASH))} expected_json = {'started': True, 'i...
'Testing whether starting a download from a unexisting URL gives an error'
@deferred(timeout=10) def test_start_download_from_bad_url(self):
post_data = {'uri': ('http://localhost:%d/test.torrent' % get_random_port())} self.should_check_equality = False return self.do_request('downloads', expected_code=500, request_type='PUT', post_data=post_data)
'Testing whether the API returns error 400 if the remove_data parameter is not passed'
@deferred(timeout=10) def test_remove_download_no_remove_data_param(self):
self.should_check_equality = False return self.do_request('downloads/abcd', expected_code=400, request_type='DELETE')
'Testing whether the API returns error 404 if a non-existent download is removed'
@deferred(timeout=10) def test_remove_download_wrong_infohash(self):
self.should_check_equality = False return self.do_request('downloads/abcd', post_data={'remove_data': True}, expected_code=404, request_type='DELETE')
'Testing whether the API returns 200 if a download is being removed'
@deferred(timeout=10) def test_remove_download(self):
def verify_removed(_): self.assertEqual(len(self.session.get_downloads()), 0) (video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') r...
'Testing whether the API returns error 404 if a non-existent download is stopped'
@deferred(timeout=10) def test_stop_download_wrong_infohash(self):
self.should_check_equality = False return self.do_request('downloads/abcd', expected_code=404, post_data={'state': 'stop'}, request_type='PATCH')
'Testing whether the API returns 200 if a download is being stopped'
@deferred(timeout=10) def test_stop_download(self):
(video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) download = self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') original_stop = download.stop def mocked_stop(): download.should_sto...
'Testing whether an error is returned when we toggle a file for inclusion out of range'
@deferred(timeout=10) def test_select_download_file_range(self):
(video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') self.should_check_equality = False return self.do_request(('downloads/%s' % infohash), ...
'Testing whether files can be correctly toggled in a download'
@deferred(timeout=10) def test_select_download_file(self):
(video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) download = self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') def mocked_set_selected_files(*_): mocked_set_selected_files.called = Tr...
'Testing whether the API returns error 404 if a non-existent download is resumed'
@deferred(timeout=10) def test_resume_download_wrong_infohash(self):
self.should_check_equality = False return self.do_request('downloads/abcd', expected_code=404, post_data={'state': 'resume'}, request_type='PATCH')
'Testing whether the API returns 200 if a download is being resumed'
@deferred(timeout=10) def test_resume_download(self):
(video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) download = self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') def mocked_restart(): download.should_restart = True def verify_resu...
'Testing whether the API returns 200 if a download is being rechecked'
@deferred(timeout=10) def test_recheck_download(self):
(video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) download = self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') def mocked_recheck(): mocked_recheck.called = True mocked_recheck.ca...
'Testing whether the API returns 400 if we supply both anon_hops and another parameter'
@deferred(timeout=10) def test_change_hops_error(self):
(video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') self.should_check_equality = False return self.do_request(('downloads/%s' % infohash), ...
'Testing whether the API returns error 400 if an unknown state is passed when modifying a download'
@deferred(timeout=10) def test_download_unknown_state(self):
(video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) self.should_check_equality = False return self.do_request(('downloads/%s' % video_tdef.get_infohash().encode('hex')), expected_code=400, post_...
'Testing whether the API returns error 404 if a non-existent download is exported'
@deferred(timeout=10) def test_export_unknown_download(self):
self.should_check_equality = False return self.do_request('downloads/abcd/torrent', expected_code=404, request_type='GET')
'Testing whether the API returns the contents of the torrent file if a download is exported'
@deferred(timeout=10) def test_export_download(self):
(video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) with open(os.path.join(TESTS_DATA_DIR, 'bak_single.torrent')) as torrent_file: raw_data = torrent_file.read() self.session.get_collect...
'Testing whether the API returns 200 if we change the amount of hops of a download'
@deferred(timeout=10) def test_change_hops(self):
(video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') return self.do_request(('downloads/%s' % infohash), post_data={'anon_hops': 1}, expected_co...
'Test the get_parameters method'
def test_get_parameters(self):
parameters = {'abc': [3]} self.assertIsNone(get_param(parameters, 'abcd')) self.assertIsNotNone(get_param(parameters, 'abc'))
'Testing whether the API returns a correct state when requested'
@deferred(timeout=10) def test_get_state(self):
self.session.lm.api_manager.root_endpoint.state_endpoint.on_tribler_exception('abcd') expected_json = {'state': 'EXCEPTION', 'last_exception': 'abcd'} return self.do_request('state', expected_code=200, expected_json=expected_json)
'Testing whether the API returns wallets when we query for them'
@deferred(timeout=20) def test_get_wallets(self):
def on_response(response): json_response = json.loads(response) self.assertIn('wallets', json_response) self.assertGreaterEqual(len(json_response['wallets']), 2) self.should_check_equality = False return self.do_request('wallets', expected_code=200).addCallback(on_response)
'Testing whether creating a wallet that already exists throws an error'
@deferred(timeout=20) def test_create_wallet_exists(self):
self.should_check_equality = False return self.do_request('wallets/DUM1', expected_code=400, request_type='PUT')
'Test creating a BTC wallet without specifying a password, which should throw an error'
@deferred(timeout=20) def test_create_wallet_btc_pw(self):
self.should_check_equality = False return self.do_request('wallets/BTC', expected_code=400, request_type='PUT')
'Test creating a BTC wallet'
@deferred(timeout=20) def test_create_wallet_btc(self):
self.session.lm.market_community.wallets['BTC'].create_wallet = (lambda password='': succeed(None)) self.should_check_equality = False return self.do_request('wallets/BTC', expected_code=200, request_type='PUT', post_data={'password': 'a'})
'Testing whether we can create a wallet'
@deferred(timeout=20) def test_create_wallet(self):
self.session.lm.market_community.wallets['DUM1'].created = False self.should_check_equality = False return self.do_request('wallets/DUM1', expected_code=200, request_type='PUT')
'Testing whether we can retrieve the balance of a wallet'
@deferred(timeout=20) def test_get_wallet_balance(self):
def on_response(response): json_response = json.loads(response) self.assertIn('balance', json_response) self.assertGreater(json_response['balance']['available'], 0) self.should_check_equality = False return self.do_request('wallets/DUM1/balance', expected_code=200).addCallback(on_res...
'Testing whether we can receive the transactions of a wallet'
@deferred(timeout=20) def test_get_wallet_transaction(self):
def on_response(response): json_response = json.loads(response) self.assertIn('transactions', json_response) self.should_check_equality = False return self.do_request('wallets/DUM1/transactions', expected_code=200).addCallback(on_response)
'Test transferring assets from a non-BTC wallet'
@deferred(timeout=20) def test_transfer_no_btc(self):
self.should_check_equality = False return self.do_request('wallets/DUM1/transfer', expected_code=400, request_type='POST')
'Test transferring assets from a non-created BTC wallet'
@deferred(timeout=20) def test_transfer_not_created(self):
self.should_check_equality = False return self.do_request('wallets/BTC/transfer', expected_code=400, request_type='POST')
'Test transferring assets when providing wrong parameters'
@deferred(timeout=20) def test_transfer_bad_params(self):
self.session.lm.market_community.wallets['BTC'].created = True self.should_check_equality = False return self.do_request('wallets/BTC/transfer', expected_code=400, request_type='POST')
'Test whether we receive the right response when we try a transfer that errors'
@deferred(timeout=20) def test_transfer_error(self):
self.session.lm.market_community.wallets['BTC'].transfer = (lambda *_: fail(Failure(RuntimeError('error')))) self.session.lm.market_community.wallets['BTC'].created = True self.should_check_equality = False post_data = {'amount': 3, 'destination': 'abc'} return self.do_request('wallets/BTC/transfer'...
'Test transferring assets'
@deferred(timeout=20) def test_transfer(self):
self.session.lm.market_community.wallets['BTC'].created = True self.session.lm.market_community.wallets['BTC'].transfer = (lambda *_: succeed('abcd')) self.should_check_equality = False post_data = {'amount': 3, 'destination': 'abc'} return self.do_request('wallets/BTC/transfer', expected_code=200, ...
'Testing whether the API returns an error if an unknown endpoint is queried'
@deferred(timeout=10) def test_channels_unknown_endpoint(self):
self.should_check_equality = False return self.do_request('channels/thisendpointdoesnotexist123', expected_code=404)
'Testing whether the API returns no channels when fetching discovered channels and there are no channels in the database'
@deferred(timeout=10) def test_get_discovered_channels_no_channels(self):
expected_json = {u'channels': []} return self.do_request('channels/discovered', expected_code=200, expected_json=expected_json)
'Testing whether the API returns inserted channels when fetching discovered channels'
@deferred(timeout=10) def test_get_discovered_channels(self):
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): ...
'Testing whether the API returns the right JSON data if a channel is created'
@deferred(timeout=10) def test_my_channel_endpoint_create(self):
def verify_channel_created(body): channel_obj = self.session.lm.channel_manager._channel_list[0] self.assertEqual(channel_obj.name, post_data['name']) self.assertEqual(channel_obj.description, post_data['description']) self.assertEqual(channel_obj.mode, post_data['mode']) sel...
'Testing whether the API returns the right JSON data if a channel is created'
@deferred(timeout=10) def test_my_channel_endpoint_create_default_mode(self):
def verify_channel_created(body): channel_obj = self.session.lm.channel_manager._channel_list[0] self.assertEqual(channel_obj.name, post_data['name']) self.assertEqual(channel_obj.description, post_data['description']) self.assertEqual(channel_obj.mode, u'closed') self.assert...
'Testing whether the API returns a formatted 500 error if DuplicateChannelNameError is raised'
@deferred(timeout=10) def test_my_channel_endpoint_create_duplicate_name_error(self):
def verify_error_message(body): error_response = json.loads(body) expected_response = {u'error': {u'handled': True, u'code': u'DuplicateChannelNameError', u'message': (u'Channel name already exists: %s' % post_data['name'])}} self.assertDictContainsSubset(expected_response[u'erro...
'Testing whether the API returns a 400 and error if the name parameter is not passed'
@deferred(timeout=10) def test_my_channel_endpoint_create_no_name_param(self):
post_data = {'description': "Video's of my cat", 'mode': 'semi-open'} expected_json = {'error': 'channel name cannot be empty'} return self.do_request('channels/discovered', expected_code=400, expected_json=expected_json, request_type='PUT', post_data=post_data)
'Testing whether the API returns the right JSON data if description parameter is not passed'
@deferred(timeout=10) def test_my_channel_endpoint_create_no_description_param(self):
def verify_channel_created(body): channel_obj = self.session.lm.channel_manager._channel_list[0] self.assertEqual(channel_obj.name, post_data['name']) self.assertEqual(channel_obj.description, u'') self.assertEqual(channel_obj.mode, post_data['mode']) self.assertDictEqual(jso...
'The startup method of this class creates a fake Dispersy instance with a fake AllChannel community. It also inserts some random channels so we have some data to work with.'
def setUp(self, autoload_discovery=True):
super(TestChannelsSubscriptionEndpoint, self).setUp(autoload_discovery) self.expected_votecast_cid = None self.expected_votecast_vote = None self.create_votecast_called = False fake_community = self.create_fake_allchannel_community() fake_community.disp_create_votecast = self.on_dispersy_create_...
'Check whether we have the expected parameters when this method is called.'
def on_dispersy_create_votecast(self, cid, vote, _):
self.assertEqual(cid, self.expected_votecast_cid) self.assertEqual(vote, self.expected_votecast_vote) self.create_votecast_called = True return succeed(None)
'Testing whether the API returns error 409 when subscribing to an already subscribed channel'
@deferred(timeout=10) def test_subscribe_channel_already_subscribed(self):
cid = self.insert_channel_in_db('rand1', 42, 'Test channel', 'Test description') self.vote_for_channel(cid, int(time.time())) expected_json = {'error': ALREADY_SUBSCRIBED_RESPONSE_MSG} return self.do_request(('channels/subscribed/%s' % 'rand1'.encode('hex')), expected_code=409, expected_json=expec...
'Testing whether the API creates a request in the AllChannel community when subscribing to a channel'
@deferred(timeout=10) def test_subscribe_channel(self):
def verify_votecast_made(_): self.assertTrue(self.create_votecast_called) expected_json = {'subscribed': True} self.expected_votecast_cid = 'rand1' self.expected_votecast_vote = VOTE_SUBSCRIBE return self.do_request(('channels/subscribed/%s' % 'rand1'.encode('hex')), expected_code=200, expec...
'Testing whether an error is returned when we subscribe to a channel and an error pops up'
@deferred(timeout=10) def test_sub_channel_throw_error(self):
def mocked_vote(*_): return fail(Failure(RuntimeError('error'))) mod_sub_endpoint = ChannelsModifySubscriptionEndpoint(self.session, '') mod_sub_endpoint.vote_for_channel = mocked_vote subscribed_endpoint = self.session.lm.api_manager.root_endpoint.children['channels'].children['subscribed'] ...
'Testing whether the API returns an error when unsubscribing if the channel with the specified CID does not exist'
@deferred(timeout=10) def test_unsubscribe_channel_not_exist(self):
expected_json = {'error': UNKNOWN_CHANNEL_RESPONSE_MSG} return self.do_request('channels/subscribed/abcdef', expected_code=404, expected_json=expected_json, request_type='DELETE')
'Testing whether the API returns error 404 when unsubscribing from an already unsubscribed channel'
@deferred(timeout=10) def test_unsubscribe_channel_not_subscribed(self):
expected_json = {'error': NOT_SUBSCRIBED_RESPONSE_MSG} self.insert_channel_in_db('rand1', 42, 'Test channel', 'Test description') return self.do_request(('channels/subscribed/%s' % 'rand1'.encode('hex')), expected_code=404, expected_json=expected_json, request_type='DELETE')
'Testing whether the API returns no channels when you have not subscribed to any channel'
@deferred(timeout=10) def test_get_subscribed_channels_no_subscriptions(self):
expected_json = {'subscribed': []} return self.do_request('channels/subscribed', expected_code=200, expected_json=expected_json)
'Testing whether the API returns the right channel when subscribed to one channel'
@deferred(timeout=10) def test_get_subscribed_channels_one_subscription(self):
expected_json = {u'subscribed': [{u'description': u'This is a description', u'id': (-1), u'dispersy_cid': unicode('rand'.encode('hex')), u'modified': int(time.time()), u'name': u'Test Channel', u'spam': 0, u'subscribed': True, u'torrents': 0, u'votes': 0}]} cid = self.insert_channel_in_db('rand', 42...
'Testing whether the API creates a request in the AllChannel community when unsubscribing from a channel'
@deferred(timeout=10) def test_unsubscribe_channel(self):
def verify_votecast_made(_): self.assertTrue(self.create_votecast_called) cid = self.insert_channel_in_db('rand1', 42, 'Test channel', 'Test description') self.vote_for_channel(cid, int(time.time())) expected_json = {'unsubscribed': True} self.expected_votecast_cid = 'rand1' self.e...
'Testing whether the API returns some popular channels if the are queried'
@deferred(timeout=10) @inlineCallbacks def test_popular_channels_endpoint(self):
def verify_channels_limit(results): json_results = json.loads(results) self.assertEqual(len(json_results['channels']), 5) def verify_channels(results): json_results = json.loads(results) self.assertEqual(len(json_results['channels']), 9) for i in xrange(0, 9): self.in...
'Testing whether error 400 is returned when a negative limit is passed to the request to fetch popular channels'
@deferred(timeout=10) def test_popular_channels_limit_neg(self):
expected_json = {'error': 'the limit parameter must be a positive number'} return self.do_request('channels/popular?limit=-5', expected_code=400, expected_json=expected_json)
'Testing whether the API returns the right JSON data if a rss feeds from a channel are fetched'
@deferred(timeout=10) def test_rss_feeds_endpoint_with_channel(self):
expected_json = {u'rssfeeds': [{u'url': u'http://test1.com/feed.xml'}, {u'url': u'http://test2.com/feed.xml'}]} channel_name = 'my channel' self.create_fake_channel(channel_name, 'this is a short description') channel_obj = self.session.lm.channel_manager.get_channel(channel_name) for...
'Testing whether the API returns a 404 if no channel has been created when adding a rss feed'
@deferred(timeout=10) def test_add_rss_feed_no_my_channel(self):
self.session.lm.channel_manager = ChannelManager(self.session) channel_cid = 'fakedispersyid'.encode('hex') expected_json = {'error': UNKNOWN_CHANNEL_RESPONSE_MSG} return self.do_request((('channels/discovered/' + channel_cid) + '/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml'), expected_code=404, expecte...
'Testing whether the API returns error 409 if a channel the rss feed already exists'
@deferred(timeout=10) def test_add_rss_feed_conflict(self):
expected_json = {'error': 'this rss feed already exists'} my_channel_id = self.create_fake_channel('my channel', 'this is a short description') channel_obj = self.session.lm.channel_manager.get_my_channel(my_channel_id) channel_obj.create_rss_feed('http://rssfeed.com/rss.xml')...
'Testing whether the API returns a 200 if a channel has been created and when adding a rss feed'
@deferred(timeout=10) def test_add_rss_feed_with_channel(self):
def verify_rss_added(_): channel_obj = self.session.lm.channel_manager.get_my_channel(my_channel_id) self.assertEqual(channel_obj.get_rss_feed_url_list(), ['http://rssfeed.com/rss.xml']) expected_json = {'added': True} my_channel_id = self.create_fake_channel('my channel', 'this is ...
'Testing whether the API returns a 404 if no channel has been created when adding a rss feed'
@deferred(timeout=10) def test_remove_rss_feed_no_channel(self):
self.session.lm.channel_manager = ChannelManager(self.session) expected_json = {'error': UNKNOWN_CHANNEL_RESPONSE_MSG} return self.do_request((('channels/discovered/' + 'fakedispersyid'.encode('hex')) + '/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml'), expected_code=404, expected_json=expected_json, request_...
'Testing whether the API returns a 404 and error if the url parameter does not exist in the existing feeds'
@deferred(timeout=10) def test_remove_rss_feed_invalid_url(self):
expected_json = {'error': 'this url is not added to your RSS feeds'} self.create_fake_channel('my channel', 'this is a short description') return self.do_request((('channels/discovered/' + 'fakedispersyid'.encode('hex')) + '/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml'...
'Testing whether the API returns a 200 if a channel has been created and when removing a rss feed'
@deferred(timeout=10) def test_remove_rss_feed_with_channel(self):
def verify_rss_removed(_): channel_obj = self.session.lm.channel_manager.get_my_channel(my_channel_id) self.assertEqual(channel_obj.get_rss_feed_url_list(), []) expected_json = {'removed': True} my_channel_id = self.create_fake_channel('my channel', 'this is a short descriptio...
'Testing whether the API returns a 404 if no channel has been created when rechecking rss feeds'
@deferred(timeout=10) def test_recheck_rss_feeds_no_channel(self):
self.session.lm.channel_manager = ChannelManager(self.session) expected_json = {'error': UNKNOWN_CHANNEL_RESPONSE_MSG} return self.do_request(('channels/discovered/%s/recheckfeeds' % 'fakedispersyid'.encode('hex')), expected_code=404, expected_json=expected_json, request_type='POST')
'Testing whether the API returns a 200 if the rss feeds are rechecked in your channel'
@deferred(timeout=10) def test_recheck_rss_feeds(self):
expected_json = {'rechecked': True} my_channel_id = self.create_fake_channel('my channel', 'this is a short description') channel_obj = self.session.lm.channel_manager.get_my_channel(my_channel_id) channel_obj._is_created = True channel_obj.create_rss_feed(os.path.join(TESTS_DATA_DIR,...
'Testing whether the API returns unauthorized error if attempting to recheck feeds in another channel'
@deferred(timeout=10) def test_get_rss_feed_no_authorization(self):
self.channel_db_handler.on_channel_from_dispersy('fake', 3, 'test name', 'test description') expected_json = {'error': UNAUTHORIZED_RESPONSE_MSG} return self.do_request(('channels/discovered/%s/rssfeeds' % 'fake'.encode('hex')), expected_code=401, expected_json=expected_json, request_type='GET')
'Testing whether the API returns error 404 if no channel object exists in the channel manager'
@deferred(timeout=10) def test_get_rss_feed_no_channel_obj(self):
self.create_fake_channel('my channel', 'this is a short description') self.session.lm.channel_manager._channel_list = [] expected_json = {'error': UNKNOWN_CHANNEL_RESPONSE_MSG} return self.do_request(('channels/discovered/%s/rssfeeds' % 'fakedispersyid'.encode('hex')), expected_code=404, ...