Skip to content

Commit

Permalink
Fix tests using magicmocked controller
Browse files Browse the repository at this point in the history
  • Loading branch information
zenmonkeykstop committed Nov 15, 2024
1 parent be83b09 commit 5e3b8a7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
50 changes: 42 additions & 8 deletions client/tests/gui/source/delete/test_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def dialog(request):
# Give the source(s) a submission
for source in request.param:
factory.File(source=source)
return DeleteSourceDialog(request.param)
return DeleteSourceDialog(request.param, len(request.param) + 1)


class TestDeleteSourceDialog:
Expand Down Expand Up @@ -68,24 +68,58 @@ def test_no_sources_continue_button_not_shown(self, dialog):
def test_correct_format_body_text(self):
"""
For n > 1 sources, ensure the warning text includes
all the journalist desginators.
all the journalist designators.
"""
sources = []
names = [
"source one",
"source two",
"source three",
"source four",
"source five",
"source six",
"source seven",
]

for item in names:
source = factory.Source(journalist_designation=item)
sources.append(source)
# pretend we've selected all but one source
fake_total = len(sources) + 1

dialog = DeleteSourceDialog(sources)
dialog = DeleteSourceDialog(sources, fake_total)
dialog_text = dialog.make_body_text(sources, fake_total)

assert "All sources have been selected" not in dialog_text
for n in names:
assert n in dialog.make_body_text(sources)
assert n in dialog_text

def test_correct_format_body_text_all_selected(self):
"""
Ensure that warning has been added when all sources selected
"""
sources = []
names = [
"source one",
"source two",
"source three",
]

for item in names:
source = factory.Source(journalist_designation=item)
sources.append(source)

dialog = DeleteSourceDialog(sources, len(sources))

assert "All sources have been selected" in dialog.make_body_text(sources, len(sources))

def test_correct_format_body_text_truncated(self):
"""
Ensure that source list is truncated correctly when over display limit
"""
sources = []
names = [f"source_{i}" for i in range(1, 46)]

for item in names:
source = factory.Source(journalist_designation=item)
sources.append(source)

dialog = DeleteSourceDialog(sources, len(sources))

assert "plus 15 additional sources" in dialog.make_body_text(sources, len(sources))
4 changes: 2 additions & 2 deletions client/tests/gui/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_deletes_conversation_when_dialog_accepted(self):

self.action.trigger()

self._controller.delete_conversation.assert_called_once_with(self._source)
(self._controller.delete_conversation.assert_called_once_with(self._source),)
self._app_state.remove_conversation_files.assert_called_once_with(
state.ConversationId("some_conversation")
)
Expand Down Expand Up @@ -91,7 +91,7 @@ def setUp(self):
self._controller = MagicMock(Controller, api=True)
self._dialog = QDialog()

def _dialog_constructor(source: Source) -> QDialog:
def _dialog_constructor(source: Source, source_total: int) -> QDialog:
return self._dialog

self.action = DeleteSourceAction(self._source, _menu, self._controller, _dialog_constructor)
Expand Down
45 changes: 36 additions & 9 deletions client/tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,9 @@ def test_MainView_delete_conversation_when_conv_wrapper_exists(mocker):
Ensure SourceConversationWrapper is deleted if it exists.
"""
source = factory.Source(uuid="123")
conversation_wrapper = SourceConversationWrapper(source, mocker.MagicMock())
controller = mocker.MagicMock()
controller.get_source_count.return_value = 2
conversation_wrapper = SourceConversationWrapper(source, controller)
conversation_wrapper.deleteLater = mocker.MagicMock()
mv = MainView(None)
mv.source_conversations = {}
Expand Down Expand Up @@ -644,6 +646,7 @@ def test_MainView_on_source_changed(mocker):
mv.source_list.selectedItems = mocker.MagicMock(return_value=[source])
mv.source_list.count = mocker.MagicMock(return_value=3)
mv.controller = mocker.MagicMock(is_authenticated=True)
mv.controller.get_source_count.return_value = 3
mocker.patch("securedrop_client.gui.widgets.source_exists", return_value=True)
mv.on_source_changed()

Expand Down Expand Up @@ -755,6 +758,7 @@ def test_MainView_on_source_changed_updates_conversation_view(mocker, session):
mv.source_list.selectedItems = mocker.MagicMock(return_value=[source])
mv.source_list.get_selected_source = mocker.MagicMock(return_value=source)
mv.controller = mocker.MagicMock(is_authenticated=True)
mv.controller.get_source_count.return_value = 1
session.add(source)
file = factory.File(source=source, filename="0-mock-doc.gpg")
message = factory.Message(source=source, filename="0-mock-msg.gpg")
Expand Down Expand Up @@ -811,6 +815,8 @@ def test_MainView_on_source_changed_SourceConversationWrapper_is_preserved(mocke

mv.controller = mocker.MagicMock(is_authenticated=True)

mv.controller.get_source_count.return_value = 2

mv.on_source_changed()
assert mv.set_conversation.call_count == 1
assert source.uuid in mv.source_conversations
Expand Down Expand Up @@ -990,7 +996,9 @@ def test_MainView_set_conversation(mocker):
"""
mv = MainView(None)

