Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_collection_post_multipart.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-2019 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 handling of multipart requests to POST Col-IRI.""" | """Tests handling of multipart requests to POST Col-IRI.""" | ||||
from io import BytesIO | from io import BytesIO | ||||
import uuid | |||||
from django.core.files.uploadedfile import InMemoryUploadedFile | from django.core.files.uploadedfile import InMemoryUploadedFile | ||||
from django.urls import reverse | from django.urls import reverse | ||||
import pytest | import pytest | ||||
from rest_framework import status | from rest_framework import status | ||||
from swh.deposit.config import COL_IRI, DEPOSIT_STATUS_DEPOSITED | from swh.deposit.config import COL_IRI, DEPOSIT_STATUS_DEPOSITED | ||||
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 | from swh.deposit.tests.common import check_archive | ||||
def test_post_deposit_multipart_without_slug_header_is_bad_request( | def test_post_deposit_multipart_without_slug_header( | ||||
authenticated_client, deposit_collection, atom_dataset | authenticated_client, deposit_collection, atom_dataset, mocker, deposit_user | ||||
): | ): | ||||
# given | # given | ||||
url = reverse(COL_IRI, args=[deposit_collection.name]) | url = reverse(COL_IRI, args=[deposit_collection.name]) | ||||
id_ = str(uuid.uuid4()) | |||||
mocker.patch("uuid.uuid4", return_value=id_) | |||||
archive_content = b"some content representing archive" | archive_content = b"some content representing archive" | ||||
archive = InMemoryUploadedFile( | archive = InMemoryUploadedFile( | ||||
BytesIO(archive_content), | BytesIO(archive_content), | ||||
field_name="archive0", | field_name="archive0", | ||||
name="archive0", | name="archive0", | ||||
content_type="application/zip", | content_type="application/zip", | ||||
size=len(archive_content), | size=len(archive_content), | ||||
charset=None, | charset=None, | ||||
Show All 13 Lines | ): | ||||
response = authenticated_client.post( | response = authenticated_client.post( | ||||
url, | url, | ||||
format="multipart", | format="multipart", | ||||
data={"archive": archive, "atom_entry": atom_entry,}, | data={"archive": archive, "atom_entry": atom_entry,}, | ||||
# + headers | # + headers | ||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
) | ) | ||||
assert b"Missing SLUG header" in response.content | assert response.status_code == status.HTTP_201_CREATED | ||||
assert response.status_code == status.HTTP_400_BAD_REQUEST | response_content = parse_xml(BytesIO(response.content)) | ||||
deposit_id = response_content["swh:deposit_id"] | |||||
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 | |||||
def test_post_deposit_multipart_zip( | def test_post_deposit_multipart_zip( | ||||
authenticated_client, deposit_collection, atom_dataset, sample_archive | authenticated_client, deposit_collection, atom_dataset, sample_archive | ||||
): | ): | ||||
"""one multipart deposit (zip+xml) should be accepted | """one multipart deposit (zip+xml) should be accepted | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 385 Lines • Show Last 20 Lines |