Skip to content

Commit

Permalink
fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Jun 6, 2019
1 parent f049421 commit 48076ee
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 250 deletions.
2 changes: 1 addition & 1 deletion securedrop_client/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def on_sync_failure(self, result: Exception) -> None:
"""
Called when syncronisation of data via the API fails.
"""
logger.info('Sync failed: "{}".'.format(result))
logger.debug('Sync failed: "{}".'.format(result))

def update_sync(self):
"""
Expand Down
58 changes: 1 addition & 57 deletions securedrop_client/message_sync.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Contains the MessageSync class, which runs in the background and loads new
Contains the ReplySync class, which runs in the background and loads new
messages from the SecureDrop server.
Copyright (C) 2018 The Freedom of the Press Foundation.
Expand Down Expand Up @@ -81,62 +81,6 @@ def fetch_the_thing(
self.decrypt_the_thing(session, filepath, msg)


# class MessageSync(APISyncObject):
# """
# Runs in the background, finding messages to download and downloading them.
# """

# """
# Signal emitted notifying that a message is ready to be displayed. The signal is a tuple of
# (str, str) containing the message's UUID and the content of the message.
# """
# message_ready = pyqtSignal([str, str])

# def run(self, loop: bool = True) -> None:
# session = self.session_maker()
# while True:
# submissions = storage.find_new_messages(session)

# for db_submission in submissions:
# try:
# sdk_submission = sdkobjects.Submission(
# uuid=db_submission.uuid
# )
# sdk_submission.source_uuid = db_submission.source.uuid
# # Need to set filename on non-Qubes platforms
# sdk_submission.filename = db_submission.filename

# if not db_submission.is_downloaded and self.api:
# # Download and decrypt
# self.fetch_the_thing(session,
# sdk_submission,
# db_submission,
# self.api.download_submission,
# storage.mark_message_as_downloaded)
# elif db_submission.is_downloaded:
# # Just decrypt file that is already on disk
# self.decrypt_the_thing(session,
# db_submission.filename,
# db_submission)

# if db_submission.content is not None:
# content = db_submission.content
# else:
# content = '<Message not yet available>'

# self.message_ready.emit(db_submission.uuid, content)
# except Exception:
# tb = traceback.format_exc()
# logger.critical("Exception while processing message!\n{}".format(tb))

# logger.debug('Messages synced')

# if not loop:
# break
# else:
# time.sleep(5) # pragma: no cover


class ReplySync(APISyncObject):
"""
Runs in the background, finding replies to download and downloading them.
Expand Down
8 changes: 2 additions & 6 deletions tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,9 +1315,7 @@ def test_ConversationView_add_message(mocker, session, source):
source = source['source'] # grab the source from the fixture dict for simplicity

mock_message_ready_signal = mocker.MagicMock()
mock_message_sync = mocker.MagicMock(message_ready=mock_message_ready_signal)
mocked_controller = mocker.MagicMock(session=session,
message_sync=mock_message_sync)
mocked_controller = mocker.MagicMock(session=session, message_ready=mock_message_ready_signal)

content = 'a sea, a bee'
message = factory.Message(source=source, content=content)
Expand Down Expand Up @@ -1350,9 +1348,7 @@ def test_ConversationView_add_message_no_content(mocker, session, source):
source = source['source'] # grab the source from the fixture dict for simplicity

mock_message_ready_signal = mocker.MagicMock()
mock_message_sync = mocker.MagicMock(message_ready=mock_message_ready_signal)
mocked_controller = mocker.MagicMock(session=session,
message_sync=mock_message_sync)
mocked_controller = mocker.MagicMock(session=session, message_ready=mock_message_ready_signal)

message = factory.Message(source=source, is_decrypted=False, content=None)
session.add(message)
Expand Down
51 changes: 3 additions & 48 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,45 +90,6 @@ def test_Controller_setup(homedir, config, mocker, session_maker):
co.gui.setup.assert_called_once_with(co)


def test_Controller_start_message_thread(homedir, config, mocker, session_maker):
"""
When starting message-fetching thread, make sure we do a few things.
Using the `config` fixture to ensure the config is written to disk.
"""
mock_gui = mocker.MagicMock()

co = Controller('http://localhost', mock_gui, session_maker, homedir)

mock_qthread = mocker.patch('securedrop_client.logic.QThread')
mocker.patch('securedrop_client.logic.MessageSync')
co.message_sync = mocker.MagicMock()

co.start_message_thread()

co.message_sync.moveToThread.assert_called_once_with(mock_qthread())
co.message_thread.started.connect.assert_called_once_with(co.message_sync.run)
co.message_thread.start.assert_called_once_with()


def test_Controller_start_message_thread_if_already_running(homedir, config, mocker, session_maker):
"""
Ensure that when starting the message thread, we don't start another thread
if it's already running. Instead, we just authenticate the existing thread.
Using the `config` fixture to ensure the config is written to disk.
"""
mock_gui = mocker.MagicMock()

co = Controller('http://localhost', mock_gui, session_maker, homedir)
co.api = 'api object'
co.message_sync = mocker.MagicMock()
co.message_thread = mocker.MagicMock()
co.message_thread.api = None

co.start_message_thread()

co.message_thread.start.assert_not_called()


def test_Controller_start_reply_thread(homedir, config, mocker, session_maker):
"""
When starting reply-fetching thread, make sure we do a few things.
Expand Down Expand Up @@ -229,7 +190,6 @@ def test_Controller_login_offline_mode(homedir, config, mocker):
co.gui = mocker.MagicMock()
co.gui.show_main_window = mocker.MagicMock()
co.gui.hide_login = mocker.MagicMock()
co.start_message_thread = mocker.MagicMock()
co.start_reply_thread = mocker.MagicMock()
co.update_sources = mocker.MagicMock()

