Exemplo n.º 1
0
def test_post_deposit_binary_upload_fail_if_content_length_missing(
        authenticated_client, deposit_collection, sample_archive, tmp_path):
    """The Content-Length header is mandatory"""
    tmp_path = str(tmp_path)
    url = reverse(COL_IRI, args=[deposit_collection.name])

    archive = create_arborescence_archive(tmp_path,
                                          "archive2",
                                          "file2",
                                          b"some content in file",
                                          up_to_size=500)

    external_id = "some-external-id"

    # when
    response = post_archive(
        authenticated_client,
        url,
        archive,
        CONTENT_LENGTH=None,
        HTTP_SLUG=external_id,
        HTTP_IN_PROGRESS="false",
    )

    # then
    assert response.status_code == status.HTTP_400_BAD_REQUEST
    assert b"the CONTENT_LENGTH header must be sent." in response.content

    with pytest.raises(Deposit.DoesNotExist):
        Deposit.objects.get(external_id=external_id)
Exemplo n.º 2
0
def test_post_deposit_binary_upload_fail_if_upload_size_limit_exceeded(
        authenticated_client, deposit_collection, sample_archive, tmp_path):
    """Binary upload must not exceed the limit set up..."""
    tmp_path = str(tmp_path)
    url = reverse(COL_IRI, args=[deposit_collection.name])

    archive = create_arborescence_archive(tmp_path,
                                          "archive2",
                                          "file2",
                                          b"some content in file",
                                          up_to_size=5000)

    external_id = "some-external-id"

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

    # 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)
Exemplo n.º 3
0
def sample_archive(tmp_path):
    """Returns a sample archive"""
    tmp_path = str(tmp_path)  # pytest version limitation in previous version
    archive = create_arborescence_archive(
        tmp_path, "archive1", "file1", b"some content in file"
    )

    return archive
def test_replace_archive_to_deposit_is_possible(
    tmp_path,
    partial_deposit,
    deposit_collection,
    authenticated_client,
    sample_archive,
    atom_dataset,
):
    """Replace all archive with another one should return a 204 response"""
    tmp_path = str(tmp_path)
    # given
    deposit = partial_deposit
    requests = DepositRequest.objects.filter(deposit=deposit, type="archive")

    assert len(list(requests)) == 1
    check_archive(sample_archive["name"], requests[0].archive.name)

    # we have no metadata for that deposit
    requests = list(
        DepositRequest.objects.filter(deposit=deposit, type="metadata"))
    assert len(requests) == 0

    response = post_atom(
        authenticated_client,
        reverse(SE_IRI, args=[deposit_collection.name, deposit.id]),
        data=atom_dataset["entry-data1"],
        HTTP_SLUG=deposit.external_id,
        HTTP_IN_PROGRESS=True,
    )

    requests = list(
        DepositRequest.objects.filter(deposit=deposit, type="metadata"))
    assert len(requests) == 1

    update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit.id])
    external_id = "some-external-id-1"
    archive2 = create_arborescence_archive(tmp_path, "archive2", "file2",
                                           b"some other content in file")

    response = put_archive(
        authenticated_client,
        update_uri,
        archive2,
        HTTP_SLUG=external_id,
        HTTP_IN_PROGRESS="false",
    )

    assert response.status_code == status.HTTP_204_NO_CONTENT

    requests = DepositRequest.objects.filter(deposit=deposit, type="archive")

    assert len(list(requests)) == 1
    check_archive(archive2["name"], requests[0].archive.name)

    # check we did not touch the other parts
    requests = list(
        DepositRequest.objects.filter(deposit=deposit, type="metadata"))
    assert len(requests) == 1
def test_add_archive_to_deposit_is_possible(
    tmp_path,
    authenticated_client,
    deposit_collection,
    partial_deposit_with_metadata,
    sample_archive,
):
    """Add another archive to a deposit return a 201 response"""
    tmp_path = str(tmp_path)
    deposit = partial_deposit_with_metadata

    requests = DepositRequest.objects.filter(deposit=deposit, type="archive")

    assert len(requests) == 1
    check_archive(sample_archive["name"], requests[0].archive.name)

    requests_meta0 = DepositRequest.objects.filter(deposit=deposit,
                                                   type="metadata")
    assert len(requests_meta0) == 1

    update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit.id])

    external_id = "some-external-id-1"
    archive2 = create_arborescence_archive(tmp_path, "archive2", "file2",
                                           b"some other content in file")

    response = post_archive(
        authenticated_client,
        update_uri,
        archive2,
        HTTP_SLUG=external_id,
        HTTP_IN_PROGRESS="false",
    )

    assert response.status_code == status.HTTP_201_CREATED

    requests = DepositRequest.objects.filter(deposit=deposit,
                                             type="archive").order_by("id")

    assert len(requests) == 2
    # first archive still exists
    check_archive(sample_archive["name"], requests[0].archive.name)
    # a new one was added
    check_archive(archive2["name"], requests[1].archive.name)

    # check we did not touch the other parts
    requests_meta1 = DepositRequest.objects.filter(deposit=deposit,
                                                   type="metadata")
    assert len(requests_meta1) == 1
    assert set(requests_meta0) == set(requests_meta1)
