Skip to content

Commit a5ee989

Browse files
authored
warehouse, tests: devolve oidc.models (#13553)
* warehouse, tests: devolve `oidc.models` This turns `oidc.models` into a directory, with `models.base` for the basic OIDC publisher classes and adjacent modules for each specific OIDC implementation (currently just GitHub). This will make adding additional OIDC publishers easier and cleaner, as each will live in its own dedicated submodule. Signed-off-by: William Woodruff <[email protected]> * tests: devolve OIDC model tests Signed-off-by: William Woodruff <[email protected]> * warehouse, tests: `models.base -> models._core` Signed-off-by: William Woodruff <[email protected]> --------- Signed-off-by: William Woodruff <[email protected]>
1 parent 26e6424 commit a5ee989

File tree

5 files changed

+288
-233
lines changed

5 files changed

+288
-233
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
import pretend
14+
15+
from warehouse.oidc.models import _core
16+
17+
18+
def test_check_claim_binary():
19+
wrapped = _core._check_claim_binary(str.__eq__)
20+
21+
assert wrapped("foo", "bar", pretend.stub()) is False
22+
assert wrapped("foo", "foo", pretend.stub()) is True
23+
24+
25+
class TestOIDCPublisher:
26+
def test_oidc_publisher_not_default_verifiable(self):
27+
publisher = _core.OIDCPublisher(projects=[])
28+
29+
assert not publisher.verify_claims(signed_claims={})

tests/unit/oidc/test_models.py renamed to tests/unit/oidc/models/test_github.py

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,17 @@
1414
import pytest
1515

1616
from tests.common.db.oidc import GitHubPublisherFactory, PendingGitHubPublisherFactory
17-
from warehouse.oidc import models
18-
19-
20-
def test_check_claim_binary():
21-
wrapped = models._check_claim_binary(str.__eq__)
22-
23-
assert wrapped("foo", "bar", pretend.stub()) is False
24-
assert wrapped("foo", "foo", pretend.stub()) is True
17+
from warehouse.oidc.models import _core, github
2518

2619

2720
@pytest.mark.parametrize("claim", ["", "repo", "repo:"])
2821
def test_check_sub(claim):
29-
assert models._check_sub(pretend.stub(), claim, pretend.stub()) is False
30-
31-
32-
class TestOIDCPublisher:
33-
def test_oidc_publisher_not_default_verifiable(self):
34-
publisher = models.OIDCPublisher(projects=[])
35-
36-
assert not publisher.verify_claims(signed_claims={})
22+
assert github._check_sub(pretend.stub(), claim, pretend.stub()) is False
3723

3824

3925
class TestGitHubPublisher:
4026
def test_github_publisher_all_known_claims(self):
41-
assert models.GitHubPublisher.all_known_claims() == {
27+
assert github.GitHubPublisher.all_known_claims() == {
4228
# verifiable claims
4329
"sub",
4430
"repository",
@@ -78,7 +64,7 @@ def test_github_publisher_all_known_claims(self):
7864
}
7965

8066
def test_github_publisher_computed_properties(self):
81-
publisher = models.GitHubPublisher(
67+
publisher = github.GitHubPublisher(
8268
repository_name="fakerepo",
8369
repository_owner="fakeowner",
8470
repository_owner_id="fakeid",
@@ -93,7 +79,7 @@ def test_github_publisher_computed_properties(self):
9379
assert publisher.publisher_url == "https://github.com/fakeowner/fakerepo"
9480

9581
def test_github_publisher_unaccounted_claims(self, monkeypatch):
96-
publisher = models.GitHubPublisher(
82+
publisher = github.GitHubPublisher(
9783
repository_name="fakerepo",
9884
repository_owner="fakeowner",
9985
repository_owner_id="fakeid",
@@ -109,12 +95,12 @@ def test_github_publisher_unaccounted_claims(self, monkeypatch):
10995
)
11096
),
11197
)
112-
monkeypatch.setattr(models, "sentry_sdk", sentry_sdk)
98+
monkeypatch.setattr(_core, "sentry_sdk", sentry_sdk)
11399

114100
# We don't care if these actually verify, only that they're present.
115101
signed_claims = {
116102
claim_name: "fake"
117-
for claim_name in models.GitHubPublisher.all_known_claims()
103+
for claim_name in github.GitHubPublisher.all_known_claims()
118104
}
119105
signed_claims["fake-claim"] = "fake"
120106
signed_claims["another-fake-claim"] = "also-fake"
@@ -128,7 +114,7 @@ def test_github_publisher_unaccounted_claims(self, monkeypatch):
128114
assert scope.fingerprint == ["another-fake-claim", "fake-claim"]
129115

130116
def test_github_publisher_missing_claims(self, monkeypatch):
131-
publisher = models.GitHubPublisher(
117+
publisher = github.GitHubPublisher(
132118
repository_name="fakerepo",
133119
repository_owner="fakeowner",
134120
repository_owner_id="fakeid",
@@ -144,11 +130,11 @@ def test_github_publisher_missing_claims(self, monkeypatch):
144130
)
145131
),
146132
)
147-
monkeypatch.setattr(models, "sentry_sdk", sentry_sdk)
133+
monkeypatch.setattr(_core, "sentry_sdk", sentry_sdk)
148134

149135
signed_claims = {
150136
claim_name: "fake"
151-
for claim_name in models.GitHubPublisher.all_known_claims()
137+
for claim_name in github.GitHubPublisher.all_known_claims()
152138
}
153139
# Pop the first signed claim, so that it's the first one to fail.
154140
signed_claims.pop("sub")
@@ -161,7 +147,7 @@ def test_github_publisher_missing_claims(self, monkeypatch):
161147
assert scope.fingerprint == ["sub"]
162148

163149
def test_github_publisher_missing_optional_claims(self, monkeypatch):
164-
publisher = models.GitHubPublisher(
150+
publisher = github.GitHubPublisher(
165151
repository_name="fakerepo",
166152
repository_owner="fakeowner",
167153
repository_owner_id="fakeid",
@@ -170,11 +156,11 @@ def test_github_publisher_missing_optional_claims(self, monkeypatch):
170156
)
171157

172158
sentry_sdk = pretend.stub(capture_message=pretend.call_recorder(lambda s: None))
173-
monkeypatch.setattr(models, "sentry_sdk", sentry_sdk)
159+
monkeypatch.setattr(_core, "sentry_sdk", sentry_sdk)
174160

175161
signed_claims = {
176162
claim_name: getattr(publisher, claim_name)
177-
for claim_name in models.GitHubPublisher.__required_verifiable_claims__
163+
for claim_name in github.GitHubPublisher.__required_verifiable_claims__
178164
}
179165
signed_claims["ref"] = "ref"
180166
signed_claims["job_workflow_ref"] = publisher.job_workflow_ref + "@ref"
@@ -185,10 +171,10 @@ def test_github_publisher_missing_optional_claims(self, monkeypatch):
185171
@pytest.mark.parametrize("environment", [None, "some-environment"])
186172
@pytest.mark.parametrize(
187173
"missing_claims",
188-
[set(), models.GitHubPublisher.__optional_verifiable_claims__.keys()],
174+
[set(), github.GitHubPublisher.__optional_verifiable_claims__.keys()],
189175
)
190176
def test_github_publisher_verifies(self, monkeypatch, environment, missing_claims):
191-
publisher = models.GitHubPublisher(
177+
publisher = github.GitHubPublisher(
192178
repository_name="fakerepo",
193179
repository_owner="fakeowner",
194180
repository_owner_id="fakeid",
@@ -214,7 +200,7 @@ def test_github_publisher_verifies(self, monkeypatch, environment, missing_claim
214200

215201
signed_claims = {
216202
claim_name: "fake"
217-
for claim_name in models.GitHubPublisher.all_known_claims()
203+
for claim_name in github.GitHubPublisher.all_known_claims()
218204
if claim_name not in missing_claims
219205
}
220206
assert publisher.verify_claims(signed_claims=signed_claims)
@@ -271,14 +257,14 @@ def test_github_publisher_verifies(self, monkeypatch, environment, missing_claim
271257
],
272258
)
273259
def test_github_publisher_job_workflow_ref(self, claim, ref, valid):
274-
publisher = models.GitHubPublisher(
260+
publisher = github.GitHubPublisher(
275261
repository_name="bar",
276262
repository_owner="foo",
277263
repository_owner_id=pretend.stub(),
278264
workflow_filename="baz.yml",
279265
)
280266

281-
check = models.GitHubPublisher.__required_verifiable_claims__[
267+
check = github.GitHubPublisher.__required_verifiable_claims__[
282268
"job_workflow_ref"
283269
]
284270
assert check(publisher.job_workflow_ref, claim, {"ref": ref}) is valid
@@ -294,7 +280,7 @@ def test_github_publisher_job_workflow_ref(self, claim, ref, valid):
294280
],
295281
)
296282
def test_github_publisher_sub_claim(self, truth, claim, valid):
297-
check = models.GitHubPublisher.__required_verifiable_claims__["sub"]
283+
check = github.GitHubPublisher.__required_verifiable_claims__["sub"]
298284
assert check(truth, claim, pretend.stub()) is valid
299285

300286
@pytest.mark.parametrize(
@@ -309,15 +295,15 @@ def test_github_publisher_sub_claim(self, truth, claim, valid):
309295
],
310296
)
311297
def test_github_publisher_environment_claim(self, truth, claim, valid):
312-
check = models.GitHubPublisher.__optional_verifiable_claims__["environment"]
298+
check = github.GitHubPublisher.__optional_verifiable_claims__["environment"]
313299
assert check(truth, claim, pretend.stub()) is valid
314300

315301

316302
class TestPendingGitHubPublisher:
317303
def test_reify_does_not_exist_yet(self, db_request):
318304
pending_publisher = PendingGitHubPublisherFactory.create()
319305
assert (
320-
db_request.db.query(models.GitHubPublisher)
306+
db_request.db.query(github.GitHubPublisher)
321307
.filter_by(
322308
repository_name=pending_publisher.repository_name,
323309
repository_owner=pending_publisher.repository_owner,
@@ -332,7 +318,7 @@ def test_reify_does_not_exist_yet(self, db_request):
332318

333319
# If an OIDC publisher for this pending publisher does not already exist,
334320
# a new one is created and the pending publisher is marked for deletion.
335-
assert isinstance(publisher, models.GitHubPublisher)
321+
assert isinstance(publisher, github.GitHubPublisher)
336322
assert pending_publisher in db_request.db.deleted
337323
assert publisher.repository_name == pending_publisher.repository_name
338324
assert publisher.repository_owner == pending_publisher.repository_owner

warehouse/oidc/models/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
from warehouse.oidc.models._core import OIDCPublisher, PendingOIDCPublisher
14+
from warehouse.oidc.models.github import GitHubPublisher, PendingGitHubPublisher
15+
16+
__all__ = [
17+
"OIDCPublisher",
18+
"PendingOIDCPublisher",
19+
"PendingGitHubPublisher",
20+
"GitHubPublisher",
21+
]

0 commit comments

Comments
 (0)