Skip to content

Commit

Permalink
Throw a try/except in FileWidget.update_file_size
Browse files Browse the repository at this point in the history
I can't get this method to fail even if the FileWidget's file has been
deleted, but just in case.
  • Loading branch information
rmol committed Mar 24, 2020
1 parent bccdd55 commit b907c09
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2245,7 +2245,11 @@ def __init__(
file_missing.connect(self._on_file_missing, type=Qt.QueuedConnection)

def update_file_size(self):
self.file_size.setText(humanize_filesize(self.file.size))
try:
self.file_size.setText(humanize_filesize(self.file.size))
except Exception as e:
logger.error(f"Could not update file size on FileWidget: {e}")
self.file_size.setText("")

def eventFilter(self, obj, event):
t = event.type()
Expand Down
20 changes: 20 additions & 0 deletions tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2397,6 +2397,26 @@ def test_FileWidget__on_print_clicked_missing_file(mocker, session, source):
dialog.assert_not_called()


def test_FileWidget_update_file_size_with_deleted_file(
mocker, homedir, config, session_maker, source
):
mock_gui = mocker.MagicMock()
controller = logic.Controller('http://localhost', mock_gui, session_maker, homedir)

file = factory.File(source=source['source'], is_downloaded=True)
controller.session.add(file)
controller.session.commit()

fw = FileWidget(file.uuid, controller, mocker.MagicMock(), mocker.MagicMock(), 0)

with mocker.patch(
"securedrop_client.gui.widgets.humanize_filesize",
side_effect=Exception("boom!")
):
fw.update_file_size()
assert fw.file_size.text() == ""


@pytest.mark.parametrize("key", [Qt.Key_Enter, Qt.Key_Return])
def test_ModalDialog_keyPressEvent_does_not_close_on_enter_or_return(mocker, key):
dialog = ModalDialog()
Expand Down

0 comments on commit b907c09

Please sign in to comment.