def mock_items(mock_box_session, mock_object_id): return [ {'type': 'file', 'id': mock_object_id}, {'type': 'folder', 'id': mock_object_id}, {'type': 'file', 'id': mock_object_id}, ], [ File(mock_box_session, mock_object_id), Folder(mock_box_session, mock_object_id), File(mock_box_session, mock_object_id), ]
def test_query_with_owner_users( mock_box_session, test_search, make_mock_box_request, search_query, search_response, search_entries, ): # pylint:disable=redefined-outer-name user1 = User(mock_box_session, '987') user2 = User(mock_box_session, '654') mock_box_session.get.return_value, _ = make_mock_box_request( response=search_response) response = test_search.query( search_query, owner_users=[user1, user2], ) for actual, expected in zip( response, [File(mock_box_session, item['id'], item) for item in search_entries]): assert actual == expected mock_box_session.get.assert_called_once_with( test_search.get_url(), params=Matcher(compare_params, { 'query': search_query, 'owner_user_ids': '987,654' }))
def test_query_with_optional_parameters( mock_box_session, test_search, make_mock_box_request, search_query, search_response, search_entries, kwargs, params ): # pylint:disable=redefined-outer-name mock_box_session.get.return_value, _ = make_mock_box_request(response=search_response) response = test_search.query( search_query, **kwargs ) for actual, expected in zip(response, [File(mock_box_session, item['id'], item) for item in search_entries]): assert actual == expected expected_params = { 'query': search_query, } expected_params.update(params) mock_box_session.get.assert_called_once_with( test_search.get_url(), params=Matcher(compare_params, expected_params) )
def upload_stream(self, file_stream, file_name): """ Upload a file to the folder. The contents are taken from the given file stream, and it will have the given name. :param file_stream: The file-like object containing the bytes :type file_stream: `file` :param file_name: The name to give the file on Box. :type file_name: `unicode` :returns: The newly uploaded file. :rtype: :class:`File` """ url = '{0}/files/content'.format(API.UPLOAD_URL) data = {'attributes': json.dumps({ 'name': file_name, 'parent': {'id': self._object_id}, })} files = { 'file': ('unused', file_stream), } box_response = self._session.post(url, data=data, files=files, expect_json_response=False) file_response = box_response.json()['entries'][0] file_id = file_response['id'] return File( session=self._session, object_id=file_id, response_object=file_response, )
def trail(self): try: return [ BoxFile(File(None, e['id'], e)).to_dict() for e in self._item.path_collection['entries'] ] except AttributeError as e: return []
def trail(self): try: trail = [ BoxFile(File(None, e['id'], e)).to_dict() for e in self._item.path_collection['entries'] ] trail.append(self.to_dict(trail=False)) return trail except AttributeError as e: return []
def token_method(request, mock_box_session, mock_object_id): """ Fixture that returns a partial method based on the method provided in request.param""" if request.param == OAuth2.refresh: return partial(OAuth2.refresh, access_token_to_refresh='fake_access_token') elif request.param == OAuth2.authenticate: return partial(OAuth2.authenticate, auth_code='fake_code') elif request.param == OAuth2.downscope_token: item = File(mock_box_session, mock_object_id) return partial(OAuth2.downscope_token, scopes=[TokenScope.ITEM_READ], item=item)
def test_box_file(self): with open('designsafe/apps/api/fixtures/box/box_file.json') as f: api_resp = f.read() original = File(session=None, object_id=u'5000948880', response_object=json.loads(api_resp)) box_file = BoxFile(original) self.assertEqual('file/5000948880', box_file.id) self.assertEqual('tigers.jpeg', box_file.name) self.assertTrue(box_file.previewable) self.assertEqual('Pictures', box_file.parent_path)
def test_query_with_shared_links( mock_box_session, make_mock_box_request, search_content_types, search_limit, search_offset, search_query, search_result_type, search_value_based_filters, search_with_shared_links_entries, search_with_shared_links_response, test_search, ): # pylint:disable=redefined-outer-name mock_box_session.get.return_value, _ = make_mock_box_request( response=search_with_shared_links_response) response = test_search.query_with_shared_links( search_query, limit=search_limit, offset=search_offset, metadata_filters=search_value_based_filters, result_type=search_result_type, content_types=search_content_types, ) for actual, expected in zip(response, [ File(mock_box_session, entry['item']['id'], entry['item']) for entry in search_with_shared_links_entries ]): assert actual.item == expected mock_box_session.get.assert_called_once_with( test_search.get_url(), params=Matcher( compare_params, { 'query': search_query, 'include_recent_shared_links': True, 'limit': search_limit, 'mdfilters': json.dumps(search_value_based_filters.as_list()), 'offset': search_offset, 'type': search_result_type, 'content_types': ','.join(search_content_types) if search_content_types else search_content_types, }))
def perform(self, oauth): session = AuthorizedSession(oauth) box_f = File(session, self.file_id) with open(self.file_path, 'wb') as f: while True: try: box_f.download_to(f) break except BoxAPIException: if not self.db.get('refreshing').decode('utf-8') == 'true': self.db.set('refreshing', 'true') oauth.refresh(oauth.access_token) self.db.set('refreshing', 'false') else: sleep(3)
def test_search_with_range_filters( mock_box_session, make_mock_box_request, test_search, search_query, search_limit, search_offset, search_range_filters, search_response, search_entries, search_result_type, search_content_types, ): # pylint:disable=redefined-outer-name mock_box_session.get.return_value, _ = make_mock_box_request( response=search_response) response = test_search.search( search_query, limit=search_limit, offset=search_offset, metadata_filters=search_range_filters, result_type=search_result_type, content_types=search_content_types, ) assert response == [ File(mock_box_session, search_entry['id'], search_entry) for search_entry in search_entries ] mock_box_session.get.assert_called_once_with( test_search.get_url(), params=Matcher( compare_params, { 'query': search_query, 'limit': search_limit, 'mdfilters': json.dumps(search_range_filters.as_list()), 'offset': search_offset, 'type': search_result_type, 'content_types': ','.join(search_content_types) if search_content_types else search_content_types, }))
def upload_stream( self, file_stream, file_name, preflight_check=False, preflight_expected_size=0, upload_using_accelerator=False, ): """ Upload a file to the folder. The contents are taken from the given file stream, and it will have the given name. :param file_stream: The file-like object containing the bytes :type file_stream: `file` :param file_name: The name to give the file on Box. :type file_name: `unicode` :param preflight_check: If specified, preflight check will be performed before actually uploading the file. :type preflight_check: `bool` :param preflight_expected_size: The size of the file to be uploaded in bytes, which is used for preflight check. The default value is '0', which means the file size is unknown. :type preflight_expected_size: `int` :param upload_using_accelerator: If specified, the upload will try to use Box Accelerator to speed up the uploads for big files. It will make an extra API call before the actual upload to get the Accelerator upload url, and then make a POST request to that url instead of the default Box upload url. It falls back to normal upload endpoint, if cannot get the Accelerator upload url. Please notice that this is a premium feature, which might not be available to your app. :type upload_using_accelerator: `bool` :returns: The newly uploaded file. :rtype: :class:`File` """ if preflight_check: self.preflight_check(size=preflight_expected_size, name=file_name) url = '{0}/files/content'.format(API.UPLOAD_URL) if upload_using_accelerator: accelerator_upload_url = self._get_accelerator_upload_url_fow_new_uploads( ) if accelerator_upload_url: url = accelerator_upload_url data = { 'attributes': json.dumps({ 'name': file_name, 'parent': { 'id': self._object_id }, }) } files = { 'file': ('unused', file_stream), } box_response = self._session.post(url, data=data, files=files, expect_json_response=False) file_response = box_response.json()['entries'][0] file_id = file_response['id'] return File( session=self._session, object_id=file_id, response_object=file_response, )
def test_file(mock_box_session, mock_object_id): return File(mock_box_session, mock_object_id)
def test_file(mock_box_session, mock_object_id): # pylint:disable=redefined-outer-name return File(mock_box_session, mock_object_id)
def mock_file(mock_box_session): test_file = File(mock_box_session, '11111') return test_file