def test_Client_call_api(safe_tmpdir): """ A new thread and APICallRunner is created / setup. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) cl.finish_api_call = mock.MagicMock() with mock.patch('securedrop_client.logic.QThread'), \ mock.patch( 'securedrop_client.logic.APICallRunner'), \ mock.patch('securedrop_client.logic.QTimer'): mock_api_call = mock.MagicMock() mock_callback = mock.MagicMock() mock_timeout = mock.MagicMock() cl.call_api(mock_api_call, mock_callback, mock_timeout, 'foo', bar='baz') assert len(cl.api_threads) == 1 thread_info = cl.api_threads[list(cl.api_threads.keys())[0]] thread = thread_info['thread'] runner = thread_info['runner'] timer = thread_info['timer'] thread.started.connect.assert_called_once_with(runner.call_api) thread.start.assert_called_once_with() runner.moveToThread.assert_called_once_with(thread) timer.timeout.connect.call_count == 1 runner.call_finished.connect.call_count == 1
def test_Client_call_api(homedir, config, mocker): """ A new thread and APICallRunner is created / setup. Using the `config` fixture to ensure the config is written to disk. """ mock_gui = mocker.MagicMock() mock_session = mocker.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, homedir) cl.finish_api_call = mocker.MagicMock() mocker.patch('securedrop_client.logic.QThread') mocker.patch('securedrop_client.logic.APICallRunner') mocker.patch('securedrop_client.logic.QTimer') mock_api_call = mocker.MagicMock() mock_callback = mocker.MagicMock() mock_timeout = mocker.MagicMock() cl.call_api(mock_api_call, mock_callback, mock_timeout, 'foo', bar='baz') assert len(cl.api_threads) == 1 thread_info = cl.api_threads[list(cl.api_threads.keys())[0]] thread = thread_info['thread'] runner = thread_info['runner'] timer = thread_info['timer'] thread.started.connect.assert_called_once_with(runner.call_api) thread.start.assert_called_once_with() runner.moveToThread.assert_called_once_with(thread) timer.timeout.connect.call_count == 1 runner.call_finished.connect.call_count == 1
def test_Client_call_api(): """ A new thread and APICallRunner is created / setup. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session) cl.finish_api_call = mock.MagicMock() with mock.patch('securedrop_client.logic.QThread') as mock_qthread, \ mock.patch('securedrop_client.logic.APICallRunner') as mock_runner: mock_api_call = mock.MagicMock() mock_callback = mock.MagicMock() mock_timeout = mock.MagicMock() cl.call_api(mock_api_call, mock_callback, mock_timeout, 'foo', bar='baz') cl.api_thread.started.connect.\ assert_called_once_with(cl.api_runner.call_api) cl.api_thread.finished.connect.\ assert_called_once_with(cl.call_reset) cl.api_thread.start.assert_called_once_with() cl.api_runner.moveToThread.assert_called_once_with(cl.api_thread) cl.api_runner.call_finished.connect.\ assert_called_once_with(mock_callback) cl.api_runner.timeout.connect.assert_called_once_with(mock_timeout) cl.finish_api_call.connect(cl.api_runner.on_cancel_timeout)
def test_Client_clean_thread_no_thread(safe_tmpdir): """ The client will ignore an attempt to reset an API call if there's no such call "in flight". """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) cl.finish_api_call = mock.MagicMock() cl.api_threads = {'a': 'b'} cl.clean_thread('foo') assert len(cl.api_threads) == 1
def test_Client_call_reset_no_thread(): """ The client will ignore an attempt to reset an API call is there's no such call "in flight". """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session) cl.finish_api_call = mock.MagicMock() cl.api_thread = None cl.call_reset() assert cl.finish_api_call.emit.call_count == 0
def test_Client_clean_thread_no_thread(homedir, config, mocker): """ The client will ignore an attempt to reset an API call if there's no such call "in flight". Using the `config` fixture to ensure the config is written to disk. """ mock_gui = mocker.MagicMock() mock_session = mocker.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, homedir) cl.finish_api_call = mocker.MagicMock() cl.api_threads = {'a': 'b'} cl.clean_thread('foo') assert len(cl.api_threads) == 1
def test_Client_call_reset(): """ Call reset emits the expected signal and resets the state of client attributes. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session) cl.finish_api_call = mock.MagicMock() cl.api_thread = True cl.call_reset() assert cl.finish_api_call.emit.call_count == 1 assert cl.api_runner is None assert cl.api_thread is None