def test_auto_renew_tokens(self, mocker): account = account_factory.get_sample_personal_account() url = 'https://test_url' normal_data = [(get_data('error_token_expired.json'), requests.codes.forbidden), ({}, requests.codes.ok)] new_tokens = get_data('personal_access_token_alt.json') def normal_callback(request, context): data, status_code = normal_data.pop(0) context.status_code = status_code return data def renew_callback(request, context): body = parse_qs(request.body) # parse_qs returns dict[str, [int | str]] for k, v in body.items(): self.assertEqual(1, len(v)) body[k] = v.pop() self.assert_compare(self.assertEqual, account.client, body, ['client_id', 'client_secret', 'redirect_uri']) self.assertEqual(account.refresh_token, body['refresh_token']) self.assertEqual('refresh_token', body['grant_type']) context.status_code = requests.codes.ok return new_tokens compares = ['access_token', 'refresh_token', 'token_type'] self.assert_compare(self.assertNotEqual, account, new_tokens, compares) mocker.post(url, json=normal_callback) mocker.post(account.client.OAUTH_TOKEN_URI, json=renew_callback) restapi.ManagedRESTClient(session=requests.Session(), net_mon=None, account=account).post(url) self.assert_compare(self.assertEqual, account, new_tokens, compares)
def callback(request, context): response_drives = [get_data('drive.json'), get_data('drive.json')] ids = [str(i) for i in range(0, 2)] for d in response_drives: d['id'] = ids.pop(0) context.status_code = codes.ok return {'value': response_drives}
def test_auto_renew_tokens(self, mocker): account = account_factory.get_sample_personal_account() url = 'https://test_url' normal_data = [(get_data('error_token_expired.json'), requests.codes.forbidden), ({}, requests.codes.ok)] new_tokens = get_data('personal_access_token_alt.json') def normal_callback(request, context): data, status_code = normal_data.pop(0) context.status_code = status_code return data def renew_callback(request, context): body = parse_qs( request.body) # parse_qs returns dict[str, [int | str]] for k, v in body.items(): self.assertEqual(1, len(v)) body[k] = v.pop() self.assert_compare(self.assertEqual, account.client, body, ['client_id', 'client_secret', 'redirect_uri']) self.assertEqual(account.refresh_token, body['refresh_token']) self.assertEqual('refresh_token', body['grant_type']) context.status_code = requests.codes.ok return new_tokens compares = ['access_token', 'refresh_token', 'token_type'] self.assert_compare(self.assertNotEqual, account, new_tokens, compares) mocker.post(url, json=normal_callback) mocker.post(account.client.OAUTH_TOKEN_URI, json=renew_callback) restapi.ManagedRESTClient(session=requests.Session(), net_mon=None, account=account).post(url) self.assert_compare(self.assertEqual, account, new_tokens, compares)
def test_purge_cache(self, mock): data = get_data('drive_alt.json') drive_root, drive = self.get_cached_drive_root_tuple() mock.get(self.account.client.API_URI + '/drives/' + drive.drive_id, json=data) self.assertNotEqual(data['id'], drive.drive_id) self.assertIs(drive_root.get_drive(drive.drive_id, skip_cache=True), drive) self.assertEqual(data['id'], drive.drive_id)
def setUp(self): self.data = get_data('user_profile.json') self.props = [ 'user_id', 'gender', 'locale', 'first_name', 'last_name', 'name' ] self.props_map = {'user_id': 'id'} self.profile = resources.UserProfile(self.data)
def test_update(self, mock_request): def callback(request, context): self.data['percentageComplete'] += 50 if self.data['percentageComplete'] == 100: self.data['status'] = options.AsyncOperationStatuses.COMPLETED context.status_code = requests.codes.see_other else: self.data[ 'status'] = options.AsyncOperationStatuses.IN_PROGRESS context.status_code = requests.codes.accepted return self.data mock_request.get('https://foo', json=callback, headers={'location': 'https://bar'}) mock_request.get('https://bar', json=get_data('image_item.json')) expects = [(50, options.AsyncOperationStatuses.IN_PROGRESS), (100, options.AsyncOperationStatuses.COMPLETED)] for exp in expects: self.session.update_status() exp_percentage, exp_status = exp self.assertEqual(self.data['operation'], self.session.operation) self.assertEqual(exp_percentage, self.session.percentage_complete) self.assertEqual(exp_status, self.session.status) self.assertIsInstance(self.session.get_item(), items.OneDriveItem)
def test_handle(self, mock_request): output = io.BytesIO() data = get_data('image_item.json') def put_callback(request, context): output.write(request.body.getvalue()) context.status_code = codes.created return data def patch_callback(request, context): j = request.json() self.assertEqual('1970-01-01T00:00:01Z', j['fileSystemInfo']['lastModifiedDateTime']) context.status_code = codes.ok return data mock_request.put(self.drive.get_item_uri(None, None) + '/test:/content', json=put_callback) mock_request.patch(self.drive.drive_uri + self.drive.drive_path + '/items/' + data['id'], json=patch_callback) os.path.getmtime = lambda p: 1 m = mock.mock_open() m.return_value = io.BytesIO(self.in_data) with mock.patch('builtins.open', m, create=True): self.task.handle() m.assert_called_once_with(self.file_path, 'rb') self.assertEqual(self.in_data, output.getvalue())
def test_client_time(self): self.data['fileSystemInfo'] = get_data('facets/filesysteminfo_facet.json') self.assertNotEqual(self.data['fileSystemInfo']['createdDateTime'], self.data['createdDateTime']) self.assertNotEqual(self.data['fileSystemInfo']['lastModifiedDateTime'], self.data['lastModifiedDateTime']) item = items.OneDriveItem(get_sample_drive_object(), self.data) self.assert_timestamps(self.data['fileSystemInfo'], item) self.assert_timestamps(self.data['fileSystemInfo'], item.fs_info)
def test_profile(self, mock): mock.get('https://apis.live.net/v5.0/me', json=get_data('user_profile.json'), status_code=requests.codes.ok) profile = self.account.profile self.assertIsInstance(profile, resources.UserProfile) self.account.profile = None self.assertIsNone(self.account.profile)
def callback(request, context): data = request.json() self.assertEqual(folder_name, data['name']) self.assertDictEqual({}, data['folder']) self.assertEqual(conflict_behavior, data['@name.conflictBehavior']) context.status_code = codes.created return get_data('new_dir_item.json')
def setUp(self): super().setup_objects() self.all_items = [] for filename in ['folder_item.json', 'folder_child_item.json']: item = items.OneDriveItem(self.drive, get_data(filename)) self.all_items.append(item) self.items_store.update_item(item) self.task = tasks.RemoveItemTask(task_base=self.task_base, local_parent_path='', name='Public', is_folder=True)
def setUp(self): self.data = get_data('image_item.json') self.data['audio'] = get_data('facets/audio_facet.json') self.data['deleted'] = get_data('facets/deleted_facet.json') self.data['photo'] = get_data('facets/photo_facet.json') self.data['video'] = get_data('facets/video_facet.json') self.data['specialFolder'] = get_data('facets/specialfolder_facet.json') self.data['location'] = get_data('facets/location_facet.json') self.data['parentReference'] = get_data('item_reference.json') self.item = items.OneDriveItem(drive=get_sample_drive_object(), data=self.data)
def setUp(self): super().setup_objects() item_data = get_data('image_item.json') item_data['name'] = 'test' item_data['size'] = 1 self.item = items.OneDriveItem(self.drive, item_data) self.task = tasks.DownloadFileTask(self.task_base, item=self.item) self.file_path = self.drive.config.local_root + '/test' self.tmp_file_path = self.drive.config.local_root + '/' + self.task.get_temp_filename()
def test_deserialize_account_bad_profile(self, mock): d = {} mock.get('https://apis.live.net/v5.0/me', json=get_data('user_profile.json'), status_code=codes.ok) self.account_store.deserialize_account_row( self.personal_account.profile.user_id, self.personal_account.TYPE, self.personal_account.dump(), self.personal_account.profile.dump() + '.', d) self.assertEqual(1, len(d))
def callback(request, context): json = request.json() self.assertEqual(json['name'], new_params['new_name']) self.assertEqual(json['description'], new_params['new_description']) self.assertDictEqual(json['parentReference'], new_params['new_parent_reference'].data) self.assertDictEqual(json['fileSystemInfo'], new_params['new_file_system_info'].data) return get_data('image_item.json')
def setUp(self): self.data = get_data('image_item.json') self.data['audio'] = get_data('facets/audio_facet.json') self.data['deleted'] = get_data('facets/deleted_facet.json') self.data['photo'] = get_data('facets/photo_facet.json') self.data['video'] = get_data('facets/video_facet.json') self.data['specialFolder'] = get_data( 'facets/specialfolder_facet.json') self.data['location'] = get_data('facets/location_facet.json') self.data['parentReference'] = get_data('item_reference.json') self.item = items.OneDriveItem(drive=get_sample_drive_object(), data=self.data)
def test_assemble_drive_error(self): d = {} drive = drives.DriveObject(self.drive_root, get_data('drive.json'), DriveConfig.default_config()) rows = [ ('did', 'aid', 'at', drive.dump() + '.'), (drive.drive_id, self.personal_account.profile.user_id, self.personal_account.TYPE, drive.dump() + '.') ] for r in rows: self.drives_store.assemble_drive_record(r, d) self.assertEqual(0, len(d), str(r))
def test_assemble_drive_error(self): d = {} drive = drives.DriveObject(self.drive_root, get_data('drive.json'), DriveConfig.default_config()) rows = [('did', 'aid', 'at', drive.dump() + '.'), (drive.drive_id, self.personal_account.profile.user_id, self.personal_account.TYPE, drive.dump() + '.')] for r in rows: self.drives_store.assemble_drive_record(r, d) self.assertEqual(0, len(d), str(r))
def test_get_all_drives(self): drive_conf = dict(DriveConfig.DEFAULT_VALUES) drive_conf['local_root'] = '/tmp' drive = drives.DriveObject(self.drive_root, get_data('drive.json'), DriveConfig(drive_conf)) self.drives_store.add_record(drive) for k, drive_value in self.drives_store.get_all_drives().items(): drive_id, account_id, account_type = k self.assertEqual(drive_id, drive_value.drive_id) self.assertEqual(drive.drive_id, drive_value.drive_id) self.assertEqual(drive.config.local_root, drive_value.config.local_root)
def test_client_time(self): self.data['fileSystemInfo'] = get_data( 'facets/filesysteminfo_facet.json') self.assertNotEqual(self.data['fileSystemInfo']['createdDateTime'], self.data['createdDateTime']) self.assertNotEqual( self.data['fileSystemInfo']['lastModifiedDateTime'], self.data['lastModifiedDateTime']) item = items.OneDriveItem(get_sample_drive_object(), self.data) self.assert_timestamps(self.data['fileSystemInfo'], item) self.assert_timestamps(self.data['fileSystemInfo'], item.fs_info)
def test_parse(self): data = get_data('upload_session.json') session = resources.UploadSession(data) self.assertEqual(data['uploadUrl'], session.upload_url) self.assertEqual(str_to_datetime(data['expirationDateTime']), session.expires_at) ranges = [] for r in session.next_ranges: if r[1] is None: ranges.append(str(r[0]) + '-') else: ranges.append('%d-%d' % r) self.assertListEqual(data['nextExpectedRanges'], ranges)
def run_get_drive(self, drive_id): """ :param str | None drive_id: """ with requests_mock.Mocker() as mock: path = '/drive' if drive_id is not None: path += 's/' + drive_id mock.get(self.account.client.API_URI + path, json=get_data('drive.json')) if drive_id is not None: drive = self.root.get_drive(drive_id) else: drive = self.root.get_default_drive() self.assertIsInstance(drive, drives.DriveObject)
def test_handle(self, mock_request): data = get_data('image_item.json') def patch_callback(request, context): j = request.json() self.assertEqual('1970-01-01T00:00:01Z', j['fileSystemInfo']['lastModifiedDateTime']) context.status_code = codes.ok return data mock_request.put(self.drive.get_item_uri(None, None) + '/test:/content', json=data, status_code=codes.created) mock_request.patch(self.drive.drive_uri + self.drive.drive_path + '/items/' + data['id'], json=patch_callback) m = mock.mock_open() m.return_value = io.BytesIO(self.in_data) with mock.patch('builtins.open', m, create=True): self.task.handle()
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'))
class TestItemReference(unittest.TestCase): data = get_data('item_reference.json') def test_parse(self): ref = resources.ItemReference(self.data) self.assertEqual(self.data['driveId'], ref.drive_id) self.assertEqual(self.data['id'], ref.id) self.assertEqual(self.data['path'], ref.path) def test_build(self): ref = resources.ItemReference.build(id='AnotherValue', path='/foo/bar') self.assertEqual('AnotherValue', ref.id) self.assertEqual('/foo/bar', ref.path) def test_build_errors(self): self.assertRaises(ValueError, resources.ItemReference.build, id=None, path=None)
def use_item_collection(self, method_name, url, params): item_set = get_data('item_collection.json')['value'] item_names = [i['name'] for i in item_set] next_link = 'https://get_children' with requests_mock.Mocker() as mock: def callback(request, context): data = {'value': [item_set.pop(0)]} if len(item_set) > 0: data['@odata.nextLink'] = next_link context.status_code = codes.ok return data mock.get(url, json=callback) mock.get(next_link, json=callback) collection = getattr(self.drive, method_name)(**params) received_names = [] while collection.has_next: page = collection.get_next() for i in page: received_names.append(i.name) self.assertListEqual(item_names, received_names)
def test_update(self, mock_request): def callback(request, context): self.data['percentageComplete'] += 50 if self.data['percentageComplete'] == 100: self.data['status'] = options.AsyncOperationStatuses.COMPLETED context.status_code = requests.codes.see_other else: self.data['status'] = options.AsyncOperationStatuses.IN_PROGRESS context.status_code = requests.codes.accepted return self.data mock_request.get('https://foo', json=callback, headers={'location': 'https://bar'}) mock_request.get('https://bar', json=get_data('image_item.json')) expects = [(50, options.AsyncOperationStatuses.IN_PROGRESS), (100, options.AsyncOperationStatuses.COMPLETED)] for exp in expects: self.session.update_status() exp_percentage, exp_status = exp self.assertEqual(self.data['operation'], self.session.operation) self.assertEqual(exp_percentage, self.session.percentage_complete) self.assertEqual(exp_status, self.session.status) self.assertIsInstance(self.session.get_item(), items.OneDriveItem)
def test_parse_type1(self): data = get_data('error_type1.json') error = errors.OneDriveError(data) self.assertEqual(data['error']['code'], error.errno) self.assertEqual(data['error']['message'], error.strerror)
def setUp(self): self.data = get_data('facets/permission_facet.json') self.facet = facets.PermissionFacet(self.data)
from onedrive_d.api import accounts from onedrive_d.api import resources from onedrive_d.tests import get_data PERSONAL_ACCOUNT_DATA = get_data('personal_access_token.json') USER_PROFILE_DATA = get_data('user_profile.json') def get_sample_personal_account(client=None): if client is None: from . import client_factory client = client_factory.get_sample_personal_client() account = accounts.PersonalAccount(client, PERSONAL_ACCOUNT_DATA) account.profile = resources.UserProfile(USER_PROFILE_DATA) return account
def setUp(self): self.data = get_data('facets/filesysteminfo_facet.json') self.facet = facets.FileSystemInfoFacet(self.data)
def assert_facet(self, filename, facet): data = get_data('facets/' + filename) obj = facet(data) assert_properties(self, data, obj) return obj
def setUp(self): self.data = { 'http_retry_after_seconds': 40, 'default_drive_config': DriveConfig(get_data('drive_config.json')) } self.user_conf = user_config.UserConfig(self.data)
def test_renew_session_failure(self, mock): mock.post(self.account.client.OAUTH_TOKEN_URI, json=get_data('error_type1.json'), status_code=requests.codes.bad) self.assertRaises(errors.OneDriveError, self.account.renew_tokens)
def test_parse_type2(self): data = get_data('error_type2.json') error = errors.OneDriveError(data) self.assertEqual(data['error'], error.errno) self.assertEqual(data['error_description'], error.strerror)
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
def setUp(self): self.data = get_data('image_item.json') self.assertNotIn('fileSystemInfo', self.data)
def setUp(self): self.data = get_data('user_profile.json') self.props = ['user_id', 'gender', 'locale', 'first_name', 'last_name', 'name'] self.props_map = {'user_id': 'id'} self.profile = resources.UserProfile(self.data)
def _add_item(self, filename): data = get_data(filename) item = items.OneDriveItem(self.drive, data) self.itemdb.update_item(item, items_db.ItemRecordStatuses.OK) self.all_items.append(item) self.all_items_data.append(data)
def callback(request, context): context.status_code = requests.codes.bad return get_data('error_token_expired.json')
def raise_error(self, filename, expected_error): data = get_data(filename) self.assertIsInstance(errors.OneDriveError(data), expected_error)
def setUp(self): self.data = get_data('drive_root.json') self.item = items.OneDriveItem(drive=get_sample_drive_object(), data=self.data)