Exemplo n.º 1
0
def test_ReplySync_run_success(mocker, session, source):
    """Test when a reply successfully downloads and decrypts."""
    reply = factory.Reply(source=source['source'],
                          is_downloaded=False,
                          is_decrypted=None,
                          content=None)
    session.add(reply)
    session.commit()

    expected_content = 'foo'

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

    # mock the fetching of replies
    mocker.patch('securedrop_client.storage.find_new_replies',
                 return_value=[reply])
    mock_download_status = mocker.patch(
        'securedrop_client.message_sync.storage.mark_reply_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)
    rs = ReplySync(api, 'mock', True)
    rs.session = session  # "patch" it with a real session
    rs.api.download_reply = mocker.MagicMock(
        return_value=(1234, "/home/user/downloads/foo"))

    mock_reply_ready = mocker.patch.object(rs, 'reply_ready')

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

    mock_decryption_status.assert_called_once_with(reply, api.session, True,
                                                   expected_content)
    mock_download_status.called_once_with(reply, api.mock_session)
    mock_reply_ready.emit.assert_called_once_with(reply.uuid, expected_content)
Exemplo n.º 2
0
def test_ReplySync_run_decryption_error(mocker, session, source):
    """Test when a reply successfully downloads, but does not successfully decrypt."""
    reply = factory.Reply(source=source['source'],
                          is_downloaded=False,
                          is_decrypted=None,
                          content=None)
    session.add(reply)
    session.commit()

    # mock the fetching of replies
    mocker.patch('securedrop_client.storage.find_new_replies',
                 return_value=[reply])
    mock_download_status = mocker.patch(
        'securedrop_client.message_sync.storage.mark_reply_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)

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

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

    mock_reply_ready = mocker.patch.object(rs, 'reply_ready')

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

    mock_download_status.assert_called_once_with(reply.uuid, session)
    mock_decryption_status.assert_called_once_with(reply, api.session, False)
    mock_reply_ready.emit.assert_called_once_with(reply.uuid,
                                                  '<Reply not yet available>')