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_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
def test_Client_call_reset(safe_tmpdir): """ 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, str(safe_tmpdir)) cl.timeout_api_call = mock.MagicMock() cl.api_thread = True cl.call_reset() assert cl.timeout_api_call.disconnect.call_count == 1 assert cl.api_runner is None assert cl.api_thread is None assert cl.timer is None
def test_Client_on_file_downloaded_success(homedir, config, mocker): ''' 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.update_sources = mocker.MagicMock() cl.api_runner = mocker.MagicMock() test_filename = "my-file-location-msg.gpg" test_object_uuid = 'uuid-of-downloaded-object' cl.call_reset = mocker.MagicMock() result_data = ('this-is-a-sha256-sum', test_filename) submission_db_object = mocker.MagicMock() submission_db_object.uuid = test_object_uuid submission_db_object.filename = test_filename mock_storage = mocker.patch('securedrop_client.logic.storage') mock_gpg = mocker.patch.object(cl.gpg, 'decrypt_submission_or_reply', return_value='filepath') mocker.patch('shutil.move') cl.on_file_downloaded(result_data, current_object=submission_db_object) mock_gpg.call_count == 1 mock_storage.mark_file_as_downloaded.assert_called_once_with( test_object_uuid, mock_session)
def test_Client_on_synced_with_key_import_fail(homedir, config, mocker): """ If there's a result to syncing, then update local storage. This is it to check that we can gracefully handle an import failure. 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.update_sources = mocker.MagicMock() cl.api_runner = mocker.MagicMock() mock_source = mocker.MagicMock() mock_source.key = { 'type': 'PGP', 'public': PUB_KEY, } mock_sources = [mock_source] result_data = (mock_sources, 'submissions', 'replies') cl.call_reset = mocker.MagicMock() mock_storage = mocker.patch('securedrop_client.logic.storage') mocker.patch.object(cl.gpg, 'import_key', side_effect=CryptoError) cl.on_synced(result_data) mock_storage.update_local_storage. \ assert_called_once_with(mock_session, mock_sources, "submissions", "replies", os.path.join(homedir, 'data')) cl.update_sources.assert_called_once_with()
def test_Client_on_login_timeout(): """ Reset the form if the API call times out. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session) cl.call_reset = mock.MagicMock() cl.on_login_timeout() cl.call_reset.assert_called_once_with() mock_gui.show_login_error.\ assert_called_once_with(error='The connection to SecureDrop timed ' 'out. Please try again.')
def test_Client_on_login_timeout(safe_tmpdir): """ Reset the form if the API call times out. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) cl.call_reset = mock.MagicMock() cl.on_login_timeout() assert cl.api is None mock_gui.show_login_error.\ assert_called_once_with(error='The connection to the SecureDrop ' 'server timed out. Please try again.')
def test_Client_on_download_timeout(safe_tmpdir): mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) cl.update_sources = mock.MagicMock() cl.api_runner = mock.MagicMock() current_object = mock.MagicMock() test_filename = "my-file-location-msg.gpg" cl.api_runner.result = ("", test_filename) cl.call_reset = mock.MagicMock() cl.set_status = mock.MagicMock() cl.on_download_timeout(current_object) cl.set_status.assert_called_once_with( "The connection to the SecureDrop server timed out. Please try again.")
def test_Client_on_login_timeout(homedir, config, mocker): """ Reset the form if the API call times out. 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.call_reset = mocker.MagicMock() cl.on_login_timeout() assert cl.api is None mock_gui.show_login_error.\ assert_called_once_with(error='The connection to the SecureDrop ' 'server timed out. Please try again.')
def test_Client_on_update_star_success(safe_tmpdir): """ If the starring occurs successfully, then a sync should occur to update locally. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) result = True cl.call_reset = mock.MagicMock() cl.sync_api = mock.MagicMock() cl.on_update_star_complete(result) cl.sync_api.assert_called_once_with() mock_gui.update_error_status.assert_called_once_with("")
def test_Client_on_update_star_failed(safe_tmpdir): """ If the starring does not occur properly, then an error should appear on the error status sidebar, and a sync will not occur. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) result = Exception('boom') cl.call_reset = mock.MagicMock() cl.sync_api = mock.MagicMock() cl.on_update_star_complete(result) cl.sync_api.assert_not_called() mock_gui.update_error_status.assert_called_once_with( 'Failed to apply change.')
def test_Client_on_update_star_success(homedir, config, mocker): """ If the starring occurs successfully, then a sync should occur to update locally. 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) result = True cl.call_reset = mocker.MagicMock() cl.sync_api = mocker.MagicMock() cl.on_update_star_complete(result) cl.sync_api.assert_called_once_with() mock_gui.update_error_status.assert_called_once_with("")
def test_Client_on_update_star_failed(homedir, config, mocker): """ If the starring does not occur properly, then an error should appear on the error status sidebar, and a sync will not occur. 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) result = Exception('boom') cl.call_reset = mocker.MagicMock() cl.sync_api = mocker.MagicMock() cl.on_update_star_complete(result) cl.sync_api.assert_not_called() mock_gui.update_error_status.assert_called_once_with( 'Failed to apply change.')
def test_Client_on_file_downloaded_api_failure(safe_tmpdir): mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) cl.update_sources = mock.MagicMock() cl.api_runner = mock.MagicMock() test_filename = "my-file-location-msg.gpg" cl.api_runner.result = ("", test_filename) cl.call_reset = mock.MagicMock() cl.set_status = mock.MagicMock() result_data = Exception('error message') submission_db_object = mock.MagicMock() submission_db_object.uuid = 'myuuid' submission_db_object.filename = 'filename' cl.on_file_downloaded(result_data, current_object=submission_db_object) cl.set_status.assert_called_once_with( "The file download failed. Please try again.")
def test_Client_on_synced_with_result(safe_tmpdir): """ If there's a result to syncing, then update local storage. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) cl.update_sources = mock.MagicMock() cl.api_runner = mock.MagicMock() result_data = ('sources', 'submissions', 'replies') cl.call_reset = mock.MagicMock() with mock.patch('securedrop_client.logic.storage') as mock_storage: cl.on_synced(result_data) mock_storage.update_local_storage.\ assert_called_once_with(mock_session, "sources", "submissions", "replies") cl.update_sources.assert_called_once_with()
def test_Client_on_synced_with_result(): """ If there's a result to syncing, then update local storage. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session) cl.update_sources = mock.MagicMock() cl.api_runner = mock.MagicMock() cl.api_runner.result = (1, 2, 3, ) cl.call_reset = mock.MagicMock() with mock.patch('securedrop_client.logic.storage') as mock_storage: cl.on_synced(True) cl.call_reset.assert_called_once_with() mock_storage.update_local_storage.assert_called_once_with(mock_session, 1, 2, 3) cl.update_sources.assert_called_once_with()
def test_Client_on_download_timeout(homedir, config, mocker): ''' 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.update_sources = mocker.MagicMock() cl.api_runner = mocker.MagicMock() current_object = mocker.MagicMock() test_filename = "my-file-location-msg.gpg" cl.api_runner.result = ("", test_filename) cl.call_reset = mocker.MagicMock() cl.set_status = mocker.MagicMock() cl.on_download_timeout(current_object) cl.set_status.assert_called_once_with( "The connection to the SecureDrop server timed out. Please try again.")
def test_Client_timeout_cleanup_with_current_object(safe_tmpdir): """ Ensure that cleanup is performed if an API call times out. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) cl.api_thread = mock.MagicMock() cl.api_runner = mock.MagicMock() cl.api_runner.current_object = mock.MagicMock() cl.call_reset = mock.MagicMock() mock_user_callback = mock.MagicMock() cl.timeout_cleanup(mock_user_callback) assert cl.api_runner.i_timed_out is True cl.call_reset.assert_called_once_with() mock_user_callback.call_count == 1
def test_Client_on_file_download_success(safe_tmpdir): mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) cl.update_sources = mock.MagicMock() cl.api_runner = mock.MagicMock() test_filename = "my-file-location-msg.gpg" test_object_uuid = 'uuid-of-downloaded-object' cl.call_reset = mock.MagicMock() result_data = ('this-is-a-sha256-sum', test_filename) submission_db_object = mock.MagicMock() submission_db_object.uuid = test_object_uuid submission_db_object.filename = test_filename with mock.patch('securedrop_client.logic.storage') as mock_storage, \ mock.patch('os.rename'): cl.on_file_download(result_data, current_object=submission_db_object) mock_storage.mark_file_as_downloaded.assert_called_once_with( test_object_uuid, mock_session)
def test_Client_on_file_downloaded_api_failure(homedir, config, mocker): ''' 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.update_sources = mocker.MagicMock() cl.api_runner = mocker.MagicMock() test_filename = "my-file-location-msg.gpg" cl.api_runner.result = ("", test_filename) cl.call_reset = mocker.MagicMock() cl.set_status = mocker.MagicMock() result_data = Exception('error message') submission_db_object = mocker.MagicMock() submission_db_object.uuid = 'myuuid' submission_db_object.filename = 'filename' cl.on_file_downloaded(result_data, current_object=submission_db_object) cl.set_status.assert_called_once_with( "The file download failed. Please try again.")
def test_Client_completed_api_call_with_current_object(safe_tmpdir): """ Ensure that cleanup is performed if an API call returns in the expected time. """ mock_gui = mock.MagicMock() mock_session = mock.MagicMock() cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir)) cl.timer = mock.MagicMock() cl.api_thread = mock.MagicMock() cl.api_runner = mock.MagicMock() cl.api_runner.current_object = mock.MagicMock() cl.call_reset = mock.MagicMock() mock_user_callback = mock.MagicMock() cl.result = mock.MagicMock() cl.completed_api_call(mock_user_callback) cl.call_reset.assert_called_once_with() mock_user_callback.call_count == 1 cl.timer.stop.assert_called_once_with()
def test_Client_on_synced_with_result(homedir, config, mocker): """ If there's a result to syncing, then update local storage. 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.update_sources = mocker.MagicMock() cl.api_runner = mocker.MagicMock() cl.gpg = mocker.MagicMock() result_data = ('sources', 'submissions', 'replies') cl.update_sources = mocker.MagicMock() cl.api_runner = mocker.MagicMock() mock_source = mocker.MagicMock() mock_source.key = { 'type': 'PGP', 'public': PUB_KEY, } mock_sources = [mock_source] result_data = (mock_sources, 'submissions', 'replies') cl.call_reset = mocker.MagicMock() mock_storage = mocker.patch('securedrop_client.logic.storage') cl.on_synced(result_data) mock_storage.update_local_storage. \ assert_called_once_with(mock_session, mock_sources, "submissions", "replies", os.path.join(homedir, 'data')) cl.update_sources.assert_called_once_with()