Expand All @@ -239,7 +199,6 @@ def test_Controller_login_offline_mode(homedir, config, mocker):
assert co.is_authenticated is False
co.gui.show_main_window.assert_called_once_with()
co.gui.hide_login.assert_called_once_with()
co.start_message_thread.assert_called_once_with()
co.update_sources.assert_called_once_with()


Expand Down Expand Up @@ -272,14 +231,12 @@ def test_Controller_on_authenticate_success(homedir, config, mocker, session_mak
co = Controller('http://localhost', mock_gui, session_maker, homedir)
co.sync_api = mocker.MagicMock()
co.api = mocker.MagicMock()
co.start_message_thread = mocker.MagicMock()
co.start_reply_thread = mocker.MagicMock()
co.api.username = 'test'

co.on_authenticate_success(True)

co.sync_api.assert_called_once_with()
co.start_message_thread.assert_called_once_with()
co.gui.show_main_window.assert_called_once_with('test')
co.gui.clear_error_status.assert_called_once_with()

Expand Down Expand Up @@ -488,17 +445,18 @@ def test_Controller_on_sync_failure(homedir, config, mocker, session_maker):
Using the `config` fixture to ensure the config is written to disk.
"""
mock_gui = mocker.MagicMock()
debug_logger = mocker.patch('securedrop_client.logic.logger.debug')

co = Controller('http://localhost', mock_gui, session_maker, homedir)

co.update_sources = mocker.MagicMock()
result_data = Exception('Boom') # Not the expected tuple.
mock_storage = mocker.patch('securedrop_client.logic.storage')

co.on_sync_failure(result_data)

assert mock_storage.update_local_storage.call_count == 0
co.update_sources.assert_called_once_with()
msg = 'Sync failed: "{}".'.format(result_data)
debug_logger.assert_called_once_with(msg)


def test_Controller_on_sync_success(homedir, config, mocker):
Expand Down Expand Up @@ -763,13 +721,10 @@ def test_Controller_logout(homedir, config, mocker, session_maker):
mock_gui = mocker.MagicMock()
co = Controller('http://localhost', mock_gui, session_maker, homedir)
co.api = mocker.MagicMock()
co.message_sync = mocker.MagicMock()
co.reply_sync = mocker.MagicMock()
co.message_sync.api = mocker.MagicMock()
co.reply_sync.api = mocker.MagicMock()
co.logout()
assert co.api is None
assert co.message_sync.api is None
assert co.reply_sync.api is None
co.gui.logout.assert_called_once_with()

Expand Down
Loading

0 comments on commit 48076ee

Please sign in to comment.