def test_upload_small_file(self): self.drive.config = drive_config.DriveConfig( {'max_put_size_bytes': 10}) in_fd = io.BytesIO(b'12345') with requests_mock.Mocker() as mock: def callback(request, context): name = request.path_url.split('/')[-2][:-1] self.assertEqual('test', name) self.assertEqual(5, len(request.body.getvalue())) qs = request.path_url.split('?', 1)[1] self.assertEqual( '@name.conflictBehavior=' + options.NameConflictBehavior.FAIL, qs) data = {'id': 'abc', 'name': name, 'size': 5, 'file': {}} context.status_code = codes.created return data mock.put(self.drive.get_item_uri('123', None) + ':/test:/content', json=callback) item = self.drive.upload_file( 'test', data=in_fd, size=5, parent_id='123', conflict_behavior=options.NameConflictBehavior.FAIL) self.assertIsInstance(item, items.OneDriveItem)
def setup_objects(self): self.rename_records = [] self.utime_records = {} self.drive = drive_factory.get_sample_drive_object() self.drive.config = drive_config.DriveConfig({}) self.items_store_mgr = db_factory.get_sample_item_storage_manager() self.items_store = self.items_store_mgr.get_item_storage(self.drive) self.task_pool = db_factory.get_sample_task_pool() self.task_base = tasks.TaskMixin(drive=self.drive, items_store=self.items_store, task_pool=self.task_pool) mock_os.mock_rename(self.rename_records) mock_os.mock_utime(self.utime_records)
def test_path_filter(self): """ Test if the path filter object is properly instantiated. """ config = drive_config.DriveConfig({ 'ignore_files': [ os.path.dirname(sys.modules['onedrive_d.tests'].__file__) + '/data/ignore_list.txt', '/tmp/foo' # bad path should not crash the program ] }) filter = config.path_filter self.assertIsInstance(filter, path_filter.PathFilter) self.assertTrue(filter.should_ignore('/foo')) self.assertTrue(filter.should_ignore('/a.swp'))
def test_set_default_config(self): """ Test both setting default config and differential dumping / loading. """ drive_config.DriveConfig.set_default_config(self.conf) data = dict(self.data) data['ignore_files'] = set(data['ignore_files']) data['ignore_files'].add('/q') data['proxies'] = {'sock5': '1.2.3.4:5'} conf2 = drive_config.DriveConfig(data) dump2 = conf2.dump() self.assertDictEqual({'ignore_files': ['/q'], 'proxies': {'sock5': '1.2.3.4:5'}}, dump2) conf3 = drive_config.DriveConfig.load(dump2) for k in self.data: self.assertEqual(getattr(conf2, k), getattr(conf3, k))
class TestDriveConfig(unittest.TestCase): data = get_data('drive_config.json') conf = drive_config.DriveConfig(data) def test_parse(self): assert_factory.assert_dict_equals_attributes(self, self.data, self.conf) def test_append_default_values(self): del self.data['max_get_size_bytes'] conf = drive_config.DriveConfig(self.data) self.assertEqual(drive_config.DriveConfig.DEFAULT_VALUES['max_get_size_bytes'], conf.max_get_size_bytes) def test_serialize(self): dump = self.conf.dump() new_conf = drive_config.DriveConfig(dump) for k, v in self.data.items(): self.assertEqual(v, getattr(new_conf, k)) def test_set_default_config(self): """ Test both setting default config and differential dumping / loading. """ drive_config.DriveConfig.set_default_config(self.conf) data = dict(self.data) data['ignore_files'] = set(data['ignore_files']) data['ignore_files'].add('/q') data['proxies'] = {'sock5': '1.2.3.4:5'} conf2 = drive_config.DriveConfig(data) dump2 = conf2.dump() self.assertDictEqual({'ignore_files': ['/q'], 'proxies': {'sock5': '1.2.3.4:5'}}, dump2) conf3 = drive_config.DriveConfig.load(dump2) for k in self.data: self.assertEqual(getattr(conf2, k), getattr(conf3, k)) def test_path_filter(self): """ Test if the path filter object is properly instantiated. """ config = drive_config.DriveConfig({ 'ignore_files': [ os.path.dirname(sys.modules['onedrive_d.tests'].__file__) + '/data/ignore_list.txt', '/tmp/foo' # bad path should not crash the program ] }) filter = config.path_filter self.assertIsInstance(filter, path_filter.PathFilter) self.assertTrue(filter.should_ignore('/foo')) self.assertTrue(filter.should_ignore('/a.swp'))
def test_download_small_file(self): self.drive.config = drive_config.DriveConfig( {'max_get_size_bytes': 10}) data = b'12345' output = io.BytesIO() with requests_mock.Mocker() as mock: def callback(request, context): self.assertNotIn('Range', request.headers) context.status_code = codes.ok return data mock.get(self.drive.get_item_uri(item_id='123', item_path=None) + '/content', content=callback) self.drive.download_file(file=output, size=len(data), item_id='123') self.assertEqual(data, output.getvalue())
def test_upload_large_file(self): self.drive.config = drive_config.DriveConfig({'max_put_size_bytes': 2}) session_url = 'https://foo/bar/accept_data' input = io.BytesIO(b'12345') output = io.BytesIO() expected_ranges = ['0-1/5', '2-3/5', '4-4/5'] response_dict = { 'uploadUrl': session_url, 'expirationDateTime': '2020-01-01T00:00:00.0Z', 'nextExpectedRanges': ['0-'] } with requests_mock.Mocker() as mock: def create_session(request, context): body = request.json()['item'] self.assertEqual('test', body['name']) self.assertEqual(options.NameConflictBehavior.RENAME, body['@name.conflictBehavior']) context.status_code = codes.ok return response_dict def accept_data(request, context): self.assertEqual(expected_ranges.pop(0), request.headers['Content-Range']) self.assertLessEqual(len(request.body), self.drive.config.max_put_size_bytes) output.write(request.body) context.status_code = codes.accepted return response_dict mock.post(self.drive.get_item_uri(item_id='123', item_path=None) + ':/test:/upload.createSession', json=create_session) mock.put(session_url, json=accept_data) self.drive.upload_file( 'test', data=input, size=5, parent_id='123', conflict_behavior=options.NameConflictBehavior.RENAME) self.assertEqual(input.getvalue(), output.getvalue())
def test_download_large_file(self): self.drive.config = drive_config.DriveConfig({'max_get_size_bytes': 2}) in_data = b'12345' output = io.BytesIO() expected_ranges = ['0-1', '2-3', '4-4'] with requests_mock.Mocker() as mock: def callback(request, context): self.assertIn('Range', request.headers) r = expected_ranges.pop(0) self.assertEqual('bytes=' + r, request.headers['Range']) f, t = request.headers['Range'].split('=', 1)[1].split('-') context.status_code = codes.partial return in_data[int(f):int(t) + 1] mock.get(self.drive.get_item_uri(item_id='123', item_path=None) + '/content', content=callback) self.drive.download_file(file=output, size=len(in_data), item_id='123') self.assertEqual(in_data, output.getvalue())
def test_serialize(self): dump = self.conf.dump() new_conf = drive_config.DriveConfig(dump) for k, v in self.data.items(): self.assertEqual(v, getattr(new_conf, k))
def test_append_default_values(self): del self.data['max_get_size_bytes'] conf = drive_config.DriveConfig(self.data) self.assertEqual(drive_config.DriveConfig.DEFAULT_VALUES['max_get_size_bytes'], conf.max_get_size_bytes)
def get_sample_drive_object(data=get_data('drive.json')): d = drives.DriveObject(root=get_sample_drive_root(), data=data, config=drive_config.DriveConfig( get_data('drive_config.json'))) return d