Skip to content

Commit

Permalink
update star functional test
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Mar 21, 2020
1 parent 0a697a7 commit a9b61b5
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
10 changes: 4 additions & 6 deletions securedrop_client/api_jobs/updatestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,23 @@ 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 error: {e}'
raise UpdateStarJobTimeoutError(error_message, self.source_uuid, self.is_starred)
raise UpdateStarJobTimeoutError(error_message, self.source_uuid)
except Exception as e:
error_message = f'Failed to update star on source {self.source_uuid} due to {e}'
raise UpdateStarJobError(error_message, self.source_uuid, self.is_starred)
raise UpdateStarJobError(error_message, self.source_uuid)


class UpdateStarJobError(Exception):
def __init__(self, message: str, source_uuid: str, is_starred: bool) -> None:
def __init__(self, message: str, source_uuid: str) -> None:
super().__init__(message)
self.source_uuid = source_uuid
self.is_starred = is_starred


class UpdateStarJobTimeoutError(RequestTimeoutError):
def __init__(self, message: str, source_uuid: str, is_starred: bool) -> None:
def __init__(self, message: str, source_uuid: str) -> None:
super().__init__()
self.message = message
self.source_uuid = source_uuid
self.is_starred = is_starred

def __str__(self) -> str:
return self.message
4 changes: 2 additions & 2 deletions securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,14 +1338,14 @@ def update(self, is_starred: bool) -> None:
self.is_starred = is_starred
self.setChecked(self.is_starred)

@pyqtSlot(str)
@pyqtSlot(str, bool)
def on_star_update_failed(self, source_uuid: str, is_starred: bool) -> None:
"""
If the star update failed to update on the server, toggle back to previous state.
"""
if self.source_uuid == source_uuid:
self.is_starred = is_starred
self.setChecked(is_starred)
self.setChecked(self.is_starred)
self.pending_count = self.pending_count - 1

@pyqtSlot(str)
Expand Down
6 changes: 4 additions & 2 deletions securedrop_client/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ class Controller(QObject):
Emits:
str: the source UUID
bool: is_starred
"""
star_update_failed = pyqtSignal(str)
star_update_failed = pyqtSignal(str, bool)

def __init__(self, hostname: str, gui, session_maker: sessionmaker,
home: str, proxy: bool = True, qubes: bool = True) -> None:
Expand Down Expand Up @@ -522,7 +523,8 @@ def on_update_star_failure(
) -> None:
if isinstance(error, UpdateStarJobError):
self.gui.update_error_status(_('Failed to update star.'))
self.star_update_failed.emit(error.source_uuid, error.is_starred)
source = self.session.query(db.Source).filter_by(uuid=error.source_uuid).one()
self.star_update_failed.emit(error.source_uuid, source.is_starred)

@login_required
def update_star(self, source_uuid: str, is_starred: bool):
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_star_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def check_for_sources():
qtbot.mouseClick(first_source_widget.star, Qt.LeftButton)
qtbot.wait(1000)
# Check the source is now checked.
assert first_source_widget.star.source.is_starred is True
assert first_source_widget.star.is_starred is True
2 changes: 1 addition & 1 deletion tests/functional/test_unstar_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def check_for_sources():
qtbot.mouseClick(first_source_widget.star, Qt.LeftButton)
qtbot.wait(1000)
# Check the source isn't checked once more.
assert first_source_widget.star.source.is_starred is False
assert first_source_widget.star.is_starred is False
8 changes: 5 additions & 3 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,13 @@ def test_Controller_on_update_star_failed(homedir, config, mocker):
gui = mocker.MagicMock()
co = Controller('http://localhost', gui, mocker.MagicMock(), homedir)
co.star_update_failed = mocker.MagicMock()
source = factory.Source()
co.session.query().filter_by().one.return_value = source

error = UpdateStarJobError('mock_message', 'mock_uuid', True)
error = UpdateStarJobError('mock_message', source.uuid)
co.on_update_star_failure(error)

co.star_update_failed.emit.assert_called_once_with(error.source_uuid, error.is_starred)
co.star_update_failed.emit.assert_called_once_with(source.uuid, source.is_starred)
gui.update_error_status.assert_called_once_with('Failed to update star.')


Expand All @@ -601,7 +603,7 @@ def test_Controller_on_update_star_failed_due_to_timeout(homedir, config, mocker
co = Controller('http://localhost', gui, mocker.MagicMock(), homedir)
co.star_update_failed = mocker.MagicMock()

error = UpdateStarJobTimeoutError('mock_message', 'mock_uuid', True)
error = UpdateStarJobTimeoutError('mock_message', 'mock_uuid')
co.on_update_star_failure(error)

co.star_update_failed.emit.assert_not_called()
Expand Down

0 comments on commit a9b61b5

Please sign in to comment.