From 583dfd200511678248d25d9af5722ab70e4e6984 Mon Sep 17 00:00:00 2001 From: Allie Crevier Date: Tue, 4 Aug 2020 14:53:25 -0700 Subject: [PATCH] update current user info during sync --- securedrop_client/api_jobs/sync.py | 15 +++++++------ tests/api_jobs/test_sync.py | 34 +++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/securedrop_client/api_jobs/sync.py b/securedrop_client/api_jobs/sync.py index 50c82cf157..8abb3a21be 100644 --- a/securedrop_client/api_jobs/sync.py +++ b/securedrop_client/api_jobs/sync.py @@ -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__) @@ -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, + ) diff --git a/tests/api_jobs/test_sync.py b/tests/api_jobs/test_sync.py index a42d18bd6b..d4930bf419 100644 --- a/tests/api_jobs/test_sync.py +++ b/tests/api_jobs/test_sync.py @@ -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)