Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warehouse, tests: devolve oidc.models #13553

Merged
merged 3 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions tests/unit/oidc/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@

from tests.common.db.oidc import GitHubPublisherFactory, PendingGitHubPublisherFactory
from warehouse.oidc import models
from warehouse.oidc.models import base, github


def test_check_claim_binary():
wrapped = models._check_claim_binary(str.__eq__)
wrapped = models.base._check_claim_binary(str.__eq__)

assert wrapped("foo", "bar", pretend.stub()) is False
assert wrapped("foo", "foo", pretend.stub()) is True


@pytest.mark.parametrize("claim", ["", "repo", "repo:"])
def test_check_sub(claim):
assert models._check_sub(pretend.stub(), claim, pretend.stub()) is False
assert models.github._check_sub(pretend.stub(), claim, pretend.stub()) is False


class TestOIDCPublisher:
Expand Down Expand Up @@ -78,7 +79,7 @@ def test_github_publisher_all_known_claims(self):
}

def test_github_publisher_computed_properties(self):
publisher = models.GitHubPublisher(
publisher = github.GitHubPublisher(
repository_name="fakerepo",
repository_owner="fakeowner",
repository_owner_id="fakeid",
Expand All @@ -93,7 +94,7 @@ def test_github_publisher_computed_properties(self):
assert publisher.publisher_url == "https://github.com/fakeowner/fakerepo"

def test_github_publisher_unaccounted_claims(self, monkeypatch):
publisher = models.GitHubPublisher(
publisher = github.GitHubPublisher(
repository_name="fakerepo",
repository_owner="fakeowner",
repository_owner_id="fakeid",
Expand All @@ -109,12 +110,12 @@ def test_github_publisher_unaccounted_claims(self, monkeypatch):
)
),
)
monkeypatch.setattr(models, "sentry_sdk", sentry_sdk)
monkeypatch.setattr(base, "sentry_sdk", sentry_sdk)

# We don't care if these actually verify, only that they're present.
signed_claims = {
claim_name: "fake"
for claim_name in models.GitHubPublisher.all_known_claims()
for claim_name in github.GitHubPublisher.all_known_claims()
}
signed_claims["fake-claim"] = "fake"
signed_claims["another-fake-claim"] = "also-fake"
Expand All @@ -128,7 +129,7 @@ def test_github_publisher_unaccounted_claims(self, monkeypatch):
assert scope.fingerprint == ["another-fake-claim", "fake-claim"]

def test_github_publisher_missing_claims(self, monkeypatch):
publisher = models.GitHubPublisher(
publisher = github.GitHubPublisher(
repository_name="fakerepo",
repository_owner="fakeowner",
repository_owner_id="fakeid",
Expand All @@ -144,11 +145,11 @@ def test_github_publisher_missing_claims(self, monkeypatch):
)
),
)
monkeypatch.setattr(models, "sentry_sdk", sentry_sdk)
monkeypatch.setattr(base, "sentry_sdk", sentry_sdk)

signed_claims = {
claim_name: "fake"
for claim_name in models.GitHubPublisher.all_known_claims()
for claim_name in github.GitHubPublisher.all_known_claims()
}
# Pop the first signed claim, so that it's the first one to fail.
signed_claims.pop("sub")
Expand All @@ -161,7 +162,7 @@ def test_github_publisher_missing_claims(self, monkeypatch):
assert scope.fingerprint == ["sub"]

