"""The gates adoption must not let through, and the evidence it must not invent. Every case here comes from running `adopt` against two real repositories. Each one passed before the fix and reported success on a repository that had live credentials in git, no confirmed owner, a name taken from the clone directory, and a contract that described almost none of its content. """ from __future__ import annotations import subprocess from pathlib import Path import pytest from stewardship import adoption BASE = {"README.md": "# Sample\n"} def _owners_plan(repo, **kwargs): assessment = adoption.assess(repo) return adoption.build_plan( repo, assessment, confirmed_name="sample", confirmed_owners=["platform"], **kwargs ) def _codes(findings, code): return [f for f in findings if f["code"] == code] # --- 1. secret classes ------------------------------------------------------ def test_document_about_secrets_is_a_warning_not_a_blocker(synthetic_repo) -> None: """A document explaining a placeholder convention carries no credential.""" repo = synthetic_repo( { **BASE, "docs/SECRETS.md": ( "# Secrets convention\n\n" "Profiles carry `` tokens instead of real credentials.\n" "The AD-join password was removed from every profile.\n" ), } ) findings = adoption.assess(repo)["findings"] assert _codes(findings, "ADOPT_SECRET_NAME_SHAPED") assert not [f for f in findings if f["severity"] == "error"] def test_live_credential_blocks_and_never_echoes_the_value(synthetic_repo) -> None: repo = synthetic_repo( {**BASE, "stack/.env": "TZ=Europe/Moscow\nPOSTGRES_PASSWORD=hunter2-live-value\n"} ) findings = adoption.assess(repo)["findings"] verified = _codes(findings, "ADOPT_SECRET_CONTENT_VERIFIED") assert [f["severity"] for f in verified] == ["error"] assert "POSTGRES_PASSWORD" in verified[0]["message"] # Invariant: a scanner that leaks what it found is worse than none. assert "hunter2-live-value" not in verified[0]["message"] @pytest.mark.parametrize( ("line", "is_credential"), [ ("AIRFLOW_FERNET_KEY=Rabc123XYZ", True), ("OLLAMA_API_KEY=sk-live-1", True), ("DB_PASSPHRASE=s3kr3t", True), # "KEY" as a bare substring cannot tell a credential from a file path. ("SSH_KEY_PATH=/home/u/.ssh/id", False), ("KEYBOARD_LAYOUT=us", False), ("BYPASS_CACHE=true", False), ("PASSENGER_ROOT=/x", False), ("TZ=Europe/Moscow", False), # Placeholders, in the three shapes real repositories use. ("POSTGRES_PASSWORD=changeme", False), ("DB_PASSWORD=", False), ("API_TOKEN=${API_TOKEN}", False), ], ) def test_key_pattern_precision(tmp_path: Path, line: str, is_credential: bool) -> None: """Which key names count, and which values read as placeholders. Exercised against the content check directly: this is a pure decision about a single line, and building a git repository per case bought only runtime. """ env = tmp_path / ".env" env.write_text(line + "\n", encoding="utf-8") assert bool(adoption._secret_content_evidence(env)) is is_credential def test_pem_private_key_is_content_evidence(synthetic_repo) -> None: repo = synthetic_repo( {**BASE, "certs/server.key": "-----BEGIN RSA PRIVATE KEY-----\nAAAA\n-----END RSA PRIVATE KEY-----\n"} ) findings = adoption.assess(repo)["findings"] assert _codes(findings, "ADOPT_SECRET_CONTENT_VERIFIED") def test_file_committed_against_its_own_ignore_rule_is_reported(synthetic_repo) -> None: """The fact worth reporting most, and the one the filename check missed.""" repo = synthetic_repo({**BASE, ".gitignore": "**/.env\n", "stack/.env": "TZ=Europe/Moscow\n"}, git=False) subprocess.run(["git", "init", "-q"], cwd=repo, check=True) subprocess.run(["git", "config", "user.email", "t@example.com"], cwd=repo, check=True) subprocess.run(["git", "config", "user.name", "T"], cwd=repo, check=True) subprocess.run(["git", "add", "-A", "-f"], cwd=repo, check=True) subprocess.run(["git", "commit", "-q", "-m", "base"], cwd=repo, check=True) findings = adoption.assess(repo)["findings"] contradiction = _codes(findings, "ADOPT_SECRET_IGNORED_BUT_TRACKED") assert [f["path"] for f in contradiction] == ["stack/.env"] assert contradiction[0]["severity"] == "warning" # --- 2. plan inherits findings and refuses on unresolved blockers ----------- def test_plan_refuses_while_an_assess_blocker_is_unresolved(synthetic_repo) -> None: repo = synthetic_repo({**BASE, "stack/.env": "POSTGRES_PASSWORD=hunter2-live-value\n"}) with pytest.raises(adoption.AdoptionError) as excinfo: _owners_plan(repo) assert excinfo.value.code == "ADOPT_ASSESS_BLOCKERS_UNRESOLVED" def test_acknowledgement_unblocks_and_is_recorded_in_the_plan(synthetic_repo) -> None: repo = synthetic_repo({**BASE, "stack/.env": "POSTGRES_PASSWORD=hunter2-live-value\n"}) plan = _owners_plan( repo, acknowledged=[ { "code": "ADOPT_SECRET_CONTENT_VERIFIED", "path": "stack/.env", "reason": "rotation scheduled", } ], ) assert plan["acknowledgedFindings"][0]["reason"] == "rotation scheduled" assert any(f["code"] == "ADOPT_SECRET_CONTENT_VERIFIED" for f in plan["findings"]) def test_acknowledgement_changes_the_plan_hash(synthetic_repo) -> None: """Approval must cover the acknowledgement, not just the file changes.""" plain = synthetic_repo({**BASE}, name="plain") without = _owners_plan(plain) with_ack = _owners_plan( plain, acknowledged=[{"code": "ADOPT_WORKTREE_DIRTY", "reason": "known"}] ) assert without["planHash"] != with_ack["planHash"] def test_acknowledgement_without_a_reason_is_refused(synthetic_repo) -> None: repo = synthetic_repo({**BASE}) with pytest.raises(adoption.AdoptionError) as excinfo: _owners_plan(repo, acknowledged=[{"code": "ADOPT_SECRET_CONTENT_VERIFIED", "reason": " "}]) assert excinfo.value.code == "ADOPT_ACKNOWLEDGEMENT_INCOMPLETE" def test_plan_carries_the_assessment_findings(synthetic_repo) -> None: repo = synthetic_repo({**BASE, "docs/guide.md": "# G\n"}) plan = _owners_plan(repo) assert any(f["code"] == "ADOPT_SOURCE_MIRROR_AMBIGUOUS" for f in plan["findings"]) # The plan's own answer settles this one, so it must not travel onward. assert not any(f["code"] == "ADOPT_OWNER_UNKNOWN" for f in plan["findings"]) # --- 3. identity and owners ------------------------------------------------- def test_plan_refuses_without_confirmed_owners(synthetic_repo) -> None: repo = synthetic_repo({**BASE}) assessment = adoption.assess(repo) with pytest.raises(adoption.AdoptionError) as excinfo: adoption.build_plan(repo, assessment, confirmed_name="sample", confirmed_owners=[]) assert excinfo.value.code == "ADOPT_OWNER_UNKNOWN" def test_name_comes_from_the_remote_keeping_its_namespace(synthetic_repo) -> None: repo = synthetic_repo({**BASE}, name="whatever-this-clone-is-called") subprocess.run( ["git", "remote", "add", "origin", "ssh://git@example.com:9922/corsus-it/OpTools/LXD.git"], cwd=repo, check=True, ) identity = adoption.assess(repo)["identity"] assert identity == {"name": "optools-lxd", "source": "git-remote"} def test_scp_style_remote_is_understood(synthetic_repo) -> None: repo = synthetic_repo({**BASE}, name="clone") subprocess.run(["git", "remote", "add", "origin", "git@example.com:group/Repo.git"], cwd=repo, check=True) assert adoption.assess(repo)["identity"]["name"] == "group-repo" def test_directory_fallback_says_it_was_inferred(synthetic_repo) -> None: repo = synthetic_repo({**BASE}, name="fallback-repo") assessment = adoption.assess(repo) assert assessment["identity"] == {"name": "fallback-repo", "source": "directory"} assert _codes(assessment["findings"], "ADOPT_NAME_INFERRED") # --- 4. content roots ------------------------------------------------------- def test_docs_is_proposed_for_an_infrastructure_repository(synthetic_repo) -> None: """`docs` is listed as evidence for `project`, which used to hide it here.""" repo = synthetic_repo({**BASE, "profiles-source/a.yaml": "{}\n", "docs/guide.md": "# G\n"}) assessment = adoption.assess(repo) assert assessment["classification"]["proposedType"] == "infrastructure-config" assert "docs" in {cr["path"] for cr in assessment["proposedContentRoots"]} def test_sibling_stacks_become_one_file_set_root(synthetic_repo) -> None: files = {**BASE} for stack in ("grafana", "loki", "n8n"): files[f"{stack}/compose.yaml"] = "services: {}\n" repo = synthetic_repo(files) roots = adoption.assess(repo)["proposedContentRoots"] stacks = [cr for cr in roots if cr.get("kind") == "file-set"] assert len(stacks) == 1 assert stacks[0]["path"] == "." and stacks[0]["selector"] == "*/compose.y*ml" def test_two_siblings_are_not_a_repeated_shape(synthetic_repo) -> None: files = {**BASE} for stack in ("grafana", "loki"): files[f"{stack}/compose.yaml"] = "services: {}\n" repo = synthetic_repo(files) assert not [cr for cr in adoption.assess(repo)["proposedContentRoots"] if cr.get("kind") == "file-set"] def test_file_set_root_does_not_forbid_the_whole_repository(synthetic_repo) -> None: files = {**BASE} for stack in ("grafana", "loki", "n8n"): files[f"{stack}/compose.yaml"] = "services: {}\n" repo = synthetic_repo(files) plan = _owners_plan(repo) assert "./**" not in plan["forbiddenChanges"] assert "*/compose.y*ml" in plan["forbiddenChanges"] # The plan must still be able to write what it authors. written = {c["path"] for c in plan["changes"] if c["action"] == "create"} assert not any(adoption.is_forbidden(path, plan["forbiddenChanges"]) for path in written) def test_defaulted_authority_asks_instead_of_asserting(synthetic_repo) -> None: repo = synthetic_repo({**BASE, "docs/guide.md": "# G\n"}) assessment = adoption.assess(repo) assert _codes(assessment["findings"], "ADOPT_SOURCE_MIRROR_AMBIGUOUS") def test_evidenced_mirror_is_not_ambiguous(synthetic_repo) -> None: """`profiles` paired with `profiles-source` is derived, not defaulted.""" repo = synthetic_repo({**BASE, "profiles/a.yaml": "{}\n", "profiles-source/a.yaml": "{}\n"}) assessment = adoption.assess(repo) mirror = next(cr for cr in assessment["proposedContentRoots"] if cr["path"] == "profiles") assert mirror["authority"] == "mirror" ambiguous = {f["path"] for f in _codes(assessment["findings"], "ADOPT_SOURCE_MIRROR_AMBIGUOUS")} assert "profiles" not in ambiguous # --- 5. markdown links are not found inside code ---------------------------- def test_link_shaped_code_is_not_a_link(tmp_path: Path) -> None: from stewardship.markdown import check_markdown_file (tmp_path / "CLAUDE.md").write_text( "# Doc\n\n" "Cast explicitly: `[string]([IO.File]::ReadAllText($path))`.\n\n" "```powershell\n" "see [also](nowhere/missing.md)\n" "```\n", encoding="utf-8", ) assert check_markdown_file(tmp_path, "CLAUDE.md") == [] def test_a_real_broken_link_is_still_reported(tmp_path: Path) -> None: from stewardship.markdown import check_markdown_file (tmp_path / "CLAUDE.md").write_text("# Doc\n\nSee [x](docs/missing.md).\n", encoding="utf-8") codes = [f.code for f in check_markdown_file(tmp_path, "CLAUDE.md")] assert codes == ["MARKDOWN_LINK_TARGET_MISSING"] # --- path confinement: "." is a whole path, never a segment ------------------ def test_repository_root_is_a_legal_content_root_path() -> None: from stewardship.paths import normalize_resource_path assert normalize_resource_path(".", allow_root=True) == "." @pytest.mark.parametrize( ("value", "allow_root"), [(".", False), ("a/./b", True), ("../escape", True), ("a/../..", True)], ) def test_dot_segments_stay_rejected(value: str, allow_root: bool) -> None: from stewardship.paths import PathViolation, normalize_resource_path with pytest.raises(PathViolation): normalize_resource_path(value, allow_root=allow_root)