def one_drive_storage(): """Fixture of onedrive storage instance with a model set """ def cred_reader(*args, **kwargs): """Fake storage_cred_reader""" print('reader') print(args) print(kwargs) return json.dumps({}) def cred_writer(*args, **kwargs): """Fake storage_cred_writer""" print('writer') print(args) print(kwargs) storage = onedrive.OneDrive(event_sink=None, storage_id='test', storage_cred_reader=cred_reader, storage_cred_writer=cred_writer) storage.model = bushn.IndexingNode('root', indexes=['_id']) folder_a = storage.model.add_child('folder_a', {'_id': '1'}) folder_a.add_child( 'file_b', { 'share_id': TEST_ID, 'shared': True, '_shared_with': {'36e8c86a197301fc', '32326b368801ea5d'} }) return storage
def _create_root_node(self): root = bushn.IndexingNode(name=None, indexes=['_id', '_list_id']) root.props[jars.METRICS] = self.get_storage_metrics() root.props['_id'] = self.root_id logger.info('New root created: %s', root) return root
def root(request): """Create a default Root node TODO: Copy pasted during move to jars. Is there a better place? :return: Node """ if request.param: return bushn.Node(name=None) else: return bushn.IndexingNode(name=None, indexes=['_id'])
def create_new_model(self, name=None): """Create a new root node and return it. Calls the api and sets the props accordingly. """ model = bushn.IndexingNode(name=None, indexes=['_id']) for group_info in self.list_avaliable_groups(): instance = self._get_sub_storage_by_group_meta( group_meta=group_info) instance.model.parent = model model.props['metrics'] = jars.StorageMetrics(self.storage_id, free_space=0, total_space=0) return model
def create_new_model(self, name=None): """Create a new root node, by calling the api and setting the props accordingly.""" model = bushn.IndexingNode(name=name, indexes=['_id']) # set the metrics url = self.urls.drive_info(group_id=self.group_id) drive_info = self.api.get(url, params={'select': 'quota'}).json() model.props['metrics'] = jars.StorageMetrics( self.storage_id, free_space=drive_info['quota']['remaining'], total_space=drive_info['quota']['total']) # set the id of the root node url = self.urls.root_info(self.group_id) root_info = self.api.get(url, params={'select': 'id'}).json() root_id = root_info['id'] model.props['_id'] = root_id return model
def create_new_model(api, storage_id): """Create a new root node, by calling the api and setting the props accordingly. """ drive_info = api.get(endpoint='drive').json() root_info = api.get(endpoint='drive/root:/:/', params={ 'select': 'id' }).json() model = bushn.IndexingNode(name=None, indexes=['_id']) model.props['metrics'] = jars.StorageMetrics( storage_id, free_space=drive_info['quota']['remaining'], total_space=drive_info['quota']['total']) root_id = root_info['id'] model.props['_id'] = root_id return model
def test_get_update(mocker): """Assert share root is deleted from the tree.""" def cred_reader(*args, **kwargs): """Fake storage_cred_reader""" print('reader') print(args) print(kwargs) return json.dumps({}) def cred_writer(*args, **kwargs): """Fake storage_cred_writer""" print('writer') print(args) print(kwargs) with mocker.patch('jars.microsoft.onedrive.OneDrive._oauth_get_unique_id'): storage = onedrive.OneDrive(event_sink=None, storage_id='test', storage_cred_reader=cred_reader, storage_cred_writer=cred_writer) storage.model = bushn.IndexingNode('root', indexes=['_id'], props={'_id': '0'}) file_b = storage.model.add_child( 'file_b', { 'share_id': TEST_ID, 'shared': True, '_shared_with': {'36e8c86a197301fc', '32326b368801ea5d'}, '_id': '2', 'is_dir': True, '_share_root': True }) with mocker.patch('jars.microsoft.onedrive.OneDriveApi.get_delta', side_effect=fake_get_delta), \ mocker.patch('jars.microsoft.onedrive.iter_share_roots', return_value=file_b), \ mocker.patch('jars.TreeToSyncEngineEngineAdapter') as mock_adapter: storage.get_update(storage.model, True) # pylint: disable=protected-access assert mock_adapter._on_delete.called_with(file_b) assert file_b not in storage.model
def init_dropbox_with_shared_folder(): """Initialize Test Dropbox storage.""" with mock.patch("jars.dropbox.Dropbox", new=MagicMock()): storage = jars.dropbox.Dropbox(storage_id='dropbox', event_sink=MagicMock(), storage_cache_dir=tempfile.gettempdir(), storage_cred_reader=lambda: '{"access_token":123}', storage_cred_writer=None) storage.model = bushn.IndexingNode('root', indexes=['_id', '_shared_folder_id']) folder_a = storage.model.add_child('a', {'_id': '1'}) folder_a.add_child('b', {'_id': '2', '_shared_folder_id': '12345'}) # Ensure the our shared node is actually part of the index. assert storage.model.index['_shared_folder_id'].keys() == {'12345'} # Ensure the share_id is properly set from the fixture. shared_node = storage.model.index['_shared_folder_id']['12345'] assert shared_node.props['_shared_folder_id'] == '12345' assert len(storage.model) == 2 + 1 return storage
def __init__(self, event_sink, storage_id, storage_cred_reader, storage_cred_writer, storage_cache_dir=None, api_root=ONEDRIVE_PRIVATE_API_URL, polling_interval=10): """Initializer of the storage :param event_sink: sink to report events to :param storage_id: unique storage id assigned to this storage :param storage_cred_reader: reader that shall be used to read credentials :param storage_cred_writer: writer to store credentials or other auth data :param storage_cache_dir: directory to cache model :param polling_interval: interval with which to poll the server. """ super().__init__(event_sink, storage_id, storage_cred_reader, storage_cred_writer, storage_cache_dir, default_model=bushn.IndexingNode(name=None, indexes=['_id'])) # todo why not simply set self.event_sink in super() self.event_sink = self._event_sink self.event_poller = None self.drive_id = self._oauth_get_unique_id(self.oauth_session, api_root=api_root) """The id used by Onedrive to identify this user. aka Drive ID in Onedrive docs""" self.api = self.API_CLASS(self.oauth_session, api_root, drive_id=self.drive_id) self.polling_interval = polling_interval """If a polling interval has been passed, use it, otherwise use the default"""
def clear_model(self): """Empty the model. Used in testing.""" self.model = bushn.IndexingNode(None, indexes=['_id'])
def clear_model(self): """Reset the model to an empty state. """ self.model = bushn.IndexingNode(None, indexes=['_id'])