Skip to content

Commit

Permalink
test: additional coverage for expected ordering of drafts/replies
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Nov 4, 2019
1 parent c3fb666 commit 4880707
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
75 changes: 75 additions & 0 deletions tests/api_jobs/test_uploads.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import pytest
import sdclientapi

Expand Down Expand Up @@ -61,6 +62,80 @@ def test_send_reply_success(homedir, mocker, session, session_maker,
assert reply.journalist_id == api_client.token_journalist_uuid


def test_drafts_ordering(homedir, mocker, session, session_maker,
reply_status_codes):
'''
Check that if a reply is successful, drafts sent before and after
continue to appear in the same order.
'''
source = factory.Source(interaction_count=1)
session.add(source)
msg_uuid = 'xyz456'

draft_reply = factory.DraftReply(uuid=msg_uuid, file_counter=1)
session.add(draft_reply)
session.commit()

# Draft reply from the previous queue job.
draft_reply_before = factory.DraftReply(
timestamp=draft_reply.timestamp - datetime.timedelta(minutes=1),
source_id=source.id,
file_counter=draft_reply.file_counter,
uuid='foo'
)
session.add(draft_reply_before)

# Draft reply that the queue will operate on next.
draft_reply_after = factory.DraftReply(
timestamp=draft_reply.timestamp + datetime.timedelta(minutes=1),
source_id=source.id,
file_counter=draft_reply.file_counter,
uuid='bar'
)

session.add(draft_reply_after)
session.commit()

gpg = GpgHelper(homedir, session_maker, is_qubes=False)

api_client = mocker.MagicMock()
api_client.token_journalist_uuid = 'journalist ID sending the reply'

encrypted_reply = 's3kr1t m3ss1dg3'
mock_encrypt = mocker.patch.object(gpg, 'encrypt_to_source', return_value=encrypted_reply)
msg = 'wat'

mock_reply_response = sdclientapi.Reply(uuid=msg_uuid, filename='2-dummy-reply')
api_client.reply_source = mocker.MagicMock()
api_client.reply_source.return_value = mock_reply_response

mock_sdk_source = mocker.Mock()
mock_source_init = mocker.patch('securedrop_client.logic.sdclientapi.Source',
return_value=mock_sdk_source)

job = SendReplyJob(
source.uuid,
msg_uuid,
msg,
gpg,
)

job.call_api(api_client, session)

# ensure message gets encrypted
mock_encrypt.assert_called_once_with(source.uuid, msg)
mock_source_init.assert_called_once_with(uuid=source.uuid)

# assert reply got added to db
reply = session.query(db.Reply).filter_by(uuid=msg_uuid).one()
assert reply.journalist_id == api_client.token_journalist_uuid

# Check the ordering displayed to the user
assert source.collection[0] == draft_reply_before
assert source.collection[1] == reply
assert source.collection[2] == draft_reply_after


def test_send_reply_failure_gpg_error(homedir, mocker, session, session_maker,
reply_status_codes):
'''
Expand Down
5 changes: 4 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ def test_source_collection_ordering_with_multiple_draft_replies():
timestamp=datetime.datetime(2001, 6, 6, 6, 0))
reply_6 = Reply(source=source, journalist=user, filename="4-reply.gpg",
size=1234, uuid='test2')
draft_reply_7 = DraftReply(uuid='6', source=source, journalist=user, file_counter=4,
timestamp=datetime.datetime(2002, 6, 6, 6, 0))
source.files = [file_1]
source.messages = [message_2]
source.replies = [reply_3, reply_6]
source.draftreplies = [draft_reply_4, draft_reply_5]
source.draftreplies = [draft_reply_4, draft_reply_5, draft_reply_7]

# Now these items should be in the source collection in the proper order
assert source.collection[0] == file_1
Expand All @@ -133,6 +135,7 @@ def test_source_collection_ordering_with_multiple_draft_replies():
assert source.collection[3] == draft_reply_4
assert source.collection[4] == draft_reply_5
assert source.collection[5] == reply_6
assert source.collection[6] == draft_reply_7


def test_file_init():
Expand Down

0 comments on commit 4880707

Please sign in to comment.