Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_collection_post_atom.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 Atom content when doing a POST Col-IRI.""" | """Tests the handling of the Atom content when doing a POST Col-IRI.""" | ||||
from io import BytesIO | from io import BytesIO | ||||
import uuid | |||||
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, DepositCollection, DepositRequest | from swh.deposit.models import Deposit, DepositCollection, DepositRequest | ||||
from swh.deposit.parsers import parse_xml | from swh.deposit.parsers import parse_xml | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | response = authenticated_client.post( | ||||
data=atom_dataset["entry-data-parsing-error-prone"], | data=atom_dataset["entry-data-parsing-error-prone"], | ||||
HTTP_SLUG="external-id", | HTTP_SLUG="external-id", | ||||
) | ) | ||||
assert response.status_code == status.HTTP_400_BAD_REQUEST | assert response.status_code == status.HTTP_400_BAD_REQUEST | ||||
assert b"Malformed xml metadata" in response.content | assert b"Malformed xml metadata" in response.content | ||||
def test_post_deposit_atom_no_slug_header( | def test_post_deposit_atom_no_slug_header( | ||||
authenticated_client, deposit_collection, atom_dataset | authenticated_client, deposit_collection, deposit_user, atom_dataset, mocker | ||||
): | ): | ||||
"""Posting an atom entry without a slug header should return a 400 | """Posting an atom entry without a slug header should generate one | ||||
""" | """ | ||||
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_) | |||||
# when | # when | ||||
response = authenticated_client.post( | response = authenticated_client.post( | ||||
url, | url, | ||||
content_type="application/atom+xml;type=entry", | content_type="application/atom+xml;type=entry", | ||||
data=atom_dataset["entry-data0"], | data=atom_dataset["entry-data0"], | ||||
# + 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_atom_with_external_identifier( | def test_post_deposit_atom_with_external_identifier( | ||||
authenticated_client, deposit_collection, atom_dataset | authenticated_client, deposit_collection, atom_dataset | ||||
): | ): | ||||
"""Posting an atom entry without a slug header should return a 400 | """Posting an atom entry without a slug header should return a 400 | ||||
""" | """ | ||||
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/atom+xml;type=entry", | content_type="application/atom+xml;type=entry", | ||||
data=atom_dataset["error-with-external-identifier"], | data=atom_dataset["error-with-external-identifier"], | ||||
# + headers | # + headers | ||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
HTTP_SLUG="something", | HTTP_SLUG="something", | ||||
) | ) | ||||
print(response.content) | |||||
assert b"The 'external_identifier' tag is deprecated" in response.content | assert b"The 'external_identifier' tag is deprecated" in response.content | ||||
assert response.status_code == status.HTTP_400_BAD_REQUEST | assert response.status_code == status.HTTP_400_BAD_REQUEST | ||||
def test_post_deposit_atom_unknown_collection(authenticated_client, atom_dataset): | def test_post_deposit_atom_unknown_collection(authenticated_client, atom_dataset): | ||||
"""Posting an atom entry to an unknown collection should return a 404 | """Posting an atom entry to an unknown collection should return a 404 | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 177 Lines • Show Last 20 Lines |