Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heartsucker committed Mar 8, 2019
1 parent 29fe11f commit 9d37899
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 99 deletions.
11 changes: 4 additions & 7 deletions tests/gui/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from PyQt5.QtWidgets import QApplication, QVBoxLayout
from securedrop_client.gui.main import Window
from securedrop_client.resources import load_icon
from securedrop_client.db import Submission
from securedrop_client.db import Message
from uuid import uuid4


Expand Down Expand Up @@ -245,13 +245,10 @@ def test_conversation_pending_message(mocker):
mock_source.journalistic_designation = 'Testy McTestface'

msg_uuid = str(uuid4())
submission = Submission(source=mock_source, uuid=msg_uuid, size=123,
filename="test.msg.gpg",
download_url='http://test/test')
message = Message(source=mock_source, uuid=msg_uuid, size=123, filename="test.msg.gpg",
download_url='http://test/test', is_downloaded=False)

submission.is_downloaded = False

mock_source.collection = [submission]
mock_source.collection = [message]

mocked_add_message = mocker.patch('securedrop_client.gui.widgets.ConversationView.add_message')
mocker.patch('securedrop_client.gui.main.QVBoxLayout')
Expand Down
36 changes: 14 additions & 22 deletions tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,10 @@ def test_FileWidget_init_left(mocker):
"""
mock_controller = mocker.MagicMock()
source = factory.Source()
submission = db.Submission(source, 'submission-uuid', 123,
'mah-reply.gpg',
'http://mah-server/mah-reply-url')
submission.is_downloaded = True
message = db.Message(source=source, uuid='uuid', size=123, filename='mah-reply.gpg',
download_url='http://mah-server/mah-reply-url', is_downloaded=True)

fw = FileWidget(source, submission, mock_controller, align='left')
fw = FileWidget(source, message, mock_controller, align='left')

layout = fw.layout()
assert isinstance(layout.takeAt(0), QWidgetItem)
Expand All @@ -702,12 +700,10 @@ def test_FileWidget_init_right(mocker):
"""
mock_controller = mocker.MagicMock()
source = factory.Source()
submission = db.Submission(source, 'submission-uuid', 123,
'mah-reply.gpg',
'http://mah-server/mah-reply-url')
submission.is_downloaded = True
message = db.Message(source=source, uuid='uuid', size=123, filename='mah-reply.gpg',
download_url='http://mah-server/mah-reply-url', is_downloaded=True)

fw = FileWidget(source, submission, mock_controller, align='right')
fw = FileWidget(source, message, mock_controller, align='right')
layout = fw.layout()
assert isinstance(layout.takeAt(0), QSpacerItem)
assert isinstance(layout.takeAt(0), QWidgetItem)
Expand All @@ -721,14 +717,12 @@ def test_FileWidget_mousePressEvent_download(mocker):
"""
mock_controller = mocker.MagicMock()
source = factory.Source()
submission = db.Submission(source, 'submission-uuid', 123,
'mah-reply.gpg',
'http://mah-server/mah-reply-url')
submission.is_downloaded = False
file_ = db.File(source=source, uuid='uuid', size=123, filename='mah-reply.gpg',
download_url='http://mah-server/mah-reply-url', is_downloaded=False)

fw = FileWidget(source, submission, mock_controller)
fw = FileWidget(source, file_, mock_controller)
fw.mouseReleaseEvent(None)
fw.controller.on_file_download.assert_called_once_with(source, submission)
fw.controller.on_file_download.assert_called_once_with(source, file_)


def test_FileWidget_mousePressEvent_open(mocker):
Expand All @@ -737,14 +731,12 @@ def test_FileWidget_mousePressEvent_open(mocker):
"""
mock_controller = mocker.MagicMock()
source = factory.Source()
submission = db.Submission(source, 'submission-uuid', 123,
'mah-reply.gpg',
'http://mah-server/mah-reply-url')
submission.is_downloaded = True
file_ = db.File(source=source, uuid='uuid', size=123, filename='mah-reply.gpg',
download_url='http://mah-server/mah-reply-url', is_downloaded=True)

