from __future__ import annotations import hashlib import importlib.util import unittest from pathlib import Path from unittest.mock import patch ROOT = Path(__file__).resolve().parents[2] SCRIPT = ROOT / "scripts" / "ci-release-resume.py" spec = importlib.util.spec_from_file_location("ci_release_resume", SCRIPT) assert spec and spec.loader resume = importlib.util.module_from_spec(spec) spec.loader.exec_module(resume) TAG = "v0.4.0-rc.15" SHA = "29ee01b6efced04b027569fbeed2e865991c8f55" FILES = { f"crank-community-admin-api-{TAG}.tar.gz": b"admin archive", f"crank-community-mcp-server-{TAG}.tar.gz": b"mcp archive", f"crank-community-ui-{TAG}.tar.gz": b"ui archive", f"crank-community-{TAG}-checksums.txt": b"", f"crank-community-{TAG}-sbom.spdx.json": b"{}", } def payloads(): files = dict(FILES) archives = [name for name in files if name.endswith(".tar.gz")] files[f"crank-community-{TAG}-checksums.txt"] = "".join( f"{hashlib.sha256(files[name]).hexdigest()} dist/{name}\n" for name in archives ).encode() manifest = "".join( f"{hashlib.sha256(data).hexdigest()} {name}\n" for name, data in files.items() ).encode() return files, manifest class ResumeValidationTests(unittest.TestCase): def setUp(self): self.env = { "GITEA_SERVER_URL": "https://git.itexp.me", "GITEA_REPOSITORY": "bsodfather/crank-community", "GITEA_TOKEN": "test-token", "RELEASE_TAG": TAG, "RESUME_SOURCE_SHA": SHA, "GITHUB_EVENT_NAME": "workflow_dispatch", "GITHUB_REF": "refs/heads/main", } self.files, self.manifest = payloads() self.calls = [] def api(self, _opener, method, url, token, payload=None, allow_404=False): self.calls.append((method, url, payload)) if url.endswith("/releases/tags/" + TAG): return ({"id": 426, "tag_name": TAG, "draft": True}, 200) if url.endswith("/tags/" + TAG): return ({"name": TAG, "commit": {"sha": SHA}}, 200) if url.endswith("/commits/" + SHA + "/status"): contexts = {"CI / Required Checks (push)", "CI / Rust Checks (push)", "CI / UI Checks (push)"} return ({ "state": "failure", "statuses": [ {"context": context, "status": "success", "updated_at": "2026-11-07T10:00:00Z"} for context in contexts ] + [ {"context": "Community Release (push)", "status": "failure", "updated_at": "2026-11-07T10:01:00Z"} ], }, 200) raise AssertionError(f"Unexpected API URL: {url}") def assets(self, _api, release_id, token, opener): self.assertEqual(release_id, 426) result = {} for name, data in {**self.files, "SHA256SUMS": self.manifest}.items(): result[name] = {"name": name, "id": len(result) + 1, "size": len(data), "browser_download_url": f"https://git.itexp.me/releases/download/{name}"} return result def download(self, origin, item, token, opener): self.assertEqual(origin, "https://git.itexp.me") return self.files.get(item["name"], self.manifest) def run_verify(self): with ( patch.object(resume, "_inputs", return_value=("https://git.itexp.me", "https://git.itexp.me/api/v1/repos/bsodfather/crank-community", TAG, "test-token")), patch.object(resume, "_check_source") as check_source, patch.object(resume, "_json_request", side_effect=self.api), patch.object(resume, "_assets", side_effect=self.assets), patch.object(resume, "_download", side_effect=self.download), ): result = resume.verify(self.env, opener=object()) check_source.assert_called_once_with(TAG, SHA, self.env) return result def test_valid_draft_is_read_only_and_outputs_source_receipt(self): result = self.run_verify() self.assertEqual(result["release_id"], 426) self.assertEqual(result["source_sha"], SHA) self.assertEqual(set(result["assets"]), set(self.files)) self.assertTrue(self.calls) self.assertTrue(all(call[0] == "GET" and call[2] is None for call in self.calls)) def test_missing_release_rejected(self): with patch.object(resume, "_inputs", return_value=("https://git.itexp.me", "https://api", TAG, "token")), \ patch.object(resume, "_check_source"), \ patch.object(resume, "_json_request", return_value=(None, 404)): with self.assertRaisesRegex(resume.PublishError, "does not already exist"): resume.verify(self.env, opener=object()) def test_source_mismatch_rejected_before_api(self): with patch.object(resume, "_inputs", return_value=("https://git.itexp.me", "https://api", TAG, "token")), \ patch.object(resume, "_check_source", side_effect=resume.PublishError("tag mismatch")), \ patch.object(resume, "_json_request") as request: with self.assertRaisesRegex(resume.PublishError, "tag mismatch"): resume.verify(self.env, opener=object()) request.assert_not_called() def test_api_tag_commit_mismatch_rejected(self): with patch.object(resume, "_inputs", return_value=("https://git.itexp.me", "https://api", TAG, "token")), \ patch.object(resume, "_check_source"), \ patch.object(resume, "_json_request", side_effect=self.api), \ patch.object(resume, "_api_tag_commit", return_value="0" * 40): with self.assertRaisesRegex(resume.PublishError, "Gitea tag does not point"): resume.verify(self.env, opener=object()) def test_unexpected_asset_set_rejected_before_download(self): assets = self.assets(None, 426, "token", None) assets["unexpected.txt"] = { "name": "unexpected.txt", "id": 99, "size": 0, "browser_download_url": "https://git.itexp.me/unexpected.txt", } with patch.object(resume, "_inputs", return_value=("https://git.itexp.me", "https://api", TAG, "token")), \ patch.object(resume, "_check_source"), \ patch.object(resume, "_json_request", side_effect=self.api), \ patch.object(resume, "_api_tag_commit", return_value=SHA), \ patch.object(resume, "_assets", return_value=assets), \ patch.object(resume, "_download") as download: with self.assertRaisesRegex(resume.PublishError, "exactly the five expected"): resume.verify(self.env, opener=object()) download.assert_not_called() def test_required_source_status_missing_or_failed_is_rejected(self): cases = ( {"state": "failure", "statuses": []}, {"state": "failure", "statuses": [ {"context": "CI / Required Checks (push)", "status": "failure", "updated_at": "2026-11-07T10:00:00Z"}, {"context": "CI / Rust Checks (push)", "status": "success", "updated_at": "2026-11-07T10:00:00Z"}, {"context": "CI / UI Checks (push)", "status": "success", "updated_at": "2026-11-07T10:00:00Z"}, ]}, {"state": "failure", "statuses": [ {"context": "CI / Required Checks (push)", "status": "success", "updated_at": "2026-11-07T09:00:00Z"}, {"context": "CI / Required Checks (push)", "status": "failure", "updated_at": "2026-11-07T10:00:00Z"}, {"context": "CI / Rust Checks (push)", "status": "success", "updated_at": "2026-11-07T10:00:00Z"}, {"context": "CI / UI Checks (push)", "status": "success", "updated_at": "2026-11-07T10:00:00Z"}, ]}, ) for status_data in cases: with self.subTest(status_data=status_data): with self.assertRaises(resume.PublishError): resume._require_source_ci(status_data, 200) def test_missing_expected_asset_rejected(self): files = dict(self.files) files.pop(f"crank-community-{TAG}-sbom.spdx.json") with self.assertRaisesRegex(resume.PublishError, "missing an expected"): resume._verify_payloads(files, self.manifest, TAG) def test_checksum_manifest_rejects_missing_or_bad_payload_entries(self): for manifest in (b"", b"0" * 64 + b" wrong.tar.gz\n"): with self.subTest(manifest=manifest): with self.assertRaises(resume.PublishError): resume._verify_payloads(self.files, manifest, TAG) def test_checksum_manifest_rejects_mismatched_archive_checksum(self): files = dict(self.files) checksum_name = f"crank-community-{TAG}-checksums.txt" files[checksum_name] = b"0" * 64 + b" crank-community-admin-api-" + TAG.encode() + b".tar.gz\n" updated_manifest = "".join( f"{hashlib.sha256(data).hexdigest()} {name}\n" for name, data in files.items() ).encode() with self.assertRaisesRegex(resume.PublishError, "checksums file"): resume._verify_payloads(files, updated_manifest, TAG) def test_download_rejects_untrusted_origin_before_network(self): item = {"name": "payload", "size": 1, "browser_download_url": "https://evil.example/payload"} opener = unittest.mock.Mock() with self.assertRaisesRegex(resume.PublishError, "untrusted download URL"): resume._download("https://git.itexp.me", item, "token", opener) opener.open.assert_not_called() def test_download_rejects_redirect_without_following(self): class Redirecting: def open(self, request, timeout): raise resume.PublishError("redirect refused") item = {"name": "payload", "size": 1, "browser_download_url": "https://git.itexp.me/payload"} with self.assertRaisesRegex(resume.PublishError, "redirect refused"): resume._download("https://git.itexp.me", item, "token", Redirecting()) class WorkflowResumeTests(unittest.TestCase): def test_resume_selects_tag_source_and_skips_asset_production(self): workflow = (ROOT / ".gitea/workflows/release.yml").read_text() self.assertIn("resume_tag:", workflow) self.assertIn("resume_source_sha:", workflow) self.assertIn("IMAGE_TAG: ${{ inputs.resume_tag || gitea.ref_name }}", workflow) self.assertIn("python3 scripts/ci-release-resume.py", workflow) self.assertIn("ref: ${{ steps.resume.outputs.release_tag }}", workflow) self.assertIn('test "$(git rev-parse HEAD)" = "$EXPECTED_SOURCE_SHA"', workflow) for step in ("Build release binaries", "Build UI dist", "Package release artifacts", "Generate SBOM"): start = workflow.index(f"- name: {step}") self.assertIn("if: inputs.resume_tag == ''", workflow[start : start + 180]) self.assertIn("if: inputs.resume_tag == '' && startsWith(gitea.ref, 'refs/tags/v')", workflow) self.assertIn("if: inputs.resume_tag == ''\n run: |\n ls -lh dist", workflow) if __name__ == "__main__": unittest.main()