diff --git a/swh/deposit/tests/api/test_collection_add_to_origin.py b/swh/deposit/tests/api/test_collection_add_to_origin.py --- a/swh/deposit/tests/api/test_collection_add_to_origin.py +++ b/swh/deposit/tests/api/test_collection_add_to_origin.py @@ -11,6 +11,7 @@ from swh.deposit.config import COL_IRI, DEPOSIT_STATUS_LOAD_SUCCESS from swh.deposit.models import Deposit from swh.deposit.parsers import parse_xml +from swh.deposit.tests.common import post_atom from ..conftest import create_deposit @@ -32,9 +33,9 @@ # adding a new deposit with the same external id as a completed deposit # creates the parenting chain - response = authenticated_client.post( + response = post_atom( + authenticated_client, 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, ) @@ -79,9 +80,9 @@ ) # adding a new deposit with the same external id as a completed deposit - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, ) @@ -99,9 +100,9 @@ origin_url = "http://example.org/foo" # adding a new deposit with the same external id as a completed deposit - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, ) @@ -124,9 +125,9 @@ # adding a new deposit with the same external id as a completed deposit # creates the parenting chain - response = authenticated_client.post( + response = post_atom( + authenticated_client, 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, ) @@ -143,9 +144,9 @@ """ origin_url = "http://example.org/foo" - response = authenticated_client.post( + response = post_atom( + authenticated_client, 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", ) diff --git a/swh/deposit/tests/api/test_collection_post_atom.py b/swh/deposit/tests/api/test_collection_post_atom.py --- a/swh/deposit/tests/api/test_collection_post_atom.py +++ b/swh/deposit/tests/api/test_collection_post_atom.py @@ -21,6 +21,7 @@ ) from swh.deposit.models import Deposit, DepositCollection, DepositRequest from swh.deposit.parsers import parse_xml +from swh.deposit.tests.common import post_atom from swh.deposit.utils import compute_metadata_context from swh.model.identifiers import SWHID, parse_swhid from swh.model.model import ( @@ -41,9 +42,9 @@ """ atom_error_with_decimal = atom_dataset["error-with-decimal"] - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_error_with_decimal, HTTP_SLUG="external-id", HTTP_IN_PROGRESS="false", @@ -70,9 +71,9 @@ """ atom_content = atom_dataset["entry-data-empty-body"] - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_content, HTTP_SLUG="external-id", ) @@ -86,9 +87,9 @@ """Posting a badly formatted atom should return a 400 response """ - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data-badly-formatted"], HTTP_SLUG="external-id", ) @@ -102,9 +103,9 @@ """Posting parsing error prone atom should return 400 """ - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data-parsing-error-prone"], HTTP_SLUG="external-id", ) @@ -118,9 +119,9 @@ """Posting a badly formatted atom should return a 400 response """ - response = authenticated_client.post( + response = post_atom( + authenticated_client, 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 @@ -138,9 +139,9 @@ """ origin_url = "http://example.org/foo" - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, HTTP_IN_PROGRESS="true", ) @@ -164,9 +165,9 @@ slug = str(uuid.uuid4()) # when - response = authenticated_client.post( + response = post_atom( + authenticated_client, url, - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data-no-origin-url"], HTTP_IN_PROGRESS="false", HTTP_SLUG=slug, @@ -194,11 +195,10 @@ mocker.patch("uuid.uuid4", return_value=slug) # when - response = authenticated_client.post( + response = post_atom( + authenticated_client, url, - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data-no-origin-url"], - # + headers HTTP_IN_PROGRESS="false", ) @@ -223,11 +223,10 @@ url = reverse(COL_IRI, args=[deposit_collection.name]) # when - response = authenticated_client.post( + response = post_atom( + authenticated_client, url, - content_type="application/atom+xml;type=entry", data=atom_dataset["error-with-external-identifier"] % external_id, - # + headers HTTP_IN_PROGRESS="false", HTTP_SLUG="something", ) @@ -252,12 +251,8 @@ ) # when - response = authenticated_client.post( - url, - content_type="application/atom+xml;type=entry", - data=document, - # + headers - HTTP_IN_PROGRESS="false", + response = post_atom( + authenticated_client, url, data=document, HTTP_IN_PROGRESS="false", ) assert b"<external_identifier> is deprecated" in response.content @@ -279,12 +274,8 @@ ) # when - response = authenticated_client.post( - url, - content_type="application/atom+xml;type=entry", - data=document, - # + headers - HTTP_IN_PROGRESS="false", + response = post_atom( + authenticated_client, url, data=document, HTTP_IN_PROGRESS="false", ) assert b"only one may be used on a given deposit" in response.content @@ -299,9 +290,9 @@ with pytest.raises(DepositCollection.DoesNotExist): DepositCollection.objects.get(name=unknown_collection) - response = authenticated_client.post( - reverse(COL_IRI, args=[unknown_collection]), # <- unknown collection - content_type="application/atom+xml;type=entry", + response = post_atom( + authenticated_client, + reverse(COL_IRI, args=[unknown_collection]), data=atom_dataset["entry-data0"], HTTP_SLUG="something", ) @@ -324,9 +315,9 @@ atom_entry_data = atom_dataset["entry-data0"] % origin_url # when - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_entry_data, HTTP_IN_PROGRESS="false", ) @@ -363,9 +354,9 @@ atom_entry_data = atom_dataset["codemeta-sample"] % origin_url # when - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_entry_data, HTTP_IN_PROGRESS="false", ) @@ -398,9 +389,9 @@ invalid_swhid = "swh:1:dir :31b5c8cc985d190b5a7ef4878128ebfdc2358f49" xml_data = atom_dataset["entry-data-with-swhid"].format(swhid=invalid_swhid) - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=xml_data, ) assert response.status_code == status.HTTP_400_BAD_REQUEST @@ -418,9 +409,9 @@ "entry-data-with-swhid-fail-metadata-functional-checks" ].format(swhid=swhid) - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=invalid_xml_data, ) assert response.status_code == status.HTTP_400_BAD_REQUEST @@ -489,9 +480,9 @@ xml_data = atom_dataset["entry-data-with-swhid"].format(swhid=swhid) deposit_client = authenticated_client.deposit_client - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=xml_data, ) @@ -570,9 +561,9 @@ """ xml_data = atom_dataset["entry-data-with-origin-reference"].format(url=url) deposit_client = authenticated_client.deposit_client - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=xml_data, ) diff --git a/swh/deposit/tests/api/test_collection_reuse_slug.py b/swh/deposit/tests/api/test_collection_reuse_slug.py --- a/swh/deposit/tests/api/test_collection_reuse_slug.py +++ b/swh/deposit/tests/api/test_collection_reuse_slug.py @@ -17,6 +17,7 @@ ) from swh.deposit.models import Deposit from swh.deposit.parsers import parse_xml +from swh.deposit.tests.common import post_atom from ..conftest import create_deposit @@ -26,9 +27,9 @@ ): deposit = rejected_deposit - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(SE_IRI, args=[deposit.collection.name, deposit.id]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data1"], HTTP_SLUG=deposit.external_id, ) @@ -55,9 +56,9 @@ origin_url = deposit_user.provider_url + deposit.external_id # adding a new deposit with the same external id - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, HTTP_SLUG=deposit.external_id, ) @@ -86,9 +87,9 @@ # adding a new deposit with the same external id as a completed deposit # creates the parenting chain - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, HTTP_SLUG=deposit.external_id, ) @@ -122,9 +123,9 @@ # adding a new deposit with the same external id as a completed deposit # creates the parenting chain - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, HTTP_SLUG=deposit.external_id, ) @@ -169,9 +170,9 @@ ) # adding a new deposit with the same external id as a completed deposit - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, HTTP_SLUG=external_id, ) @@ -219,9 +220,9 @@ ) # adding a new deposit with the same external id as a completed deposit - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, HTTP_SLUG=deposit.external_id, ) diff --git a/swh/deposit/tests/api/test_deposit_update.py b/swh/deposit/tests/api/test_deposit_update.py --- a/swh/deposit/tests/api/test_deposit_update.py +++ b/swh/deposit/tests/api/test_deposit_update.py @@ -18,6 +18,7 @@ SE_IRI, ) from swh.deposit.models import Deposit, DepositRequest +from swh.deposit.tests.common import post_atom def test_add_both_archive_and_metadata_to_deposit( @@ -100,12 +101,8 @@ assert deposit.status == DEPOSIT_STATUS_PARTIAL update_uri = reverse(SE_IRI, args=[deposit_collection.name, deposit.id]) - response = authenticated_client.post( - update_uri, - content_type="application/atom+xml;type=entry", - data="", - size=0, - HTTP_IN_PROGRESS=False, + response = post_atom( + authenticated_client, update_uri, data="", size=0, HTTP_IN_PROGRESS=False, ) assert response.status_code == status.HTTP_200_OK diff --git a/swh/deposit/tests/api/test_deposit_update_atom.py b/swh/deposit/tests/api/test_deposit_update_atom.py --- a/swh/deposit/tests/api/test_deposit_update_atom.py +++ b/swh/deposit/tests/api/test_deposit_update_atom.py @@ -21,6 +21,7 @@ ) from swh.deposit.models import Deposit, DepositCollection, DepositRequest from swh.deposit.parsers import parse_xml +from swh.deposit.tests.common import post_atom, put_atom from swh.model.hashutil import hash_to_bytes from swh.model.identifiers import parse_swhid, swhid from swh.model.model import ( @@ -46,9 +47,9 @@ deposit = Deposit.objects.get(origin_url=origin_url) # when - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data1"], HTTP_IN_PROGRESS="True", ) @@ -78,11 +79,8 @@ assert False, f"missing SE-IRI from {response_content['link']}" # when updating the first deposit post - response = authenticated_client.post( - se_iri, - content_type="application/atom+xml;type=entry", - data=atom_entry_data, - HTTP_IN_PROGRESS="False", + response = post_atom( + authenticated_client, se_iri, data=atom_entry_data, HTTP_IN_PROGRESS="False", ) # then @@ -141,10 +139,8 @@ update_uri = reverse(EDIT_IRI, args=[deposit_collection.name, deposit.id]) - response = authenticated_client.put( - update_uri, - content_type="application/atom+xml;type=entry", - data=atom_dataset["entry-data1"], + response = put_atom( + authenticated_client, update_uri, data=atom_dataset["entry-data1"], ) assert response.status_code == status.HTTP_204_NO_CONTENT @@ -186,9 +182,7 @@ update_uri = reverse(SE_IRI, args=[deposit_collection.name, deposit.id]) atom_entry = atom_dataset["entry-data1"] - response = authenticated_client.post( - update_uri, content_type="application/atom+xml;type=entry", data=atom_entry - ) + response = post_atom(authenticated_client, update_uri, data=atom_entry) assert response.status_code == status.HTTP_201_CREATED @@ -221,11 +215,7 @@ assert True url = reverse(SE_IRI, args=[deposit_collection, unknown_deposit_id]) - response = authenticated_client.post( - url, - content_type="application/atom+xml;type=entry", - data=atom_dataset["entry-data1"], - ) + response = post_atom(authenticated_client, url, data=atom_dataset["entry-data1"],) assert response.status_code == status.HTTP_404_NOT_FOUND response_content = parse_xml(response.content) assert ( @@ -247,11 +237,7 @@ assert True url = reverse(SE_IRI, args=[unknown_collection_name, deposit.id]) - response = authenticated_client.post( - url, - content_type="application/atom+xml;type=entry", - data=atom_dataset["entry-data1"], - ) + response = post_atom(authenticated_client, url, data=atom_dataset["entry-data1"],) assert response.status_code == status.HTTP_404_NOT_FOUND response_content = parse_xml(response.content) assert "Unknown collection name" in response_content["sword:error"]["atom:summary"] @@ -269,11 +255,7 @@ except Deposit.DoesNotExist: assert True url = reverse(EDIT_IRI, args=[deposit_collection.name, unknown_deposit_id]) - response = authenticated_client.put( - url, - content_type="application/atom+xml;type=entry", - data=atom_dataset["entry-data1"], - ) + response = put_atom(authenticated_client, url, data=atom_dataset["entry-data1"],) assert response.status_code == status.HTTP_404_NOT_FOUND response_content = parse_xml(response.content) assert ( @@ -311,10 +293,8 @@ deposit = partial_deposit # when update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit.id]) - response = authenticated_client.put( - update_uri, - content_type="application/atom+xml;type=entry", - data=atom_dataset["entry-data1"], + response = put_atom( + authenticated_client, update_uri, data=atom_dataset["entry-data1"], ) # then assert response.status_code == status.HTTP_400_BAD_REQUEST @@ -366,9 +346,9 @@ nb_metadata = len(actual_existing_requests_metadata) update_uri = reverse(EDIT_IRI, args=[deposit_collection.name, complete_deposit.id]) - response = authenticated_client.put( + response = put_atom( + authenticated_client, update_uri, - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data1"], HTTP_X_CHECK_SWHID=complete_deposit.swhid, ) @@ -452,9 +432,9 @@ assert complete_deposit.swhid != incorrect_swhid update_uri = reverse(EDIT_IRI, args=[deposit_collection.name, complete_deposit.id]) - response = authenticated_client.put( + response = put_atom( + authenticated_client, update_uri, - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data1"], HTTP_X_CHECK_SWHID=incorrect_swhid, ) @@ -477,9 +457,9 @@ """ update_uri = reverse(EDIT_IRI, args=[deposit_collection.name, complete_deposit.id]) - response = authenticated_client.put( + response = put_atom( + authenticated_client, update_uri, - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data-ko"], HTTP_X_CHECK_SWHID=complete_deposit.swhid, ) @@ -504,9 +484,9 @@ update_uri = reverse(EDIT_IRI, args=[deposit_collection.name, complete_deposit.id]) atom_content = atom_dataset["entry-data-empty-body"] - response = authenticated_client.put( + response = put_atom( + authenticated_client, update_uri, - content_type="application/atom+xml;type=entry", data=atom_content, HTTP_X_CHECK_SWHID=complete_deposit.swhid, ) @@ -530,9 +510,9 @@ """ update_uri = reverse(EDIT_IRI, args=[deposit_collection.name, complete_deposit.id]) - response = authenticated_client.put( + response = put_atom( + authenticated_client, update_uri, - content_type="application/atom+xml;type=entry", # no title, nor author, nor name fields data=atom_dataset["entry-data-fail-metadata-functional-checks"], HTTP_X_CHECK_SWHID=complete_deposit.swhid, @@ -560,9 +540,9 @@ origin_url = deposit_user.provider_url + external_id url = reverse(COL_IRI, args=[deposit_collection.name]) - response = authenticated_client.post( + response = post_atom( + authenticated_client, url, - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, HTTP_IN_PROGRESS="true", ) @@ -578,11 +558,10 @@ assert False, response_content # when - response = authenticated_client.put( + response = put_atom( + authenticated_client, edit_iri, - content_type="application/atom+xml;type=entry", data=atom_dataset["error-with-external-identifier"] % external_id, - # + headers HTTP_IN_PROGRESS="false", ) @@ -600,9 +579,9 @@ origin_url = deposit_user.provider_url + external_id url = reverse(COL_IRI, args=[deposit_collection.name]) - response = authenticated_client.post( + response = post_atom( + authenticated_client, url, - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, HTTP_IN_PROGRESS="true", ) @@ -618,11 +597,10 @@ assert False, response_content # when - response = authenticated_client.put( + response = put_atom( + authenticated_client, edit_iri, - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data-with-origin-reference"].format(url=origin_url), - # + headers HTTP_IN_PROGRESS="false", ) diff --git a/swh/deposit/tests/api/test_deposit_update_binary.py b/swh/deposit/tests/api/test_deposit_update_binary.py --- a/swh/deposit/tests/api/test_deposit_update_binary.py +++ b/swh/deposit/tests/api/test_deposit_update_binary.py @@ -18,7 +18,9 @@ check_archive, create_arborescence_archive, post_archive, + post_atom, put_archive, + put_atom, ) @@ -123,9 +125,9 @@ requests = list(DepositRequest.objects.filter(deposit=deposit, type="metadata")) assert len(requests) == 0 - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(SE_IRI, args=[deposit_collection.name, deposit.id]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data1"], HTTP_SLUG=deposit.external_id, HTTP_IN_PROGRESS=True, @@ -340,9 +342,9 @@ # replacing metadata is no longer possible since the deposit's # status is ready - r = authenticated_client.put( + r = put_atom( + authenticated_client, edit_iri, - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data-deposit-binary"], CONTENT_LENGTH=len(atom_dataset["entry-data-deposit-binary"]), HTTP_SLUG=external_id, @@ -353,9 +355,9 @@ # adding new metadata is no longer possible since the # deposit's status is ready - r = authenticated_client.post( + r = post_atom( + authenticated_client, se_iri, - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data-deposit-binary"], CONTENT_LENGTH=len(atom_dataset["entry-data-deposit-binary"]), HTTP_SLUG=external_id, diff --git a/swh/deposit/tests/common.py b/swh/deposit/tests/common.py --- a/swh/deposit/tests/common.py +++ b/swh/deposit/tests/common.py @@ -163,3 +163,15 @@ def put_archive(authenticated_client, *args, **kwargs): return _post_or_put_archive(authenticated_client.put, *args, **kwargs) + + +def post_atom(authenticated_client, url, data, **kwargs): + return authenticated_client.post( + url, content_type="application/atom+xml;type=entry", data=data, **kwargs + ) + + +def put_atom(authenticated_client, url, data, **kwargs): + return authenticated_client.put( + url, content_type="application/atom+xml;type=entry", data=data, **kwargs + ) diff --git a/swh/deposit/tests/conftest.py b/swh/deposit/tests/conftest.py --- a/swh/deposit/tests/conftest.py +++ b/swh/deposit/tests/conftest.py @@ -33,7 +33,11 @@ setup_django_for, ) from swh.deposit.parsers import parse_xml -from swh.deposit.tests.common import create_arborescence_archive, post_archive +from swh.deposit.tests.common import ( + create_arborescence_archive, + post_archive, + post_atom, +) from swh.model.identifiers import DIRECTORY, REVISION, SNAPSHOT, swhid from swh.scheduler import get_scheduler @@ -344,9 +348,9 @@ origin_url = deposit.client.provider_url + deposit.external_id - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(SE_IRI, args=[collection_name, deposit.id]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data0"] % origin_url, HTTP_IN_PROGRESS="true", ) @@ -420,9 +424,9 @@ deposit_collection, authenticated_client, atom_dataset ): - response = authenticated_client.post( + response = post_atom( + authenticated_client, reverse(COL_IRI, args=[deposit_collection.name]), - content_type="application/atom+xml;type=entry", data=atom_dataset["entry-data1"], HTTP_SLUG="external-id-partial", HTTP_IN_PROGRESS=True,