diff --git a/docs/README.rst b/docs/README.rst --- a/docs/README.rst +++ b/docs/README.rst @@ -38,8 +38,8 @@ retrieve it using the :ref:`vault ` feature of the SWH Archive platform. The differences between a piece of code uploaded using the deposit rather than simply -asking SWH to archive a repository using the `save code now -`_ feature are: +asking SWH to archive a repository using the :swh_web:`save code now ` feature +are: - a deposited artifact is provided from one of the SWH partners which is regarded as a trusted authority, diff --git a/swh/deposit/api/common.py b/swh/deposit/api/common.py --- a/swh/deposit/api/common.py +++ b/swh/deposit/api/common.py @@ -807,8 +807,18 @@ - 415 (unsupported media type) if a wrong media type is provided """ + metadata_stream = request.data + empty_atom_entry_summary = "Empty body request is not supported." + empty_atom_entry_desc = ( + "Atom entry request is about non-empty metadata deposit." + ) + if not metadata_stream: + raise DepositError( + BAD_REQUEST, empty_atom_entry_summary, empty_atom_entry_desc + ) + try: - raw_metadata, metadata = self._read_metadata(request.data) + raw_metadata, metadata = self._read_metadata(metadata_stream) except ParserError: raise DepositError( BAD_REQUEST, @@ -819,10 +829,7 @@ if metadata is None: raise DepositError( - BAD_REQUEST, - "Empty body request is not supported", - "Atom entry deposit is supposed to send for metadata. " - "If the body is empty, there is no metadata.", + BAD_REQUEST, empty_atom_entry_summary, empty_atom_entry_desc ) self._set_deposit_origin_from_metadata(deposit, metadata, headers) 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 @@ -149,6 +149,23 @@ assert b"Empty body request is not supported" in response.content +def test_post_deposit_atom_400_with_empty_request( + authenticated_client, deposit_collection +): + """Posting empty request should return a 400 response + + """ + response = post_atom( + authenticated_client, + reverse(COL_IRI, args=[deposit_collection.name]), + data={}, + HTTP_SLUG="external-id", + CONTENT_LENGTH=0, + ) + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert b"Empty body request is not supported" in response.content + + def test_post_deposit_atom_400_badly_formatted_atom( authenticated_client, deposit_collection, atom_dataset ):