Exemple #1
0
def test_post_deposit_multipart_without_origin_url(
    authenticated_client,
    deposit_collection,
    atom_dataset,
    mocker,
    deposit_user,
    sample_archive,
):
    # given
    url = reverse(COL_IRI, args=[deposit_collection.name])
    data_atom_entry = atom_dataset["entry-data-deposit-binary"]
    id_ = str(uuid.uuid4())
    mocker.patch("uuid.uuid4", return_value=id_)

    # when
    response = post_multipart(
        authenticated_client,
        url,
        sample_archive,
        data_atom_entry,
        HTTP_IN_PROGRESS="false",
    )

    assert response.status_code == status.HTTP_201_CREATED
    response_content = parse_xml(response.content)
    deposit_id = int(
        response_content.findtext("swh:deposit_id", namespaces=NAMESPACES))

    deposit = Deposit.objects.get(pk=deposit_id)
    assert deposit.collection == deposit_collection
    assert deposit.origin_url == deposit_user.provider_url + id_
    assert deposit.status == DEPOSIT_STATUS_DEPOSITED
Exemple #2
0
def test_post_deposit_multipart_if_upload_size_limit_exceeded(
        authenticated_client, deposit_collection, atom_dataset,
        sample_archive):
    # given
    url = reverse(COL_IRI, args=[deposit_collection.name])

    archive = {
        **sample_archive,
        "data": sample_archive["data"] * 100,
    }
    data_atom_entry = atom_dataset["entry-data-deposit-binary"]

    external_id = "external-id"

    # when
    response = post_multipart(
        authenticated_client,
        url,
        archive,
        data_atom_entry,
        HTTP_IN_PROGRESS="false",
        HTTP_SLUG=external_id,
    )

    # then
    assert response.status_code == status.HTTP_413_REQUEST_ENTITY_TOO_LARGE
    assert b"Upload size limit exceeded" in response.content

    with pytest.raises(Deposit.DoesNotExist):
        Deposit.objects.get(external_id=external_id)
Exemple #3
0
def test_post_deposit_multipart(
    authenticated_client,
    deposit_collection,
    atom_dataset,
    mocker,
    deposit_user,
    sample_archive,
):
    # given
    external_id = "foobar"
    origin_url = deposit_user.provider_url + external_id
    url = reverse(COL_IRI, args=[deposit_collection.name])
    data_atom_entry = atom_dataset["entry-data0"] % origin_url

    # when
    response = post_multipart(
        authenticated_client,
        url,
        sample_archive,
        data_atom_entry,
        HTTP_IN_PROGRESS="false",
    )

    assert response.status_code == status.HTTP_201_CREATED
    response_content = parse_xml(response.content)
    deposit_id = int(
        response_content.findtext("swh:deposit_id", namespaces=NAMESPACES))

    deposit = Deposit.objects.get(pk=deposit_id)
    assert deposit.collection == deposit_collection
    assert deposit.origin_url == origin_url
    assert deposit.status == DEPOSIT_STATUS_DEPOSITED
def test_add_both_archive_and_metadata_to_deposit(
    authenticated_client,
    deposit_collection,
    partial_deposit_with_metadata,
    atom_dataset,
    sample_archive,
    deposit_user,
):
    """Scenario: Add both a new archive and new metadata to a partial deposit is ok

    Response: 201

    """
    deposit = partial_deposit_with_metadata
    origin_url = deposit_user.provider_url + deposit.external_id
    requests = DepositRequest.objects.filter(deposit=deposit, type="metadata")
    assert len(requests) == 1

    requests_archive0 = DepositRequest.objects.filter(deposit=deposit,
                                                      type="archive")
    assert len(requests_archive0) == 1

    data_atom_entry = atom_dataset["entry-data1"]
    response = post_multipart(
        authenticated_client,
        reverse(SE_IRI, args=[deposit_collection.name, deposit.id]),
        sample_archive,
        data_atom_entry,
    )

    assert response.status_code == status.HTTP_201_CREATED
    requests = DepositRequest.objects.filter(deposit=deposit,
                                             type="metadata").order_by("id")

    assert len(requests) == 1 + 1, "New deposit request archive got added"
    expected_raw_meta0 = atom_dataset["entry-data0"] % origin_url
    # a new one was added
    assert requests[0].raw_metadata == expected_raw_meta0
    assert requests[1].raw_metadata == data_atom_entry

    # check we did not touch the other parts
    requests_archive1 = DepositRequest.objects.filter(deposit=deposit,
                                                      type="archive")
    assert len(
        requests_archive1) == 1 + 1, "New deposit request metadata got added"
