From 8021f5be578507aa07cc424c48da57ca68b93647 Mon Sep 17 00:00:00 2001 From: Allie Crevier Date: Thu, 30 Jan 2020 11:03:31 -0800 Subject: [PATCH] update tests after metadata queue and sync_api removal --- securedrop_client/queue.py | 4 +-- tests/api_jobs/test_downloads.py | 1 - tests/test_logic.py | 59 -------------------------------- tests/test_queue.py | 34 ++++-------------- 4 files changed, 7 insertions(+), 91 deletions(-) diff --git a/securedrop_client/queue.py b/securedrop_client/queue.py index b92d4d00f6..f11a7c36e4 100644 --- a/securedrop_client/queue.py +++ b/securedrop_client/queue.py @@ -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 diff --git a/tests/api_jobs/test_downloads.py b/tests/api_jobs/test_downloads.py index a451b18a37..18dc1df9ae 100644 --- a/tests/api_jobs/test_downloads.py +++ b/tests/api_jobs/test_downloads.py @@ -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 diff --git a/tests/test_logic.py b/tests/test_logic.py index 78c7b37911..7ec4215084 100644 --- a/tests/test_logic.py +++ b/tests/test_logic.py @@ -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 @@ -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 @@ -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.') @@ -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) @@ -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( @@ -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) @@ -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): @@ -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') @@ -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()) @@ -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): @@ -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) @@ -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( @@ -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) @@ -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( @@ -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') @@ -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( @@ -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') @@ -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( diff --git a/tests/test_queue.py b/tests/test_queue.py index 00ea340499..2444873119 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -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 @@ -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 @@ -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') @@ -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 @@ -323,10 +320,8 @@ 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() @@ -334,7 +329,6 @@ def test_ApiJobQueue_resume_queues_emits_resume_signal(mocker): 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): @@ -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')() @@ -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 @@ -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): @@ -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): @@ -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