Skip to content

Commit

Permalink
cleanup files during the failure case too
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Oct 22, 2019
1 parent 3d22675 commit 6d3e940
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion securedrop_client/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 9 additions & 0 deletions securedrop_client/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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]],
Expand Down
4 changes: 2 additions & 2 deletions tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 6d3e940

Please sign in to comment.