Skip to content

Commit

Permalink
test: update tests for file_downloaded signal
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Mar 27, 2019
1 parent 5fe36d5 commit 5a15b6f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 20 deletions.
93 changes: 73 additions & 20 deletions tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,16 +673,15 @@ 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)
file_ = factory.File(is_downloaded=True)

fw = FileWidget(source, message, mock_controller, align='left')
fw = FileWidget(source, file_, 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


Expand All @@ -691,15 +690,14 @@ 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)
file_ = factory.File(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, file_, 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


Expand All @@ -708,11 +706,11 @@ 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)
file_ = factory.File(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_)

Expand All @@ -722,15 +720,70 @@ 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)
file_ = factory.File(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_ = factory.File(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_ = factory.File(is_downloaded=True)

fw = FileWidget(source, file_, mock_controller, mock_signal)
fw.clear = mocker.MagicMock()
fw.update = mocker.MagicMock()

fw._on_file_download(file_.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_ = factory.File(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.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_ready = 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()
Expand All @@ -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_ready.emit.assert_called_once_with(test_object_uuid)


def test_Client_on_file_downloaded_api_failure(homedir, config, mocker):
'''
Expand All @@ -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_ready = 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"
Expand All @@ -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_ready.emit.assert_not_called()


def test_Client_on_file_downloaded_decrypt_failure(homedir, config, mocker):
Expand All @@ -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_ready = 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()
Expand All @@ -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_ready.emit.assert_not_called()


def test_Client_on_file_download_user_not_signed_in(homedir, config, mocker):
Expand Down

0 comments on commit 5a15b6f

Please sign in to comment.