Skip to content

Commit

Permalink
update current user info during sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Aug 6, 2020
1 parent 291350c commit 583dfd2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
15 changes: 9 additions & 6 deletions securedrop_client/api_jobs/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.orm.session import Session

from securedrop_client.api_jobs.base import ApiJob
from securedrop_client.storage import get_remote_data, update_local_storage
from securedrop_client.storage import get_remote_data, update_and_get_user, update_local_storage

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -37,8 +37,11 @@ def call_api(self, api_client: API, session: Session) -> Any:
# This timeout is used for 3 different requests: `get_sources`, `get_all_submissions`, and
# `get_all_replies`
api_client.default_request_timeout = 60
remote_sources, remote_submissions, remote_replies = get_remote_data(api_client)

update_local_storage(
session, remote_sources, remote_submissions, remote_replies, self.data_dir
)
sources, submissions, replies = get_remote_data(api_client)

update_local_storage(session, sources, submissions, replies, self.data_dir)
user = api_client.get_current_user()
if "uuid" in user and "username" in user and "first_name" in user and "last_name" in user:
update_and_get_user(
user["uuid"], user["username"], user["first_name"], user["last_name"], session,
)
34 changes: 31 additions & 3 deletions tests/api_jobs/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,37 @@ def test_MetadataSyncJob_success(mocker, homedir, session, session_maker):
"securedrop_client.api_jobs.sync.get_remote_data", return_value=([mock_source], [], [])
)

api_client = mocker.MagicMock()
api_client.default_request_timeout = mocker.MagicMock()
api_client.default_request_timeout = mocker.MagicMock()
user = factory.User(uuid="mock1", username="mock1", firstname="mock1", lastname="mock1")
session.add(user)

api_client = mocker.patch("securedrop_client.logic.sdclientapi.API")

user = {"uuid": "mock1", "username": "mock1", "first_name": "mock1", "last_name": "mock1"}
mocker.patch.object(api_client, "get_current_user", return_value=user)

job.call_api(api_client, session)

assert mock_get_remote_data.call_count == 1


def test_MetadataSyncJob_success_current_user_name_change(mocker, homedir, session, session_maker):
job = MetadataSyncJob(homedir)

mock_source = factory.RemoteSource(
key={"type": "PGP", "public": PUB_KEY, "fingerprint": "123456ABC"}
)

mock_get_remote_data = mocker.patch(
"securedrop_client.api_jobs.sync.get_remote_data", return_value=([mock_source], [], [])
)

user = factory.User(uuid="mock1", username="mock1", firstname="mock1", lastname="mock1")
session.add(user)

api_client = mocker.patch("securedrop_client.logic.sdclientapi.API")

user = {"uuid": "mock2", "username": "mock2", "first_name": "mock2", "last_name": "mock2"}
mocker.patch.object(api_client, "get_current_user", return_value=user)

job.call_api(api_client, session)

Expand Down

0 comments on commit 583dfd2

Please sign in to comment.