Exemplo n.º 1
0
def test_Controller_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()
    co = Controller('http://localhost', mock_gui, mock_session, homedir)
    co.file_ready = mocker.MagicMock()  # signal when file is downloaded
    co.update_sources = mocker.MagicMock()
    co.api_runner = mocker.MagicMock()

    test_filename = "1-my-file-location-msg.gpg"
    co.api_runner.result = ("", test_filename)
    co.call_reset = mocker.MagicMock()
    co.set_status = mocker.MagicMock()
    result_data = Exception('error message')
    submission_db_object = mocker.MagicMock()
    submission_db_object.uuid = 'myuuid'
    submission_db_object.filename = 'filename'

    co.on_file_download_failure(result_data,
                                current_object=submission_db_object)

    co.set_status.assert_called_once_with(
        "The file download failed. Please try again.")
    co.file_ready.emit.assert_not_called()
Exemplo n.º 2
0
def test_Controller_on_sync_success_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()
    co = Controller('http://localhost', mock_gui, mock_session, homedir)
    co.update_sources = mocker.MagicMock()
    co.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')

    co.call_reset = mocker.MagicMock()
    mock_storage = mocker.patch('securedrop_client.logic.storage')
    mocker.patch.object(co.gpg, 'import_key', side_effect=CryptoError)
    co.on_sync_success(result_data)
    mock_storage.update_local_storage. \
        assert_called_once_with(mock_session, mock_sources, "submissions",
                                "replies",
                                os.path.join(homedir, 'data'))
    co.update_sources.assert_called_once_with()
Exemplo n.º 3
0
def test_Controller_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()
    co = Controller('http://localhost', mock_gui, mock_session, homedir)
    co.update_sources = mocker.MagicMock()
    co.api_runner = mocker.MagicMock()
    co.file_ready = mocker.MagicMock()  # signal when file is downloaded

    test_filename = "1-my-file-location-msg.gpg"
    test_object_uuid = 'uuid-of-downloaded-object'
    co.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(co.gpg,
                                   'decrypt_submission_or_reply',
                                   return_value='filepath')
    mocker.patch('shutil.move')

    co.on_file_download_success(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)
    mock_storage.set_object_decryption_status_with_content.assert_called_once_with(
        submission_db_object, mock_session, True)

    # Signal should be emitted with UUID of the successfully downloaded object
    co.file_ready.emit.assert_called_once_with(test_object_uuid)
Exemplo n.º 4
0
def test_Controller_on_sync_success(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()
    co = Controller('http://localhost', mock_gui, mock_session, homedir)
    co.update_sources = mocker.MagicMock()
    co.api_runner = mocker.MagicMock()
    co.gpg = mocker.MagicMock()

    result_data = ('sources', 'submissions', 'replies')

    co.update_sources = mocker.MagicMock()
    co.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')

    co.call_reset = mocker.MagicMock()
    mock_storage = mocker.patch('securedrop_client.logic.storage')
    co.on_sync_success(result_data)
    mock_storage.update_local_storage. \
        assert_called_once_with(mock_session, mock_sources, "submissions",
                                "replies",
                                os.path.join(homedir, 'data'))

    co.update_sources.assert_called_once_with()
Exemplo n.º 5
0
def test_Controller_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()
    co = Controller('http://localhost', mock_gui, mock_session, homedir)

    co.update_sources = mocker.MagicMock()
    co.api_runner = mocker.MagicMock()
    co.file_ready = mocker.MagicMock()  # signal when file is downloaded

    test_filename = "1-my-file-location-msg.gpg"
    co.api_runner.result = ("", test_filename)
    co.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(co.gpg,
                                   'decrypt_submission_or_reply',
                                   side_effect=CryptoError())
    mock_storage = mocker.patch('securedrop_client.logic.storage')
    mocker.patch('shutil.move')

    co.on_file_download_success(result_data,
                                current_object=submission_db_object)
    mock_gpg.call_count == 1
    co.set_status.assert_called_once_with(
        "Failed to decrypt file, please try again or talk to your administrator."
    )
    mock_storage.set_object_decryption_status_with_content.assert_called_once_with(
        submission_db_object, mock_session, False)
    co.file_ready.emit.assert_not_called()