diff --git a/tests/gui/test_widgets.py b/tests/gui/test_widgets.py index 4cd6f2c81d..70db7e4eba 100644 --- a/tests/gui/test_widgets.py +++ b/tests/gui/test_widgets.py @@ -673,16 +673,16 @@ def test_FileWidget_init_left(mocker): Check the FileWidget is configured correctly for align-left. """ mock_controller = mocker.MagicMock() + mock_signal = mocker.MagicMock() # not important for this test source = factory.Source() message = db.Message(source=source, uuid='uuid', size=123, filename='1-mah-reply.gpg', download_url='http://mah-server/mah-reply-url', is_downloaded=True) - fw = FileWidget(source, message, mock_controller, align='left') + fw = FileWidget(source, message, mock_controller, mock_signal, align='left') - layout = fw.layout() - assert isinstance(layout.takeAt(0), QWidgetItem) - assert isinstance(layout.takeAt(0), QWidgetItem) - assert isinstance(layout.takeAt(0), QSpacerItem) + assert isinstance(fw.layout.takeAt(0), QWidgetItem) + assert isinstance(fw.layout.takeAt(0), QWidgetItem) + assert isinstance(fw.layout.takeAt(0), QSpacerItem) assert fw.controller == mock_controller @@ -691,15 +691,15 @@ def test_FileWidget_init_right(mocker): Check the FileWidget is configured correctly for align-right. """ mock_controller = mocker.MagicMock() + mock_signal = mocker.MagicMock() # not important for this test source = factory.Source() message = db.Message(source=source, uuid='uuid', size=123, filename='1-mah-reply.gpg', download_url='http://mah-server/mah-reply-url', is_downloaded=True) - fw = FileWidget(source, message, mock_controller, align='right') - layout = fw.layout() - assert isinstance(layout.takeAt(0), QSpacerItem) - assert isinstance(layout.takeAt(0), QWidgetItem) - assert isinstance(layout.takeAt(0), QWidgetItem) + fw = FileWidget(source, message, mock_controller, mock_signal, align='right') + assert isinstance(fw.layout.takeAt(0), QSpacerItem) + assert isinstance(fw.layout.takeAt(0), QWidgetItem) + assert isinstance(fw.layout.takeAt(0), QWidgetItem) assert fw.controller == mock_controller @@ -708,11 +708,12 @@ def test_FileWidget_mousePressEvent_download(mocker): Should fire the expected download event handler in the logic layer. """ mock_controller = mocker.MagicMock() + mock_signal = mocker.MagicMock() # not important for this test source = factory.Source() file_ = db.File(source=source, uuid='uuid', size=123, filename='1-mah-reply.gpg', download_url='http://mah-server/mah-reply-url', is_downloaded=False) - fw = FileWidget(source, file_, mock_controller) + fw = FileWidget(source, file_, mock_controller, mock_signal) fw.mouseReleaseEvent(None) fw.controller.on_file_download.assert_called_once_with(source, file_) @@ -722,15 +723,74 @@ def test_FileWidget_mousePressEvent_open(mocker): Should fire the expected open event handler in the logic layer. """ mock_controller = mocker.MagicMock() + mock_signal = mocker.MagicMock() # not important for this test source = factory.Source() file_ = db.File(source=source, uuid='uuid', size=123, filename='1-mah-reply.gpg', download_url='http://mah-server/mah-reply-url', is_downloaded=True) - fw = FileWidget(source, file_, mock_controller) + fw = FileWidget(source, file_, mock_controller, mock_signal) fw.mouseReleaseEvent(None) fw.controller.on_file_open.assert_called_once_with(file_) +def test_FileWidget_clear_deletes_items(mocker, homedir): + """ + Calling the clear() method on FileWidget should delete the existing items in the layout. + """ + mock_controller = mocker.MagicMock() + mock_signal = mocker.MagicMock() # not important for this test + source = factory.Source() + file_ = db.File(source=source, uuid='uuid', size=123, filename='1-mah-reply.gpg', + download_url='http://mah-server/mah-reply-url', is_downloaded=True) + + fw = FileWidget(source, file_, mock_controller, mock_signal) + assert fw.layout.count() != 0 + + fw.clear() + + assert fw.layout.count() == 0 + + +def test_FileWidget_on_file_download_updates_items_when_uuid_matches(mocker, homedir): + """ + The _on_file_download method should clear and update the FileWidget + """ + mock_controller = mocker.MagicMock() + mock_signal = mocker.MagicMock() # not important for this test + source = factory.Source() + file_ = db.File(source=source, uuid='uuid', size=123, filename='1-mah-reply.gpg', + download_url='http://mah-server/mah-reply-url', is_downloaded=True) + + fw = FileWidget(source, file_, mock_controller, mock_signal) + fw.clear = mocker.MagicMock() + fw.update = mocker.MagicMock() + + fw._on_file_download('uuid') + + fw.clear.assert_called_once_with() + fw.update.assert_called_once_with() + + +def test_FileWidget_on_file_download_updates_items_when_uuid_does_not_match(mocker, homedir): + """ + The _on_file_download method should clear and update the FileWidget + """ + mock_controller = mocker.MagicMock() + mock_signal = mocker.MagicMock() # not important for this test + source = factory.Source() + file_ = db.File(source=source, uuid='uuid', size=123, filename='1-mah-reply.gpg', + download_url='http://mah-server/mah-reply-url', is_downloaded=True) + + fw = FileWidget(source, file_, mock_controller, mock_signal) + fw.clear = mocker.MagicMock() + fw.update = mocker.MagicMock() + + fw._on_file_download('not a matching uuid') + + fw.clear.assert_not_called() + fw.update.assert_not_called() + + def test_ConversationView_init(mocker, homedir): """ Ensure the conversation view has a layout to add widgets to. diff --git a/tests/test_logic.py b/tests/test_logic.py index 9d8cc9ceaf..618be17268 100644 --- a/tests/test_logic.py +++ b/tests/test_logic.py @@ -974,6 +974,7 @@ def test_Client_on_file_downloaded_success(homedir, config, mocker): cl = Client('http://localhost', mock_gui, mock_session, homedir) cl.update_sources = mocker.MagicMock() cl.api_runner = mocker.MagicMock() + cl.file_downloaded = mocker.MagicMock() # signal when file is downloaded test_filename = "1-my-file-location-msg.gpg" test_object_uuid = 'uuid-of-downloaded-object' cl.call_reset = mocker.MagicMock() @@ -991,6 +992,9 @@ def test_Client_on_file_downloaded_success(homedir, config, mocker): mock_storage.set_object_decryption_status_with_content.assert_called_once_with( submission_db_object, mock_session, True) + # Signal should be emitted with UUID of the successfully downloaded object + cl.file_downloaded.emit.assert_called_once_with(test_object_uuid) + def test_Client_on_file_downloaded_api_failure(homedir, config, mocker): ''' @@ -999,6 +1003,7 @@ def test_Client_on_file_downloaded_api_failure(homedir, config, mocker): mock_gui = mocker.MagicMock() mock_session = mocker.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, homedir) + cl.file_downloaded = mocker.MagicMock() # signal when file is downloaded cl.update_sources = mocker.MagicMock() cl.api_runner = mocker.MagicMock() test_filename = "1-my-file-location-msg.gpg" @@ -1012,6 +1017,7 @@ def test_Client_on_file_downloaded_api_failure(homedir, config, mocker): cl.on_file_downloaded(result_data, current_object=submission_db_object) cl.set_status.assert_called_once_with( "The file download failed. Please try again.") + cl.file_downloaded.emit.assert_not_called() def test_Client_on_file_downloaded_decrypt_failure(homedir, config, mocker): @@ -1023,6 +1029,7 @@ def test_Client_on_file_downloaded_decrypt_failure(homedir, config, mocker): cl = Client('http://localhost', mock_gui, mock_session, homedir) cl.update_sources = mocker.MagicMock() cl.api_runner = mocker.MagicMock() + cl.file_downloaded = mocker.MagicMock() # signal when file is downloaded test_filename = "1-my-file-location-msg.gpg" cl.api_runner.result = ("", test_filename) cl.set_status = mocker.MagicMock() @@ -1041,6 +1048,7 @@ def test_Client_on_file_downloaded_decrypt_failure(homedir, config, mocker): "Failed to decrypt file, please try again or talk to your administrator.") mock_storage.set_object_decryption_status_with_content.assert_called_once_with( submission_db_object, mock_session, False) + cl.file_downloaded.emit.assert_not_called() def test_Client_on_file_download_user_not_signed_in(homedir, config, mocker):