|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Front Matter. |
| 4 | +# |
| 5 | +# Invenio-RDM-Records is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the MIT License; see LICENSE file for more details. |
| 7 | + |
| 8 | +"""Test that parent DOI uses same provider and prefix as child record.""" |
| 9 | + |
| 10 | +import pytest |
| 11 | +from invenio_i18n import lazy_gettext as _ |
| 12 | + |
| 13 | +from invenio_rdm_records.proxies import current_rdm_records |
| 14 | +from invenio_rdm_records.resources.serializers import ( |
| 15 | + CrossrefXMLSerializer, |
| 16 | + DataCite45JSONSerializer, |
| 17 | +) |
| 18 | +from invenio_rdm_records.services.pids import providers |
| 19 | +from tests.fake_crossref_client import FakeCrossrefClient |
| 20 | +from tests.fake_datacite_client import FakeDataCiteClient |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(scope="module") |
| 24 | +def app_config(app_config): |
| 25 | + """Override app_config to include Crossref provider.""" |
| 26 | + fake_datacite = FakeDataCiteClient("datacite", config_prefix="DATACITE") |
| 27 | + fake_crossref = FakeCrossrefClient("crossref", config_prefix="CROSSREF") |
| 28 | + |
| 29 | + app_config["CROSSREF_ENABLED"] = True |
| 30 | + app_config["CROSSREF_USERNAME"] = "INVALID" |
| 31 | + app_config["CROSSREF_PASSWORD"] = "INVALID" |
| 32 | + app_config["CROSSREF_DEPOSITOR"] = "INVALID" |
| 33 | + app_config["CROSSREF_EMAIL"] = "info@example.org" |
| 34 | + app_config["CROSSREF_REGISTRANT"] = "INVALID" |
| 35 | + app_config["CROSSREF_PREFIX"] = "10.1234" |
| 36 | + |
| 37 | + app_config["RDM_PERSISTENT_IDENTIFIER_PROVIDERS"] = [ |
| 38 | + providers.DataCitePIDProvider( |
| 39 | + "datacite", |
| 40 | + client=fake_datacite, |
| 41 | + label=_("DOI"), |
| 42 | + ), |
| 43 | + providers.CrossrefPIDProvider( |
| 44 | + "crossref", |
| 45 | + client=fake_crossref, |
| 46 | + label=_("DOI"), |
| 47 | + ), |
| 48 | + providers.ExternalPIDProvider( |
| 49 | + "external", |
| 50 | + "doi", |
| 51 | + validators=[ |
| 52 | + providers.BlockedPrefixes( |
| 53 | + config_names=["DATACITE_PREFIX", "CROSSREF_PREFIX"] |
| 54 | + ) |
| 55 | + ], |
| 56 | + label=_("DOI"), |
| 57 | + ), |
| 58 | + providers.OAIPIDProvider( |
| 59 | + "oai", |
| 60 | + label=_("OAI ID"), |
| 61 | + ), |
| 62 | + ] |
| 63 | + |
| 64 | + app_config["RDM_PARENT_PERSISTENT_IDENTIFIER_PROVIDERS"] = [ |
| 65 | + providers.DataCitePIDProvider( |
| 66 | + "datacite", |
| 67 | + client=fake_datacite, |
| 68 | + serializer=DataCite45JSONSerializer(is_parent=True), |
| 69 | + label=_("Concept DOI"), |
| 70 | + ), |
| 71 | + providers.CrossrefPIDProvider( |
| 72 | + "crossref", |
| 73 | + client=fake_crossref, |
| 74 | + serializer=CrossrefXMLSerializer(), |
| 75 | + label=_("Concept DOI"), |
| 76 | + ), |
| 77 | + ] |
| 78 | + |
| 79 | + app_config["RDM_PERSISTENT_IDENTIFIERS"] = { |
| 80 | + "doi": { |
| 81 | + "providers": ["datacite", "crossref", "external"], |
| 82 | + "required": True, |
| 83 | + "label": _("DOI"), |
| 84 | + "validator": lambda x: True, |
| 85 | + "normalizer": lambda x: x.lower(), |
| 86 | + "is_enabled": lambda app: True, |
| 87 | + "ui": {"default_selected": "yes"}, |
| 88 | + }, |
| 89 | + "oai": { |
| 90 | + "providers": ["oai"], |
| 91 | + "required": True, |
| 92 | + "label": _("OAI"), |
| 93 | + "is_enabled": lambda app: True, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + return app_config |
| 98 | + |
| 99 | + |
| 100 | +@pytest.fixture() |
| 101 | +def service(running_app): |
| 102 | + """Service fixture.""" |
| 103 | + return current_rdm_records.records_service |
| 104 | + |
| 105 | + |
| 106 | +def test_parent_doi_uses_child_provider_crossref( |
| 107 | + running_app, search_clear, minimal_record, superuser_identity, service, location |
| 108 | +): |
| 109 | + """Test that parent DOI uses same Crossref provider and prefix as child.""" |
| 110 | + # Create draft with a Crossref DOI identifier (prefix is implicit in the identifier) |
| 111 | + minimal_record["pids"] = { |
| 112 | + "doi": { |
| 113 | + "identifier": "10.1234/crossref.parent.test1", |
| 114 | + "provider": "crossref", |
| 115 | + "client": "crossref", |
| 116 | + } |
| 117 | + } |
| 118 | + draft = service.create(superuser_identity, minimal_record) |
| 119 | + |
| 120 | + # Publish the record |
| 121 | + record = service.publish(superuser_identity, draft.id) |
| 122 | + |
| 123 | + # Check child DOI |
| 124 | + child_doi = record["pids"]["doi"] |
| 125 | + assert child_doi["provider"] == "crossref" |
| 126 | + assert child_doi["identifier"] == "10.1234/crossref.parent.test1" |
| 127 | + |
| 128 | + |
| 129 | +def test_parent_doi_uses_child_provider_datacite( |
| 130 | + running_app, search_clear, minimal_record, superuser_identity, service, location |
| 131 | +): |
| 132 | + """Test that parent DOI uses DataCite provider by default.""" |
| 133 | + # Create draft without explicit DOI - DataCite creates one automatically |
| 134 | + draft = service.create(superuser_identity, minimal_record) |
| 135 | + draft = service.pids.create(superuser_identity, draft.id, "doi") |
| 136 | + |
| 137 | + # Publish the record |
| 138 | + record = service.publish(superuser_identity, draft.id) |
| 139 | + |
| 140 | + # Check child DOI |
| 141 | + child_doi = record["pids"]["doi"] |
| 142 | + assert child_doi["provider"] == "datacite" |
| 143 | + assert child_doi["identifier"].startswith("10.1234/") |
| 144 | + |
| 145 | + # Check parent DOI |
| 146 | + parent_doi = record["parent"]["pids"]["doi"] |
| 147 | + assert parent_doi["provider"] == "datacite" |
| 148 | + assert parent_doi["identifier"].startswith("10.1234/") |
| 149 | + |
| 150 | + |
| 151 | +def test_parent_doi_default_provider_and_prefix( |
| 152 | + running_app, search_clear, minimal_record, superuser_identity, service, location |
| 153 | +): |
| 154 | + """Test that parent DOI uses default provider/prefix when child has none specified.""" |
| 155 | + # Create draft without explicit provider/prefix (uses defaults) |
| 156 | + draft = service.create(superuser_identity, minimal_record) |
| 157 | + |
| 158 | + # Create DOI with defaults |
| 159 | + draft = service.pids.create(superuser_identity, draft.id, "doi") |
| 160 | + |
| 161 | + # Publish the record |
| 162 | + record = service.publish(superuser_identity, draft.id) |
| 163 | + |
| 164 | + # Check child DOI |
| 165 | + child_doi = record["pids"]["doi"] |
| 166 | + assert child_doi["provider"] == "datacite" |
| 167 | + assert child_doi["identifier"].startswith("10.1234/") |
| 168 | + |
| 169 | + # Check parent DOI |
| 170 | + parent_doi = record["parent"]["pids"]["doi"] |
| 171 | + assert parent_doi["provider"] == "datacite" |
| 172 | + assert parent_doi["identifier"].startswith("10.1234/") |
| 173 | + |
| 174 | + |
| 175 | +def test_parent_doi_consistency_across_versions( |
| 176 | + running_app, search_clear, minimal_record, superuser_identity, service, location |
| 177 | +): |
| 178 | + """Test that parent DOI stays consistent across versions.""" |
| 179 | + from copy import deepcopy |
| 180 | + |
| 181 | + # Create and publish first version |
| 182 | + draft = service.create(superuser_identity, minimal_record) |
| 183 | + draft = service.pids.create(superuser_identity, draft.id, "doi") |
| 184 | + record_v1 = service.publish(superuser_identity, draft.id) |
| 185 | + |
| 186 | + # Get parent DOI from first version |
| 187 | + parent_doi_v1 = record_v1["parent"]["pids"]["doi"]["identifier"] |
| 188 | + assert parent_doi_v1.startswith("10.1234/") |
| 189 | + |
| 190 | + # Create second version |
| 191 | + draft_v2 = service.new_version(superuser_identity, record_v1.id) |
| 192 | + draft_v2 = service.pids.create(superuser_identity, draft_v2.id, "doi") |
| 193 | + |
| 194 | + # Update metadata for v2 |
| 195 | + data_v2 = deepcopy(draft_v2.data) |
| 196 | + data_v2["metadata"]["title"] = "Updated Title" |
| 197 | + data_v2["metadata"]["publication_date"] = "2020-06-01" |
| 198 | + draft_v2 = service.update_draft(superuser_identity, draft_v2.id, data_v2) |
| 199 | + |
| 200 | + # Publish v2 |
| 201 | + record_v2 = service.publish(superuser_identity, draft_v2.id) |
| 202 | + |
| 203 | + # Parent DOI should be the same across versions |
| 204 | + parent_doi_v2 = record_v2["parent"]["pids"]["doi"]["identifier"] |
| 205 | + assert parent_doi_v2 == parent_doi_v1 |
| 206 | + |
| 207 | + # Child DOIs should be different |
| 208 | + child_doi_v1 = record_v1["pids"]["doi"]["identifier"] |
| 209 | + child_doi_v2 = record_v2["pids"]["doi"]["identifier"] |
| 210 | + assert child_doi_v1 != child_doi_v2 |
| 211 | + |
| 212 | + # Both child DOIs should use the same prefix |
| 213 | + assert child_doi_v1.startswith("10.1234/") |
| 214 | + assert child_doi_v2.startswith("10.1234/") |
0 commit comments