diff --git a/swh/deposit/cli/client.py b/swh/deposit/cli/client.py --- a/swh/deposit/cli/client.py +++ b/swh/deposit/cli/client.py @@ -604,16 +604,24 @@ from xml.etree import ElementTree from swh.deposit.client import PublicApiDepositClient - from swh.deposit.utils import parse_swh_reference + from swh.deposit.utils import parse_swh_metadata_provenance, parse_swh_reference # Parse to check for a swhid presence within the metadata file with open(metadata_path, "r") as f: metadata_raw = f.read() - actual_swhid = parse_swh_reference(ElementTree.fromstring(metadata_raw)) + metadata_tree = ElementTree.fromstring(metadata_raw) + actual_swhid = parse_swh_reference(metadata_tree) if not actual_swhid: raise InputError("A SWHID must be provided for a metadata-only deposit") + meta_prov_url = parse_swh_metadata_provenance(metadata_tree) + if not meta_prov_url: + logger.warning( + "A '' should be provided for a metadata-only " + "deposit" + ) + with trap_and_report_exceptions(): client = PublicApiDepositClient(url=_url(url), auth=(username, password)) collection = _collection(client) diff --git a/swh/deposit/tests/cli/test_client.py b/swh/deposit/tests/cli/test_client.py --- a/swh/deposit/tests/cli/test_client.py +++ b/swh/deposit/tests/cli/test_client.py @@ -9,6 +9,7 @@ import json import logging import os +from typing import Optional from unittest.mock import MagicMock from xml.etree import ElementTree @@ -800,8 +801,17 @@ } +@pytest.mark.parametrize( + "metadata_entry_key", ["entry-data-with-swhid", "entry-data-with-swhid-no-prov"] +) def test_cli_metadata_only_deposit_full_metadata_file( - datadir, requests_mock_datadir, cli_runner, atom_dataset, tmp_path, + datadir, + requests_mock_datadir, + cli_runner, + atom_dataset, + tmp_path, + metadata_entry_key, + caplog, ): """Post metadata-only deposit through cli @@ -810,7 +820,7 @@ """ api_url_basename = "deposit.test.metadataonly" swhid = "swh:1:dir:ef04a768181417fbc5eef4243e2507915f24deea" - metadata = atom_dataset["entry-data-with-swhid"].format(swhid=swhid) + metadata = atom_dataset[metadata_entry_key].format(swhid=swhid) metadata_path = os.path.join(tmp_path, "entry-data-with-swhid.xml") with open(metadata_path, "w") as m: m.write(metadata) @@ -841,6 +851,19 @@ assert "error" not in actual_deposit_status assert actual_deposit_status == expected_deposit_status + count_warnings = 0 + warning_record: Optional[str] = None + for (_, log_level, msg) in caplog.record_tuples: + if log_level == logging.WARNING: + count_warnings += 1 + warning_record = msg + + if "no-prov" in metadata_entry_key: + assert count_warnings == 1 + assert "metadata-provenance>' should be provided" in warning_record + else: + assert count_warnings == 0 + def test_cli_metadata_only_deposit_invalid_swhid( datadir, requests_mock_datadir, cli_runner, atom_dataset, tmp_path, diff --git a/swh/deposit/tests/data/atom/entry-data-with-swhid.xml b/swh/deposit/tests/data/atom/entry-data-with-swhid-no-prov.xml copy from swh/deposit/tests/data/atom/entry-data-with-swhid.xml copy to swh/deposit/tests/data/atom/entry-data-with-swhid-no-prov.xml diff --git a/swh/deposit/tests/data/atom/entry-data-with-swhid.xml b/swh/deposit/tests/data/atom/entry-data-with-swhid.xml --- a/swh/deposit/tests/data/atom/entry-data-with-swhid.xml +++ b/swh/deposit/tests/data/atom/entry-data-with-swhid.xml @@ -1,6 +1,7 @@ Awesome Compiler urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a @@ -9,5 +10,8 @@ + + https://example.org/awesome/compiler/metadata +