def test_ApiJobQueue_start_if_queues_running(mocker): ''' Ensure token is passed to the queues that are already started. ''' mock_api = mocker.MagicMock() mock_client = mocker.MagicMock() mock_session_maker = mocker.MagicMock() job_queue = ApiJobQueue(mock_client, mock_session_maker) mock_main_queue = mocker.patch.object(job_queue, 'main_queue') mock_download_file_queue = mocker.patch.object(job_queue, 'download_file_queue') mock_main_thread = mocker.patch.object(job_queue, 'main_thread') mock_download_file_thread = mocker.patch.object(job_queue, 'download_file_thread') job_queue.main_thread.isRunning = mocker.MagicMock(return_value=True) job_queue.download_file_thread.isRunning = mocker.MagicMock( return_value=True) job_queue.start(mock_api) assert mock_main_queue.api_client == mock_api assert mock_download_file_queue.api_client == mock_api assert not mock_main_thread.start.called assert not mock_download_file_thread.start.called
def test_ApiJobQueue_stop_stops_queue_threads(mocker): job_queue = ApiJobQueue(mocker.MagicMock(), mocker.MagicMock()) job_queue.stop() assert not job_queue.main_thread.isRunning() assert not job_queue.download_file_thread.isRunning()
def test_ApiJobQueue_enqueue_when_queues_are_not_running(mocker): mock_client = mocker.MagicMock() mock_session_maker = mocker.MagicMock() job_queue = ApiJobQueue(mock_client, mock_session_maker) job_priority = 2 dummy_job = factory.dummy_job_factory(mocker, 'mock')() 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_main_queue = mocker.patch.object(job_queue, 'main_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') job_queue.main_queue.api_client = 'has a value' job_queue.download_file_queue.api_client = 'has a value' job_queue.stop( ) # queues are already not running, but just in case the code changes one day dl_job = FileDownloadJob('mock', 'mock', 'mock') job_queue.enqueue(dl_job) mock_download_file_add_job.assert_not_called() mock_main_queue_add_job.assert_not_called()
def test_ApiJobQueue_start_if_queues_not_running(mocker): """ Ensure token is passed to the queues and that they are started. """ mock_api = mocker.MagicMock() mock_client = mocker.MagicMock() mock_session_maker = mocker.MagicMock() job_queue = ApiJobQueue(mock_client, mock_session_maker) mock_main_queue = mocker.patch.object(job_queue, "main_queue") mock_download_file_queue = mocker.patch.object(job_queue, "download_file_queue") mock_main_thread = mocker.patch.object(job_queue, "main_thread") mock_download_file_thread = mocker.patch.object(job_queue, "download_file_thread") job_queue.main_thread.isRunning = mocker.MagicMock(return_value=False) job_queue.download_file_thread.isRunning = mocker.MagicMock( return_value=False) job_queue.start(mock_api) assert mock_main_queue.api_client == mock_api assert mock_download_file_queue.api_client == mock_api mock_main_thread.start.assert_called_once_with() mock_download_file_thread.start.assert_called_once_with()
def test_ApiJobQueue_stop_results_in_queue_threads_not_running(mocker): job_queue = ApiJobQueue(mocker.MagicMock(), mocker.MagicMock()) job_queue.main_thread = mocker.MagicMock() job_queue.download_file_thread = mocker.MagicMock() job_queue.stop() job_queue.main_thread.quit.assert_called_once_with() job_queue.download_file_thread.quit.assert_called_once_with()
def __init__(self, hostname: str, gui, session_maker: sessionmaker, home: str, proxy: bool = True, qubes: bool = True) -> None: """ The hostname, gui and session objects are used to coordinate with the various other layers of the application: the location of the SecureDrop proxy, the user interface and SqlAlchemy local storage respectively. """ check_dir_permissions(home) super().__init__() # Controller is unauthenticated by default self.__is_authenticated = False # used for finding DB in sync thread self.home = home # boolean flag for whether or not the client is operating behind a proxy self.proxy = proxy # boolean flag for whether the client is running within Qubes # (regardless of proxy state, to support local dev in an AppVM) self.qubes = qubes # Location of the SecureDrop server. self.hostname = hostname # Reference to the UI window. self.gui = gui # Reference to the API for secure drop proxy. self.api = None # type: sdclientapi.API # Reference to the SqlAlchemy `sessionmaker` and `session` self.session_maker = session_maker self.session = session_maker() # Queue that handles running API job self.api_job_queue = ApiJobQueue(self.api, self.session_maker) self.api_job_queue.paused.connect(self.on_queue_paused) # Contains active threads calling the API. self.api_threads = {} # type: Dict[str, Dict] self.gpg = GpgHelper(home, self.session_maker, proxy) self.export = Export() self.sync_flag = os.path.join(home, 'sync_flag') # File data. self.data_dir = os.path.join(self.home, 'data')
def test_ApiJobQueue_on_file_download_queue_paused(mocker): job_queue = ApiJobQueue(mocker.MagicMock(), mocker.MagicMock()) mocker.patch.object(job_queue, 'paused') pause_job = PauseQueueJob() mocker.patch('securedrop_client.queue.PauseQueueJob', return_value=pause_job) job_queue.on_file_download_queue_paused() job_queue.paused.emit.assert_called_once_with()
def test_ApiJobQueue_on_main_queue_paused(mocker): job_queue = ApiJobQueue(mocker.MagicMock(), mocker.MagicMock()) mocker.patch.object(job_queue, "paused") pause_job = PauseQueueJob() mocker.patch("securedrop_client.queue.PauseQueueJob", return_value=pause_job) job_queue.on_main_queue_paused() job_queue.paused.emit.assert_called_once_with()
def test_ApiJobQueue_pause_queues(mocker): job_queue = ApiJobQueue(mocker.MagicMock(), mocker.MagicMock()) mocker.patch.object(job_queue, 'paused') pause_job = PauseQueueJob() mocker.patch('securedrop_client.queue.PauseQueueJob', return_value=pause_job) job_queue.on_queue_paused() job_queue.paused.emit()
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') 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()
def test_ApiJobQueue_logout_removes_api_client(mocker): mock_client = mocker.MagicMock() mock_session_maker = mocker.MagicMock() 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.logout() assert job_queue.main_queue.api_client is None assert job_queue.download_file_queue.api_client is None
def test_ApiJobQueue_enqueue_when_queues_are_running(mocker): mock_client = mocker.MagicMock() mock_session_maker = mocker.MagicMock() job_queue = ApiJobQueue(mock_client, mock_session_maker) job_priority = 2 dummy_job = factory.dummy_job_factory(mocker, 'mock')() 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_main_queue = mocker.patch.object(job_queue, 'main_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') job_queue.main_queue.api_client = 'has a value' job_queue.download_file_queue.api_client = 'has a value' job_queue.main_thread.isRunning = mocker.MagicMock(return_value=True) job_queue.download_file_thread.isRunning = mocker.MagicMock( return_value=True) dl_job = FileDownloadJob('mock', 'mock', 'mock') job_queue.enqueue(dl_job) mock_download_file_add_job.assert_called_once_with(dl_job) assert not mock_main_queue_add_job.called # reset for next test mock_download_file_queue.reset_mock() mock_download_file_add_job.reset_mock() mock_main_queue.reset_mock() mock_main_queue_add_job.reset_mock() job_queue.enqueue(FileDownloadJob('mock', 'mock', 'mock')) assert not mock_main_queue_add_job.called # reset for next test mock_download_file_queue.reset_mock() mock_download_file_add_job.reset_mock() mock_main_queue.reset_mock() mock_main_queue_add_job.reset_mock() job_queue.enqueue(dummy_job) mock_main_queue_add_job.assert_called_once_with(dummy_job) assert not mock_download_file_add_job.called
def test_ApiJobQueue_resume_queues_does_not_emit_resume_signal_if_queues_are_not_running( mocker): """ Ensure resume signal is not emitted if the queues ar not running. """ job_queue = ApiJobQueue(mocker.MagicMock(), mocker.MagicMock()) mocker.patch.object(job_queue.main_queue, 'resume') mocker.patch.object(job_queue.download_file_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.resume_queues() job_queue.main_queue.resume.emit.assert_not_called() job_queue.download_file_queue.resume.emit.assert_not_called()
def test_ApiJobQueue_resume_queues_emits_resume_signal_if_queues_are_running( mocker): """ Ensure resume signal is emitted if the queues are running. """ job_queue = ApiJobQueue(mocker.MagicMock(), mocker.MagicMock()) mocker.patch.object(job_queue.main_queue, "resume") mocker.patch.object(job_queue.download_file_queue, "resume") job_queue.main_thread.isRunning = mocker.MagicMock(return_value=True) job_queue.download_file_thread.isRunning = mocker.MagicMock( return_value=True) job_queue.resume_queues() job_queue.main_queue.resume.emit.assert_called_once_with() job_queue.download_file_queue.resume.emit.assert_called_once_with()
def test_ApiJobQueue_enqueue_no_auth(mocker): mock_client = mocker.MagicMock() mock_session_maker = mocker.MagicMock() 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_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') job_queue.main_queue.api_client = None job_queue.download_file_queue.api_client = None dummy_job = factory.dummy_job_factory(mocker, 'mock')() job_queue.JOB_PRIORITIES = {type(dummy_job): 1} job_queue.enqueue(dummy_job) assert mock_download_file_add_job.call_count == 0 assert mock_main_queue_add_job.call_count == 0
def test_ApiJobQueue_login_if_queues_running(mocker): mock_api = mocker.MagicMock() mock_client = mocker.MagicMock() mock_session_maker = mocker.MagicMock() job_queue = ApiJobQueue(mock_client, mock_session_maker) mock_main_queue = mocker.patch.object(job_queue, 'main_queue') mock_download_file_queue = mocker.patch.object(job_queue, 'download_file_queue') mock_main_thread = mocker.patch.object(job_queue, 'main_thread') mock_download_file_thread = mocker.patch.object(job_queue, 'download_file_thread') job_queue.main_thread.isRunning = mocker.MagicMock(return_value=True) job_queue.download_file_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 not mock_main_thread.start.called assert not mock_download_file_thread.start.called