Skip to content

Commit

Permalink
Remove old way of creating accounts based on replies
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Feb 8, 2022
1 parent 6010926 commit 2b93c90
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
43 changes: 15 additions & 28 deletions securedrop_client/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import shutil
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Tuple, Type, Union
from typing import Any, List, Tuple, Type, Union

from dateutil.parser import parse
from sdclientapi import API
Expand All @@ -37,6 +37,7 @@
from sqlalchemy.orm.session import Session

from securedrop_client.db import (
DeletedUser,
DraftReply,
File,
Message,
Expand Down Expand Up @@ -409,38 +410,24 @@ def update_replies(
* New replies have an entry created in the local database.
* Local replies not returned in the remote replies are deleted from the
local database unless they are pending or failed.
If a reply references a new journalist username, add them to the database
as a new user.
"""
local_replies_by_uuid = {r.uuid: r for r in local_replies}
users: Dict[str, User] = {}
deleted_user = session.query(User).filter_by(username="deleted").one_or_none()
source_cache = SourceCache(session)
for reply in remote_replies:
user = users.get(reply.journalist_uuid)

# If the account for the sender does not exist, then replies will need to be re-associated
# to a local "deleted" user account. The only reason this might happen is if the pre-2.2.0
# server returns a `journalist_uuid` that does not correspond to a uuid for a real user
# account.
user = session.query(User).filter_by(uuid=reply.journalist_uuid).one_or_none()
if not user:
user = create_or_update_user(
reply.journalist_uuid,
reply.journalist_username,
reply.journalist_first_name,
reply.journalist_last_name,
session,
)
users[reply.journalist_uuid] = user
elif (
user.username != reply.journalist_username
or user.firstname != reply.journalist_first_name
or user.lastname != reply.journalist_last_name
):
user = create_or_update_user(
reply.journalist_uuid,
reply.journalist_username,
reply.journalist_first_name,
reply.journalist_last_name,
session,
)
users[reply.journalist_uuid] = user
logger.debug(f"Sender account with uuid='{reply.journalist_uuid}' does not exist")
if not deleted_user:
deleted_user = DeletedUser()
session.add(deleted_user)
session.commit()
user = deleted_user
logger.debug(f"Setting `journalist_id` for Reply '{reply.uuid}' to '{user.id}'")

local_reply = local_replies_by_uuid.get(reply.uuid)
if local_reply:
Expand Down
5 changes: 0 additions & 5 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,6 @@ def test_update_replies_deletes_files_associated_with_the_reply(homedir, mocker)
abs_local_filename = add_test_file_to_temp_dir(source_directory, local_filename_when_decrypted)
local_replies = [local_reply]

# There needs to be a corresponding local_source.
local_source = mocker.MagicMock()
local_source.uuid = "test-source-uuid"
local_source.id = 666
mock_session.query().filter_by.return_value = [local_source]
update_replies(remote_replies, local_replies, mock_session, homedir)

# Ensure the file associated with the reply are deleted on disk.
Expand Down

0 comments on commit 2b93c90

Please sign in to comment.