Exemple #5
0
def test_post_deposit_multipart_zip(authenticated_client, deposit_collection,
                                    atom_dataset, sample_archive):
    """one multipart deposit (zip+xml) should be accepted"""
    # given
    url = reverse(COL_IRI, args=[deposit_collection.name])
    data_atom_entry = atom_dataset["entry-data-deposit-binary"]
    external_id = "external-id"

    # when
    response = post_multipart(
        authenticated_client,
        url,
        sample_archive,
        data_atom_entry,
        HTTP_IN_PROGRESS="false",
        HTTP_SLUG=external_id,
    )

    # then
    assert response.status_code == status.HTTP_201_CREATED

    response_content = parse_xml(response.content)
    deposit_id = int(
        response_content.findtext("swh:deposit_id", namespaces=NAMESPACES))

    deposit = Deposit.objects.get(pk=deposit_id)
    assert deposit.status == DEPOSIT_STATUS_DEPOSITED
    assert deposit.external_id == external_id
    assert deposit.collection == deposit_collection
    assert deposit.swhid is None

    deposit_requests = DepositRequest.objects.filter(deposit=deposit)
    assert len(deposit_requests) == 2
    for deposit_request in deposit_requests:
        assert deposit_request.deposit == deposit
        if deposit_request.type == "archive":
            check_archive(sample_archive["name"], deposit_request.archive.name)
            assert deposit_request.raw_metadata is None
        else:
            assert (parse_xml(deposit_request.raw_metadata).findtext(
                "atom:id", namespaces=NAMESPACES) ==
                    "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a")
            assert deposit_request.raw_metadata == data_atom_entry
Exemple #6
0
def test_post_deposit_multipart_400_when_badly_formatted_xml(
        authenticated_client, deposit_collection, sample_archive,
        atom_dataset):
    # given
    url = reverse(COL_IRI, args=[deposit_collection.name])
    data_atom_entry_ko = atom_dataset["entry-data-ko"]

    # when
    response = post_multipart(
        authenticated_client,
        url,
        sample_archive,
        data_atom_entry_ko,
        HTTP_IN_PROGRESS="false",
        HTTP_SLUG="external-id",
    )

    assert b"Malformed xml metadata" in response.content
    assert response.status_code == status.HTTP_400_BAD_REQUEST
Exemple #7
0
def test_post_deposit_multipart_put_to_replace_metadata(
        authenticated_client, deposit_collection, atom_dataset,
        sample_archive):
    """One multipart deposit followed by a metadata update should be
    accepted

    """
    # given
    url = reverse(COL_IRI, args=[deposit_collection.name])
    data_atom_entry = atom_dataset["entry-data-deposit-binary"]
    external_id = "external-id"

    # when
    response = post_multipart(
        authenticated_client,
        url,
        sample_archive,
        data_atom_entry,
        HTTP_IN_PROGRESS="true",
        HTTP_SLUG=external_id,
    )

    # then
    assert response.status_code == status.HTTP_201_CREATED

    response_content = parse_xml(response.content)
    deposit_id = int(
        response_content.findtext("swh:deposit_id", namespaces=NAMESPACES))

    deposit = Deposit.objects.get(pk=deposit_id)
    assert deposit.status == "partial"
    assert deposit.external_id == external_id
    assert deposit.collection == deposit_collection
    assert deposit.swhid is None

    deposit_requests = DepositRequest.objects.filter(deposit=deposit)

    assert len(deposit_requests) == 2
    for deposit_request in deposit_requests:
        assert deposit_request.deposit == deposit
        if deposit_request.type == "archive":
            check_archive(sample_archive["name"], deposit_request.archive.name)
        else:
            assert (parse_xml(deposit_request.raw_metadata).findtext(
                "atom:id", namespaces=NAMESPACES) ==
                    "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a")
            assert deposit_request.raw_metadata == data_atom_entry

    replace_metadata_uri = response["location"]
    response = authenticated_client.put(
        replace_metadata_uri,
        content_type="application/atom+xml;type=entry",
        data=atom_dataset["entry-data-deposit-binary"],
        HTTP_IN_PROGRESS="false",
    )

    assert response.status_code == status.HTTP_204_NO_CONTENT

    # deposit_id did not change
    deposit = Deposit.objects.get(pk=deposit_id)
    assert deposit.status == DEPOSIT_STATUS_DEPOSITED
    assert deposit.external_id == external_id
    assert deposit.collection == deposit_collection
    assert deposit.swhid is None

    deposit_requests = DepositRequest.objects.filter(deposit=deposit)
    assert len(deposit_requests) == 2
    for deposit_request in deposit_requests:
        assert deposit_request.deposit == deposit
        if deposit_request.type == "archive":
            check_archive(sample_archive["name"], deposit_request.archive.name)
        else:
            assert (parse_xml(deposit_request.raw_metadata).findtext(
                "atom:id", namespaces=NAMESPACES) ==
                    "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a")
            assert (deposit_request.raw_metadata ==
                    atom_dataset["entry-data-deposit-binary"])