Add the pytest suite and fix the bugs it found

The suite takes its data from the vectors and fixtures: 3635 tests, 97.9% branch
coverage, and paperspec check and paperspec stress give the same results as before.
Fixed: receipts.verify_signature raised KeyError for a record without a format;
traces.DOC_TAG matched a tag followed by a line feed; paperspec load and paperspec check
left SQLite connections open; the site builder copied the output folder's subfolders into
itself.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B8cL4zN4XpaQF5cBtASzfq
This commit is contained in:
alexmandClaude committed 2026-10-19 04:32:44 -04:00
1 parent 30d310f64d
commit a8fc7237ae
76 files changed
+15564 -80

No files matched your search

+369
View File
@@ -0,0 +1,369 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 Scienting
#
# Source Code: https://git.scient.ing/papers/paperspec
# See the LICENSE.md file for full license terms.
"""Rights records, policies, stage checks and backend conditions (spec/14)."""
from typing import ClassVar
import pytest
from helpers_lib1 import case_params, read_cases
from paperspec import rights
SHA = "a" * 64
OTHER_SHA = "b" * 64
NOW = "2026-10-11T00:00:00Z"
ALL_KEYS = [
*rights.USES,
*(f"ai/{p}/{q}" for p in rights.PURPOSES for q in rights.PROCESSORS),
]
def witness(uses=None, **extra):
"""A witness whose rights record holds `uses` and the extra record members."""
record = {"basis": "subscription", "uses": uses or {}}
record.update({k: v for k, v in extra.items() if k not in ("origin", "sha256")})
return {
"rights": record,
"origin": extra.get("origin", {"doi": "10.5555/x"}),
"sha256": extra.get("sha256", SHA),
}
def policy(uses, **applies):
"""A policy granting `uses`, applying to the witnesses `applies` describes."""
out = {"uses": uses}
if applies:
out["applies_to"] = applies
return out
@pytest.mark.parametrize(
"case", case_params([c for c in read_cases("rights") if "stage" in c])
)
def test_stage_vector(case):
w = {
"rights": case["rights"],
"origin": case["origin"],
"sha256": case["witness_sha256"],
}
uses, expired = rights.effective(w, case["policies"], case["now"])
allowed, lacking = rights.check(case["requires"], case["processor"], uses)
assert {"allowed": allowed, "lacking": lacking, "expired": expired} == case[
"expect"
]
@pytest.mark.parametrize(
"case", case_params([c for c in read_cases("rights") if "backend" in c])
)
def test_backend_vector(case):
allowed, failed = rights.backend_allowed(case["backend"], case["profile"])
assert {"allowed": allowed, "failed": failed} == case["expect"]
def test_combining_vector():
case = next(c for c in read_cases("rights") if c["id"] == "combine")
assert rights.combine([rights.flat(r) for r in case["rights"]]) == case["expect"]
class TestFlat:
def test_an_empty_record_is_unknown_everywhere(self):
flat = rights.flat({})
assert list(flat) == ALL_KEYS
assert set(flat.values()) == {"unknown"}
def test_there_are_five_uses_and_eight_ai_pairs(self):
assert len(rights.USES) == 5
assert len(rights.AI_PAIRS) == 8
assert len(rights.flat({})) == 13
def test_values_are_kept_and_anything_but_a_boolean_is_unknown(self):
flat = rights.flat(
{
"uses": {
"read": True,
"tdm": False,
"redistribute": "unknown",
"quote-public": "yes",
"accessible-copy": None,
"ai": {"internal/local": True, "external/outside": False},
}
}
)
assert flat["read"] is True
assert flat["tdm"] is False
assert flat["redistribute"] == "unknown"
assert flat["quote-public"] == "unknown"
assert flat["accessible-copy"] == "unknown"
assert flat["ai/internal/local"] is True
assert flat["ai/external/outside"] is False
assert flat["ai/internal/tenancy"] == "unknown"
class TestEffective:
def test_without_policies_the_flat_uses_are_returned(self):
w = witness({"read": True, "tdm": False})
uses, expired = rights.effective(w, [], NOW)
assert uses == rights.flat(w["rights"])
assert expired is False
def test_an_expired_record_allows_nothing(self):
w = witness({"read": True}, valid_until="2026-10-11T00:00:00Z")
uses, expired = rights.effective(w, [policy({"tdm": True})], NOW)
assert expired is True
assert set(uses) == set(ALL_KEYS)
assert set(uses.values()) == {False}
def test_a_record_is_current_until_the_instant_it_expires(self):
w = witness({"read": True}, valid_until="2026-10-11T00:00:01Z")
assert rights.effective(w, [], NOW)[1] is False
assert rights.effective(w, [], "2026-10-11T00:00:01Z")[1] is True
def test_a_null_expiry_never_expires(self):
w = witness({"read": True}, valid_until=None)
assert rights.effective(w, [], "9999-01-01T00:00:00Z")[1] is False
def test_a_policy_settles_an_unknown_use(self):
w = witness({"read": True})
uses, _ = rights.effective(
w, [policy({"tdm": True, "ai": {"internal/tenancy": True}})], NOW
)
assert uses["tdm"] is True
assert uses["ai/internal/tenancy"] is True
assert uses["redistribute"] == "unknown"
def test_a_policy_never_overrides_a_known_use(self):
w = witness({"read": False, "tdm": True})
uses, _ = rights.effective(w, [policy({"read": True, "tdm": False})], NOW)
assert uses["read"] is False
assert uses["tdm"] is True
def test_a_policy_can_settle_a_use_as_false(self):
uses, _ = rights.effective(witness(), [policy({"read": False})], NOW)
assert uses["read"] is False
def test_the_first_applicable_policy_to_speak_wins(self):
policies = [policy({"read": True}), policy({"read": False, "tdm": True})]
uses, _ = rights.effective(witness(), policies, NOW)
assert uses["read"] is True
assert uses["tdm"] is True
def test_a_policy_that_says_unknown_settles_nothing(self):
uses, _ = rights.effective(witness(), [policy({"read": "unknown"})], NOW)
assert uses["read"] == "unknown"
def test_a_policy_without_uses_changes_nothing(self):
uses, _ = rights.effective(witness({"read": True}), [{"applies_to": {}}], NOW)
assert uses == rights.flat({"uses": {"read": True}})
@pytest.mark.parametrize(
("when", "applies"),
[
({"at": "2026-10-11T00:00:00Z"}, True),
({"at": "2026-10-11T00:00:01Z"}, False),
({"valid_until": "2026-10-11T00:00:00Z"}, False),
({"valid_until": "2026-10-11T00:00:01Z"}, True),
({"valid_until": None, "at": None}, True),
],
)
def test_a_policy_applies_from_its_time_to_its_expiry(self, when, applies):
p = dict(policy({"read": True}), **when)
uses, _ = rights.effective(witness(), [p], NOW)
assert (uses["read"] is True) is applies
@pytest.mark.parametrize(
("applies", "w", "expected"),
[
({"basis": ["subscription"]}, witness(), True),
({"basis": ["open-license"]}, witness(), False),
({"license": ["CC-BY-4.0"]}, witness(license={"spdx": "CC-BY-4.0"}), True),
({"license": ["CC-BY-4.0"]}, witness(license={"spdx": "MIT"}), False),
({"license": ["CC-BY-4.0"]}, witness(license={"text": "terms"}), False),
({"license": ["CC-BY-4.0"]}, witness(license=None), False),
({"license": ["CC-BY-4.0"]}, witness(), False),
({"doi_prefixes": ["10.5555/"]}, witness(), True),
({"doi_prefixes": ["10.1/", "10.5555/"]}, witness(), True),
({"doi_prefixes": ["10.1/"]}, witness(), False),
({"doi_prefixes": ["10.5555/"]}, witness(origin={}), False),
({"doi_prefixes": ["10.5555/"]}, witness(origin=None), False),
({"doi_prefixes": ["10.5555/"]}, witness(origin={"doi": None}), False),
({"witnesses": [SHA]}, witness(), True),
({"witnesses": [OTHER_SHA]}, witness(), False),
({}, witness(), True),
(
{"basis": ["subscription"], "doi_prefixes": ["10.1/"]},
witness(),
False,
),
],
)
def test_a_policy_applies_to_the_witnesses_it_describes(self, applies, w, expected):
p = policy({"read": True})
p["applies_to"] = applies
uses, _ = rights.effective(w, [p], NOW)
assert (uses["read"] is True) is expected
class TestCombine:
@pytest.mark.parametrize(
("values", "combined"),
[
([True, True], True),
([True, False], False),
([True, "unknown"], "unknown"),
([False, "unknown"], False),
(["unknown", "unknown"], "unknown"),
([False, False], False),
([True], True),
(["unknown", True, False], False),
],
)
def test_false_beats_unknown_beats_true(self, values, combined):
flats = [dict.fromkeys(ALL_KEYS, v) for v in values]
assert set(rights.combine(flats).values()) == {combined}
def test_it_combines_each_use_separately(self):
a = rights.flat({"uses": {"read": True, "tdm": True}})
b = rights.flat({"uses": {"read": True, "tdm": False}})
out = rights.combine([a, b])
assert out["read"] is True
assert out["tdm"] is False
assert out["redistribute"] == "unknown"
def test_combining_one_record_is_that_record(self):
flat = rights.flat({"uses": {"read": True, "tdm": False}})
assert rights.combine([flat]) == flat
class TestCheck:
ALL_TRUE = dict.fromkeys(ALL_KEYS, True)
def test_a_stage_with_no_requirements_is_allowed(self):
assert rights.check({}, "outside", {}) == (True, [])
def test_a_use_that_is_true_is_met(self):
assert rights.check({"uses": ["read", "tdm"]}, "local", self.ALL_TRUE) == (
True,
[],
)
def test_false_and_unknown_uses_are_lacking_and_unknown_is_marked(self):
uses = dict(self.ALL_TRUE, tdm=False, read="unknown")
allowed, lacking = rights.check(
{"uses": ["tdm", "read", "redistribute"]}, "local", uses
)
assert allowed is False
assert lacking == ["tdm", "read?"]
def test_an_ai_stage_needs_the_purpose_and_processor_pair(self):
stage = {"ai": {"purpose": "external"}}
assert rights.check(stage, "tenancy", self.ALL_TRUE) == (True, [])
uses = dict(self.ALL_TRUE, **{"ai/external/tenancy": False})
assert rights.check(stage, "tenancy", uses) == (False, ["ai/external/tenancy"])
uses = dict(self.ALL_TRUE, **{"ai/external/tenancy": "unknown"})
assert rights.check(stage, "tenancy", uses) == (False, ["ai/external/tenancy?"])
def test_a_processor_the_stage_does_not_allow_is_lacking_before_its_pair(self):
stage = {"ai": {"purpose": "internal", "processors": ["local", "tenancy"]}}
uses = dict(self.ALL_TRUE, **{"ai/internal/outside": "unknown"})
assert rights.check(stage, "outside", uses) == (
False,
["processor:outside", "ai/internal/outside?"],
)
assert rights.check(stage, "local", uses) == (True, [])
def test_an_empty_list_of_allowed_processors_allows_any(self):
stage = {"ai": {"purpose": "internal", "processors": []}}
assert rights.check(stage, "outside", self.ALL_TRUE) == (True, [])
def test_lacking_lists_uses_before_the_ai_pair(self):
stage = {"uses": ["tdm"], "ai": {"purpose": "internal"}}
uses = dict(self.ALL_TRUE, tdm="unknown", **{"ai/internal/local": False})
assert rights.check(stage, "local", uses)[1] == ["tdm?", "ai/internal/local"]
class TestBackendAllowed:
CAP: ClassVar[dict] = {"id": "cap", "kind": "revenue-cap", "usd": 100}
COMPETE: ClassVar[dict] = {
"id": "rival",
"kind": "no-competition",
"vendor": "acme",
}
PURPOSE: ClassVar[dict] = {
"id": "why",
"kind": "purpose",
"allowed": ["research", "education"],
}
def test_a_backend_without_conditions_is_allowed_to_anyone(self):
assert rights.backend_allowed({}, {}) == (True, [])
assert rights.backend_allowed(
{"conditions": []}, {"purpose": "commercial"}
) == (
True,
[],
)
@pytest.mark.parametrize(
("profile", "passes"),
[
({"revenue_or_funding_usd": {"at_most": 100}}, True),
({"revenue_or_funding_usd": {"at_most": 99}}, True),
({"revenue_or_funding_usd": {"at_most": 101}}, False),
({"revenue_or_funding_usd": {"at_most": None}}, False),
({"revenue_or_funding_usd": {}}, False),
({"revenue_or_funding_usd": None}, False),
({}, False),
],
)
def test_a_revenue_cap_needs_a_known_bound_within_it(self, profile, passes):
assert rights.backend_allowed({"conditions": [self.CAP]}, profile) == (
(True, []) if passes else (False, ["cap"])
)
@pytest.mark.parametrize(
("competes_with", "passes"),
[
([], True),
(["other"], True),
(["other", "acme"], False),
(["acme"], False),
(None, False),
],
)
def test_a_no_competition_condition_needs_a_known_list_without_the_vendor(
self, competes_with, passes
):
profile = {"competes_with": competes_with}
assert rights.backend_allowed({"conditions": [self.COMPETE]}, profile) == (
(True, []) if passes else (False, ["rival"])
)
assert rights.backend_allowed({"conditions": [self.COMPETE]}, {}) == (
False,
["rival"],
)
@pytest.mark.parametrize(
("purpose", "passes"),
[("research", True), ("education", True), ("commercial", False), (None, False)],
)
def test_a_purpose_condition_needs_a_listed_purpose(self, purpose, passes):
assert rights.backend_allowed(
{"conditions": [self.PURPOSE]}, {"purpose": purpose}
) == ((True, []) if passes else (False, ["why"]))
def test_a_condition_of_an_unknown_kind_always_fails(self):
backend = {"conditions": [{"id": "novel", "kind": "from-the-future"}]}
assert rights.backend_allowed(backend, {"purpose": "research"}) == (
False,
["novel"],
)
def test_every_failed_condition_is_listed_in_order(self):
backend = {"conditions": [self.PURPOSE, self.CAP, self.COMPETE]}
profile = {"purpose": "commercial", "revenue_or_funding_usd": {"at_most": 5}}
assert rights.backend_allowed(backend, profile) == (False, ["why", "rival"])