diff --git a/swh/deposit/tests/api/test_collection_post_binary.py b/swh/deposit/tests/api/test_collection_post_binary.py --- a/swh/deposit/tests/api/test_collection_post_binary.py +++ b/swh/deposit/tests/api/test_collection_post_binary.py @@ -15,7 +15,11 @@ from swh.deposit.config import COL_IRI, DEPOSIT_STATUS_DEPOSITED from swh.deposit.models import Deposit, DepositRequest 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, + post_archive, +) def test_post_deposit_binary_no_slug( @@ -30,16 +34,8 @@ url = reverse(COL_IRI, args=[deposit_collection.name]) # when - response = authenticated_client.post( - url, - content_type="application/zip", # as zip - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], - HTTP_CONTENT_MD5=sample_archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", - HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", + response = post_archive( + authenticated_client, url, sample_archive, in_progress="false", ) assert response.status_code == status.HTTP_201_CREATED @@ -66,15 +62,10 @@ # when response = authenticated_client.post( url, - content_type="application/octet-stream", - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], + sample_archive, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=sample_archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", + content_type="application/octet-stream", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", ) # then @@ -96,18 +87,12 @@ external_id = "some-external-id-1" # when - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/zip", # as zip - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], - # other headers needs HTTP_ prefix to be taken into account + sample_archive, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=sample_archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (sample_archive["name"],), ) # then @@ -158,16 +143,12 @@ external_id = "some-external-id" # when - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/zip", - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], + sample_archive, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=sample_archive["md5sum"], HTTP_PACKAGING="something-unsupported", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", ) # then @@ -193,16 +174,13 @@ external_id = "some-external-id" # when - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/zip", - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], + sample_archive, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=sample_archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", + HTTP_CONTENT_DISPOSITION=None, ) # then @@ -225,18 +203,13 @@ external_id = "some-external-id-1" # when - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/zip", - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], + sample_archive, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=sample_archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", HTTP_ON_BEHALF_OF="someone", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", ) # then @@ -262,17 +235,12 @@ external_id = "some-external-id" # when - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/zip", - data=archive["data"], - # + headers - CONTENT_LENGTH=archive["length"], + archive, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", ) # then @@ -299,17 +267,13 @@ external_id = "some-external-id" # when - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/zip", - data=archive["data"], - # + headers + archive, CONTENT_LENGTH=None, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", ) # then @@ -329,17 +293,12 @@ url = reverse(COL_IRI, args=[deposit_collection.name]) # when - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/zip", # as zip - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], + sample_archive, HTTP_SLUG="some-external-id-1", - HTTP_CONTENT_MD5=sample_archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", ) # then @@ -355,17 +314,13 @@ assert deposits[0] == deposit # second post - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/x-tar", # as zip - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], + sample_archive, + content_type="application/x-tar", HTTP_SLUG="another-external-id", - HTTP_CONTENT_MD5=sample_archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename1", ) assert response.status_code == status.HTTP_201_CREATED 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 @@ -14,7 +14,12 @@ from swh.deposit.config import COL_IRI, DEPOSIT_STATUS_DEPOSITED, EM_IRI, SE_IRI from swh.deposit.models import Deposit, DepositRequest 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, + post_archive, + put_archive, +) def test_post_deposit_binary_and_post_to_add_another_archive( @@ -29,17 +34,12 @@ external_id = "some-external-id-1" # when - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/zip", # as zip - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], + sample_archive, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=sample_archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="true", - HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (sample_archive["name"],), ) # then @@ -68,16 +68,8 @@ update_uri = reverse(EM_IRI, args=[deposit_collection.name, deposit_id]) # adding another archive for the deposit and finalizing it - response = authenticated_client.post( - update_uri, - content_type="application/zip", # as zip - data=archive2["data"], - # + headers - CONTENT_LENGTH=archive2["length"], - HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=archive2["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", - HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (archive2["name"]), + response = post_archive( + authenticated_client, update_uri, archive2, HTTP_SLUG=external_id, ) assert response.status_code == status.HTTP_201_CREATED @@ -148,17 +140,12 @@ tmp_path, "archive2", "file2", b"some other content in file" ) - response = authenticated_client.put( + response = put_archive( + authenticated_client, update_uri, - content_type="application/zip", # as zip - data=archive2["data"], - # + headers - CONTENT_LENGTH=archive2["length"], + archive2, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=archive2["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (archive2["name"],), ) assert response.status_code == status.HTTP_204_NO_CONTENT @@ -249,17 +236,12 @@ tmp_path, "archive2", "file2", b"some other content in file" ) - response = authenticated_client.post( + response = post_archive( + authenticated_client, update_uri, - content_type="application/zip", # as zip - data=archive2["data"], - # + headers - CONTENT_LENGTH=archive2["length"], + archive2, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=archive2["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (archive2["name"],), ) assert response.status_code == status.HTTP_201_CREATED @@ -292,17 +274,12 @@ external_id = "some-external-id-1" # when - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/zip", # as zip - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], + sample_archive, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=sample_archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", ) # then @@ -319,7 +296,7 @@ deposit_request = DepositRequest.objects.get(deposit=deposit) assert deposit_request.deposit == deposit - check_archive("filename0", deposit_request.archive.name) + check_archive(sample_archive["name"], deposit_request.archive.name) # updating/adding is forbidden @@ -337,16 +314,12 @@ # replacing file is no longer possible since the deposit's # status is ready - r = authenticated_client.put( + r = put_archive( + authenticated_client, em_iri, - content_type="application/zip", - data=archive2["data"], - CONTENT_LENGTH=archive2["length"], + archive2, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=archive2["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", ) assert r.status_code == status.HTTP_400_BAD_REQUEST @@ -354,16 +327,12 @@ # adding file is no longer possible since the deposit's status # is ready - r = authenticated_client.post( + r = post_archive( + authenticated_client, em_iri, - content_type="application/zip", - data=archive2["data"], - CONTENT_LENGTH=archive2["length"], + archive2, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=archive2["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS="false", - HTTP_CONTENT_DISPOSITION="attachment; filename=filename0", ) assert r.status_code == status.HTTP_400_BAD_REQUEST 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 @@ -144,3 +144,22 @@ else: pattern = re.compile(".*/%s" % archive_name) assert pattern.match(archive_name_to_check) is not None + + +def _post_or_put_archive(f, url, archive, slug=None, in_progress=None, **kwargs): + default_kwargs = dict( + content_type="application/zip", + CONTENT_LENGTH=archive["length"], + HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (archive["name"],), + HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", + ) + kwargs = {**default_kwargs, **kwargs} + return f(url, data=archive["data"], HTTP_CONTENT_MD5=archive["md5sum"], **kwargs,) + + +def post_archive(authenticated_client, *args, **kwargs): + return _post_or_put_archive(authenticated_client.post, *args, **kwargs) + + +def put_archive(authenticated_client, *args, **kwargs): + return _post_or_put_archive(authenticated_client.put, *args, **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,7 @@ setup_django_for, ) from swh.deposit.parsers import parse_xml -from swh.deposit.tests.common import create_arborescence_archive +from swh.deposit.tests.common import create_arborescence_archive, post_archive from swh.model.identifiers import DIRECTORY, REVISION, SNAPSHOT, swhid from swh.scheduler import get_scheduler @@ -301,17 +301,12 @@ """ url = reverse(COL_IRI, args=[collection_name]) # when - response = authenticated_client.post( + response = post_archive( + authenticated_client, url, - content_type="application/zip", # as zip - data=sample_archive["data"], - # + headers - CONTENT_LENGTH=sample_archive["length"], + sample_archive, HTTP_SLUG=external_id, - HTTP_CONTENT_MD5=sample_archive["md5sum"], - HTTP_PACKAGING="http://purl.org/net/sword/package/SimpleZip", HTTP_IN_PROGRESS=str(in_progress).lower(), - HTTP_CONTENT_DISPOSITION="attachment; filename=%s" % (sample_archive["name"]), ) # then