forked from pypi/warehouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more detailed error messages to job_workflow_ref check (pypi#14319)
- Loading branch information
Showing
2 changed files
with
87 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,54 +236,115 @@ def test_github_publisher_verifies(self, monkeypatch, environment, missing_claim | |
) | ||
|
||
@pytest.mark.parametrize( | ||
("claim", "ref", "valid"), | ||
("claim", "ref", "valid", "expected"), | ||
[ | ||
# okay: workflow name, followed by a nonempty ref | ||
( | ||
"foo/bar/.github/workflows/baz.yml@refs/tags/v0.0.1", | ||
"refs/tags/v0.0.1", | ||
True, | ||
None, | ||
), | ||
( | ||
"foo/bar/.github/workflows/baz.yml@refs/pulls/6", | ||
"refs/pulls/6", | ||
True, | ||
None, | ||
), | ||
("foo/bar/.github/workflows/baz.yml@refs/pulls/6", "refs/pulls/6", True), | ||
( | ||
"foo/bar/.github/workflows/baz.yml@refs/heads/main", | ||
"refs/heads/main", | ||
True, | ||
None, | ||
), | ||
( | ||
"foo/bar/.github/workflows/baz.yml@notrailingslash", | ||
"notrailingslash", | ||
True, | ||
None, | ||
), | ||
# bad: workflow name, empty or missing ref | ||
("foo/bar/.github/workflows/baz.yml@emptyref", "", False), | ||
("foo/bar/.github/workflows/baz.yml@missingref", None, False), | ||
( | ||
"foo/bar/.github/workflows/baz.yml@emptyref", | ||
"", | ||
False, | ||
"The ref claim is empty", | ||
), | ||
( | ||
"foo/bar/.github/workflows/baz.yml@missingref", | ||
None, | ||
False, | ||
"The ref claim is empty", | ||
), | ||
# bad: workflow name with various attempted impersonations | ||
( | ||
"foo/bar/.github/workflows/[email protected]@notrailingslash", | ||
"notrailingslash", | ||
False, | ||
"The claim does not match, expecting " | ||
"'foo/bar/.github/workflows/baz.yml@notrailingslash', got " | ||
"'foo/bar/.github/workflows/[email protected]@notrailingslash'", | ||
), | ||
( | ||
"foo/bar/.github/workflows/[email protected]@refs/pulls/6", | ||
"refs/pulls/6", | ||
False, | ||
"The claim does not match, expecting " | ||
"'foo/bar/.github/workflows/baz.yml@refs/pulls/6', got " | ||
"'foo/bar/.github/workflows/[email protected]@refs/pulls/6'", | ||
), | ||
# bad: missing tail or workflow name or otherwise partial | ||
("foo/bar/.github/workflows/baz.yml@", "notrailingslash", False), | ||
("foo/bar/.github/workflows/@", "notrailingslash", False), | ||
("foo/bar/.github/workflows/", "notrailingslash", False), | ||
("baz.yml", "notrailingslash", False), | ||
( | ||
"foo/bar/.github/workflows/baz.yml@", | ||
"notrailingslash", | ||
False, | ||
"The claim does not match, expecting " | ||
"'foo/bar/.github/workflows/baz.yml@notrailingslash', got " | ||
"'foo/bar/.github/workflows/baz.yml@'", | ||
), | ||
( | ||
"foo/bar/.github/workflows/@", | ||
"notrailingslash", | ||
False, | ||
"The claim does not match, expecting " | ||
"'foo/bar/.github/workflows/baz.yml@notrailingslash', got " | ||
"'foo/bar/.github/workflows/@'", | ||
), | ||
( | ||
"foo/bar/.github/workflows/", | ||
"notrailingslash", | ||
False, | ||
"The claim does not match, expecting " | ||
"'foo/bar/.github/workflows/baz.yml@notrailingslash', got " | ||
"'foo/bar/.github/workflows/'", | ||
), | ||
( | ||
"baz.yml", | ||
"notrailingslash", | ||
False, | ||
"The claim does not match, expecting " | ||
"'foo/bar/.github/workflows/baz.yml@notrailingslash', got 'baz.yml'", | ||
), | ||
( | ||
"foo/bar/.github/workflows/[email protected]@", | ||
"notrailingslash", | ||
False, | ||
"The claim does not match, expecting " | ||
"'foo/bar/.github/workflows/baz.yml@notrailingslash', got " | ||
"'foo/bar/.github/workflows/[email protected]@'", | ||
), | ||
( | ||
"foo/bar/.github/workflows/baz.yml@@", | ||
"notrailingslash", | ||
False, | ||
"The claim does not match, expecting " | ||
"'foo/bar/.github/workflows/baz.yml@notrailingslash', got " | ||
"'foo/bar/.github/workflows/baz.yml@@'", | ||
), | ||
("foo/bar/.github/workflows/baz.yml@@", "notrailingslash", False), | ||
("", "notrailingslash", False), | ||
("", "notrailingslash", False, "The job_workflow_ref claim is empty"), | ||
], | ||
) | ||
def test_github_publisher_job_workflow_ref(self, claim, ref, valid): | ||
def test_github_publisher_job_workflow_ref(self, claim, ref, valid, expected): | ||
publisher = github.GitHubPublisher( | ||
repository_name="bar", | ||
repository_owner="foo", | ||
|
@@ -294,7 +355,12 @@ def test_github_publisher_job_workflow_ref(self, claim, ref, valid): | |
check = github.GitHubPublisher.__required_verifiable_claims__[ | ||
"job_workflow_ref" | ||
] | ||
assert check(publisher.job_workflow_ref, claim, {"ref": ref}) is valid | ||
if valid: | ||
assert check(publisher.job_workflow_ref, claim, {"ref": ref}) is True | ||
else: | ||
with pytest.raises(errors.InvalidPublisherError) as e: | ||
check(publisher.job_workflow_ref, claim, {"ref": ref}) is True | ||
assert str(e.value) == expected | ||
|
||
@pytest.mark.parametrize( | ||
("truth", "claim", "valid"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters