diff --git a/swh/deposit/tests/api/test_checks.py b/swh/deposit/tests/api/test_checks.py
index 7e1eba3f..3cc96c0b 100644
--- a/swh/deposit/tests/api/test_checks.py
+++ b/swh/deposit/tests/api/test_checks.py
@@ -1,621 +1,667 @@
# Copyright (C) 2017-2022 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
# disable flake8 on this file because of line length
# flake8: noqa
import re
import textwrap
from typing import Any, Dict
from xml.etree import ElementTree
import pytest
from swh.deposit.api.checks import (
METADATA_PROVENANCE_KEY,
SUGGESTED_FIELDS_MISSING,
check_metadata,
)
METADATA_PROVENANCE_DICT: Dict[str, Any] = {
"swh:deposit": {
METADATA_PROVENANCE_KEY: {"schema:url": "some-metadata-provenance-url"}
}
}
XMLNS = """xmlns="http://www.w3.org/2005/Atom"
xmlns:swh="https://www.softwareheritage.org/schema/2018/deposit"
xmlns:codemeta="https://doi.org/10.5063/SCHEMA/CODEMETA-2.0"
xmlns:schema="http://schema.org/" """
PROVENANCE_XML = """
some-metadata-provenance-url
"""
_parameters1 = [
textwrap.dedent(metadata_ok)
for (metadata_ok,) in [
(
f"""\
something
something-else
foo
someone
{PROVENANCE_XML}
""",
),
(
f"""\
something
something-else
foo
no one
{PROVENANCE_XML}
""",
),
(
f"""\
some url
bar
no one
{PROVENANCE_XML}
""",
),
(
f"""\
some url
bar
+ someone
+ should allow anything here
+
+ {PROVENANCE_XML}
+
+ """,
+ ),
+ (
+ f"""\
+
+ some url
+ bar
+
+ should allow anything here
+ someone
+
+ {PROVENANCE_XML}
+
+ """,
+ ),
+ (
+ f"""\
+
+ some url
+ bar
+
+ should allow anything here
+ someone
should allow anything here
{PROVENANCE_XML}
""",
),
(
f"""\
some url
some id
nar
no one
2020-12-21
2020-12-21
2020-12-25
{PROVENANCE_XML}
""",
),
(
# technically, only Date is allowed for datePublished; but we allow DateTime
# for backward compatibility with old swh-deposit versions
f"""\
some url
some id
nar
no one
2020-12-21T12:00:00
2020-12-21T12:00:00
2020-12-25T12:00:00
{PROVENANCE_XML}
""",
),
(
f"""\
something
something-else
bar
someone
""",
),
(
f"""\
something
something-else
bar
someone
some-metadata-provenance-url
""",
),
(
f"""\
something
something-else
bar
someone
some-metadata-provenance-url
""",
),
(
f"""\
something
something-else
bar
someone
some-metadata-provenance-url
""",
),
]
]
@pytest.mark.parametrize(
"metadata_ok", _parameters1,
)
def test_api_checks_check_metadata_ok(metadata_ok, swh_checks_deposit):
actual_check, detail = check_metadata(ElementTree.fromstring(metadata_ok))
assert actual_check is True, f"Unexpected result: {detail}"
if "swh:deposit" in metadata_ok:
# no missing suggested field
assert detail is None
else:
# missing suggested field
assert detail == {
"metadata": [
{
"fields": [METADATA_PROVENANCE_KEY],
"summary": SUGGESTED_FIELDS_MISSING,
}
]
}
_parameters2 = [
(textwrap.dedent(metadata_ko), expected_summary)
for (metadata_ko, expected_summary) in [
(
f"""\
something
something-else
someone
{PROVENANCE_XML}
""",
{
"summary": "Mandatory fields are missing",
"fields": ["atom:name or atom:title or codemeta:name"],
},
),
(
f"""\
something
something-else
foobar
{PROVENANCE_XML}
""",
{
"summary": "Mandatory fields are missing",
"fields": ["atom:author or codemeta:author"],
},
),
(
f"""\
something
something-else
bar
someone
{PROVENANCE_XML}
""",
{
"summary": "Mandatory fields are missing",
"fields": ["atom:name or atom:title or codemeta:name"],
},
),
(
f"""\
something
something-else
foobar
foo
{PROVENANCE_XML}
""",
{
"summary": "Mandatory fields are missing",
"fields": ["atom:author or codemeta:author"],
},
),
(
f"""\
something
something-else
bar
someone
{PROVENANCE_XML}
""",
{
"summary": "Mandatory fields are missing",
"fields": ["atom:author or codemeta:author"],
},
),
]
]
@pytest.mark.parametrize("metadata_ko,expected_summary", _parameters2)
def test_api_checks_check_metadata_ko(
metadata_ko, expected_summary, swh_checks_deposit
):
actual_check, error_detail = check_metadata(ElementTree.fromstring(metadata_ko))
assert actual_check is False
assert error_detail == {"metadata": [expected_summary]}
_parameters3 = [
(textwrap.dedent(metadata_ko), expected_summary)
for (metadata_ko, expected_summary) in [
(
f"""\
some url
bar
no one
{PROVENANCE_XML}
""",
[
{
"summary": ".*Reason: a simple content element can't have child elements.*",
"fields": ["codemeta:name"],
},
],
),
(
f"""\
some url
bar
no one
{PROVENANCE_XML}
""",
[
{
"summary": ".*Reason: character data between child elements.*",
"fields": ["codemeta:author"],
},
],
),
+ (
+ f"""\
+
+ some url
+ bar
+
+ should allow anything here
+
+ {PROVENANCE_XML}
+
+ """,
+ [
+ {
+ "summary": ".*Tag '?codemeta:name'? expected.*",
+ "fields": ["codemeta:author"],
+ },
+ ],
+ ),
(
f"""\
something
something-else
bar
someone
2020-aa-21
2020-12-bb
{PROVENANCE_XML}
""",
[
{
"summary": ".*Reason: invalid value '2020-aa-21'.*",
"fields": ["codemeta:datePublished"],
},
{
"summary": ".*Reason: invalid value '2020-12-bb'.*",
"fields": ["codemeta:dateCreated"],
},
],
),
(
f"""\
some url
someid
bar
no one
2020-aa-21
2020-12-bb
""",
[
{
"summary": ".*Reason: invalid value '2020-aa-21'.*",
"fields": ["codemeta:datePublished"],
},
{
"summary": ".*Reason: invalid value '2020-12-bb'.*",
"fields": ["codemeta:dateCreated"],
},
],
),
(
f"""\
some url
someid
bar
no one
2020-12-aa
""",
[
{
"summary": ".*Reason: invalid value '2020-12-aa'.*",
"fields": ["codemeta:dateModified"],
},
],
),
(
f"""\
something
something-else
bar
someone
""",
[
{
"summary": (
r".*Reason: Unexpected child with tag 'swh:invalid'.*"
r"Instance:.*swh:invalid.*"
),
"fields": ["swh:deposit"],
}
],
),
(
f"""\
something
something-else
bar
someone
""",
[
{
"summary": (
r".*Reason: Unexpected child with tag 'swh:add_to_origin'.*"
),
"fields": ["swh:deposit"],
}
],
),
(
f"""\
something
something-else
bar
someone
""",
[
{
"summary": (
r".*Reason: assertion test if false.*"
r"Schema:\n*"
r' *]+ id="swhdeposit-incompatible-create-and-add".*'
),
"fields": ["swh:deposit"],
}
],
),
(
f"""\
something
something-else
bar
someone
""",
[
{
"summary": (
r".*Reason: assertion test if false.*"
r"Schema:\n*"
r' *]+ id="swhdeposit-incompatible-create-and-reference".*'
),
"fields": ["swh:deposit"],
}
],
),
(
f"""\
something
something-else
bar
someone
""",
[
{
"summary": (
r".*Reason: assertion test if false.*"
r"Schema:\n*"
r' *]+ id="swhdeposit-incompatible-add-and-reference".*'
),
"fields": ["swh:deposit"],
}
],
),
(
f"""\
something
something-else
bar
someone
""",
[
{
"summary": r".*Reason: Unexpected child with tag 'swh:origin'.*",
"fields": ["swh:deposit"],
},
],
),
(
f"""\
something
something-else
bar
someone
""",
[
{
"summary": r".*Reason: Unexpected child with tag 'swh:origin'.*",
"fields": ["swh:deposit"],
},
],
),
(
f"""\
something
something-else
bar
someone
""",
[
{
"summary": r".*Reason: Unexpected child with tag 'swh:object'.*",
"fields": ["swh:deposit"],
},
],
),
]
]
@pytest.mark.parametrize("metadata_ko,expected_summaries", _parameters3)
def test_api_checks_check_metadata_ko_schema(
metadata_ko, expected_summaries, swh_checks_deposit
):
actual_check, error_detail = check_metadata(ElementTree.fromstring(metadata_ko))
assert actual_check is False
assert len(error_detail["metadata"]) == len(expected_summaries), error_detail[
"metadata"
]
for (detail, expected_summary) in zip(error_detail["metadata"], expected_summaries):
assert detail["fields"] == expected_summary["fields"]
# xmlschema returns very detailed errors, we cannot reasonably test them
# for equality
summary = detail["summary"]
assert re.match(
expected_summary["summary"], summary, re.DOTALL
), f"Failed to match {expected_summary['summary']!r} with:\n{summary}"
diff --git a/swh/deposit/xsd/codemeta.xsd b/swh/deposit/xsd/codemeta.xsd
index cc6efeba..75bfc8e6 100644
--- a/swh/deposit/xsd/codemeta.xsd
+++ b/swh/deposit/xsd/codemeta.xsd
@@ -1,34 +1,36 @@
-
-
+
+
-
-
+
+