diff --git a/swh/deposit/api/checks.py b/swh/deposit/api/checks.py --- a/swh/deposit/api/checks.py +++ b/swh/deposit/api/checks.py @@ -14,7 +14,6 @@ from typing import Dict, Optional, Tuple MANDATORY_FIELDS_MISSING = "Mandatory fields are missing" -ALTERNATE_FIELDS_MISSING = "Mandatory alternate fields are missing" def check_metadata(metadata: Dict) -> Tuple[bool, Optional[Dict]]: @@ -28,36 +27,22 @@ ok (False, ) otherwise. """ - # following fields are mandatory - required_fields = { - "atom:author": False, - } # at least one value per couple below is mandatory alternate_fields = { - ("atom:name", "atom:title"): False, + ("atom:name", "atom:title", "codemeta:name"): False, + ("atom:author", "codemeta:author"): False, } for field, value in metadata.items(): - for name in required_fields: - if name in field: - required_fields[name] = True - for possible_names in alternate_fields: for possible_name in possible_names: if possible_name in field: alternate_fields[possible_names] = True continue - mandatory_result = [k for k, v in required_fields.items() if not v] - optional_result = [" or ".join(k) for k, v in alternate_fields.items() if not v] + mandatory_result = [" or ".join(k) for k, v in alternate_fields.items() if not v] - if mandatory_result == [] and optional_result == []: + if mandatory_result == []: return True, None - detail = [] - if mandatory_result != []: - detail.append({"summary": MANDATORY_FIELDS_MISSING, "fields": mandatory_result}) - if optional_result != []: - detail.append( - {"summary": ALTERNATE_FIELDS_MISSING, "fields": optional_result,} - ) + detail = [{"summary": MANDATORY_FIELDS_MISSING, "fields": mandatory_result}] return False, {"metadata": detail} diff --git a/swh/deposit/tests/api/test_checks.py b/swh/deposit/tests/api/test_checks.py --- a/swh/deposit/tests/api/test_checks.py +++ b/swh/deposit/tests/api/test_checks.py @@ -23,11 +23,12 @@ "atom:title": "bar", "atom:author": "no one", }, + {"atom:url": "some url", "codemeta:name": "bar", "codemeta:author": "no one",}, ], ) def test_api_checks_check_metadata_ok(metadata_ok, swh_checks_deposit): actual_check, detail = check_metadata(metadata_ok) - assert actual_check is True + assert actual_check is True, detail assert detail is None @@ -41,8 +42,8 @@ "atom:author": "someone", }, { - "summary": "Mandatory alternate fields are missing", - "fields": ["atom:name or atom:title"], + "summary": "Mandatory fields are missing", + "fields": ["atom:name or atom:title or codemeta:name"], }, ), ( @@ -51,7 +52,34 @@ "atom:external_identifier": "something-else", "atom:title": "foobar", }, - {"summary": "Mandatory fields are missing", "fields": ["atom:author"],}, + { + "summary": "Mandatory fields are missing", + "fields": ["atom:author or codemeta:author"], + }, + ), + ( + { + "atom:url": "something", + "atom:external_identifier": "something-else", + "codemeta:title": "bar", + "atom:author": "someone", + }, + { + "summary": "Mandatory fields are missing", + "fields": ["atom:name or atom:title or codemeta:name"], + }, + ), + ( + { + "atom:url": "something", + "atom:external_identifier": "something-else", + "atom:title": "foobar", + "author": "foo", + }, + { + "summary": "Mandatory fields are missing", + "fields": ["atom:author or codemeta:author"], + }, ), ], ) diff --git a/swh/deposit/tests/api/test_deposit_private_check.py b/swh/deposit/tests/api/test_deposit_private_check.py --- a/swh/deposit/tests/api/test_deposit_private_check.py +++ b/swh/deposit/tests/api/test_deposit_private_check.py @@ -7,7 +7,7 @@ import pytest from rest_framework import status -from swh.deposit.api.checks import ALTERNATE_FIELDS_MISSING, MANDATORY_FIELDS_MISSING +from swh.deposit.api.checks import MANDATORY_FIELDS_MISSING from swh.deposit.api.private.deposit_check import ( MANDATORY_ARCHIVE_INVALID, MANDATORY_ARCHIVE_MISSING, @@ -130,13 +130,15 @@ assert len(details["archive"]) == 1 assert details["archive"][0]["summary"] == MANDATORY_ARCHIVE_UNSUPPORTED # metadata check failure - assert len(details["metadata"]) == 2 + assert len(details["metadata"]) == 1 mandatory = details["metadata"][0] assert mandatory["summary"] == MANDATORY_FIELDS_MISSING - assert set(mandatory["fields"]) == set(["atom:author"]) - alternate = details["metadata"][1] - assert alternate["summary"] == ALTERNATE_FIELDS_MISSING - assert alternate["fields"] == ["atom:name or atom:title"] + assert set(mandatory["fields"]) == set( + [ + "atom:author or codemeta:author", + "atom:name or atom:title or codemeta:name", + ] + ) deposit = Deposit.objects.get(pk=deposit.id) assert deposit.status == DEPOSIT_STATUS_REJECTED 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 @@ -796,11 +796,12 @@ assert response.status_code == status.HTTP_400_BAD_REQUEST assert b"Functional metadata checks failure" in response.content # detail on the errors - assert b"- Mandatory fields are missing (atom:author)" in response.content - assert ( - b"- Mandatory alternate fields are missing (atom:name or atom:title)" - in response.content + msg = ( + b"- Mandatory fields are missing (" + b"atom:name or atom:title or codemeta:name, " + b"atom:author or codemeta:author)" ) + assert msg in response.content def test_put_atom_with_create_origin_and_external_identifier(