Skip to content

Commit

Permalink
update tests after metadata queue and sync_api removal
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Jan 30, 2020
1 parent 37ff3db commit 8021f5b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 91 deletions.
4 changes: 1 addition & 3 deletions securedrop_client/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ def resume_queues(self) -> None:

def enqueue(self, job: ApiJob) -> None:
# Prevent api jobs being added to the queue when not logged in.
if (not self.main_queue.api_client or
not self.download_file_queue.api_client or
not self.metadata_queue.api_client):
if (not self.main_queue.api_client or not self.download_file_queue.api_client):
logger.info('Not adding job, we are not logged in')
return

Expand Down
1 change: 0 additions & 1 deletion tests/api_jobs/test_downloads.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import pytest
from typing import Tuple
from uuid import UUID

from sdclientapi import BaseError
from sdclientapi import Submission as SdkSubmission
Expand Down
59 changes: 0 additions & 59 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,41 +343,6 @@ def test_Controller_authenticated_no_api(homedir, config, mocker, session_maker)
assert co.authenticated() is False


def test_Controller_sync_api_not_authenticated(homedir, config, mocker, session_maker):
"""
If the API isn't authenticated, don't sync.
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.authenticated = mocker.MagicMock(return_value=False)
co.api_job_queue = mocker.MagicMock()
co.api_job_queue.enqueue = mocker.MagicMock()

co.sync_api()

co.api_job_queue.enqueue.call_count == 0


def test_Controller_sync_api(homedir, config, mocker, session_maker):
"""
Sync the API is authenticated.
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.authenticated = mocker.MagicMock(return_value=True)
co.api_job_queue = mocker.MagicMock()
co.api_job_queue.enqueue = mocker.MagicMock()

co.sync_api()

co.api_job_queue.enqueue.call_count == 1


def test_Controller_last_sync_with_file(homedir, config, mocker, session_maker):
"""
The flag indicating the time of the last sync with the API is stored in a
Expand Down Expand Up @@ -526,7 +491,6 @@ def test_Controller_on_update_star_success(homedir, config, mocker, session_make
co = Controller('http://localhost', mock_gui, session_maker, homedir)
result = True
co.call_reset = mocker.MagicMock()
co.sync_api = mocker.MagicMock()
co.on_update_star_success(result)
assert mock_gui.clear_error_status.called

Expand All @@ -541,9 +505,7 @@ def test_Controller_on_update_star_failed(homedir, config, mocker, session_maker
co = Controller('http://localhost', mock_gui, session_maker, homedir)
result = Exception('boom')
co.call_reset = mocker.MagicMock()
co.sync_api = mocker.MagicMock()
co.on_update_star_failure(result)
co.sync_api.assert_not_called()
mock_gui.update_error_status.assert_called_once_with('Failed to update star.')


Expand Down Expand Up @@ -990,14 +952,12 @@ def test_Controller_on_file_open_file_missing(mocker, homedir, session_maker, se
When file does not exist, test that we log and send status update to user.
"""
co = Controller('http://localhost', mocker.MagicMock(), session_maker, homedir)
co.sync_api = mocker.MagicMock()
file = factory.File(source=source['source'])
file.original_filename = 'original_filename.mock'
session.add(file)
session.commit()
mocker.patch('securedrop_client.logic.Controller.get_file', return_value=file)
debug_logger = mocker.patch('securedrop_client.logic.logger.debug')
co.sync_api = mocker.MagicMock()

co.on_file_open(file.uuid)

Expand All @@ -1006,7 +966,6 @@ def test_Controller_on_file_open_file_missing(mocker, homedir, session_maker, se
file.original_filename)
co.gui.update_error_status.assert_called_once_with(user_error)
debug_logger.assert_called_once_with(log_msg)
co.sync_api.assert_called_once_with()


