Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Mar 20, 2020
1 parent 9344a99 commit 027edb3
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 148 deletions.
2 changes: 1 addition & 1 deletion securedrop_client/api_jobs/updatestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def call_api(self, api_client: API, session: Session) -> str:

return self.source_uuid
except (RequestTimeoutError, ServerConnectionError) as e:
error_message = f'Failed to update star on source {self.source_uuid} due to {e}'
error_message = f'Failed to update star on source {self.source_uuid} due to error: {e}'
raise UpdateStarJobTimeoutError(error_message, self.source_uuid, self.is_starred)
except Exception as e:
error_message = f'Failed to update star on source {self.source_uuid} due to {e}'
Expand Down
9 changes: 4 additions & 5 deletions securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,6 @@ def __init__(self, controller: Controller, source: Source):
# Store source
self.source_uuid = source.uuid
self.source = source
self.source_uuid = source.uuid

# Set styles
self.setStyleSheet(self.CSS)
Expand All @@ -1081,7 +1080,7 @@ def __init__(self, controller: Controller, source: Source):
gutter_layout = QVBoxLayout(self.gutter)
gutter_layout.setContentsMargins(0, 0, 0, 0)
gutter_layout.setSpacing(0)
self.star = StarToggleButton(self.controller, self.source)
self.star = StarToggleButton(self.controller, self.source_uuid, source.is_starred)
gutter_layout.addWidget(self.star)
gutter_layout.addStretch()

Expand Down Expand Up @@ -1206,12 +1205,12 @@ class StarToggleButton(SvgToggleButton):
}
'''

def __init__(self, controller: Controller, source: Source):
def __init__(self, controller: Controller, source_uuid: str, is_starred: bool):
super().__init__(on='star_on.svg', off='star_off.svg', svg_size=QSize(16, 16))

self.controller = controller
self.source_uuid = source.uuid
self.is_starred = source.is_starred
self.source_uuid = source_uuid
self.is_starred = is_starred
self.pending_count = 0
self.wait_until_next_sync = False

Expand Down
32 changes: 28 additions & 4 deletions tests/api_jobs/test_updatestar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from securedrop_client.api_jobs.updatestar import UpdateStarJob, UpdateStarJobException
from securedrop_client.api_jobs.updatestar import UpdateStarJob, UpdateStarJobError, \
UpdateStarJobTimeoutError
from sdclientapi import RequestTimeoutError, ServerConnectionError

from tests import factory


Expand Down Expand Up @@ -61,9 +64,9 @@ def test_unstar_if_star(homedir, mocker, session, session_maker):
api_client.remove_star.assert_called_once_with(mock_sdk_source)


def test_failure_to_star(homedir, mocker, session, session_maker):
def test_call_api_raises_UpdateStarJobError(homedir, mocker, session, session_maker):
'''
Check if we call remove_star method if a source is stared.
Check that UpdateStarJobError is raised if remove_star fails due to an exception.
'''
source = factory.Source()
source.is_starred = True
Expand All @@ -80,5 +83,26 @@ def test_failure_to_star(homedir, mocker, session, session_maker):
source.is_starred
)

with pytest.raises(UpdateStarJobException):
with pytest.raises(UpdateStarJobError):
job.call_api(api_client, session)


@pytest.mark.parametrize("exception", [RequestTimeoutError, ServerConnectionError])
def test_call_api_raises_UpdateStarJobTimeoutError(mocker, session, exception):
'''
Check that UpdateStarJobTimeoutError is raised if remove_star fails due to a timeout.
'''
source = factory.Source()
source.is_starred = True
session.add(source)
session.commit()

api_client = mocker.MagicMock()
api_client.remove_star = mocker.MagicMock()
api_client.remove_star.side_effect = exception()

job = UpdateStarJob(source.uuid, source.is_starred)

error = f'Failed to update star on source {source.uuid} due to error'
with pytest.raises(UpdateStarJobTimeoutError, match=error):
job.call_api(api_client, session)
36 changes: 0 additions & 36 deletions tests/gui/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,6 @@ def test_SvgToggleButton_init(mocker):
setIconSize_fn.assert_called_once_with(svg_size)


def test_SvgToggleButton_enable(mocker):
"""
Ensure enable.
"""
stb = SvgToggleButton(on='mock_on', off='mock_off')
stb.enable()
assert stb.isEnabled() is True


def test_SvgToggleButton_disable(mocker):
"""
Ensure disable.
"""
stb = SvgToggleButton(on='mock_on', off='mock_off')
stb.disable()
assert stb.isEnabled() is False


def test_SvgToggleButton_toggle(mocker):
"""
Make sure we're not calling this a toggle button for no reason.
Expand Down Expand Up @@ -95,24 +77,6 @@ def test_SvgPushButton_init(mocker):
setIconSize_fn.assert_called_once_with(svg_size)


def test_SvgPushButton_enable(mocker):
"""
Ensure enable.
"""
spb = SvgPushButton(normal='mock1', disabled='mock2', active='mock3', selected='mock4')
spb.enable()
assert spb.isEnabled() is True


def test_SvgPushButton_disable(mocker):
"""
Ensure disable.
"""
spb = SvgPushButton(normal='mock1', disabled='mock2', active='mock3', selected='mock4')
spb.disable()
assert spb.isEnabled() is False


def test_SvgLabel_init(mocker):
"""
Ensure SvgLabel calls the expected methods correctly to set the icon and size.
Expand Down
Loading

0 comments on commit 027edb3

Please sign in to comment.