fw = FileWidget(source, submission, mock_controller)
fw = FileWidget(source, file_, mock_controller)
fw.mouseReleaseEvent(None)
fw.controller.on_file_open.assert_called_once_with(submission)
fw.controller.on_file_open.assert_called_once_with(file_)


def test_ConversationView_init(mocker, homedir):
Expand Down
14 changes: 7 additions & 7 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,18 +951,18 @@ def test_Client_on_file_download_Submission(homedir, config, mocker):
mock_session = mocker.MagicMock()
cl = Client('http://localhost', mock_gui, mock_session, homedir)
source = factory.Source()
submission = db.Submission(source, 'submission-uuid', 1234,
'myfile.doc.gpg', 'http://myserver/myfile')
file_ = db.File(source=source, uuid='uuid', size=1234, filename='myfile.doc.gpg',
download_url='http://myserver/myfile', is_downloaded=False)
cl.call_api = mocker.MagicMock()
cl.api = mocker.MagicMock()
submission_sdk_object = mocker.MagicMock()
mock_submission = mocker.patch('sdclientapi.Submission')
mock_submission.return_value = submission_sdk_object
cl.on_file_download(source, submission)
cl.on_file_download(source, file_)
cl.call_api.assert_called_once_with(
cl.api.download_submission, cl.on_file_downloaded,
cl.on_download_timeout, submission_sdk_object,
cl.data_dir, current_object=submission)
cl.data_dir, current_object=file_)


def test_Client_on_file_downloaded_success(homedir, config, mocker):
Expand Down Expand Up @@ -1048,14 +1048,14 @@ def test_Client_on_file_download_user_not_signed_in(homedir, config, mocker):
mock_session = mocker.MagicMock()
cl = Client('http://localhost', mock_gui, mock_session, homedir)
source = factory.Source()
submission = db.Submission(source, 'submission-uuid', 1234,
'myfile.doc.gpg', 'http://myserver/myfile')
file_ = db.File(source=source, uuid='uuid', size=1234, filename='myfile.doc.gpg',
download_url='http://myserver/myfile', is_downloaded=False)
cl.on_action_requiring_login = mocker.MagicMock()
cl.api = None
submission_sdk_object = mocker.MagicMock()
mock_submission = mocker.patch('sdclientapi.Submission')
mock_submission.return_value = submission_sdk_object
cl.on_file_download(source, submission)
cl.on_file_download(source, file_)
cl.on_action_requiring_login.assert_called_once_with()