def test_access_to_existing_deposit_with_multiple_archives(
        tmp_path, authenticated_client, deposit_collection, partial_deposit,
        sample_archive):
    """Access to deposit should stream a 200 response with its raw contents"""
    deposit = partial_deposit
    archive2 = create_arborescence_archive(tmp_path, "archive2", "file2",
                                           b"some other content in file")

    # Add a second archive to deposit
    update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit.id])
    response = authenticated_client.post(
        update_uri,
        content_type="application/zip",  # as zip
        data=archive2["data"],
        # + headers
        CONTENT_LENGTH=archive2["length"],
        HTTP_SLUG=deposit.external_id,
        HTTP_CONTENT_MD5=archive2["md5sum"],
        HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip",
        HTTP_IN_PROGRESS="false",
        HTTP_CONTENT_DISPOSITION="attachment; filename=%s" %
        (archive2["name"], ),
    )
    assert response.status_code == status.HTTP_201_CREATED

    for i, url in enumerate(
            private_get_raw_url_endpoints(deposit_collection, deposit)):
        response = authenticated_client.get(url)

        assert response.status_code == status.HTTP_200_OK
        assert response["content-type"] == "application/tar"
        # write the response stream in a temporary archive
        archive_path = join(tmp_path, f"archive_{i}.tar")
        with open(archive_path, "wb") as f:
            for chunk in response.streaming_content:
                f.write(chunk)

        # to check its properties are correct
        tfile = tarfile.open(archive_path)
        assert set(tfile.getnames()) == {".", "./file1", "./file2"}
        assert tfile.extractfile("./file1").read() == b"some content in file"
        assert tfile.extractfile(
            "./file2").read() == b"some other content in file"
def create_deposit_archive_with_archive(
    root_path, archive_extension, client, collection_name
):
    # we create the holding archive to a given extension
    archive = create_arborescence_archive(
        root_path,
        "archive1",
        "file1",
        b"some content in file",
        extension=archive_extension,
    )

    # now we create an archive holding the first created archive
    invalid_archive = create_archive_with_archive(root_path, "invalid.tgz", archive)

    # we deposit it
    response = client.post(
        reverse(COL_IRI, args=[collection_name]),
        content_type="application/x-tar",
        data=invalid_archive["data"],
        CONTENT_LENGTH=invalid_archive["length"],
        HTTP_MD5SUM=invalid_archive["md5sum"],
        HTTP_SLUG="external-id",
        HTTP_IN_PROGRESS=False,
        HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (invalid_archive["name"],),
    )

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

    deposit = Deposit.objects.get(pk=deposit_id)
    assert DEPOSIT_STATUS_DEPOSITED == deposit.status
    return deposit
def test_post_deposit_binary_and_post_to_add_another_archive(
        authenticated_client, deposit_collection, sample_archive, tmp_path):
    """Updating a deposit should return a 201 with receipt"""
    tmp_path = str(tmp_path)
    url = reverse(COL_IRI, args=[deposit_collection.name])

    external_id = "some-external-id-1"

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

    # then
    assert response.status_code == status.HTTP_201_CREATED

    response_content = parse_xml(response.content)
    deposit_id = 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_request = DepositRequest.objects.get(deposit=deposit)
    assert deposit_request.deposit == deposit
    assert deposit_request.type == "archive"
    check_archive(sample_archive["name"], deposit_request.archive.name)

    # 2nd archive to upload
    archive2 = create_arborescence_archive(tmp_path, "archive2", "file2",
                                           b"some other content in file")

    # uri to update the content
    update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit_id])

    # adding another archive for the deposit and finalizing it
    response = post_archive(
        authenticated_client,
        update_uri,
        archive2,
        HTTP_SLUG=external_id,
    )

    assert response.status_code == status.HTTP_201_CREATED
    response_content = parse_xml(response.content)

    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 = list(
        DepositRequest.objects.filter(deposit=deposit).order_by("id"))

    # 2 deposit requests for the same deposit
    assert len(deposit_requests) == 2
    assert deposit_requests[0].deposit == deposit
    assert deposit_requests[0].type == "archive"
    check_archive(sample_archive["name"], deposit_requests[0].archive.name)

    assert deposit_requests[1].deposit == deposit
    assert deposit_requests[1].type == "archive"
    check_archive(archive2["name"], deposit_requests[1].archive.name)

    # only 1 deposit in db
    deposits = Deposit.objects.all()
    assert len(deposits) == 1
