class DownloadTests(TestCase): """ Test download TODO: add more tests with different download argument """ def setUp(self): # Initialize a new API instance self.api = API() self.api.login(section='test') if os.path.exists(TEST_DOWNLOAD_FILE): os.remove(TEST_DOWNLOAD_FILE) def test_download(self): # Clean up all files in the downloads directory downloads_directory = self.api.downloads_directory entries = downloads_directory.list() delete_entries(entries) # Upload a file uploaded_file = self.api.upload(TEST_UPLOAD_FILE) assert isinstance(uploaded_file, File) time.sleep(5) entries = downloads_directory.list() assert entries entry = entries[0] entry.download(path=pjoin(DOWNLOADS_DIR)) delete_entries(entries)
class DownloadsDirectoryTests(TestCase): """ Test tasks and files within the downloads directory """ def setUp(self): # Initialize a new API instance self.api = API() self.api.login(section='test') # Clean up all files in the downloads directory downloads_directory = self.api.downloads_directory entries = downloads_directory.list() delete_entries(entries) def test_transferred_task_directories(self): """ Test the task with an associated directory """ filename = TEST_TORRENT2['filename'] info_hash = TEST_TORRENT2['info_hash'] assert self.api.add_task_bt(filename) tasks = self.api.get_tasks() # Get the target task as `task` task = None for _task in tasks: if _task.info_hash == info_hash: task = _task break assert task # Wait until the task is transferred # Task.reload() must be implemented before wait_task_transferred() # can be used here #wait_task_transferred(task) assert task.status_human == 'TRANSFERRED' task_directory = task.directory entries = task_directory.list() dirs = [] files = [] for entry in entries: if isinstance(entry, File): files.append(entry) elif isinstance(entry, Directory): dirs.append(entry) assert len(dirs) == 2 assert len(files) == 1 for directory in dirs: if directory.name == 'BK': assert directory.count == 11 assert len(directory.list()) == 11 if directory.name == 'MP3': assert directory.count == 2 assert len(directory.list()) == 2 def tearDown(self): # Clean up all tasks tasks = self.api.get_tasks() delete_entries(tasks) # Clean up all files in the downloads directory downloads_directory = self.api.downloads_directory entries = downloads_directory.list() delete_entries(entries)
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 URLTaskTests(TestCase): def setUp(self): # Initialize a new API instance self.api = API() self.api.login(section='test') # Clean up all tasks tasks = self.api.get_tasks() delete_entries(tasks) def test_add_task_url_http(self): """ `API.add_task_url(target_url=HTTP)` """ url = TEST_TARGET_URL1['url'] info_hash = TEST_TARGET_URL1['info_hash'] assert self.api.add_task_url(url) wait_task_transferred(self.api.get_tasks, info_hash) tasks = self.api.get_tasks() is_task_created(tasks, info_hash, False) def test_add_task_url_magnet(self): """ `API.add_task_url(target_url=MAGNET)` """ url = TEST_TARGET_URL2['url'] info_hash = TEST_TARGET_URL2['info_hash'] assert self.api.add_task_url(url) wait_task_transferred(self.api.get_tasks, info_hash) tasks = self.api.get_tasks() is_task_created(tasks, info_hash) def tearDown(self): # Clean up all tasks tasks = self.api.get_tasks() delete_entries(tasks)
class TestCookies(TestCase): def setUp(self): if os.path.exists(TEST_COOKIE_FILE): os.remove(TEST_COOKIE_FILE) self.api = API(auto_logout=False, persistent=True, cookies_filename=TEST_COOKIE_FILE) self.api.login(section='test') def test_cookies(self): self.api.__del__() self.api = API(auto_logout=False, persistent=True, cookies_filename=TEST_COOKIE_FILE) assert self.api.has_logged_in
class TaskTests(TestCase): """Test general task interface""" def setUp(self): # Initialize a new API instance self.api = API() self.api.login(section='test') # Clean up all tasks tasks = self.api.get_tasks() delete_entries(tasks) def test_get_tasks(self): filename1 = TEST_TORRENT1['filename'] filename2 = TEST_TORRENT2['filename'] url1 = TEST_TARGET_URL1['url'] url2 = TEST_TARGET_URL2['url'] # Test creation assert self.api.add_task_bt(filename1) assert self.api.add_task_bt(filename2) assert self.api.add_task_url(url1) assert self.api.add_task_url(url2) # Test count time.sleep(5) tasks = self.api.get_tasks() assert len(tasks) == 4 tasks = self.api.get_tasks(3) assert len(tasks) == 3 def test_task_attrs(self): filename1 = TEST_TORRENT1['filename'] url1 = TEST_TARGET_URL1['url'] assert self.api.add_task_bt(filename1) assert self.api.add_task_url(url1) tasks = self.api.get_tasks() for task in tasks: assert isinstance(task.add_time, datetime.datetime) assert isinstance(task.last_update, datetime.datetime) assert isinstance(task.left_time, int) assert task.name assert task.move in [0, 1, 2] assert task.peers >= 0 assert task.percent_done >= 0 assert task.size >= 0 assert task.size_human assert isinstance(task.status, int) assert task.status_human def tearDown(self): # Clean up all tasks tasks = self.api.get_tasks() delete_entries(tasks)
class AuthTests(TestCase): """ Test login and logout """ def setUp(self): # Initialize a new API instance self.api = API() self.api.login(section='test') def test_login(self): assert self.api.login(section='test') def test_logout(self): assert self.api.logout() assert not self.api.has_logged_in
class TestPrivateAPI(TestCase): def __init__(self, *args, **kwargs): super(TestPrivateAPI, self).__init__(*args, **kwargs) self.api = API() self.api.login(section='test') def test_req_offline_space(self): self.api._signatures = {} self.api._lixian_timestamp = None func = getattr(self.api, '_req_offline_space') func() assert 'offline_space' in self.api._signatures assert self.api._lixian_timestamp is not None def test_load_upload_url(self): url = self.api._load_upload_url() assert url
class CookiesTests(TestCase): """ Test cookies """ def setUp(self): if os.path.exists(TEST_COOKIE_FILE): os.remove(TEST_COOKIE_FILE) self.api = API(persistent=True, cookies_filename=TEST_COOKIE_FILE) self.api.login(section='test') self.api.save_cookies() def test_cookies(self): self.api = API(persistent=True, cookies_filename=TEST_COOKIE_FILE) assert self.api.has_logged_in def tearDown(self): if os.path.exists(TEST_COOKIE_FILE): os.remove(TEST_COOKIE_FILE)
class PrivateAPITests(TestCase): """ Test private methods TODO: add more private methods for tests """ def __init__(self, *args, **kwargs): super(PrivateAPITests, self).__init__(*args, **kwargs) self.api = API() self.api.login(section='test') def test_req_offline_space(self): self.api._signatures = {} self.api._lixian_timestamp = None func = getattr(self.api, '_req_offline_space') func() assert 'offline_space' in self.api._signatures assert self.api._lixian_timestamp is not None def test_load_upload_url(self): url = self.api._load_upload_url() assert url
class UploadTests(TestCase): """Test upload""" def setUp(self): # Initialize a new API instance self.api = API() self.api.login(section='test') def test_upload_downloads_directory(self): """Upload to downloads directory (default)""" # Clean up all files in the downloads directory downloads_directory = self.api.downloads_directory entries = downloads_directory.list() delete_entries(entries) uploaded_file = self.api.upload(TEST_UPLOAD_FILE) assert isinstance(uploaded_file, File) time.sleep(5) entries = downloads_directory.list() assert entries entry = entries[0] assert entry.fid == uploaded_file.fid delete_entries(entries)
class SearchTests(TestCase): def setUp(self): # Initialize a new API instance self.api = API() self.api.login(section='test') # Clean up all files in the downloads directory downloads_directory = self.api.downloads_directory entries = downloads_directory.list() delete_entries(entries) # Create a task that is transferred filename = TEST_TORRENT2['filename'] assert self.api.add_task_bt(filename) def test_search(self): keyword1 = '.jpg' count1 = 12 result1 = self.api.search(keyword1) assert len(result1) == count1 keyword2 = 'IMG_0004.jpg' count2 = 1 result2 = self.api.search(keyword2) assert len(result2) == count2 keyword3 = 'nothing' count3 = 0 result3 = self.api.search(keyword3) assert len(result3) == count3 def tearDown(self): # Clean up all tasks tasks = self.api.get_tasks() delete_entries(tasks) # Clean up all files in the downloads directory downloads_directory = self.api.downloads_directory entries = downloads_directory.list() delete_entries(entries)
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)
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 BitTorrentTaskTests(TestCase): """ Test torrent-based BitTorrent tasks """ def setUp(self): # Initialize a new API instance self.api = API() self.api.login(section='test') # Clean up all tasks tasks = self.api.get_tasks() delete_entries(tasks) def test_add_task_bt_select_false(self): """ `API.add_task_bt(filename, select=False)` """ filename = TEST_TORRENT1['filename'] info_hash = TEST_TORRENT1['info_hash'] assert self.api.add_task_bt(filename) wait_task_transferred(self.api.get_tasks, info_hash) tasks = self.api.get_tasks() assert is_task_created(tasks, info_hash) def test_add_task_bt_select_true(self): """ `API.add_task_bt(filename, select=True)` """ filename = TEST_TORRENT2['filename'] info_hash = TEST_TORRENT2['info_hash'] torrent = self.api.add_task_bt(filename, select=True) assert isinstance(torrent, Torrent) torrent.files[0].unselect() torrent.files[1].unselect() assert len(torrent.selected_files) == torrent.file_count - 2 assert torrent.submit() wait_task_transferred(self.api.get_tasks, info_hash) tasks = self.api.get_tasks() assert is_task_created(tasks, info_hash) def test_delete_tasks(self): """ `Task.delete()` """ filename = TEST_TORRENT2['filename'] info_hash = TEST_TORRENT2['info_hash'] assert self.api.add_task_bt(filename) wait_task_transferred(self.api.get_tasks, info_hash) tasks = self.api.get_tasks() is_task_created = False for task in tasks: if task.info_hash == info_hash: is_task_created = True assert not task.is_deleted assert task.delete() with self.assertRaises(TaskError) as cm: task.delete() assert cm.exception.message == 'This task is already deleted.' assert task.is_deleted break else: assert is_task_created def tearDown(self): # Clean up all tasks tasks = self.api.get_tasks() delete_entries(tasks)
class FileTests(TestCase): """Test file manipulation in downloads directory""" def setUp(self): # Initialize a new API instance self.api = API() self.api.login(section='test') def test_move_files(self): """Move files from downloads directory to its parent directory""" # Clean up all files in the downloads directory downloads_directory = self.api.downloads_directory entries = downloads_directory.list() delete_entries(entries) uploaded_file = self.api.upload(TEST_UPLOAD_FILE) assert isinstance(uploaded_file, File) time.sleep(5) entries = downloads_directory.list() assert entries entry = entries[0] assert entry.fid == uploaded_file.fid dest_dir = downloads_directory.parent assert self.api.move([entry], dest_dir) old_entry = entry assert old_entry.cid == dest_dir.cid for entry in dest_dir.list(): if isinstance(entry, File): assert entry.fid == old_entry.fid break else: assert False # Test moving directories dir1 = self.api.mkdir(downloads_directory, TEST_EDIT_FILENAME) assert self.api.move([dir1], dest_dir) old_dir1 = dir1 assert old_dir1.pid == dest_dir.cid for entry in dest_dir.list(): if isinstance(entry, Directory): if entry != downloads_directory: assert entry == old_dir1 break else: assert False entries = [ entry for entry in dest_dir.list() if entry != downloads_directory ] delete_entries(entries) def test_edit_files(self): """Move files from downloads directory to its parent directory""" # Clean up all files in the downloads directory downloads_directory = self.api.downloads_directory entries = downloads_directory.list() delete_entries(entries) uploaded_file = self.api.upload(TEST_UPLOAD_FILE) assert isinstance(uploaded_file, File) time.sleep(5) entries = downloads_directory.list() assert entries entry = entries[0] assert entry.fid == uploaded_file.fid assert self.api.edit(entry, TEST_EDIT_FILENAME) edited_entry = downloads_directory.list()[0] assert edited_entry.name == TEST_EDIT_FILENAME def test_mkdir(self): # Clean up all files in the downloads directory downloads_directory = self.api.downloads_directory entries = downloads_directory.list() delete_entries(entries) new_dir = self.api.mkdir(downloads_directory, TEST_NEW_DIRNAME) assert new_dir.name == TEST_NEW_DIRNAME assert new_dir.parent == downloads_directory new_dir2 = downloads_directory.list()[0] assert new_dir2 == new_dir entries = downloads_directory.list() delete_entries(entries)