コード例 #1
0
def test_APICallRunner_call_api(mocker):
    """
    A result is obtained so emit True and put the result in self.result.
    """
    mock_api_call = mocker.MagicMock(return_value='foo')
    mock_api_call.__name__ = 'my_function'
    mock_current_object = mocker.MagicMock()
    cr = APICallRunner(mock_api_call, mock_current_object, 'foo', bar='baz')
    cr.call_succeeded = mocker.MagicMock()
    cr.call_api()
    assert cr.result == 'foo'
    cr.call_succeeded.emit.assert_called_once_with()
コード例 #2
0
def test_APICallRunner_with_exception():
    """
    An exception has occured so emit False.
    """
    ex = Exception('boom')
    mock_api_call = mock.MagicMock(side_effect=ex)
    mock_api_call.__name__ = 'my_function'
    cr = APICallRunner(mock_api_call, 'foo', bar='baz')
    cr.call_finished = mock.MagicMock()
    with mock.patch('securedrop_client.logic.QTimer') as mock_timer:
        cr.call_api()
    assert cr.result == ex
    cr.call_finished.emit.assert_called_once_with(False)
コード例 #3
0
def test_APICallRunner_call_api():
    """
    A result is obtained so emit True and put the result in self.result.
    """
    mock_api_call = mock.MagicMock(return_value='foo')
    mock_api_call.__name__ = 'my_function'
    cr = APICallRunner(mock_api_call, 'foo', bar='baz')
    cr.call_finished = mock.MagicMock()
    with mock.patch('securedrop_client.logic.QTimer') as mock_timer:
        cr.call_api()
    assert cr.timer == mock_timer()
    assert cr.result == 'foo'
    cr.call_finished.emit.assert_called_once_with(True)
コード例 #4
0
def test_APICallRunner_with_exception(mocker):
    """
    An exception has occured so emit False.
    """
    ex = Exception('boom')
    mock_api_call = mocker.MagicMock(side_effect=ex)
    mock_api_call.__name__ = 'my_function'
    mock_current_object = mocker.MagicMock()
    cr = APICallRunner(mock_api_call, mock_current_object, 'foo', bar='baz')
    cr.call_failed = mocker.MagicMock()
    mocker.patch('securedrop_client.logic.QTimer')
    cr.call_api()
    assert cr.result == ex
    cr.call_failed.emit.assert_called_once_with()
コード例 #5
0
def test_APICallRunner_api_call_timeout(mocker):
    """
    Ensure that if a RequestTimeoutError is raised, both the failure and timeout signals are
    emitted.
    """
    mock_api = mocker.MagicMock()
    mock_api.fake_request = mocker.MagicMock(side_effect=RequestTimeoutError())

    runner = APICallRunner(mock_api.fake_request)

    mock_failure_signal = mocker.patch.object(runner, 'call_failed')
    mock_timeout_signal = mocker.patch.object(runner, 'call_timed_out')

    runner.call_api()

    mock_api.fake_request.assert_called_once_with()
    mock_failure_signal.emit.assert_called_once_with()
    mock_timeout_signal.emit.assert_called_once_with()