Skip to content

Commit

Permalink
WiP star updated without sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
ntoll authored and sssoleileraaa committed Jan 22, 2020
1 parent 3112d0f commit bdcfc2e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
4 changes: 3 additions & 1 deletion securedrop_client/api_jobs/updatestar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import sdclientapi

from typing import Tuple

from sdclientapi import API
from sqlalchemy.orm.session import Session

Expand All @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 17 additions & 1 deletion tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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.
Expand Down
17 changes: 12 additions & 5 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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):
Expand Down Expand Up @@ -1520,16 +1521,22 @@ 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,
source.is_starred
)

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)

Expand Down

0 comments on commit bdcfc2e

Please sign in to comment.