-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #412 moves update_star calls under queue
Now we are using the same general queue for update_star method on sources.
- Loading branch information
Showing
4 changed files
with
170 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import logging | ||
import sdclientapi | ||
|
||
from sdclientapi import API | ||
from sqlalchemy.orm.session import Session | ||
|
||
from securedrop_client.api_jobs.base import ApiJob | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class UpdateStarJob(ApiJob): | ||
def __init__(self, source_uuid: str, star_status: bool) -> None: | ||
super().__init__() | ||
self.source_uuid = source_uuid | ||
self.star_status = star_status | ||
|
||
def call_api(self, api_client: API, session: Session) -> str: | ||
''' | ||
Override ApiJob. | ||
Star or Unstar an user on the server | ||
''' | ||
try: | ||
source_sdk_object = sdclientapi.Source(uuid=self.source_uuid) | ||
|
||
if self.star_status: | ||
api_client.remove_star(source_sdk_object) | ||
else: | ||
api_client.add_star(source_sdk_object) | ||
|
||
return self.source_uuid | ||
except Exception as e: | ||
error_message = "Failed to update star on source {uuid} due to {exception}".format( | ||
uuid=self.source_uuid, exception=repr(e)) | ||
raise UpdateStarJobException(error_message, self.source_uuid) | ||
|
||
|
||
class UpdateStarJobException(Exception): | ||
def __init__(self, message: str, source_uuid: str): | ||
super().__init__(message) | ||
self.source_uuid = source_uuid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import pytest | ||
|
||
from securedrop_client.api_jobs.updatestar import UpdateStarJob, UpdateStarJobException | ||
from tests import factory | ||
|
||
|
||
def test_star_if_unstar(homedir, mocker, session, session_maker): | ||
''' | ||
Check if we call add_star method if a source is not stared. | ||
''' | ||
source = factory.Source() | ||
session.add(source) | ||
session.commit() | ||
|
||
api_client = mocker.MagicMock() | ||
|
||
api_client.add_star = mocker.MagicMock() | ||
|
||
mock_sdk_source = mocker.Mock() | ||
mock_source_init = mocker.patch('securedrop_client.logic.sdclientapi.Source', | ||
return_value=mock_sdk_source) | ||
|
||
job = UpdateStarJob( | ||
source.uuid, | ||
source.is_starred | ||
) | ||
|
||
job.call_api(api_client, session) | ||
|
||
# ensure we call add_star with right uuid for source | ||
mock_source_init.assert_called_once_with(uuid=source.uuid) | ||
api_client.add_star.assert_called_once_with(mock_sdk_source) | ||
|
||
|
||
def test_unstar_if_star(homedir, mocker, session, session_maker): | ||
''' | ||
Check if we call remove_star method if a source is stared. | ||
''' | ||
source = factory.Source() | ||
source.is_starred = True | ||
session.add(source) | ||
session.commit() | ||
|
||
api_client = mocker.MagicMock() | ||
|
||
api_client.remove_star = mocker.MagicMock() | ||
|
||
mock_sdk_source = mocker.Mock() | ||
mock_source_init = mocker.patch('securedrop_client.logic.sdclientapi.Source', | ||
return_value=mock_sdk_source) | ||
|
||
job = UpdateStarJob( | ||
source.uuid, | ||
source.is_starred | ||
) | ||
|
||
job.call_api(api_client, session) | ||
|
||
# ensure we call remove start wtih right source uuid | ||
mock_source_init.assert_called_once_with(uuid=source.uuid) | ||
api_client.remove_star.assert_called_once_with(mock_sdk_source) | ||
|
||
|
||
def test_failure_to_star(homedir, mocker, session, session_maker): | ||
''' | ||
Check if we call remove_star method if a source is stared. | ||
''' | ||
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 | ||
) | ||
|
||
with pytest.raises(UpdateStarJobException): | ||
job.call_api(api_client, session) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters