예제 #1
0
def test_MessageSync_run_success(mocker, session, source):
    """Test when a message successfully downloads and decrypts."""
    message = factory.Message(source=source['source'],
                              is_downloaded=False,
                              is_decrypted=None,
                              content=None)
    session.add(message)
    session.commit()

    expected_content = 'foo'

    def set_object_decryption_status_with_content_side_effect(
            *nargs, **kwargs):
        message.content = expected_content

    # mock the fetching of submissions
    mocker.patch('securedrop_client.storage.find_new_messages',
                 return_value=[message])
    mock_download_status = mocker.patch(
        'securedrop_client.message_sync.storage.mark_message_as_downloaded')
    mock_decryption_status = mocker.patch(
        'securedrop_client.message_sync.storage.set_object_decryption_status_with_content',
        side_effect=set_object_decryption_status_with_content_side_effect)

    # don't create the signal
    mocker.patch('securedrop_client.message_sync.pyqtSignal')

    def mock_decrypt_submission_or_reply(filepath, plaintext_filename, is_doc):
        with open(plaintext_filename, 'w') as f:
            f.write(expected_content)

    # mock the GpgHelper creation since we don't have directories/keys setup
    mock_gpg_helper = mocker.MagicMock(
        decrypt_submission_or_reply=mock_decrypt_submission_or_reply)
    mocker.patch('securedrop_client.message_sync.GpgHelper',
                 return_value=mock_gpg_helper)

    api = mocker.MagicMock(session=session)
    ms = MessageSync(api, 'mock', True)
    ms.session = session  # "patch" it with a real session
    ms.api.download_submission = mocker.MagicMock(
        return_value=(1234, "/home/user/downloads/foo"))

    mock_message_ready = mocker.patch.object(ms, 'message_ready')

    # check that it runs without raising exceptions
    ms.run(False)

    mock_decryption_status.assert_called_once_with(message, api.session, True,
                                                   expected_content)
    mock_download_status.called_once_with(message, api.mock_session)
    mock_message_ready.emit.assert_called_once_with(message.uuid,
                                                    expected_content)
예제 #2
0
def test_MessageSync_run_decryption_error(mocker, session, source):
    """Test when a message successfully downloads, but does not successfully decrypt."""
    message = factory.Message(source=source['source'],
                              is_downloaded=False,
                              is_decrypted=None,
                              content=None)
    session.add(message)
    session.commit()

    # mock the fetching of submissions
    mocker.patch('securedrop_client.storage.find_new_messages',
                 return_value=[message])
    mock_download_status = mocker.patch(
        'securedrop_client.message_sync.storage.mark_message_as_downloaded')
    mock_decryption_status = mocker.patch(
        'securedrop_client.message_sync.storage.set_object_decryption_status_with_content'
    )

    # don't create the signal
    mocker.patch('securedrop_client.message_sync.pyqtSignal')
    # mock the GpgHelper creation since we don't have directories/keys setup
    mocker.patch('securedrop_client.message_sync.GpgHelper')

    api = mocker.MagicMock(session=session)

    ms = MessageSync(api, 'mock', True)
    ms.session = session  # "patch" it with a real session
    mocker.patch.object(ms.gpg,
                        'decrypt_submission_or_reply',
                        side_effect=CryptoError)

    ms.api.download_submission = mocker.MagicMock(
        return_value=(1234, "/home/user/downloads/foo"))

    mock_message_ready = mocker.patch.object(ms, 'message_ready')

    # check that it runs without raising exceptions
    ms.run(False)

    mock_download_status.assert_called_once_with(message.uuid, session)
    mock_decryption_status.assert_called_once_with(message, api.session, False)
    mock_message_ready.emit.assert_called_once_with(
        message.uuid, '<Message not yet available>')