def test_github_publisher_missing_optional_claims(self, monkeypatch):
publisher = models.GitHubPublisher(
publisher = github.GitHubPublisher(
repository_name="fakerepo",
repository_owner="fakeowner",
repository_owner_id="fakeid",
Expand All @@ -170,11 +171,11 @@ def test_github_publisher_missing_optional_claims(self, monkeypatch):
)

sentry_sdk = pretend.stub(capture_message=pretend.call_recorder(lambda s: None))
monkeypatch.setattr(models, "sentry_sdk", sentry_sdk)
monkeypatch.setattr(base, "sentry_sdk", sentry_sdk)

signed_claims = {
claim_name: getattr(publisher, claim_name)
for claim_name in models.GitHubPublisher.__required_verifiable_claims__
for claim_name in github.GitHubPublisher.__required_verifiable_claims__
}
signed_claims["ref"] = "ref"
signed_claims["job_workflow_ref"] = publisher.job_workflow_ref + "@ref"
Expand All @@ -185,10 +186,10 @@ def test_github_publisher_missing_optional_claims(self, monkeypatch):
@pytest.mark.parametrize("environment", [None, "some-environment"])
@pytest.mark.parametrize(
"missing_claims",
[set(), models.GitHubPublisher.__optional_verifiable_claims__.keys()],
[set(), github.GitHubPublisher.__optional_verifiable_claims__.keys()],
)
def test_github_publisher_verifies(self, monkeypatch, environment, missing_claims):
publisher = models.GitHubPublisher(
publisher = github.GitHubPublisher(
repository_name="fakerepo",
repository_owner="fakeowner",
repository_owner_id="fakeid",
Expand All @@ -214,7 +215,7 @@ def test_github_publisher_verifies(self, monkeypatch, environment, missing_claim

signed_claims = {
claim_name: "fake"
for claim_name in models.GitHubPublisher.all_known_claims()
for claim_name in github.GitHubPublisher.all_known_claims()
if claim_name not in missing_claims
}
assert publisher.verify_claims(signed_claims=signed_claims)
Expand Down Expand Up @@ -271,14 +272,14 @@ def test_github_publisher_verifies(self, monkeypatch, environment, missing_claim
],
)
def test_github_publisher_job_workflow_ref(self, claim, ref, valid):
publisher = models.GitHubPublisher(
publisher = github.GitHubPublisher(
repository_name="bar",
repository_owner="foo",
repository_owner_id=pretend.stub(),
workflow_filename="baz.yml",
)

check = models.GitHubPublisher.__required_verifiable_claims__[
check = github.GitHubPublisher.__required_verifiable_claims__[
"job_workflow_ref"
]
assert check(publisher.job_workflow_ref, claim, {"ref": ref}) is valid
Expand All @@ -294,7 +295,7 @@ def test_github_publisher_job_workflow_ref(self, claim, ref, valid):
],
)
def test_github_publisher_sub_claim(self, truth, claim, valid):
check = models.GitHubPublisher.__required_verifiable_claims__["sub"]
check = github.GitHubPublisher.__required_verifiable_claims__["sub"]
assert check(truth, claim, pretend.stub()) is valid

@pytest.mark.parametrize(
Expand All @@ -309,15 +310,15 @@ def test_github_publisher_sub_claim(self, truth, claim, valid):
],
)
def test_github_publisher_environment_claim(self, truth, claim, valid):
check = models.GitHubPublisher.__optional_verifiable_claims__["environment"]
check = github.GitHubPublisher.__optional_verifiable_claims__["environment"]
assert check(truth, claim, pretend.stub()) is valid


class TestPendingGitHubPublisher:
def test_reify_does_not_exist_yet(self, db_request):
pending_publisher = PendingGitHubPublisherFactory.create()
assert (
db_request.db.query(models.GitHubPublisher)
db_request.db.query(github.GitHubPublisher)
.filter_by(
repository_name=pending_publisher.repository_name,
repository_owner=pending_publisher.repository_owner,
Expand All @@ -332,7 +333,7 @@ def test_reify_does_not_exist_yet(self, db_request):

# If an OIDC publisher for this pending publisher does not already exist,
# a new one is created and the pending publisher is marked for deletion.
assert isinstance(publisher, models.GitHubPublisher)
assert isinstance(publisher, github.GitHubPublisher)
assert pending_publisher in db_request.db.deleted
assert publisher.repository_name == pending_publisher.repository_name
assert publisher.repository_owner == pending_publisher.repository_owner
Expand Down
21 changes: 21 additions & 0 deletions warehouse/oidc/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from warehouse.oidc.models.base import OIDCPublisher, PendingOIDCPublisher
from warehouse.oidc.models.github import GitHubPublisher, PendingGitHubPublisher

__all__ = [
"OIDCPublisher",
"PendingOIDCPublisher",
"PendingGitHubPublisher",
"GitHubPublisher",
]
Loading