def test_Controller_on_file_open_file_missing_not_qubes(
Expand All @@ -1017,14 +976,12 @@ def test_Controller_on_file_open_file_missing_not_qubes(
"""
co = Controller('http://localhost', mocker.MagicMock(), session_maker, homedir)
co.qubes = False
co.sync_api = mocker.MagicMock()
file = factory.File(source=source['source'])
file.original_filename = 'original_filename.mock'
session.add(file)
session.commit()
mocker.patch('securedrop_client.logic.Controller.get_file', return_value=file)
debug_logger = mocker.patch('securedrop_client.logic.logger.debug')
co.sync_api = mocker.MagicMock()

co.on_file_open(file.uuid)

Expand All @@ -1033,7 +990,6 @@ def test_Controller_on_file_open_file_missing_not_qubes(
file.original_filename)
co.gui.update_error_status.assert_called_once_with(user_error)
debug_logger.assert_called_once_with(log_msg)
co.sync_api.assert_called_once_with()


def test_Controller_download_new_replies_with_new_reply(mocker, session, session_maker, homedir):
Expand Down Expand Up @@ -1261,7 +1217,6 @@ def test_Controller_on_delete_source_failure(homedir, config, mocker, session_ma
'''
mock_gui = mocker.MagicMock()
co = Controller('http://localhost', mock_gui, session_maker, homedir)
co.sync_api = mocker.MagicMock()
co.on_delete_source_failure(Exception())
co.gui.update_error_status.assert_called_with('Failed to delete source at server')

Expand Down Expand Up @@ -1360,7 +1315,6 @@ def test_Controller_on_reply_success(homedir, mocker, session_maker, session):
Check that when the method is called, the client emits the correct signal.
'''
co = Controller('http://localhost', mocker.MagicMock(), session_maker, homedir)
mocker.patch.object(co, 'sync_api')
reply_succeeded = mocker.patch.object(co, 'reply_succeeded')
reply_failed = mocker.patch.object(co, 'reply_failed')
reply = factory.Reply(source=factory.Source())
Expand All @@ -1371,7 +1325,6 @@ def test_Controller_on_reply_success(homedir, mocker, session_maker, session):
assert debug_logger.call_args_list[0][0][0] == '{} sent successfully'.format(reply.uuid)
reply_succeeded.emit.assert_called_once_with(reply.uuid)
reply_failed.emit.assert_not_called()
co.sync_api.assert_not_called()


def test_Controller_on_reply_failure(homedir, mocker, session_maker):
Expand Down Expand Up @@ -1570,13 +1523,11 @@ def test_Controller_print_file_file_missing(homedir, mocker, session, session_ma
should be communicated to the user.
"""
co = Controller('http://localhost', mocker.MagicMock(), session_maker, homedir)
co.sync_api = mocker.MagicMock()
file = factory.File(source=factory.Source(), original_filename='mock_filename')
session.add(file)
session.commit()
mocker.patch('securedrop_client.logic.Controller.get_file', return_value=file)
debug_logger = mocker.patch('securedrop_client.logic.logger.debug')
co.sync_api = mocker.MagicMock()

co.print_file(file.uuid)

Expand All @@ -1585,7 +1536,6 @@ def test_Controller_print_file_file_missing(homedir, mocker, session, session_ma
file.original_filename)
co.gui.update_error_status.assert_called_once_with(user_error)
debug_logger.assert_called_once_with(log_msg)
co.sync_api.assert_called_once_with()


def test_Controller_print_file_file_missing_not_qubes(
Expand All @@ -1597,13 +1547,11 @@ def test_Controller_print_file_file_missing_not_qubes(
"""
co = Controller('http://localhost', mocker.MagicMock(), session_maker, homedir)
co.qubes = False
co.sync_api = mocker.MagicMock()
file = factory.File(source=factory.Source(), original_filename='mock_filename')
session.add(file)
session.commit()
mocker.patch('securedrop_client.logic.Controller.get_file', return_value=file)
debug_logger = mocker.patch('securedrop_client.logic.logger.debug')
co.sync_api = mocker.MagicMock()

co.print_file(file.uuid)

Expand All @@ -1612,7 +1560,6 @@ def test_Controller_print_file_file_missing_not_qubes(
file.original_filename)
co.gui.update_error_status.assert_called_once_with(user_error)
debug_logger.assert_called_once_with(log_msg)
co.sync_api.assert_called_once_with()


def test_Controller_print_file_when_orig_file_already_exists(
Expand Down Expand Up @@ -1764,13 +1711,11 @@ def test_Controller_export_file_to_usb_drive_file_missing(homedir, mocker, sessi
should be communicated to the user.
"""
co = Controller('http://localhost', mocker.MagicMock(), session_maker, homedir)
co.sync_api = mocker.MagicMock()
file = factory.File(source=factory.Source(), original_filename='mock_filename')
session.add(file)
session.commit()
mocker.patch('securedrop_client.logic.Controller.get_file', return_value=file)
debug_logger = mocker.patch('securedrop_client.logic.logger.debug')
co.sync_api = mocker.MagicMock()

co.export_file_to_usb_drive(file.uuid, 'mock passphrase')

Expand All @@ -1779,7 +1724,6 @@ def test_Controller_export_file_to_usb_drive_file_missing(homedir, mocker, sessi
file.original_filename)
co.gui.update_error_status.assert_called_once_with(user_error)
debug_logger.assert_called_once_with(log_msg)
co.sync_api.assert_called_once_with()


def test_Controller_export_file_to_usb_drive_file_missing_not_qubes(
Expand All @@ -1791,13 +1735,11 @@ def test_Controller_export_file_to_usb_drive_file_missing_not_qubes(
"""
co = Controller('http://localhost', mocker.MagicMock(), session_maker, homedir)
co.qubes = False
co.sync_api = mocker.MagicMock()
file = factory.File(source=factory.Source(), original_filename='mock_filename')
session.add(file)
session.commit()
mocker.patch('securedrop_client.logic.Controller.get_file', return_value=file)
debug_logger = mocker.patch('securedrop_client.logic.logger.debug')
co.sync_api = mocker.MagicMock()

co.export_file_to_usb_drive(file.uuid, 'mock passphrase')

Expand All @@ -1806,7 +1748,6 @@ def test_Controller_export_file_to_usb_drive_file_missing_not_qubes(
file.original_filename)
co.gui.update_error_status.assert_called_once_with(user_error)
debug_logger.assert_called_once_with(log_msg)
co.sync_api.assert_called_once_with()


def test_Controller_export_file_to_usb_drive_when_orig_file_already_exists(
Expand Down
34 changes: 6 additions & 28 deletions tests/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
'''
from queue import Queue, Full
from sdclientapi import RequestTimeoutError
import pytest

from securedrop_client.api_jobs.downloads import FileDownloadJob, MetadataSyncJob
from securedrop_client.api_jobs.downloads import FileDownloadJob
from securedrop_client.api_jobs.base import ApiInaccessibleError, PauseQueueJob
from securedrop_client.queue import RunnableQueue, ApiJobQueue
from tests import factory
Expand Down Expand Up @@ -58,8 +59,9 @@ def test_RunnableQueue_with_size_constraint(mocker):
queue.JOB_PRIORITIES = {dummy_job_cls: 1, PauseQueueJob: 2}

queue.add_job(dummy_job_cls())
queue.add_job(dummy_job_cls())
queue.add_job(dummy_job_cls())
with pytest.raises(Full):
queue.add_job(dummy_job_cls())
queue.add_job(dummy_job_cls())

assert queue.queue.qsize() == 1

Expand Down Expand Up @@ -264,14 +266,11 @@ def test_ApiJobQueue_enqueue(mocker):
job_queue.JOB_PRIORITIES = {FileDownloadJob: job_priority, type(dummy_job): job_priority}

mock_download_file_queue = mocker.patch.object(job_queue, 'download_file_queue')
mock_metadata_queue = mocker.patch.object(job_queue, 'metadata_queue')
mock_main_queue = mocker.patch.object(job_queue, 'main_queue')
mock_download_file_add_job = mocker.patch.object(mock_download_file_queue, 'add_job')
mock_metadata_add_job = mocker.patch.object(mock_metadata_queue, 'add_job')
mock_main_queue_add_job = mocker.patch.object(mock_main_queue, 'add_job')
job_queue.main_queue.api_client = 'has a value'
job_queue.download_file_queue.api_client = 'has a value'
job_queue.metadata_queue.api_client = 'has a value'
mock_start_queues = mocker.patch.object(job_queue, 'start_queues')

dl_job = FileDownloadJob('mock', 'mock', 'mock')
Expand All @@ -286,10 +285,8 @@ def test_ApiJobQueue_enqueue(mocker):
mock_main_queue.reset_mock()
mock_main_queue_add_job.reset_mock()

md_job = MetadataSyncJob("mock", "mock")
job_queue.enqueue(md_job)
job_queue.enqueue(FileDownloadJob('mock', 'mock', 'mock'))

mock_metadata_add_job.assert_called_once_with(md_job)
assert not mock_main_queue_add_job.called

# reset for next test
Expand Down Expand Up @@ -323,18 +320,15 @@ def test_ApiJobQueue_resume_queues_emits_resume_signal(mocker):
job_queue = ApiJobQueue(mocker.MagicMock(), mocker.MagicMock())
mocker.patch.object(job_queue.main_queue, 'resume')
mocker.patch.object(job_queue.download_file_queue, 'resume')
mocker.patch.object(job_queue.metadata_queue, 'resume')
job_queue.main_thread.isRunning = mocker.MagicMock(return_value=False)
job_queue.download_file_thread.isRunning = mocker.MagicMock(return_value=False)
job_queue.metadata_thread.isRunning = mocker.MagicMock(return_value=False)
job_queue.start_queues = mocker.MagicMock()

job_queue.resume_queues()

job_queue.start_queues.assert_called_once_with()
job_queue.main_queue.resume.emit.assert_called_once_with()
job_queue.download_file_queue.resume.emit.assert_called_once_with()
job_queue.metadata_queue.resume.emit.assert_called_once_with()


def test_ApiJobQueue_enqueue_no_auth(mocker):
Expand All @@ -344,13 +338,10 @@ def test_ApiJobQueue_enqueue_no_auth(mocker):
job_queue = ApiJobQueue(mock_client, mock_session_maker)
mock_download_file_queue = mocker.patch.object(job_queue, 'download_file_queue')
mock_main_queue = mocker.patch.object(job_queue, 'main_queue')
mock_metadata_queue = mocker.patch.object(job_queue, 'metadata_queue')
mock_download_file_add_job = mocker.patch.object(mock_download_file_queue, 'add_job')
mock_main_queue_add_job = mocker.patch.object(mock_main_queue, 'add_job')
mock_metadata_queue_add_job = mocker.patch.object(mock_metadata_queue, "add_job")
job_queue.main_queue.api_client = None
job_queue.download_file_queue.api_client = None
job_queue.metadata_queue.api_client = None
mock_start_queues = mocker.patch.object(job_queue, 'start_queues')

dummy_job = factory.dummy_job_factory(mocker, 'mock')()
Expand All @@ -359,7 +350,6 @@ def test_ApiJobQueue_enqueue_no_auth(mocker):

assert mock_download_file_add_job.call_count == 0
assert mock_main_queue_add_job.call_count == 0
assert mock_metadata_queue_add_job.call_count == 0
assert mock_start_queues.call_count == 0


Expand All @@ -372,23 +362,18 @@ def test_ApiJobQueue_login_if_queues_not_running(mocker):

mock_main_queue = mocker.patch.object(job_queue, 'main_queue')
mock_download_file_queue = mocker.patch.object(job_queue, 'download_file_queue')
mock_metadata_queue = mocker.patch.object(job_queue, 'metadata_queue')
mock_main_thread = mocker.patch.object(job_queue, 'main_thread')
mock_download_file_thread = mocker.patch.object(job_queue, 'download_file_thread')
mock_metadata_thread = mocker.patch.object(job_queue, 'metadata_thread')
job_queue.main_thread.isRunning = mocker.MagicMock(return_value=False)
job_queue.download_file_thread.isRunning = mocker.MagicMock(return_value=False)
job_queue.metadata_thread.isRunning = mocker.MagicMock(return_value=False)

job_queue.login(mock_api)

assert mock_main_queue.api_client == mock_api
assert mock_download_file_queue.api_client == mock_api
assert mock_metadata_queue.api_client == mock_api

mock_main_thread.start.assert_called_once_with()
mock_download_file_thread.start.assert_called_once_with()
mock_metadata_thread.start.assert_called_once_with()


def test_ApiJobQueue_login_if_queues_running(mocker):
Expand All @@ -400,23 +385,18 @@ def test_ApiJobQueue_login_if_queues_running(mocker):

mock_main_queue = mocker.patch.object(job_queue, 'main_queue')
mock_download_file_queue = mocker.patch.object(job_queue, 'download_file_queue')
mock_metadata_queue = mocker.patch.object(job_queue, 'metadata_queue')
mock_main_thread = mocker.patch.object(job_queue, 'main_thread')
mock_download_file_thread = mocker.patch.object(job_queue, 'download_file_thread')
mock_metadata_thread = mocker.patch.object(job_queue, 'metadata_thread')
job_queue.main_thread.isRunning = mocker.MagicMock(return_value=True)
job_queue.download_file_thread.isRunning = mocker.MagicMock(return_value=True)
job_queue.metadata_thread.isRunning = mocker.MagicMock(return_value=True)

job_queue.login(mock_api)

assert mock_main_queue.api_client == mock_api
assert mock_download_file_queue.api_client == mock_api
assert mock_metadata_queue.api_client == mock_api

assert not mock_main_thread.start.called
assert not mock_download_file_thread.start.called
assert not mock_metadata_thread.start.called


def test_ApiJobQueue_logout_removes_api_client(mocker):
Expand All @@ -426,10 +406,8 @@ def test_ApiJobQueue_logout_removes_api_client(mocker):
job_queue = ApiJobQueue(mock_client, mock_session_maker)
job_queue.main_queue.api_client = 'my token!!!'
job_queue.download_file_queue.api_client = 'my token!!!'
job_queue.metadata_queue.api_client = 'my token!!!'

job_queue.logout()

assert job_queue.main_queue.api_client is None
assert job_queue.download_file_queue.api_client is None
assert job_queue.metadata_queue.api_client is None

0 comments on commit 8021f5b

Please sign in to comment.