Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_deposit_update.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 SE-IRI.""" | """Tests updates on SE-IRI.""" | ||||
from io import BytesIO | |||||
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 ( | from swh.deposit.config import ( | ||||
DEPOSIT_STATUS_DEPOSITED, | DEPOSIT_STATUS_DEPOSITED, | ||||
DEPOSIT_STATUS_PARTIAL, | DEPOSIT_STATUS_PARTIAL, | ||||
EDIT_IRI, | EDIT_IRI, | ||||
SE_IRI, | SE_IRI, | ||||
) | ) | ||||
from swh.deposit.models import Deposit, DepositRequest | from swh.deposit.models import Deposit, DepositRequest | ||||
from swh.deposit.tests.common import post_atom | from swh.deposit.tests.common import post_atom, post_multipart, put_multipart | ||||
def test_add_both_archive_and_metadata_to_deposit( | def test_add_both_archive_and_metadata_to_deposit( | ||||
authenticated_client, | authenticated_client, | ||||
deposit_collection, | deposit_collection, | ||||
partial_deposit_with_metadata, | partial_deposit_with_metadata, | ||||
atom_dataset, | atom_dataset, | ||||
sample_archive, | sample_archive, | ||||
deposit_user, | deposit_user, | ||||
): | ): | ||||
"""Scenario: Add both a new archive and new metadata to a partial deposit is ok | """Scenario: Add both a new archive and new metadata to a partial deposit is ok | ||||
Response: 201 | Response: 201 | ||||
""" | """ | ||||
deposit = partial_deposit_with_metadata | deposit = partial_deposit_with_metadata | ||||
origin_url = deposit_user.provider_url + deposit.external_id | origin_url = deposit_user.provider_url + deposit.external_id | ||||
requests = DepositRequest.objects.filter(deposit=deposit, type="metadata") | requests = DepositRequest.objects.filter(deposit=deposit, type="metadata") | ||||
assert len(requests) == 1 | assert len(requests) == 1 | ||||
requests_archive0 = DepositRequest.objects.filter(deposit=deposit, type="archive") | requests_archive0 = DepositRequest.objects.filter(deposit=deposit, type="archive") | ||||
assert len(requests_archive0) == 1 | assert len(requests_archive0) == 1 | ||||
update_uri = reverse(EDIT_IRI, args=[deposit_collection.name, deposit.id]) | |||||
archive = InMemoryUploadedFile( | |||||
BytesIO(sample_archive["data"]), | |||||
field_name=sample_archive["name"], | |||||
name=sample_archive["name"], | |||||
content_type="application/x-tar", | |||||
size=sample_archive["length"], | |||||
charset=None, | |||||
) | |||||
data_atom_entry = atom_dataset["entry-data1"] | data_atom_entry = atom_dataset["entry-data1"] | ||||
atom_entry = InMemoryUploadedFile( | response = post_multipart( | ||||
BytesIO(data_atom_entry.encode("utf-8")), | authenticated_client, | ||||
field_name="atom0", | reverse(SE_IRI, args=[deposit_collection.name, deposit.id]), | ||||
name="atom0", | sample_archive, | ||||
content_type='application/atom+xml; charset="utf-8"', | data_atom_entry, | ||||
size=len(data_atom_entry), | |||||
charset="utf-8", | |||||
) | |||||
update_uri = reverse(SE_IRI, args=[deposit_collection.name, deposit.id]) | |||||
response = authenticated_client.post( | |||||
update_uri, | |||||
format="multipart", | |||||
data={"archive": archive, "atom_entry": atom_entry,}, | |||||
) | ) | ||||
assert response.status_code == status.HTTP_201_CREATED | assert response.status_code == status.HTTP_201_CREATED | ||||
requests = DepositRequest.objects.filter(deposit=deposit, type="metadata").order_by( | requests = DepositRequest.objects.filter(deposit=deposit, type="metadata").order_by( | ||||
"id" | "id" | ||||
) | ) | ||||
assert len(requests) == 1 + 1, "New deposit request archive got added" | assert len(requests) == 1 + 1, "New deposit request archive got added" | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | ): | ||||
requests_meta = DepositRequest.objects.filter(deposit=deposit, type="metadata") | requests_meta = DepositRequest.objects.filter(deposit=deposit, type="metadata") | ||||
assert len(requests_meta) == 1 | assert len(requests_meta) == 1 | ||||
request_meta0 = requests_meta[0] | request_meta0 = requests_meta[0] | ||||
assert request_meta0.raw_metadata == raw_metadata0 | assert request_meta0.raw_metadata == raw_metadata0 | ||||
requests_archive0 = DepositRequest.objects.filter(deposit=deposit, type="archive") | requests_archive0 = DepositRequest.objects.filter(deposit=deposit, type="archive") | ||||
assert len(requests_archive0) == 1 | assert len(requests_archive0) == 1 | ||||
archive = InMemoryUploadedFile( | |||||
BytesIO(sample_archive["data"]), | |||||
field_name=sample_archive["name"], | |||||
name=sample_archive["name"], | |||||
content_type="application/x-tar", | |||||
size=sample_archive["length"], | |||||
charset=None, | |||||
) | |||||
data_atom_entry = atom_dataset["entry-data1"] | data_atom_entry = atom_dataset["entry-data1"] | ||||
atom_entry = InMemoryUploadedFile( | response = put_multipart( | ||||
BytesIO(data_atom_entry.encode("utf-8")), | authenticated_client, | ||||
field_name="atom0", | reverse(EDIT_IRI, args=[deposit_collection.name, deposit.id]), | ||||
name="atom0", | sample_archive, | ||||
content_type='application/atom+xml; charset="utf-8"', | data_atom_entry, | ||||
size=len(data_atom_entry), | |||||
charset="utf-8", | |||||
) | |||||
update_uri = reverse(EDIT_IRI, args=[deposit_collection.name, deposit.id]) | |||||
response = authenticated_client.put( | |||||
update_uri, | |||||
format="multipart", | |||||
data={"archive": archive, "atom_entry": atom_entry,}, | |||||
) | ) | ||||
assert response.status_code == status.HTTP_204_NO_CONTENT | assert response.status_code == status.HTTP_204_NO_CONTENT | ||||
# check we updated the metadata part | # check we updated the metadata part | ||||
requests_meta = DepositRequest.objects.filter(deposit=deposit, type="metadata") | requests_meta = DepositRequest.objects.filter(deposit=deposit, type="metadata") | ||||
assert len(requests_meta) == 1 | assert len(requests_meta) == 1 | ||||
request_meta1 = requests_meta[0] | request_meta1 = requests_meta[0] | ||||
Show All 9 Lines |