Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_collection_post_atom.py
Show First 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | response = authenticated_client.post( | ||||
content_type="application/atom+xml;type=entry", | content_type="application/atom+xml;type=entry", | ||||
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_403_wrong_origin_url_prefix( | def test_post_deposit_atom_400_both_create_origin_and_add_to_origin( | ||||
authenticated_client, deposit_collection, atom_dataset | |||||
): | |||||
"""Posting a badly formatted atom should return a 400 response | |||||
""" | |||||
response = authenticated_client.post( | |||||
reverse(COL_IRI, args=[deposit_collection.name]), | |||||
content_type="application/atom+xml;type=entry", | |||||
data=atom_dataset["entry-data-with-both-create-origin-and-add-to-origin"], | |||||
) | |||||
assert response.status_code == status.HTTP_400_BAD_REQUEST | |||||
assert ( | |||||
b"<swh:create_origin> and <swh:add_to_origin> " | |||||
b"are mutually exclusive" | |||||
) in response.content | |||||
def test_add_deposit_with_add_to_origin_and_external_identifier( | |||||
authenticated_client, | |||||
deposit_collection, | |||||
completed_deposit, | |||||
atom_dataset, | |||||
deposit_user, | |||||
): | |||||
"""Posting deposit with <swh:add_to_origin> creates a new deposit with parent | |||||
""" | |||||
# given multiple deposit already loaded | |||||
origin_url = deposit_user.provider_url + completed_deposit.external_id | |||||
# adding a new deposit with the same external id as a completed deposit | |||||
# creates the parenting chain | |||||
response = authenticated_client.post( | |||||
reverse(COL_IRI, args=[deposit_collection.name]), | |||||
content_type="application/atom+xml;type=entry", | |||||
data=atom_dataset["entry-data-with-both-add-to-origin-and-external-id"] | |||||
% origin_url, | |||||
) | |||||
assert response.status_code == status.HTTP_400_BAD_REQUEST | |||||
assert b"<external_identifier> is deprecated." in response.content | |||||
def test_post_deposit_atom_403_create_wrong_origin_url_prefix( | |||||
authenticated_client, deposit_collection, atom_dataset, deposit_user | authenticated_client, deposit_collection, atom_dataset, deposit_user | ||||
): | ): | ||||
"""Creating an origin for a prefix not owned by the client is forbidden | """Creating an origin for a prefix not owned by the client is forbidden | ||||
""" | """ | ||||
origin_url = "http://example.org/foo" | origin_url = "http://example.org/foo" | ||||
response = authenticated_client.post( | response = authenticated_client.post( | ||||
reverse(COL_IRI, args=[deposit_collection.name]), | reverse(COL_IRI, args=[deposit_collection.name]), | ||||
content_type="application/atom+xml;type=entry", | content_type="application/atom+xml;type=entry", | ||||
data=atom_dataset["entry-data0"] % origin_url, | data=atom_dataset["entry-data0"] % origin_url, | ||||
HTTP_IN_PROGRESS="true", | HTTP_IN_PROGRESS="true", | ||||
) | ) | ||||
assert response.status_code == status.HTTP_403_FORBIDDEN | assert response.status_code == status.HTTP_403_FORBIDDEN | ||||
expected_msg = ( | expected_msg = ( | ||||
f"Cannot create origin {origin_url}, " | f"Cannot create origin {origin_url}, " | ||||
f"it must start with {deposit_user.provider_url}" | f"it must start with {deposit_user.provider_url}" | ||||
) | ) | ||||
assert expected_msg in response.content.decode() | assert expected_msg in response.content.decode() | ||||
def test_post_deposit_atom_403_add_to_wrong_origin_url_prefix( | |||||
authenticated_client, deposit_collection, atom_dataset, deposit_user | |||||
): | |||||
"""Creating an origin for a prefix not owned by the client is forbidden | |||||
""" | |||||
origin_url = "http://example.org/foo" | |||||
response = authenticated_client.post( | |||||
reverse(COL_IRI, args=[deposit_collection.name]), | |||||
content_type="application/atom+xml;type=entry", | |||||
data=atom_dataset["entry-data-with-add-to-origin"] % origin_url, | |||||
HTTP_IN_PROGRESS="true", | |||||
) | |||||
assert response.status_code == status.HTTP_403_FORBIDDEN | |||||
expected_msg = ( | |||||
f"Cannot create origin {origin_url}, " | |||||
f"it must start with {deposit_user.provider_url}" | |||||
) | |||||
assert expected_msg in response.content.decode() | |||||
def test_post_deposit_atom_use_slug_header( | def test_post_deposit_atom_use_slug_header( | ||||
authenticated_client, deposit_collection, deposit_user, atom_dataset, mocker | authenticated_client, deposit_collection, deposit_user, atom_dataset, mocker | ||||
): | ): | ||||
"""Posting an atom entry with a slug header but no origin url generates | """Posting an atom entry with a slug header but no origin url generates | ||||
an origin url from the slug | an origin url from the slug | ||||
""" | """ | ||||
url = reverse(COL_IRI, args=[deposit_collection.name]) | url = reverse(COL_IRI, args=[deposit_collection.name]) | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | ): | ||||
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.collection == deposit_collection | assert deposit.collection == deposit_collection | ||||
assert deposit.origin_url == deposit_user.provider_url + slug | assert deposit.origin_url == deposit_user.provider_url + slug | ||||
assert deposit.status == DEPOSIT_STATUS_DEPOSITED | assert deposit.status == DEPOSIT_STATUS_DEPOSITED | ||||
def test_post_deposit_atom_with_external_identifier( | def test_post_deposit_atom_with_mismatched_slug_and_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 with mismatched slug header and external_identifier | ||||
should return a 400 | |||||
""" | """ | ||||
external_id = "foobar" | external_id = "foobar" | ||||
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, | ||||
▲ Show 20 Lines • Show All 244 Lines • Show Last 20 Lines |