class PublicAPITests(TestCase): """Test public methods""" def __init__(self, *args, **kwargs): """Initialize once for all test functions in this TestCase""" super(PublicAPITests, self).__init__(*args, **kwargs) self.api = API() self.api.login(section='test') def test_get_storage_info(self): storage_info = self.api.get_storage_info() assert 'total' in storage_info assert 'used' in storage_info def test_task_count(self): task_count = self.api.task_count assert isinstance(task_count, int) assert task_count >= 0 def test_task_quota(self): task_quota = self.api.task_quota assert isinstance(task_quota, int) def test_get_user_info(self): user_info = self.api.get_user_info() assert isinstance(user_info, dict) def test_user_id(self): user_id = self.api.user_id assert int(user_id) def test_username(self): username = self.api.username assert isinstance(username, str)
class TestAPI(TestCase): def __init__(self, *args, **kwargs): super(TestAPI, self).__init__(*args, **kwargs) self.api = API() self.api.login(section='test') def test_storage_info(self): res = self.api.get_storage_info() assert 'total' in res assert 'used' in res res = self.api.get_storage_info(human=True) assert 'total' in res assert 'used' in res def test_tasks_directories(self): time.sleep(5) # Avoid 'Task is being transferred' task_count = self.api.task_count tasks = self.api.get_tasks(LARGE_COUNT) self.assertEqual(len(tasks), task_count) tasks = self.api.get_tasks(SMALL_COUNT) self.assertEqual(len(tasks), SMALL_COUNT) t = None for tt in tasks: if isinstance(tt, Directory): t = tt break else: return t = tasks[0] dd = self.api.downloads_directory if t.status_human == 'TRANSFERRED': d = t.directory p = t.parent assert d.parent is p assert p.cid == dd.cid assert t.count == len(t.list(LARGE_COUNT)) for t in tasks: if t.info_hash == TEST_TORRENT2['info_hash']: td = t.directory entries = td.list() for entry in entries: if isinstance(entry, Directory): entry.list() elif isinstance(entry, File): assert entry.url def test_delete_file(self): tasks = self.api.get_tasks() for t in tasks: if t.info_hash == TEST_TORRENT2['info_hash']: # Delete file try: d1 = t.directory except TaskError: time.sleep(20) try: d1 = t.directory except TaskError: return d1_count = d1.count d2 = d1.list()[0] d2_count = d2.count files = d2.list() f1 = files[0] assert f1.delete() d2.reload() assert d2.count == d2_count - 1 # Sleep to avoid JobError time.sleep(2) assert d2.delete() d1.reload() assert d1.count == d1_count - 1 def test_search(self): """Directory is assumed to have more than 40 torrent files""" keyword = 'torrent' s1 = self.api.search(keyword) assert len(s1) == 30 s2 = self.api.search(keyword, 10) assert len(s2) == 10 s3 = self.api.search(keyword, 40) assert len(s3) == 40
class TestAPI(TestCase): def __init__(self, *args, **kwargs): super(TestAPI, self).__init__(*args, **kwargs) self.api = API() self.api.login(section='test') def test_login_logout(self): credential = conf.get_credential('test') username = credential['username'] password = credential['password'] if self.api.has_logged_in: assert self.api.logout() assert self.api.login(username, password) def test_login_credentials(self): if self.api.has_logged_in: assert self.api.logout() assert self.api.login(section='test') def test_tasks_directories(self): task_count = self.api.task_count tasks = self.api.get_tasks(LARGE_COUNT) self.assertEqual(len(tasks), task_count) tasks = self.api.get_tasks(SMALL_COUNT) self.assertEqual(len(tasks), SMALL_COUNT) t = tasks[0] dd = self.api.downloads_directory if t.status_human == 'TRANSFERRED': d = t.directory p = t.parent assert d.parent is p assert p.cid == dd.cid assert t.count == len(t.list(LARGE_COUNT)) for t in tasks: if t.info_hash == TEST_TORRENT2['info_hash']: td = t.directory entries = td.list() for entry in entries: if isinstance(entry, Directory): entry.list() elif isinstance(entry, File): assert entry.url def test_delete_file(self): tasks = self.api.get_tasks() for t in tasks: if t.info_hash == TEST_TORRENT2['info_hash']: # Delete file try: d1 = t.directory except TaskError: time.sleep(20) try: d1 = t.directory except TaskError: return d1_count = d1.count d2 = d1.list()[1] d2_count = d2.count files = d2.list() f1 = files[0] assert f1.delete() d2.reload() assert d2.count == d2_count - 1 # Sleep to avoid JobError time.sleep(2) assert d2.delete() d1.reload() assert d1.count == d1_count - 1 def test_add_delete_task_bt(self): h1 = TEST_TORRENT1['info_hash'] h2 = TEST_TORRENT2['info_hash'] tasks = self.api.get_tasks() for task in tasks: if task.info_hash == h1: assert task.delete() if task.info_hash == h2: assert task.delete() assert self.api.add_task_bt(TEST_TORRENT1['filename']) u = self.api.add_task_bt(TEST_TORRENT2['filename'], select=True) assert isinstance(u, Torrent) files = u.files file_count = u.file_count files[0].unselect() files[1].unselect() assert len(u.selected_files) == file_count - 2 assert u.submit() def test_storage_info(self): res = self.api.get_storage_info() assert 'total' in res assert 'used' in res res = self.api.get_storage_info(human=True) assert 'total' in res assert 'used' in res def test_add_task_url(self): ''' NOT FINISHED YET! TODO: * Check the target_url is not in the task list already. * add the target_url * checked it added successfully ''' res = self.api.add_task_url(TEST_TARGET_URL)