|
1 | 1 | """Internal implementation tests.""" |
2 | 2 |
|
| 3 | +import json |
3 | 4 | import os |
4 | 5 | from hashlib import sha256 |
5 | 6 | from pathlib import Path |
|
8 | 9 | import pypi_attestations._impl as impl |
9 | 10 | import pytest |
10 | 11 | import sigstore |
11 | | -from pydantic import ValidationError |
| 12 | +from pydantic import TypeAdapter, ValidationError |
12 | 13 | from sigstore.dsse import DigestSet, StatementBuilder, Subject |
13 | 14 | from sigstore.models import Bundle |
14 | 15 | from sigstore.oidc import IdentityToken |
@@ -462,3 +463,73 @@ def test_ultranormalize_dist_filename(input: str, normalized: str) -> None: |
462 | 463 | def test_ultranormalize_dist_filename_invalid(input: str) -> None: |
463 | 464 | with pytest.raises(ValueError): |
464 | 465 | impl._ultranormalize_dist_filename(input) |
| 466 | + |
| 467 | + |
| 468 | +class TestPublisher: |
| 469 | + def test_discriminator(self) -> None: |
| 470 | + gh_raw = {"kind": "GitHub", "repository": "foo/bar", "workflow": "publish.yml"} |
| 471 | + gh = TypeAdapter(impl.Publisher).validate_python(gh_raw) |
| 472 | + |
| 473 | + assert isinstance(gh, impl.GitHubPublisher) |
| 474 | + assert gh.repository == "foo/bar" |
| 475 | + assert gh.workflow == "publish.yml" |
| 476 | + assert TypeAdapter(impl.Publisher).validate_json(json.dumps(gh_raw)) == gh |
| 477 | + |
| 478 | + gl_raw = {"kind": "GitLab", "repository": "foo/bar/baz", "environment": "publish"} |
| 479 | + gl = TypeAdapter(impl.Publisher).validate_python(gl_raw) |
| 480 | + assert isinstance(gl, impl.GitLabPublisher) |
| 481 | + assert gl.repository == "foo/bar/baz" |
| 482 | + assert gl.environment == "publish" |
| 483 | + assert TypeAdapter(impl.Publisher).validate_json(json.dumps(gl_raw)) == gl |
| 484 | + |
| 485 | + def test_wrong_kind(self) -> None: |
| 486 | + with pytest.raises(ValueError, match="Input should be 'GitHub'"): |
| 487 | + impl.GitHubPublisher(kind="wrong", repository="foo/bar", workflow="publish.yml") |
| 488 | + |
| 489 | + with pytest.raises(ValueError, match="Input should be 'GitLab'"): |
| 490 | + impl.GitLabPublisher(kind="GitHub", repository="foo/bar") |
| 491 | + |
| 492 | + def test_claims(self) -> None: |
| 493 | + raw = { |
| 494 | + "kind": "GitHub", |
| 495 | + "repository": "foo/bar", |
| 496 | + "workflow": "publish.yml", |
| 497 | + "claims": { |
| 498 | + "this": "is-preserved", |
| 499 | + "this-too": 123, |
| 500 | + }, |
| 501 | + } |
| 502 | + pub = TypeAdapter(impl.Publisher).validate_python(raw) |
| 503 | + |
| 504 | + assert pub.claims == { |
| 505 | + "this": "is-preserved", |
| 506 | + "this-too": 123, |
| 507 | + } |
| 508 | + |
| 509 | + |
| 510 | +class TestProvenance: |
| 511 | + def test_version(self) -> None: |
| 512 | + attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes()) |
| 513 | + provenance = impl.Provenance( |
| 514 | + attestation_bundles=[ |
| 515 | + impl.AttestationBundle( |
| 516 | + publisher=impl.GitHubPublisher(repository="foo/bar", workflow="publish.yml"), |
| 517 | + attestations=[attestation], |
| 518 | + ) |
| 519 | + ] |
| 520 | + ) |
| 521 | + assert provenance.version == 1 |
| 522 | + |
| 523 | + # Setting any other version doesn't work. |
| 524 | + with pytest.raises(ValueError): |
| 525 | + provenance = impl.Provenance( |
| 526 | + version=2, |
| 527 | + attestation_bundles=[ |
| 528 | + impl.AttestationBundle( |
| 529 | + publisher=impl.GitHubPublisher( |
| 530 | + repository="foo/bar", workflow="publish.yml" |
| 531 | + ), |
| 532 | + attestations=[attestation], |
| 533 | + ) |
| 534 | + ], |
| 535 | + ) |
0 commit comments