From bdcfc2eb373a7955b082d8cc3f96eac946b03c89 Mon Sep 17 00:00:00 2001 From: "Nicholas H.Tollervey" Date: Tue, 21 Jan 2020 15:46:24 +0000 Subject: [PATCH] WiP star updated without sync. --- securedrop_client/api_jobs/updatestar.py | 4 +++- securedrop_client/gui/widgets.py | 5 ++++- tests/gui/test_widgets.py | 18 +++++++++++++++++- tests/test_logic.py | 17 ++++++++++++----- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/securedrop_client/api_jobs/updatestar.py b/securedrop_client/api_jobs/updatestar.py index e0c46258a..0bb3ae9d3 100644 --- a/securedrop_client/api_jobs/updatestar.py +++ b/securedrop_client/api_jobs/updatestar.py @@ -1,6 +1,8 @@ import logging import sdclientapi +from typing import Tuple + from sdclientapi import API from sqlalchemy.orm.session import Session @@ -15,7 +17,7 @@ def __init__(self, source_uuid: str, star_status: bool) -> None: self.source_uuid = source_uuid self.star_status = star_status - def call_api(self, api_client: API, session: Session) -> str: + def call_api(self, api_client: API, session: Session) -> Tuple[str, bool]: ''' Override ApiJob. diff --git a/securedrop_client/gui/widgets.py b/securedrop_client/gui/widgets.py index 264fb799e..13499d911 100644 --- a/securedrop_client/gui/widgets.py +++ b/securedrop_client/gui/widgets.py @@ -1125,7 +1125,10 @@ def on_update(self, result): The result is a uuid for the source and boolean flag for the new state of the star. """ - self.setChecked(result[1]) + enabled = result[1] + self.source.is_starred = enabled + self.controller.update_sources() + self.setChecked(enabled) class DeleteSourceMessageBox: diff --git a/tests/gui/test_widgets.py b/tests/gui/test_widgets.py index b962826c4..629ebc19b 100644 --- a/tests/gui/test_widgets.py +++ b/tests/gui/test_widgets.py @@ -895,7 +895,7 @@ def test_StarToggleButton_on_toggle(mocker): stb.on_toggle() - stb.controller.update_star.assert_called_once_with(source) + stb.controller.update_star.assert_called_once_with(source, stb.on_update) assert stb.isCheckable() is True @@ -929,6 +929,22 @@ def test_StarToggleButton_on_toggle_offline_when_checked(mocker): set_icon_fn.assert_called_with(on='star_on.svg', off='star_on.svg') +def test_StarToggleButton_on_update(mocker): + """ + Ensure the on_update callback updates the state of the source and UI + element to the current "enabled" state. + """ + source = mocker.MagicMock() + source.is_starred = True + stb = StarToggleButton(source) + stb.setChecked = mocker.MagicMock() + stb.controller = mocker.MagicMock() + stb.on_update(("uuid", False)) + assert source.is_starred is False + stb.controller.update_sources.assert_called_once_with() + stb.setChecked.assert_called_once_with(False) + + def test_LoginDialog_setup(mocker, i18n): """ The LoginView is correctly initialised. diff --git a/tests/test_logic.py b/tests/test_logic.py index 78d6d46c9..5df603c2f 100644 --- a/tests/test_logic.py +++ b/tests/test_logic.py @@ -529,9 +529,10 @@ def test_Controller_update_star_not_logged_in(homedir, config, mocker, session_m mock_gui = mocker.MagicMock() co = Controller('http://localhost', mock_gui, session_maker, homedir) source_db_object = mocker.MagicMock() + mock_callback = mocker.MagicMock() co.on_action_requiring_login = mocker.MagicMock() co.api = None - co.update_star(source_db_object) + co.update_star(source_db_object, mock_callback) co.on_action_requiring_login.assert_called_with() @@ -547,7 +548,7 @@ def test_Controller_on_update_star_success(homedir, config, mocker, session_make co.call_reset = mocker.MagicMock() co.sync_api = mocker.MagicMock() co.on_update_star_success(result) - co.sync_api.assert_called_once_with() + assert mock_gui.clear_error_status.called def test_Controller_on_update_star_failed(homedir, config, mocker, session_maker): @@ -1520,7 +1521,9 @@ def test_Controller_call_update_star_success(homedir, config, mocker, session_ma session.add(source) session.commit() - co.update_star(source) + mock_callback = mocker.MagicMock() + + co.update_star(source, mock_callback) mock_job_cls.assert_called_once_with( source.uuid, @@ -1528,8 +1531,12 @@ def test_Controller_call_update_star_success(homedir, config, mocker, session_ma ) mock_queue.enqueue.assert_called_once_with(mock_job) - mock_success_signal.connect.assert_called_once_with( - co.on_update_star_success, type=Qt.QueuedConnection) + assert mock_success_signal.connect.call_count == 2 + cal = mock_success_signal.connect.call_args_list + assert cal[0][0][0] == co.on_update_star_success + assert cal[0][1]['type'] == Qt.QueuedConnection + assert cal[1][0][0] == mock_callback + assert cal[1][1]['type'] == Qt.QueuedConnection mock_failure_signal.connect.assert_called_once_with( co.on_update_star_failure, type=Qt.QueuedConnection)