scw = SourceConversationWrapper(factory.Source(), mocker.MagicMock())
mv_controller = mocker.MagicMock()
mv_controller.get_source_count.return_value = 2
scw = SourceConversationWrapper(factory.Source(), mv_controller)
mv.set_conversation(scw)

assert mv.view_layout.widget(mv.CONVERSATION_INDEX) == scw
Expand Down Expand Up @@ -3856,6 +3864,7 @@ def test_SourceConversationWrapper_on_conversation_updated(mocker, qtbot):

get_file = mocker.MagicMock(return_value=file)
controller = mocker.MagicMock(get_file=get_file)
controller.get_source_count.return_value = 1

scw = SourceConversationWrapper(source, controller, None)
scw.conversation_title_bar.updated.setText("CANARY")
Expand All @@ -3876,6 +3885,7 @@ def test_SourceConversationWrapper_on_source_deleted(mocker):
mv.source_list = mocker.MagicMock()
mv.source_list.get_selected_source = mocker.MagicMock(return_value=source)
mv.controller = mocker.MagicMock(is_authenticated=True)
mv.controller.get_source_count.return_value = 1

# Detached sourceconversationwrapper, just for unit testing
scw = SourceConversationWrapper(source, mv.controller, None)
Expand All @@ -3892,7 +3902,9 @@ def test_SourceConversationWrapper_on_source_deleted(mocker):


def test_SourceConversationWrapper_on_source_deleted_wrong_uuid(mocker):
scw = SourceConversationWrapper(factory.Source(uuid="123"), mocker.MagicMock())
controller = mocker.MagicMock()
controller.get_source_count.return_value = 1
scw = SourceConversationWrapper(factory.Source(uuid="123"), controller)
scw.on_source_deleted("321")
assert not scw.conversation_title_bar.isHidden()
assert not scw.conversation_view.isHidden()
Expand All @@ -3901,7 +3913,9 @@ def test_SourceConversationWrapper_on_source_deleted_wrong_uuid(mocker):


def test_SourceConversationWrapper_on_source_deletion_failed(mocker):
scw = SourceConversationWrapper(factory.Source(uuid="123"), mocker.MagicMock())
controller = mocker.MagicMock()
controller.get_source_count.return_value = 1
scw = SourceConversationWrapper(factory.Source(uuid="123"), controller)
scw.on_source_deleted("123")

scw.on_source_deletion_failed("123")
Expand All @@ -3913,7 +3927,9 @@ def test_SourceConversationWrapper_on_source_deletion_failed(mocker):


def test_SourceConversationWrapper_on_source_deletion_failed_wrong_uuid(mocker):
scw = SourceConversationWrapper(factory.Source(uuid="123"), mocker.MagicMock())
controller = mocker.MagicMock()
controller.get_source_count.return_value = 1
scw = SourceConversationWrapper(factory.Source(uuid="123"), controller)
scw.on_source_deleted("123")

scw.on_source_deletion_failed("321")
Expand All @@ -3930,6 +3946,7 @@ def test_SourceConversationWrapper_on_conversation_deleted(mocker):
mv.source_list = mocker.MagicMock()
mv.source_list.get_selected_source = mocker.MagicMock(return_value=source)
mv.controller = mocker.MagicMock(is_authenticated=True)
mv.controller.get_source_count.return_value = 1
mocker.patch("securedrop_client.gui.widgets.source_exists", return_value=True)
mv.show()
scw = SourceConversationWrapper(source, mv.controller, None)
Expand All @@ -3948,7 +3965,9 @@ def test_SourceConversationWrapper_on_conversation_deleted(mocker):


def test_SourceConversationWrapper_on_conversation_deleted_wrong_uuid(mocker):
scw = SourceConversationWrapper(factory.Source(uuid="123"), mocker.MagicMock())
controller = mocker.MagicMock()
controller.get_source_count.return_value = 1
scw = SourceConversationWrapper(factory.Source(uuid="123"), controller)
scw.on_conversation_deleted("321")
assert not scw.conversation_title_bar.isHidden()
assert not scw.conversation_view.isHidden()
Expand All @@ -3958,7 +3977,9 @@ def test_SourceConversationWrapper_on_conversation_deleted_wrong_uuid(mocker):


