Esempio n. 1
0
    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)
Esempio n. 2
0
 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}
Esempio n. 3
0
 def setUp(self):
     data = get_data('folder_item.json')
     parent_task = get_sample_task_base()
     self.item = OneDriveItem(data=data, drive=parent_task.drive)
     self.task = DeleteItemTask(parent_task, rel_parent_path='/', item_name=self.item.name,
                                is_folder=self.item.is_folder)
     self.task.items_store.update_item(self.item)
 def test_handle_failed_fallback(self, mock_req):
     os.path.isfile = lambda p: True
     self.build_mock(mock_req, LOCATION_STR, requests.codes.accepted, get_data('async_failed.json'))
     self.task.handle()
     t = self.task.task_pool.pop_task()
     self.assertIsInstance(t, up_task.UploadFileTask)
     self.assertEqual(self.task.local_path, t.local_path)
Esempio n. 5
0
 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):
     task_base = get_sample_task_base()
     task_base.rel_parent_path = '/'
     task_base.item_name = 'to.txt'
     self.data = get_data('image_item.json')
     self.async_status = AsyncCopySession(task_base.drive, {'Location': LOCATION_STR})
     self.task = copy_task.AsyncCopyMonitorTask(task_base, self.async_status)
 def test_handle_in_progress(self, mock_req):
     self.assertIsNone(self.task.task_pool.pop_task())
     copy_task.AsyncCopyMonitorTask.POLLING_INTERVAL_SEC = 0.005
     self.build_mock(mock_req, LOCATION_STR, requests.codes.accepted, get_data('async_in_progress.json'))
     self.task.handle()
     time.sleep(0.02)
     self.assertIs(self.task, self.task.task_pool.pop_task())
 def setUp(self):
     self.data = get_data('folder_item.json')
     parent_task = get_sample_task_base()
     self.task = CreateDirTask(parent_task, '/', self.data['name'])
     self.mock_call = mock.Mock(return_value=OneDriveItem(drive=self.task.drive, data=self.data))
     self.task.drive.create_dir = self.mock_call
     self._backup_isdir = os.path.isdir
Esempio n. 9
0
 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')
Esempio n. 10
0
 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)
Esempio n. 11
0
 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)
Esempio n. 12
0
 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):
     self.data = get_data('image_item.json')
     self.data['name'] = 'test'
     self.data['size'] = 1
     self.parent_task = get_sample_task_base()
     self.item = OneDriveItem(drive=self.parent_task.drive, data=self.data)
     # The '/' in relative path is generated by MergeDirTask at root. Merging root itself has rel parent path ''.
     self.task = DownloadFileTask(self.parent_task, rel_parent_path='/', item=self.item)
     self.calls_hist = setup_os_mock()
Esempio n. 14
0
 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))
Esempio n. 15
0
 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)
Esempio n. 16
0
 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))
Esempio n. 17
0
 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)
Esempio n. 18
0
 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)
Esempio n. 19
0
    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)
Esempio n. 20
0
    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
                context.headers['location'] = 'https://bar'
            else:
                self.data['status'] = options.AsyncOperationStatuses.IN_PROGRESS
                context.status_code = requests.codes.accepted
            return self.data

        mock_request.get('https://foo', json=callback)
        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)
Esempio n. 21
0
 def callback(request, context):
     data = get_data('drive_root.json')
     context.status_code = codes.ok
     return data
Esempio n. 22
0
 def setUp(self):
     self.data = get_data('drive.json')
     self.drive = drive_factory.get_sample_drive_object(self.data)
Esempio n. 23
0
 def test_handle_error(self):
     self.task.drive.copy_item = mock.Mock(side_effect=OneDriveError(get_data('error_type1.json')))
     self.task.handle()
Esempio n. 24
0
 def setUp(self):
     task_base = get_sample_task_base()
     self.data = get_data('image_item.json')
     self.item = OneDriveItem(drive=task_base.drive, data=self.data)
     self.task = copy_task.AsyncCopyItemTask(task_base, '/', 'to.txt', self.item)
Esempio n. 25
0
 def setUp(self):
     self.data = get_data('facets/permission_facet.json')
     self.facet = facets.PermissionFacet(self.data)
Esempio n. 26
0
 def assert_facet(self, filename, facet):
     data = get_data('facets/' + filename)
     obj = facet(data)
     assert_properties(self, data, obj)
     return obj
Esempio n. 27
0
 def setUp(self):
     self.data = get_data('facets/filesysteminfo_facet.json')
     self.facet = facets.FileSystemInfoFacet(self.data)
Esempio n. 28
0
 def setUp(self):
     self.data = get_data('image_item.json')
     self.assertNotIn('fileSystemInfo', self.data)
Esempio n. 29
0
 def callback(request, context):
     context.status_code = requests.codes.bad
     return get_data('error_token_expired.json')
Esempio n. 30
0
 def setUp(self):
     self.data = get_data('drive_root.json')
     self.item = items.OneDriveItem(drive=get_sample_drive_object(), data=self.data)