コード例 #1
0
def test_ReplySync_run_failure():
    reply = mock.MagicMock()
    reply.download_url = "http://foo"
    reply.filename = "foo.gpg"

    fh = mock.MagicMock()
    fh.name = "foo"

    with mock.patch('securedrop_client.storage.find_new_replies',
                    return_value=[
                        reply
                    ]),\
            mock.patch('subprocess.call',
                       return_value=1), \
            mock.patch('shutil.move'), \
            mock.patch('shutil.copy'), \
            mock.patch('os.unlink'), \
            mock.patch('securedrop_client.message_sync.storage'
                       '.mark_reply_as_downloaded'), \
            mock.patch(
                'tempfile.NamedTemporaryFile',
                return_value=fh), \
            mock.patch('builtins.open', mock.mock_open(read_data="blah")):

        api = mock.MagicMock()
        home = "/home/user/.sd"
        is_qubes = False

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

        ms.run(False)
コード例 #2
0
def test_ReplySync_run_success(mocker):
    reply = mocker.MagicMock()
    reply.uuid = 'mock id'
    reply.download_url = "http://foo"
    reply.filename = "foo.gpg"

    api = mocker.MagicMock()
    home = "/home/user/.sd"
    is_qubes = True

    # mock the fetching of replies
    mocker.patch('securedrop_client.storage.find_new_replies', return_value=[reply])
    # don't create the signal
    mocker.patch('securedrop_client.message_sync.pyqtSignal')
    # mock the handling of the replies
    mocker.patch('securedrop_client.message_sync.storage.mark_reply_as_downloaded')
    mocker.patch('securedrop_client.message_sync.GpgHelper')

    api = mocker.MagicMock()
    home = "/home/user/.sd"
    is_qubes = True

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

    mock_reply_downloaded = mocker.Mock()
    mock_emit = mocker.Mock()
    mock_reply_downloaded.emit = mock_emit
    mocker.patch.object(ms, 'reply_downloaded', new=mock_reply_downloaded)

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

    assert mock_emit.called
コード例 #3
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)
コード例 #4
0
def test_ReplySync_exception():
    """
    Mostly here for code coverage- makes sure that if an exception is
    raised in the download thread, the code which catches it is actually
    run
    """
    reply = mock.MagicMock()
    api = mock.MagicMock()
    home = "/home/user/.sd"
    is_qubes = False
    rs = ReplySync(api, home, is_qubes)

    with mock.patch('securedrop_client.storage.find_new_replies',
                    return_value=[
                        reply
                    ]),\
        mock.patch("sdclientapi.sdlocalobjects.Reply",
                   mock.MagicMock(side_effect=Exception())):
        rs.run(False)
コード例 #5
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>')
コード例 #6
0
def test_ReplySync_run_failure(mocker):
    reply = mocker.MagicMock()
    reply.download_url = "http://foo"
    reply.filename = "foo.gpg"

    # mock finding new replies
    mocker.patch('securedrop_client.storage.find_new_replies', return_value=[reply])
    # mock handling the new reply
    mocker.patch('securedrop_client.message_sync.storage.mark_file_as_downloaded')
    mocker.patch('securedrop_client.message_sync.GpgHelper')

    api = mocker.MagicMock()
    home = "/home/user/.sd"
    is_qubes = False

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

    # check that it runs without raise exceptions
    ms.run(False)
コード例 #7
0
def test_ReplySync_exception(mocker):
    """
    Mostly here for code coverage- makes sure that if an exception is
    raised in the download thread, the code which catches it is actually
    run
    """
    reply = factory.Reply()
    api = mocker.MagicMock()
    home = "/home/user/.sd"
    is_qubes = False

    mocker.patch('securedrop_client.storage.find_new_replies',
                 return_value=[reply])
    mocker.patch('securedrop_client.message_sync.GpgHelper')
    mocker.patch("sdclientapi.sdlocalobjects.Reply",
                 mocker.MagicMock(side_effect=Exception()))

    rs = ReplySync(api, home, is_qubes)

    # check that it runs without raise exceptions
    rs.run(False)
コード例 #8
0
def test_ReplySync_run_failure(mocker, session, source):
    reply = factory.Reply(source=source['source'])
    session.add(reply)
    session.commit()

    # mock finding new replies
    mocker.patch('securedrop_client.storage.find_new_replies',
                 return_value=[reply])
    # mock handling the new reply
    mocker.patch(
        'securedrop_client.message_sync.storage.mark_reply_as_downloaded')
    mocker.patch('securedrop_client.message_sync.GpgHelper')

    api = mocker.MagicMock()
    home = "/home/user/.sd"
    is_qubes = False

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

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