Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_deposit_update_binary.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
"""Tests updates on EM-IRI""" | """Tests updates on EM-IRI""" | ||||
from io import BytesIO | from io import BytesIO | ||||
from django.core.files.uploadedfile import InMemoryUploadedFile | from django.core.files.uploadedfile import InMemoryUploadedFile | ||||
from django.urls import reverse | from django.urls import reverse | ||||
from rest_framework import status | from rest_framework import status | ||||
from swh.deposit.config import COL_IRI, DEPOSIT_STATUS_DEPOSITED, EM_IRI, SE_IRI | from swh.deposit.config import COL_IRI, DEPOSIT_STATUS_DEPOSITED, EM_IRI, SE_IRI | ||||
from swh.deposit.models import Deposit, DepositRequest | from swh.deposit.models import Deposit, DepositRequest | ||||
from swh.deposit.parsers import parse_xml | from swh.deposit.parsers import parse_xml | ||||
from swh.deposit.tests.common import check_archive, create_arborescence_archive | from swh.deposit.tests.common import ( | ||||
check_archive, | |||||
create_arborescence_archive, | |||||
post_archive, | |||||
put_archive, | |||||
) | |||||
def test_post_deposit_binary_and_post_to_add_another_archive( | def test_post_deposit_binary_and_post_to_add_another_archive( | ||||
authenticated_client, deposit_collection, sample_archive, tmp_path | authenticated_client, deposit_collection, sample_archive, tmp_path | ||||
): | ): | ||||
"""Updating a deposit should return a 201 with receipt | """Updating a deposit should return a 201 with receipt | ||||
""" | """ | ||||
tmp_path = str(tmp_path) | tmp_path = str(tmp_path) | ||||
url = reverse(COL_IRI, args=[deposit_collection.name]) | url = reverse(COL_IRI, args=[deposit_collection.name]) | ||||
external_id = "some-external-id-1" | external_id = "some-external-id-1" | ||||
# when | # when | ||||
response = authenticated_client.post( | response = post_archive( | ||||
authenticated_client, | |||||
url, | url, | ||||
content_type="application/zip", # as zip | sample_archive, | ||||
data=sample_archive["data"], | |||||
# + headers | |||||
CONTENT_LENGTH=sample_archive["length"], | |||||
HTTP_SLUG=external_id, | HTTP_SLUG=external_id, | ||||
HTTP_CONTENT_MD5=sample_archive["md5sum"], | |||||
HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", | |||||
HTTP_IN_PROGRESS="true", | HTTP_IN_PROGRESS="true", | ||||
HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (sample_archive["name"],), | |||||
) | ) | ||||
# then | # then | ||||
assert response.status_code == status.HTTP_201_CREATED | assert response.status_code == status.HTTP_201_CREATED | ||||
response_content = parse_xml(BytesIO(response.content)) | response_content = parse_xml(BytesIO(response.content)) | ||||
deposit_id = response_content["swh:deposit_id"] | deposit_id = response_content["swh:deposit_id"] | ||||
Show All 12 Lines | ): | ||||
archive2 = create_arborescence_archive( | archive2 = create_arborescence_archive( | ||||
tmp_path, "archive2", "file2", b"some other content in file" | tmp_path, "archive2", "file2", b"some other content in file" | ||||
) | ) | ||||
# uri to update the content | # uri to update the content | ||||
update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit_id]) | update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit_id]) | ||||
# adding another archive for the deposit and finalizing it | # adding another archive for the deposit and finalizing it | ||||
response = authenticated_client.post( | response = post_archive( | ||||
update_uri, | authenticated_client, update_uri, archive2, HTTP_SLUG=external_id, | ||||
content_type="application/zip", # as zip | |||||
data=archive2["data"], | |||||
# + headers | |||||
CONTENT_LENGTH=archive2["length"], | |||||
HTTP_SLUG=external_id, | |||||
HTTP_CONTENT_MD5=archive2["md5sum"], | |||||
HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", | |||||
HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (archive2["name"]), | |||||
) | ) | ||||
assert response.status_code == status.HTTP_201_CREATED | assert response.status_code == status.HTTP_201_CREATED | ||||
response_content = parse_xml(BytesIO(response.content)) | response_content = parse_xml(BytesIO(response.content)) | ||||
deposit = Deposit.objects.get(pk=deposit_id) | deposit = Deposit.objects.get(pk=deposit_id) | ||||
assert deposit.status == DEPOSIT_STATUS_DEPOSITED | assert deposit.status == DEPOSIT_STATUS_DEPOSITED | ||||
assert deposit.external_id == external_id | assert deposit.external_id == external_id | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | ): | ||||
assert len(requests) == 1 | assert len(requests) == 1 | ||||
update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit.id]) | update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit.id]) | ||||
external_id = "some-external-id-1" | external_id = "some-external-id-1" | ||||
archive2 = create_arborescence_archive( | archive2 = create_arborescence_archive( | ||||
tmp_path, "archive2", "file2", b"some other content in file" | tmp_path, "archive2", "file2", b"some other content in file" | ||||
) | ) | ||||
response = authenticated_client.put( | response = put_archive( | ||||
authenticated_client, | |||||
update_uri, | update_uri, | ||||
content_type="application/zip", # as zip | archive2, | ||||
data=archive2["data"], | |||||
# + headers | |||||
CONTENT_LENGTH=archive2["length"], | |||||
HTTP_SLUG=external_id, | HTTP_SLUG=external_id, | ||||
HTTP_CONTENT_MD5=archive2["md5sum"], | |||||
HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", | |||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (archive2["name"],), | |||||
) | ) | ||||
assert response.status_code == status.HTTP_204_NO_CONTENT | assert response.status_code == status.HTTP_204_NO_CONTENT | ||||
requests = DepositRequest.objects.filter(deposit=deposit, type="archive") | requests = DepositRequest.objects.filter(deposit=deposit, type="archive") | ||||
assert len(list(requests)) == 1 | assert len(list(requests)) == 1 | ||||
check_archive(archive2["name"], requests[0].archive.name) | check_archive(archive2["name"], requests[0].archive.name) | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | ): | ||||
update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit.id]) | update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit.id]) | ||||
external_id = "some-external-id-1" | external_id = "some-external-id-1" | ||||
archive2 = create_arborescence_archive( | archive2 = create_arborescence_archive( | ||||
tmp_path, "archive2", "file2", b"some other content in file" | tmp_path, "archive2", "file2", b"some other content in file" | ||||
) | ) | ||||
response = authenticated_client.post( | response = post_archive( | ||||
authenticated_client, | |||||
update_uri, | update_uri, | ||||
content_type="application/zip", # as zip | archive2, | ||||
data=archive2["data"], | |||||
# + headers | |||||
CONTENT_LENGTH=archive2["length"], | |||||
HTTP_SLUG=external_id, | HTTP_SLUG=external_id, | ||||
HTTP_CONTENT_MD5=archive2["md5sum"], | |||||
HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", | |||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (archive2["name"],), | |||||
) | ) | ||||
assert response.status_code == status.HTTP_201_CREATED | assert response.status_code == status.HTTP_201_CREATED | ||||
requests = DepositRequest.objects.filter(deposit=deposit, type="archive").order_by( | requests = DepositRequest.objects.filter(deposit=deposit, type="archive").order_by( | ||||
"id" | "id" | ||||
) | ) | ||||
Show All 16 Lines | ): | ||||
""" | """ | ||||
tmp_path = str(tmp_path) | tmp_path = str(tmp_path) | ||||
url = reverse(COL_IRI, args=[deposit_collection.name]) | url = reverse(COL_IRI, args=[deposit_collection.name]) | ||||
external_id = "some-external-id-1" | external_id = "some-external-id-1" | ||||
# when | # when | ||||
response = authenticated_client.post( | response = post_archive( | ||||
authenticated_client, | |||||
url, | url, | ||||
content_type="application/zip", # as zip | sample_archive, | ||||
data=sample_archive["data"], | |||||
# + headers | |||||
CONTENT_LENGTH=sample_archive["length"], | |||||
HTTP_SLUG=external_id, | HTTP_SLUG=external_id, | ||||
HTTP_CONTENT_MD5=sample_archive["md5sum"], | |||||
HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", | |||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", | |||||
) | ) | ||||
# then | # then | ||||
assert response.status_code == status.HTTP_201_CREATED | assert response.status_code == status.HTTP_201_CREATED | ||||
response_content = parse_xml(BytesIO(response.content)) | response_content = parse_xml(BytesIO(response.content)) | ||||
deposit_id = response_content["swh:deposit_id"] | deposit_id = response_content["swh:deposit_id"] | ||||
deposit = Deposit.objects.get(pk=deposit_id) | deposit = Deposit.objects.get(pk=deposit_id) | ||||
assert deposit.status == DEPOSIT_STATUS_DEPOSITED | assert deposit.status == DEPOSIT_STATUS_DEPOSITED | ||||
assert deposit.external_id == external_id | assert deposit.external_id == external_id | ||||
assert deposit.collection == deposit_collection | assert deposit.collection == deposit_collection | ||||
assert deposit.swhid is None | assert deposit.swhid is None | ||||
deposit_request = DepositRequest.objects.get(deposit=deposit) | deposit_request = DepositRequest.objects.get(deposit=deposit) | ||||
assert deposit_request.deposit == deposit | assert deposit_request.deposit == deposit | ||||
check_archive("filename0", deposit_request.archive.name) | check_archive(sample_archive["name"], deposit_request.archive.name) | ||||
# updating/adding is forbidden | # updating/adding is forbidden | ||||
# uri to update the content | # uri to update the content | ||||
edit_iri = reverse("edit_iri", args=[deposit_collection.name, deposit_id]) | edit_iri = reverse("edit_iri", args=[deposit_collection.name, deposit_id]) | ||||
se_iri = reverse("se_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]) | em_iri = reverse("em_iri", args=[deposit_collection.name, deposit_id]) | ||||
# Testing all update/add endpoint should fail | # Testing all update/add endpoint should fail | ||||
# since the status is ready | # since the status is ready | ||||
archive2 = create_arborescence_archive( | archive2 = create_arborescence_archive( | ||||
tmp_path, "archive2", "file2", b"some content in file 2" | tmp_path, "archive2", "file2", b"some content in file 2" | ||||
) | ) | ||||
# replacing file is no longer possible since the deposit's | # replacing file is no longer possible since the deposit's | ||||
# status is ready | # status is ready | ||||
r = authenticated_client.put( | r = put_archive( | ||||
authenticated_client, | |||||
em_iri, | em_iri, | ||||
content_type="application/zip", | archive2, | ||||
data=archive2["data"], | |||||
CONTENT_LENGTH=archive2["length"], | |||||
HTTP_SLUG=external_id, | HTTP_SLUG=external_id, | ||||
HTTP_CONTENT_MD5=archive2["md5sum"], | |||||
HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", | |||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", | |||||
) | ) | ||||
assert r.status_code == status.HTTP_400_BAD_REQUEST | assert r.status_code == status.HTTP_400_BAD_REQUEST | ||||
assert b"You can only act on deposit with status 'partial'" in r.content | assert b"You can only act on deposit with status 'partial'" in r.content | ||||
# adding file is no longer possible since the deposit's status | # adding file is no longer possible since the deposit's status | ||||
# is ready | # is ready | ||||
r = authenticated_client.post( | r = post_archive( | ||||
authenticated_client, | |||||
em_iri, | em_iri, | ||||
content_type="application/zip", | archive2, | ||||
data=archive2["data"], | |||||
CONTENT_LENGTH=archive2["length"], | |||||
HTTP_SLUG=external_id, | HTTP_SLUG=external_id, | ||||
HTTP_CONTENT_MD5=archive2["md5sum"], | |||||
HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", | |||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", | |||||
) | ) | ||||
assert r.status_code == status.HTTP_400_BAD_REQUEST | assert r.status_code == status.HTTP_400_BAD_REQUEST | ||||
assert b"You can only act on deposit with status 'partial'" in r.content | assert b"You can only act on deposit with status 'partial'" in r.content | ||||
# replacing metadata is no longer possible since the deposit's | # replacing metadata is no longer possible since the deposit's | ||||
# status is ready | # status is ready | ||||
r = authenticated_client.put( | r = authenticated_client.put( | ||||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |