Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_collection_post_atom.py
Show First 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | response = authenticated_client.post( | ||||
# + headers | # + headers | ||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
) | ) | ||||
assert b"Missing SLUG header" in response.content | assert b"Missing SLUG header" 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_with_external_identifier( | |||||
authenticated_client, deposit_collection, atom_dataset | |||||
): | |||||
"""Posting an atom entry without a slug header should return a 400 | |||||
""" | |||||
url = reverse(COL_IRI, args=[deposit_collection.name]) | |||||
# when | |||||
response = authenticated_client.post( | |||||
url, | |||||
content_type="application/atom+xml;type=entry", | |||||
data=atom_dataset["error-with-external-identifier"], | |||||
# + headers | |||||
HTTP_IN_PROGRESS="false", | |||||
HTTP_SLUG="something", | |||||
) | |||||
print(response.content) | |||||
assert b"The 'external_identifier' tag is deprecated" in response.content | |||||
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 | ||||
""" | """ | ||||
unknown_collection = "unknown-one" | unknown_collection = "unknown-one" | ||||
with pytest.raises(DepositCollection.DoesNotExist): | with pytest.raises(DepositCollection.DoesNotExist): | ||||
DepositCollection.objects.get(name=unknown_collection) | DepositCollection.objects.get(name=unknown_collection) | ||||
Show All 14 Lines | ): | ||||
""" | """ | ||||
# given | # given | ||||
external_id = "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a" | external_id = "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a" | ||||
with pytest.raises(Deposit.DoesNotExist): | with pytest.raises(Deposit.DoesNotExist): | ||||
Deposit.objects.get(external_id=external_id) | Deposit.objects.get(external_id=external_id) | ||||
atom_entry_data = atom_dataset["entry-data0"] % external_id | atom_entry_data = atom_dataset["entry-data0"] | ||||
# when | # when | ||||
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_entry_data, | data=atom_entry_data, | ||||
HTTP_SLUG=external_id, | HTTP_SLUG=external_id, | ||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
Show All 24 Lines | ): | ||||
""" | """ | ||||
# given | # given | ||||
external_id = "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a" | external_id = "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a" | ||||
with pytest.raises(Deposit.DoesNotExist): | with pytest.raises(Deposit.DoesNotExist): | ||||
Deposit.objects.get(external_id=external_id) | Deposit.objects.get(external_id=external_id) | ||||
atom_entry_data = atom_dataset["codemeta-sample"] % external_id | atom_entry_data = atom_dataset["codemeta-sample"] | ||||
# when | # when | ||||
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_entry_data, | data=atom_entry_data, | ||||
HTTP_SLUG=external_id, | HTTP_SLUG=external_id, | ||||
HTTP_IN_PROGRESS="false", | HTTP_IN_PROGRESS="false", | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | ): | ||||
assert deposit.collection == deposit_collection | assert deposit.collection == deposit_collection | ||||
assert deposit.external_id == external_id | assert deposit.external_id == external_id | ||||
assert deposit.status == "partial" | assert deposit.status == "partial" | ||||
# one associated request to a deposit | # one associated request to a deposit | ||||
deposit_requests = DepositRequest.objects.filter(deposit=deposit) | deposit_requests = DepositRequest.objects.filter(deposit=deposit) | ||||
assert len(deposit_requests) == 1 | assert len(deposit_requests) == 1 | ||||
atom_entry_data = atom_dataset["entry-data-minimal"] % external_id.encode( | atom_entry_data = atom_dataset["entry-data-minimal"] | ||||
"utf-8" | |||||
) # noqa | |||||
for link in response_content["atom:link"]: | for link in response_content["atom:link"]: | ||||
if link["@rel"] == "http://purl.org/net/sword/terms/add": | if link["@rel"] == "http://purl.org/net/sword/terms/add": | ||||
se_iri = link["@href"] | se_iri = link["@href"] | ||||
break | break | ||||
else: | else: | ||||
assert False, f"missing SE-IRI from {response_content['link']}" | assert False, f"missing SE-IRI from {response_content['link']}" | ||||
# when updating the first deposit post | # when updating the first deposit post | ||||
response = authenticated_client.post( | response = authenticated_client.post( | ||||
se_iri, | se_iri, | ||||
content_type="application/atom+xml;type=entry", | content_type="application/atom+xml;type=entry", | ||||
data=atom_entry_data, | data=atom_entry_data, | ||||
HTTP_IN_PROGRESS="False", | HTTP_IN_PROGRESS="False", | ||||
) | ) | ||||
# then | # then | ||||
assert response.status_code == status.HTTP_201_CREATED | assert response.status_code == status.HTTP_201_CREATED, response.content | ||||
response_content = parse_xml(BytesIO(response.content)) | response_content = parse_xml(BytesIO(response.content)) | ||||
deposit_id = int(response_content["swh:deposit_id"]) | deposit_id = int(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.external_id == external_id | assert deposit.external_id == external_id | ||||
assert deposit.status == DEPOSIT_STATUS_DEPOSITED | assert deposit.status == DEPOSIT_STATUS_DEPOSITED | ||||
Show All 18 Lines |