def test_post_deposit_then_update_refused(authenticated_client,
                                          deposit_collection, sample_archive,
                                          atom_dataset, tmp_path):
    """Updating a deposit with status 'ready' should return a 400"""
    tmp_path = str(tmp_path)
    url = reverse(COL_IRI, args=[deposit_collection.name])

    external_id = "some-external-id-1"

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

    # then
    assert response.status_code == status.HTTP_201_CREATED

    response_content = parse_xml(response.content)
    deposit_id = 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_request = DepositRequest.objects.get(deposit=deposit)
    assert deposit_request.deposit == deposit
    check_archive(sample_archive["name"], deposit_request.archive.name)

    # updating/adding is forbidden

    # uri to update the content
    edit_iri = reverse("edit_iri", args=[deposit_collection.name, deposit_id])
    se_iri = reverse("se_iri", args=[deposit_collection.name, deposit_id])
    em_iri = reverse("em_iri", args=[deposit_collection.name, deposit_id])

    # Testing all update/add endpoint should fail
    # since the status is ready

    archive2 = create_arborescence_archive(tmp_path, "archive2", "file2",
                                           b"some content in file 2")

    # replacing file is no longer possible since the deposit's
    # status is ready
    r = put_archive(
        authenticated_client,
        em_iri,
        archive2,
        HTTP_SLUG=external_id,
        HTTP_IN_PROGRESS="false",
    )

    assert r.status_code == status.HTTP_400_BAD_REQUEST
    assert (ET.fromstring(r.content).findtext("atom:summary",
                                              namespaces=NAMESPACES) ==
            "You can only act on deposit with status 'partial'")

    # adding file is no longer possible since the deposit's status
    # is ready
    r = post_archive(
        authenticated_client,
        em_iri,
        archive2,
        HTTP_SLUG=external_id,
        HTTP_IN_PROGRESS="false",
    )

    assert r.status_code == status.HTTP_400_BAD_REQUEST
    assert (ET.fromstring(r.content).findtext("atom:summary",
                                              namespaces=NAMESPACES) ==
            "You can only act on deposit with status 'partial'")

    # replacing metadata is no longer possible since the deposit's
    # status is ready
    r = put_atom(
        authenticated_client,
        edit_iri,
        data=atom_dataset["entry-data-deposit-binary"],
        CONTENT_LENGTH=len(atom_dataset["entry-data-deposit-binary"]),
        HTTP_SLUG=external_id,
    )

    assert r.status_code == status.HTTP_400_BAD_REQUEST
    assert (ET.fromstring(r.content).findtext("atom:summary",
                                              namespaces=NAMESPACES) ==
            "You can only act on deposit with status 'partial'")

    # adding new metadata is no longer possible since the
    # deposit's status is ready
    r = post_atom(
        authenticated_client,
        se_iri,
        data=atom_dataset["entry-data-deposit-binary"],
        CONTENT_LENGTH=len(atom_dataset["entry-data-deposit-binary"]),
        HTTP_SLUG=external_id,
    )

    assert r.status_code == status.HTTP_400_BAD_REQUEST
    assert (ET.fromstring(r.content).findtext("atom:summary",
                                              namespaces=NAMESPACES) ==
            "You can only act on deposit with status 'partial'")

    archive_content = b"some content representing archive"
    archive = InMemoryUploadedFile(
        BytesIO(archive_content),
        field_name="archive0",
        name="archive0",
        content_type="application/zip",
        size=len(archive_content),
        charset=None,
    )

    atom_entry = InMemoryUploadedFile(
        BytesIO(atom_dataset["entry-data-deposit-binary"].encode("utf-8")),
        field_name="atom0",
        name="atom0",
        content_type='application/atom+xml; charset="utf-8"',
        size=len(atom_dataset["entry-data-deposit-binary"]),
        charset="utf-8",
    )

    # replacing multipart metadata is no longer possible since the
    # deposit's status is ready
    r = authenticated_client.put(
        edit_iri,
        format="multipart",
        data={
            "archive": archive,
            "atom_entry": atom_entry,
        },
    )

    assert r.status_code == status.HTTP_400_BAD_REQUEST
    assert (ET.fromstring(r.content).findtext("atom:summary",
                                              namespaces=NAMESPACES) ==
            "You can only act on deposit with status 'partial'")

    # adding new metadata is no longer possible since the
    # deposit's status is ready
    r = authenticated_client.post(
        se_iri,
        format="multipart",
        data={
            "archive": archive,
            "atom_entry": atom_entry,
        },
    )

    assert r.status_code == status.HTTP_400_BAD_REQUEST
    assert (ET.fromstring(r.content).findtext("atom:summary",
                                              namespaces=NAMESPACES) ==
            "You can only act on deposit with status 'partial'")