From 6d3e940d085884ace3adfece58a2db031568a2d7 Mon Sep 17 00:00:00 2001 From: Allie Crevier Date: Tue, 22 Oct 2019 10:57:22 -0700 Subject: [PATCH] cleanup files during the failure case too --- securedrop_client/export.py | 3 ++- securedrop_client/logic.py | 9 +++++++++ tests/test_export.py | 4 ++-- tests/test_logic.py | 12 ++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/securedrop_client/export.py b/securedrop_client/export.py index 56dace7782..be371f0d1a 100644 --- a/securedrop_client/export.py +++ b/securedrop_client/export.py @@ -256,4 +256,5 @@ def send_file_to_usb_device(self, filepaths: List[str], passphrase: str) -> None logger.debug('Export successful') self.export_usb_call_success.emit(filepaths) except ExportError as e: - self.export_usb_call_failure.emit(e.status) + logger.error(e) + self.export_usb_call_failure.emit(filepaths) diff --git a/securedrop_client/logic.py b/securedrop_client/logic.py index cc8932fcf2..01affb18e8 100644 --- a/securedrop_client/logic.py +++ b/securedrop_client/logic.py @@ -181,6 +181,7 @@ def __init__(self, hostname: str, gui, session_maker: sessionmaker, self.export = Export() self.export.export_usb_call_success.connect(self.on_export_usb_call_success) + self.export.export_usb_call_failure.connect(self.on_export_usb_call_failure) self.sync_flag = os.path.join(home, 'sync_flag') @@ -675,6 +676,14 @@ def on_export_usb_call_success(self, filepaths: List[str]): if os.path.exists(filepath): os.remove(filepath) + def on_export_usb_call_failure(self, filepaths: List[str]): + ''' + Clean export files that are hard links to the file on disk. + ''' + for filepath in filepaths: + if os.path.exists(filepath): + os.remove(filepath) + def on_submission_download( self, submission_type: Union[Type[db.File], Type[db.Message]], diff --git a/tests/test_export.py b/tests/test_export.py index 659c2b03ec..af735a9140 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -36,13 +36,13 @@ def test_send_file_to_usb_device_error(mocker): export = Export() export.export_usb_call_failure = mocker.MagicMock() export.export_usb_call_failure.emit = mocker.MagicMock() - error = ExportError('bang') + error = ExportError('[mock_filepath]') _run_disk_export = mocker.patch.object(export, '_run_disk_export', side_effect=error) export.send_file_to_usb_device(['mock_filepath'], 'mock passphrase') _run_disk_export.assert_called_once_with('mock_temp_dir', ['mock_filepath'], 'mock passphrase') - export.export_usb_call_failure.emit.assert_called_once_with(error.status) + export.export_usb_call_failure.emit.assert_called_once_with(['mock_filepath']) def test_run_preflight_checks(mocker): diff --git a/tests/test_logic.py b/tests/test_logic.py index c067f20565..eb7a951c22 100644 --- a/tests/test_logic.py +++ b/tests/test_logic.py @@ -1762,6 +1762,18 @@ def test_on_export_usb_call_success(mocker, homedir): assert os_remove.call_args_list[1][0][0] == 'mock_filepath_2' +def test_on_export_usb_call_failure(mocker, homedir): + co = Controller('http://localhost', mocker.MagicMock(), mocker.MagicMock(), homedir) + mocker.patch('os.path.exists', return_value=True) + os_remove = mocker.patch('os.remove') + + co.on_export_usb_call_failure(['mock_filepath_1', 'mock_filepath_2']) + + assert os_remove.call_count == 2 + assert os_remove.call_args_list[0][0][0] == 'mock_filepath_1' + assert os_remove.call_args_list[1][0][0] == 'mock_filepath_2' + + def test_get_file(mocker, session, homedir): co = Controller('http://localhost', mocker.MagicMock(), mocker.MagicMock(), homedir) storage = mocker.patch('securedrop_client.logic.storage')