def test_APICallRunner_on_cancel_timeout(): """ Ensure the timer's stop method is called. """ mock_api_call = mock.MagicMock() cr = APICallRunner(mock_api_call, 'foo', bar='baz') cr.timer = mock.MagicMock() cr.on_cancel_timeout() cr.timer.stop.assert_called_once_with()
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()
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)
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)
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()
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()
def test_APICallRunner_init(): """ Ensure everything is set up as expected. """ mock_api_call = mock.MagicMock() cr = APICallRunner(mock_api_call, 'foo', bar='baz') assert cr.api_call == mock_api_call assert cr.args == ('foo', ) assert cr.kwargs == {'bar': 'baz', }
def test_APICallRunner_init(mocker): """ Ensure everything is set up as expected. """ mock_api_call = mocker.MagicMock() mock_current_object = mocker.MagicMock() cr = APICallRunner(mock_api_call, mock_current_object, 'foo', bar='baz') assert cr.api_call == mock_api_call assert cr.current_object == mock_current_object assert cr.args == ('foo', ) assert cr.kwargs == { 'bar': 'baz', }