Ejemplo n.º 1
0
def test_ApiInaccessibleError_init():
    # check default value
    err = ApiInaccessibleError()
    assert str(err).startswith("API is inaccessible")
    assert isinstance(err, Exception)

    # check custom
    msg = "foo"
    err = ApiInaccessibleError(msg)
    assert str(err) == msg
Ejemplo n.º 2
0
def test_ApiSyncBackgroundTask_sync_catches_ApiInaccessibleError(
        mocker, session_maker, homedir):
    '''
    Ensure sync calls the parent processing function of MetadataSyncJob, catches
    ApiInaccessibleError exception, and emits failure signal.
    '''
    api_client = mocker.MagicMock()
    api_sync = ApiSync(api_client, session_maker, mocker.MagicMock(), homedir)
    sync_started = mocker.patch.object(api_sync.api_sync_bg_task,
                                       'sync_started')
    success_signal = mocker.patch(
        'securedrop_client.sync.MetadataSyncJob.success_signal')
    failure_signal = mocker.patch(
        'securedrop_client.sync.MetadataSyncJob.failure_signal')
    error = ApiInaccessibleError()
    _do_call_api_fn = mocker.patch(
        'securedrop_client.sync.MetadataSyncJob._do_call_api',
        side_effect=error)

    api_sync.api_sync_bg_task.sync()

    assert _do_call_api_fn.called
    sync_started.emit.assert_called_once_with()
    success_signal.emit.assert_not_called()
    failure_signal.emit.assert_called_once_with(error)
Ejemplo n.º 3
0
def test_RunnableQueue_job_ApiInaccessibleError(mocker):
    '''
    Add two jobs to the queue. The first runs into an auth error, and then gets resubmitted for the
    next pass through the loop.
    '''
    queue = RunnableQueue(mocker.MagicMock(), mocker.MagicMock())
    queue.pause = mocker.MagicMock()
    job_cls = factory.dummy_job_factory(mocker, ApiInaccessibleError())
    queue.JOB_PRIORITIES = {PauseQueueJob: 0, job_cls: 1}

    # ApiInaccessibleError will cause the queue to pause, use our fake pause method instead
    def fake_pause() -> None:
        queue.add_job(PauseQueueJob())

    queue.pause.emit = fake_pause

    # Add two jobs that timeout during processing to the queues
    job1 = job_cls()
    job2 = job_cls()
    queue.add_job(job1)
    queue.add_job(job2)

    # attempt to process job1 knowing that it times out
    queue.process()
    assert queue.queue.qsize() == 2  # queue contains: job1, job2

    # now process after making it so job1 no longer times out
    job1.return_value = 'mock'
    queue.process()
    assert queue.queue.qsize() == 1  # queue contains: job2
    assert queue.queue.get(block=True) == (1, job2)
Ejemplo n.º 4
0
def test_RunnableQueue_does_not_run_jobs_when_not_authed(mocker):
    '''
    Check that a job that sees an ApiInaccessibleError does not get resubmitted since it is not
    authorized and that its api_client is None.
    '''
    queue = RunnableQueue(mocker.MagicMock(), mocker.MagicMock())
    job_cls = factory.dummy_job_factory(mocker, ApiInaccessibleError())
    queue.JOB_PRIORITIES = {PauseQueueJob: 0, job_cls: 1}

    # Add a job that results in an ApiInaccessibleError to the queue
    job = job_cls()
    queue.add_job(job)

    # attempt to process job knowing that it errors
    queue.process()
    assert queue.queue.qsize(
    ) == 0  # queue should not contain job since it was not resubmitted
    assert queue.api_client is None
Ejemplo n.º 5
0
def test_RunnableQueue_does_not_run_jobs_when_not_authed(mocker):
    '''
    Add a job to the queue, ensure we don't run it when not authenticated.
    '''
    queue = RunnableQueue(mocker.MagicMock(), mocker.MagicMock())
    queue.pause = mocker.MagicMock()
    job_cls = factory.dummy_job_factory(mocker, ApiInaccessibleError())
    queue.JOB_PRIORITIES = {PauseQueueJob: 0, job_cls: 1}

    # ApiInaccessibleError will cause the queue to pause, use our fake pause method instead
    def fake_pause() -> None:
        queue.add_job(PauseQueueJob())

    queue.pause.emit = fake_pause

    # Add a job that results in an ApiInaccessibleError to the queue
    job = job_cls()
    queue.add_job(job)

    # attempt to process job1 knowing that it times out
    queue.process()
    assert queue.queue.qsize() == 1  # queue contains: job1