Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_collection_post_binary.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 the handling of the binary content when doing a POST Col-IRI.""" | """Tests the handling of the binary content when doing a 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, EM_IRI | from swh.deposit.config import COL_IRI, DEPOSIT_STATUS_DEPOSITED, EM_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 | ||||
def test_post_deposit_binary_no_slug( | def test_post_deposit_binary_no_slug( | ||||
authenticated_client, deposit_collection, sample_archive | authenticated_client, deposit_collection, sample_archive, deposit_user, mocker | ||||
): | ): | ||||
"""Posting a binary deposit without slug header should return 400 | """Posting a binary deposit without slug header should generate one | ||||
""" | """ | ||||
id_ = str(uuid.uuid4()) | |||||
mocker.patch("uuid.uuid4", return_value=id_) | |||||
url = reverse(COL_IRI, args=[deposit_collection.name]) | url = reverse(COL_IRI, args=[deposit_collection.name]) | ||||
# when | # when | ||||
response = authenticated_client.post( | response = authenticated_client.post( | ||||
url, | url, | ||||
content_type="application/zip", # as zip | content_type="application/zip", # as zip | ||||
data=sample_archive["data"], | data=sample_archive["data"], | ||||
# + headers | # + headers | ||||
CONTENT_LENGTH=sample_archive["length"], | CONTENT_LENGTH=sample_archive["length"], | ||||
HTTP_CONTENT_MD5=sample_archive["md5sum"], | HTTP_CONTENT_MD5=sample_archive["md5sum"], | ||||
HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", | HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", | ||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", | HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", | ||||
) | ) | ||||
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_binary_support( | def test_post_deposit_binary_support( | ||||
authenticated_client, deposit_collection, sample_archive | authenticated_client, deposit_collection, sample_archive | ||||
): | ): | ||||
"""Binary upload with content-type not in [zip,x-tar] should return 415 | """Binary upload with content-type not in [zip,x-tar] should return 415 | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 570 Lines • Show Last 20 Lines |