def test_SourceConversationWrapper__on_conversation_deletion_successful(mocker):
scw = SourceConversationWrapper(factory.Source(uuid="123"), mocker.MagicMock())
controller = mocker.MagicMock()
controller.get_source_count.return_value = 1
scw = SourceConversationWrapper(factory.Source(uuid="123"), controller)
scw.on_conversation_deleted("123")

scw._on_conversation_deletion_successful("123", datetime.now())
Expand All @@ -3971,7 +3992,9 @@ def test_SourceConversationWrapper__on_conversation_deletion_successful(mocker):


def test_SourceConversationWrapper_on_conversation_deletion_failed(mocker):
scw = SourceConversationWrapper(factory.Source(uuid="123"), mocker.MagicMock())
controller = mocker.MagicMock()
controller.get_source_count.return_value = 1
scw = SourceConversationWrapper(factory.Source(uuid="123"), controller)
scw.on_conversation_deleted("123")

scw.on_conversation_deletion_failed("123")
Expand All @@ -3984,7 +4007,9 @@ def test_SourceConversationWrapper_on_conversation_deletion_failed(mocker):


def test_SourceConversationWrapper_on_conversation_deletion_failed_wrong_uuid(mocker):
scw = SourceConversationWrapper(factory.Source(uuid="123"), mocker.MagicMock())
controller = mocker.MagicMock()
controller.get_source_count.return_value = 1
scw = SourceConversationWrapper(factory.Source(uuid="123"), controller)
scw.on_conversation_deleted("123")

scw.on_conversation_deletion_failed("321")
Expand Down Expand Up @@ -4453,6 +4478,7 @@ def test_ConversationView_add_not_downloaded_file(mocker, homedir, source, sessi
def test_DeleteSource_from_source_menu_when_user_is_loggedout(mocker):
mock_controller = mocker.MagicMock()
mock_controller.api = None
mock_controller.get_source_count.return_value = 1
mock_source = factory.Source()
mock_delete_source_dialog_instance = mocker.MagicMock(DeleteSourceDialog)
mock_delete_source_dialog = mocker.MagicMock()
Expand Down Expand Up @@ -5326,6 +5352,7 @@ def test_SourceProfileShortWidget_update_timestamp(mocker):
instance with the last_updated value from the source..
"""
mock_controller = mocker.MagicMock()
mock_controller.get_source_count.return_value = 1
mock_source = mocker.MagicMock()
mock_source.last_updated = datetime.now()
mock_source.journalist_designation = "wimple horse knackered unittest"
Expand Down
5 changes: 5 additions & 0 deletions client/tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def main_window(mocker, homedir):
)
controller.authenticated_user = factory.User()
controller.qubes = False
mocker.patch("securedrop_client.logic.Controller.get_source_count", return_value=1)
gui.setup(controller)

# Create a source widget
Expand Down Expand Up @@ -84,6 +85,7 @@ def main_window_no_key(mocker, homedir):
)
controller.authenticated_user = factory.User()
controller.qubes = False
mocker.patch("securedrop_client.logic.Controller.get_source_count", return_value=1)
gui.setup(controller)

# Create a source widget
Expand Down Expand Up @@ -135,6 +137,7 @@ def modal_dialog(mocker, homedir):
)
controller.authenticated_user = factory.User()
controller.qubes = False
mocker.patch("securedrop_client.logic.Controller.get_source_count", return_value=1)
gui.setup(controller)
gui.login_dialog.close()
dialog = ModalDialog()
Expand Down Expand Up @@ -187,6 +190,7 @@ def print_dialog(mocker, homedir, mock_export):
)
controller.authenticated_user = factory.User()
controller.qubes = False
mocker.patch("securedrop_client.logic.Controller.get_source_count", return_value=1)
gui.setup(controller)
gui.login_dialog.close()
dialog = conversation.PrintDialog(mock_export, "file_name", ["/mock/export/file"])
Expand Down Expand Up @@ -218,6 +222,7 @@ def export_file_wizard(mocker, homedir, mock_export):
)
controller.authenticated_user = factory.User()
controller.qubes = False
mocker.patch("securedrop_client.logic.Controller.get_source_count", return_value=1)
gui.setup(controller)
gui.login_dialog.close()
dialog = conversation.ExportWizard(mock_export, "file_name", ["/mock/export/filepath"])
Expand Down

0 comments on commit 5e3b8a7

Please sign in to comment.