Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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),
]
@pytest.mark.parametrize('subclass', [BaseAPIJSONObject, BaseObject, Folder])
def test_meta_does_not_register_new_subclass_in_default_translator_if_item_type_is_not_defined_in_namespace(
subclass,
default_translator,
original_default_translator,
):
class Foo(subclass):
pass
assert Foo not in default_translator.values()
assert default_translator == original_default_translator
# pylint:disable=redefined-outer-name
expected_url = "{0}/users/{1}/folders/0".format(API.BASE_API_URL, mock_user.object_id)
expected_body = json.dumps({
'owned_by': {
'id': mock_user.object_id
},
})
move_items_response = {
'type': 'folder',
'id': '12345',
'name': 'That Other User\'s Content',
}
mock_box_session.put.return_value.json.return_value = move_items_response
moved_item = mock_user.transfer_content(mock_user, notify=notify, fields=fields)
mock_box_session.put.assert_called_once_with(expected_url, data=expected_body, params=expected_params)
assert isinstance(moved_item, Folder)
assert moved_item.id == move_items_response['id']
assert moved_item.name == move_items_response['name']
assert moved_item._session == mock_box_session # pylint:disable=protected-access
def test_search_instantiates_search_and_calls_search(
mock_client,
mock_box_session,
search_response,
file_id,
folder_id,
):
# pylint:disable=redefined-outer-name
mock_box_session.get.return_value = search_response
search_term = 'lolcatz'
search_result = mock_client.search().query(
search_term,
10,
0,
ancestor_folders=[Folder(mock_box_session, folder_id)],
file_extensions=['.jpg'],
)
assert search_result.next().object_id == file_id
assert search_result.next().object_id == folder_id
'id': '22222',
'name': 'Test File',
'modified_by': {
'type': 'user',
'id': '33333',
'name': 'Test User',
},
},
],
}
results = default_translator.translate(mock_box_session, response_object=response_object)
test_folder = results['entries'][0]
test_file = results['entries'][1]
assert isinstance(test_folder, Folder)
assert isinstance(test_file, File)
assert test_folder.object_id == '11111'
assert test_folder.name == 'Test Folder'
assert test_file.object_id == '22222'
assert test_file.name == 'Test File'
user_1 = test_folder.created_by
user_2 = test_file.modified_by
assert isinstance(user_1, User)
assert isinstance(user_2, User)
assert user_1 == user_2
assert test_folder._session == mock_box_session # pylint:disable=protected-access
assert test_file._session == mock_box_session # pylint:disable=protected-access
assert user_1._session == mock_box_session # pylint:disable=protected-access
assert user_2._session == mock_box_session # pylint:disable=protected-access
def test_meta_overrides_registration_if_subclass_redefines_item_type(default_translator, original_default_translator):
class FolderSubclass(Folder):
_item_type = 'folder'
assert default_translator.get('folder') is FolderSubclass
assert set(default_translator.keys()) == set(original_default_translator.keys())
events_response, # initial call to get now stream position
Timeout,
reconnect_long_poll_response,
max_retries_long_poll_response,
new_change_long_poll_response,
events_response,
new_change_long_poll_response,
empty_events_response,
EscapeGenerator("A fake exception for the session to throw so that the generator won't block forever"),
]
events = test_events.generate_events_with_long_polling(**stream_type_kwargs)
event = next(events)
assert isinstance(event, Event)
assert event.event_id == mock_event_json['event_id']
assert isinstance(event.source, Folder)
assert event.source.id == mock_event_json['source']['id']
with pytest.raises(EscapeGenerator):
next(events)
events.close()
mock_box_session.options.assert_called_with(expected_url, params=expected_stream_type_params)
mock_box_session.get.assert_any_call(expected_url, params={'stream_position': 'now', 'limit': 0, 'stream_type': expected_stream_type})
assert '/events' in expected_url
mock_box_session.get.assert_any_call(
expected_url,
params=dict(limit=100, stream_position=initial_stream_position, **expected_stream_type_params),
)
mock_box_session.get.assert_any_call(
long_poll_url,
timeout=retry_timeout,
params={'stream_position': initial_stream_position},
)
def objects_for_comparison(test_file, test_folder, mock_user, request, mock_box_session):
cases = {
'identical_object': (test_file, test_file, True),
'same_file': (test_file, File(mock_box_session, test_file.object_id), True),
'same_folder': (test_folder, Folder(mock_box_session, test_folder.object_id), True),
'same_user': (mock_user, User(mock_box_session, mock_user.object_id), True),
'against_none': (test_file, None, False),
'different_ids': (File(mock_box_session, '1'), File(mock_box_session, '2'), False),
'different_types': (File(mock_box_session, '1'), Folder(mock_box_session, '1'), False),
'not_api_object': (test_file, {'id': test_file.object_id, 'type': 'file'}, False),
}
return cases[request.param]
:param recursive:
Whether or not the folder should be deleted if it isn't empty.
:type recursive:
`bool`
:param etag:
If specified, instruct the Box API to delete the folder only if the current version's etag matches.
:type etag:
`unicode` or None
:returns:
Whether or not the update was successful.
:rtype:
`bool`
:raises: :class:`BoxAPIException` if the specified etag doesn't match the latest version of the folder.
"""
# pylint:disable=arguments-differ
return super(Folder, self).delete({'recursive': recursive}, etag)