From 0914d920526a0928400fdba731b6c574b758a62f Mon Sep 17 00:00:00 2001 From: "Nicholas H.Tollervey" Date: Wed, 22 Jan 2020 09:56:25 +0000 Subject: [PATCH] Tests pass... Feedback required. --- securedrop_client/storage.py | 12 +++++++----- tests/test_logic.py | 8 +++++--- tests/test_storage.py | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/securedrop_client/storage.py b/securedrop_client/storage.py index 5bfe0c35f..172991bb0 100644 --- a/securedrop_client/storage.py +++ b/securedrop_client/storage.py @@ -47,13 +47,15 @@ def get_local_sources(session: Session) -> List[Source]: return session.query(Source).all() -def delete_local_source_by_uuid(session: Session, uuid: str) -> Source: +def delete_local_source_by_uuid(session: Session, uuid: str) -> None: """ - Return the source with the referenced UUID. + Delete the source with the referenced UUID. """ - source = session.query(Source).filter_by(uuid=uuid) - session.delete(source) - session.commit() + source = session.query(Source).filter_by(uuid=uuid).one_or_none() + if source: + session.delete(source) + session.commit() + logger.info("Deleted source with UUID {} from local database.".format(uuid)) def get_local_messages(session: Session) -> List[Message]: diff --git a/tests/test_logic.py b/tests/test_logic.py index 5df603c2f..be68625da 100644 --- a/tests/test_logic.py +++ b/tests/test_logic.py @@ -1271,10 +1271,12 @@ def test_Controller_on_delete_source_success(homedir, config, mocker, session_ma Using the `config` fixture to ensure the config is written to disk. ''' mock_gui = mocker.MagicMock() + storage = mocker.patch('securedrop_client.logic.storage') co = Controller('http://localhost', mock_gui, session_maker, homedir) - co.sync_api = mocker.MagicMock() - co.on_delete_source_success(True) - co.sync_api.assert_called_with() + co.update_sources = mocker.MagicMock() + co.on_delete_source_success("uuid") + storage.delete_local_source_by_uuid.assert_called_once_with(co.session, "uuid") + assert co.update_sources.call_count == 1 def test_Controller_on_delete_source_failure(homedir, config, mocker, session_maker): diff --git a/tests/test_storage.py b/tests/test_storage.py index 55408a0e1..125d6ca60 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -17,7 +17,7 @@ delete_single_submission_or_reply_on_disk, rename_file, get_local_files, find_new_files, \ source_exists, set_message_or_reply_content, mark_as_downloaded, mark_as_decrypted, get_file, \ get_message, get_reply, update_and_get_user, update_missing_files, mark_as_not_downloaded, \ - mark_all_pending_drafts_as_failed + mark_all_pending_drafts_as_failed, delete_local_source_by_uuid from securedrop_client import db from tests import factory @@ -72,6 +72,22 @@ def test_get_local_sources(mocker): mock_session.query.assert_called_once_with(securedrop_client.db.Source) +def test_delete_local_source_by_uuid(mocker): + """ + Delete the referenced source in the session. + """ + mock_session = mocker.MagicMock() + source = make_remote_source() + mock_session.query().filter_by().one_or_none.return_value = source + mock_session.query.reset_mock() + delete_local_source_by_uuid(mock_session, "uuid") + mock_session.query.assert_called_once_with(securedrop_client.db.Source) + mock_session.query().filter_by.assert_called_once_with(uuid="uuid") + assert mock_session.query().filter_by().one_or_none.call_count == 1 + mock_session.delete.assert_called_once_with(source) + mock_session.commit.assert_called_once_with() + + def test_get_local_messages(mocker): """ At this moment, just return all messages.