Ejemplo n.º 1
0
def test_Controller_on_file_download_Submission(homedir, config, mocker):
    """
    If the handler is passed a submission, check the download_submission
    function is the one called against the API.
    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.call_api = mocker.MagicMock()
    co.api = mocker.MagicMock()

    source = factory.Source()
    file_ = db.File(source=source,
                    uuid='uuid',
                    size=1234,
                    filename='1-myfile.doc.gpg',
                    download_url='http://myserver/myfile',
                    is_downloaded=False)

    submission_sdk_object = mocker.MagicMock()
    mock_submission = mocker.patch('sdclientapi.Submission')
    mock_submission.return_value = submission_sdk_object

    co.on_file_download(source, file_)
    co.call_api.assert_called_once_with(
        co.api.download_submission,
        co.on_file_download_success,
        co.on_file_download_failure,
        submission_sdk_object,
        co.data_dir,
        current_object=file_,
    )
Ejemplo n.º 2
0
def test_delete_single_submission_or_reply_single_file_no_folder(
        homedir, mocker):
    """
    This test checks that calling the delete_single_submission_or_reply
    method deletes the file even if its not in a per-document folder.
    """

    source = factory.Source(journalist_designation="dissolved-steak")
    file_server_filename = "1-dissolved-steak-msg.gpg"
    test_obj = db.File(
        source=source,
        uuid="test",
        size=123,
        filename=file_server_filename,
        download_url="http://test/test",
    )
    original_location = test_obj.location
    test_obj.location = mocker.MagicMock(side_effect=[
        os.path.join(homedir, file_server_filename),
        original_location(homedir)
    ])
    add_test_file_to_temp_dir(homedir, file_server_filename)

    delete_single_submission_or_reply_on_disk(test_obj, homedir)

    with pytest.raises(FileNotFoundError):
        open(os.path.join(homedir, file_server_filename), "r")
Ejemplo n.º 3
0
def test_delete_single_submission_or_reply_single_file(homedir, mocker):
    """
    This test checks that calling the delete_single_submission_or_reply
    method deletes the file as well as the folder it's inside.
    """

    source = factory.Source(journalist_designation="dissolved-steak")
    file_server_filename = "1-dissolved-steak-msg.gpg"
    test_obj = db.File(
        source=source,
        uuid="test",
        size=123,
        filename=file_server_filename,
        download_url="http://test/test",
    )
    source_directory = os.path.dirname(test_obj.location(homedir))
    add_test_file_to_temp_dir(source_directory, file_server_filename)

    delete_single_submission_or_reply_on_disk(test_obj, homedir)

    # Ensure both file and its containing folder are gone.
    with pytest.raises(FileNotFoundError):
        open(os.path.join(source_directory, file_server_filename), "r")

    with pytest.raises(FileNotFoundError):
        open(source_directory, "r")
Ejemplo n.º 4
0
def File(**attrs):
    global FILE_COUNT
    FILE_COUNT += 1
    defaults = dict(
        uuid='source-uuid-{}'.format(FILE_COUNT),
        filename='{}-reply.gpg'.format(FILE_COUNT),
        size=123,
        download_url='http://wat.onion/abc',
        is_decrypted=True,
        is_downloaded=True,
    )

    defaults.update(attrs)

    return db.File(**defaults)
Ejemplo n.º 5
0
def File(**attrs):
    global FILE_COUNT
    FILE_COUNT += 1
    defaults = dict(
        uuid="file-uuid-{}".format(FILE_COUNT),
        filename="{}-doc.gz.gpg".format(FILE_COUNT),
        size=123,
        download_url="http://wat.onion/abc",
        is_decrypted=True,
        is_downloaded=True,
    )

    defaults.update(attrs)

    return db.File(**defaults)
Ejemplo n.º 6
0
def test_Controller_on_file_download_user_not_signed_in(
        homedir, config, mocker):
    """
    If a user clicks the download button but is not logged in,
    an error should appear.
    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)
    source = factory.Source()
    file_ = db.File(source=source,
                    uuid='uuid',
                    size=1234,
                    filename='1-myfile.doc.gpg',
                    download_url='http://myserver/myfile',
                    is_downloaded=False)
    co.on_action_requiring_login = mocker.MagicMock()
    co.api = None
    submission_sdk_object = mocker.MagicMock()
    mock_submission = mocker.patch('sdclientapi.Submission')
    mock_submission.return_value = submission_sdk_object
    co.on_file_download(source, file_)
    co.on_action_requiring_login.assert_called_once_with()
Ejemplo n.º 7
0
def test_update_sources_deletes_files_associated_with_the_source(
        homedir, mocker, session_maker):
    """
    Check that:

    * Sources are deleted on disk after sync.
    """
    mock_session = mocker.MagicMock()

    # Test scenario: one source locally, no sources on server.
    remote_sources = []

    # A local source object. To ensure that all submissions/replies from
    # various stages of processing are cleaned up, we'll add several filenames
    # associated with each message, document, and reply for each stage of processing.
    # This simulates if a step failed.
    msg_server_filename = "1-pericardial-surfacing-msg.gpg"
    msg_local_filename_decrypted = "1-pericardial-surfacing-msg"

    file_server_filename = "1-pericardial-surfacing-doc.gz.gpg"
    file_local_filename_decompressed = "1-pericardial-surfacing-doc"
    file_local_filename_decrypted = "1-pericardial-surfacing-doc.gz"

    reply_server_filename = "1-pericardial-surfacing-reply.gpg"
    reply_local_filename_decrypted = "1-pericardial-surfacing-reply"

    # Here we're not mocking out the models use so that we can use the collection attribute.
    local_source = factory.Source(journalist_designation="beep_boop")
    file_submission = db.File(
        source=local_source,
        uuid="test",
        size=123,
        filename=file_server_filename,
        download_url="http://test/test",
    )
    msg_submission = db.File(
        source=local_source,
        uuid="test",
        size=123,
        filename=msg_server_filename,
        download_url="http://test/test",
    )
    user = db.User(username="******")
    reply = db.Reply(source=local_source,
                     journalist=user,
                     filename=reply_server_filename,
                     size=1234,
                     uuid="test")
    local_source.submissions = [file_submission, msg_submission]
    local_source.replies = [reply]

    # Make the test files on disk in tmpdir so we can check they get deleted.
    test_filename_absolute_paths = []
    sourcedir = os.path.join(homedir, local_source.journalist_filename)
    for test_filename in [
            msg_server_filename,
            msg_local_filename_decrypted,
            file_server_filename,
            file_local_filename_decompressed,
            file_local_filename_decrypted,
            reply_server_filename,
            reply_local_filename_decrypted,
    ]:
        abs_server_filename = add_test_file_to_temp_dir(
            sourcedir, test_filename)
        test_filename_absolute_paths.append(abs_server_filename)

    local_sources = [local_source]

    update_sources(remote_sources, local_sources, mock_session, homedir)

    # Ensure the files associated with the reply are deleted on disk.
    for test_filename in test_filename_absolute_paths:
        assert not os.path.exists(test_filename)

    # Ensure the record for the local source is gone, along with its
    # related files.
    mock_session.delete.assert_called_with(local_source)

    # Session is committed to database.
    assert mock_session.commit.call_count == 1