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_file_downloaded_decrypt_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.set_status = mocker.MagicMock() result_data = ('this-is-a-sha256-sum', test_filename) submission_db_object = mocker.MagicMock() submission_db_object.uuid = 'myuuid' submission_db_object.filename = 'filename' mock_gpg = mocker.patch.object(cl.gpg, 'decrypt_submission_or_reply', side_effect=CryptoError()) mocker.patch('shutil.move') cl.on_file_downloaded(result_data, current_object=submission_db_object) mock_gpg.call_count == 1 cl.set_status.assert_called_once_with( "Failed to download and decrypt file, please try again.")
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_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_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()
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_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_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_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_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_downloaded_decrypt_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.set_status = mock.MagicMock() result_data = ('this-is-a-sha256-sum', test_filename) submission_db_object = mock.MagicMock() submission_db_object.uuid = 'myuuid' submission_db_object.filename = 'filename' with mock.patch('securedrop_client.crypto.decrypt_submission_or_reply', return_value=(1, '')) as mock_gpg, \ mock.patch('shutil.move'): cl.on_file_downloaded(result_data, current_object=submission_db_object) mock_gpg.call_count == 1 cl.set_status.assert_called_once_with( "Failed to download and decrypt file, please try again.")
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()