Expand Down
24 changes: 15 additions & 9 deletions tests/test_message_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Make sure the message sync object behaves as expected.
"""
from securedrop_client.crypto import CryptoError
from securedrop_client.db import Message, File
from securedrop_client.message_sync import MessageSync, ReplySync


Expand All @@ -28,15 +29,17 @@ def test_MessageSync_init(mocker):
assert ms.session == mock_session_class()


def test_MessageSync_run_success(mocker):
submission = mocker.MagicMock()
submission.uuid = 'mock id'
submission.download_url = "http://foo"
submission.filename = "foo.gpg"
def test_MessageSync_run_success(mocker, source):
submission1 = File(source=source['source'], uuid='uuid1', filename='filename',
download_url='http://test.net')
submission2 = Message(source=source['source'], uuid='uuid2', filename='filename',
download_url='http://test.net')

# mock the fetching of submissions
mocker.patch('securedrop_client.storage.find_new_submissions', return_value=[submission])
mocker.patch('securedrop_client.storage.find_new_messages', return_value=[submission1])
mocker.patch('securedrop_client.storage.find_new_files', return_value=[submission2])
mocker.patch('securedrop_client.message_sync.storage.mark_file_as_downloaded')
mocker.patch('securedrop_client.message_sync.storage.mark_message_as_downloaded')
# don't create the signal
mocker.patch('securedrop_client.message_sync.pyqtSignal')
# mock the GpgHelper creation since we don't have directories/keys setup
Expand Down Expand Up @@ -72,7 +75,8 @@ def test_MessageSync_exception(homedir, config, mocker):
is_qubes = False

# mock to return the submission we want
mocker.patch('securedrop_client.storage.find_new_submissions', return_value=[submission])
mocker.patch('securedrop_client.storage.find_new_messages', return_value=[submission])
mocker.patch('securedrop_client.storage.find_new_files', return_value=[])
# mock to prevent GpgHelper from raising errors on init
mocker.patch('securedrop_client.crypto.safe_mkdir')

Expand All @@ -89,9 +93,11 @@ def test_MessageSync_run_failure(mocker):
submission.filename = "foo.gpg"

# mock the fetching of submissions
mocker.patch('securedrop_client.storage.find_new_submissions', return_value=[submission])
mocker.patch('securedrop_client.storage.find_new_messages', return_value=[submission])
mocker.patch('securedrop_client.storage.find_new_files', return_value=[])
# mock the handling of the downloaded files
mocker.patch('securedrop_client.message_sync.storage.mark_file_as_downloaded')
mocker.patch('securedrop_client.message_sync.storage.mark_message_as_downloaded')
# mock the GpgHelper creation since we don't have directories/keys setup
mocker.patch('securedrop_client.message_sync.GpgHelper')

Expand Down Expand Up @@ -171,7 +177,7 @@ def test_ReplySync_run_failure(mocker):
# mock finding new replies
mocker.patch('securedrop_client.storage.find_new_replies', return_value=[reply])
# mock handling the new reply
mocker.patch('securedrop_client.message_sync.storage.mark_file_as_downloaded')
mocker.patch('securedrop_client.message_sync.storage.mark_reply_as_downloaded')
mocker.patch('securedrop_client.message_sync.GpgHelper')

api = mocker.MagicMock()
Expand Down
31 changes: 20 additions & 11 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tests import factory
from securedrop_client.db import Reply, Submission, User
from securedrop_client.db import Reply, File, Message, User


def test_string_representation_of_user():
Expand All @@ -12,12 +12,18 @@ def test_string_representation_of_source():
source.__repr__()


def test_string_representation_of_submission():
def test_string_representation_of_message():
source = factory.Source()
submission = Submission(source=source, uuid="test", size=123,
filename="test.docx",
download_url='http://test/test')
submission.__repr__()
msg = Message(source=source, uuid="test", size=123, filename="test.docx",
download_url='http://test/test')
msg.__repr__()


def test_string_representation_of_file():
source = factory.Source()
file_ = File(source=source, uuid="test", size=123, filename="test.docx",
download_url='http://test/test')
file_.__repr__()


def test_string_representation_of_reply():
Expand All @@ -31,15 +37,18 @@ def test_string_representation_of_reply():
def test_source_collection():
# Create some test submissions and replies
source = factory.Source()
submission = Submission(source=source, uuid="test", size=123,
filename="2-test.doc.gpg",
download_url='http://test/test')
file_ = File(source=source, uuid="test", size=123, filename="2-test.doc.gpg",
download_url='http://test/test')
message = Message(source=source, uuid="test", size=123, filename="3-test.doc.gpg",
download_url='http://test/test')
user = User('hehe')
reply = Reply(source=source, journalist=user, filename="1-reply.gpg",
size=1234, uuid='test')
source.submissions = [submission]
source.files = [file_]
source.messages = [message]
source.replies = [reply]

# Now these items should be in the source collection in the proper order
assert source.collection[0] == reply
assert source.collection[1] == submission
assert source.collection[1] == file_
assert source.collection[2] == message
Loading

0 comments on commit 9d37899

Please sign in to comment.