25
25
PendingActiveStatePublisher ,
26
26
)
27
27
28
- ORG_ID = "00000000-0000-1000-8000-000000000000"
29
- PROJECT_ID = "00000000-0000-1000-8000-000000000001"
30
- USER_ID = "00000000-0000-1000-8000-000000000002"
31
- BRANCH_ID = "00000000-0000-1000-8000-000000000003"
28
+ ORG_URL_NAME = "fakeorg"
29
+ PROJECT_NAME = "fakeproject"
30
+ ACTOR_ID = "00000000-0000-1000-8000-000000000002"
31
+ ACTOR = "fakeuser"
32
+ INGREDIENT = "fakeingredientname"
32
33
# This follows the format of the subject that ActiveState sends us. We don't
33
34
# validate the format when verifying the JWT. That should happen when the
34
35
# Publisher is configured. We just need to make sure that the subject matches
35
36
#
36
37
# Technically, the branch should only be present if a branch was provided in the JWT
37
38
# claims
38
- SUBJECT = "org:fake_org_id :project:fake_project_id:branch_id:fake_branch_id "
39
+ SUBJECT = f "org:{ ORG_URL_NAME } :project:{ PROJECT_NAME } "
39
40
40
41
41
42
def test_lookup_strategies ():
@@ -48,27 +49,30 @@ def test_lookup_strategies():
48
49
49
50
def new_signed_claims (
50
51
sub : str = SUBJECT ,
51
- organization_id : str = ORG_ID ,
52
- org_url_name : str = "fakeorg" ,
53
- project_id : str = PROJECT_ID ,
54
- project_name : str = "fakeproject" ,
52
+ actor : str = ACTOR ,
53
+ actor_id : str = ACTOR_ID ,
54
+ ingredient : str = INGREDIENT ,
55
+ organization : str = ORG_URL_NAME ,
56
+ org_id : str = "fakeorgid" ,
57
+ project : str = PROJECT_NAME ,
58
+ project_id : str = "fakeprojectid" ,
55
59
project_path : str = "fakeorg/fakeproject" ,
56
- user_id : str = USER_ID ,
57
60
project_visibility : str = "public" ,
58
61
branch_id : str | None = None ,
59
62
) -> SignedClaims :
60
- project_name = "fakeproject"
61
- org_url_name = "fakeorg"
62
63
claims = SignedClaims (
63
64
{
64
65
"sub" : sub ,
65
- "organization_id" : organization_id ,
66
- "organization_url_name" : org_url_name ,
66
+ "actor" : actor ,
67
+ "actor_id" : actor_id ,
68
+ "ingredient" : ingredient ,
69
+ "organization_id" : org_id ,
70
+ "organization" : organization ,
71
+ "project_visibility" : project_visibility ,
67
72
"project_id" : project_id ,
68
- "project_name" : project_name ,
69
73
"project_path" : project_path ,
70
- "user_id " : user_id ,
71
- "project_visibility " : project_visibility ,
74
+ "project " : project ,
75
+ "builder " : "pypi-publisher" ,
72
76
}
73
77
)
74
78
if branch_id :
@@ -85,9 +89,7 @@ def test_publisher_name(self):
85
89
def test_publisher_url (self ):
86
90
org_name = "fakeorg"
87
91
project_name = "fakeproject"
88
- publisher = ActiveStatePublisher (
89
- organization_url_name = org_name , activestate_project_name = project_name
90
- )
92
+ publisher = ActiveStatePublisher (organization = org_name , project = project_name )
91
93
92
94
assert (
93
95
publisher .publisher_url ()
@@ -97,9 +99,7 @@ def test_publisher_url(self):
97
99
def test_stringifies_as_project_url (self ):
98
100
org_name = "fakeorg"
99
101
project_name = "fakeproject"
100
- publisher = ActiveStatePublisher (
101
- organization_url_name = org_name , activestate_project_name = project_name
102
- )
102
+ publisher = ActiveStatePublisher (organization = org_name , project = project_name )
103
103
104
104
assert (
105
105
str (publisher )
@@ -109,9 +109,11 @@ def test_stringifies_as_project_url(self):
109
109
def test_activestate_publisher_all_known_claims (self ):
110
110
assert ActiveStatePublisher .all_known_claims () == {
111
111
# verifiable claims
112
- "organization_id" ,
113
- "project_id" ,
114
- "user_id" ,
112
+ "organization" ,
113
+ "project" ,
114
+ "actor_id" ,
115
+ "actor" ,
116
+ "builder" ,
115
117
"sub" ,
116
118
# optional verifiable claims
117
119
"branch_id" ,
@@ -123,15 +125,14 @@ def test_activestate_publisher_all_known_claims(self):
123
125
"aud" ,
124
126
# unchecked claims
125
127
"project_visibility" ,
126
- "project_name" ,
127
128
"project_path" ,
128
- "organization_url_name" ,
129
+ "ingredient" ,
130
+ "organization_id" ,
131
+ "project_id" ,
129
132
}
130
133
131
134
def test_activestate_publisher_unaccounted_claims (self , monkeypatch ):
132
- publisher = ActiveStatePublisher (
133
- sub = SUBJECT ,
134
- )
135
+ publisher = ActiveStatePublisher ()
135
136
136
137
scope = pretend .stub ()
137
138
sentry_sdk = pretend .stub (
@@ -161,24 +162,28 @@ def test_activestate_publisher_unaccounted_claims(self, monkeypatch):
161
162
@pytest .mark .parametrize (
162
163
("claim_to_drop" , "valid" ),
163
164
[
164
- ("organization_id" , False ),
165
- ("project_id" , False ),
166
- ("user_id" , False ),
167
- ("branch_id" , True ),
168
- ("organization_url_name" , True ),
165
+ ("organization" , False ),
166
+ ("project" , False ),
167
+ ("actor_id" , False ),
168
+ ("actor" , False ),
169
+ ("builder" , False ),
170
+ ("ingredient" , True ),
171
+ ("organization_id" , True ),
172
+ ("project_id" , True ),
169
173
("project_visibility" , True ),
170
- ("project_name" , True ),
171
174
("project_path" , True ),
175
+ ("branch_id" , True ),
172
176
],
173
177
)
174
178
def test_activestate_publisher_missing_claims (
175
179
self , monkeypatch , claim_to_drop : str , valid : bool
176
180
):
177
181
publisher = ActiveStatePublisher (
178
- sub = SUBJECT ,
179
- organization_id = ORG_ID ,
180
- project_id = PROJECT_ID ,
181
- user_id = USER_ID ,
182
+ organization = ORG_URL_NAME ,
183
+ project = PROJECT_NAME ,
184
+ actor_id = ACTOR_ID ,
185
+ actor = ACTOR ,
186
+ ingredient = INGREDIENT ,
182
187
)
183
188
184
189
scope = pretend .stub ()
@@ -211,128 +216,99 @@ def test_activestate_publisher_missing_claims(
211
216
@pytest .mark .parametrize (
212
217
("expect" , "actual" , "valid" ),
213
218
[
214
- (ORG_ID , ORG_ID , True ),
215
- (ORG_ID , PROJECT_ID , False ),
219
+ (ORG_URL_NAME , ORG_URL_NAME , True ),
220
+ (ORG_URL_NAME , PROJECT_NAME , False ),
216
221
],
217
222
)
218
223
def test_activestate_publisher_org_id_verified (
219
224
self , expect : str , actual : str , valid : bool
220
225
):
221
226
publisher = ActiveStatePublisher (
222
- sub = SUBJECT ,
223
- organization_id = actual ,
224
- project_id = PROJECT_ID ,
225
- user_id = USER_ID ,
227
+ organization = actual ,
228
+ project = PROJECT_NAME ,
229
+ actor_id = ACTOR_ID ,
230
+ actor = ACTOR ,
231
+ ingredient = INGREDIENT ,
226
232
)
227
233
228
- signed_claims = new_signed_claims (organization_id = expect )
234
+ signed_claims = new_signed_claims (organization = expect )
229
235
assert publisher .verify_claims (signed_claims = signed_claims ) is valid
230
236
231
237
@pytest .mark .parametrize (
232
238
("expect" , "actual" , "valid" ),
233
239
[
234
- (BRANCH_ID , BRANCH_ID , True ),
235
- (BRANCH_ID , PROJECT_ID , False ),
236
- # If it's configured in the publisher, it must be present in the claim
237
- (BRANCH_ID , None , False ),
238
- # If it's not configured in the publisher, we don't care what it is
239
- # in the claim
240
- (None , None , True ),
241
- (None , PROJECT_ID , True ),
242
- ],
243
- )
244
- def test_activestate_publisher_branch_id_verified (
245
- self , expect : str , actual : str , valid : bool
246
- ):
247
- publisher = ActiveStatePublisher (
248
- sub = SUBJECT ,
249
- organization_id = ORG_ID ,
250
- project_id = PROJECT_ID ,
251
- user_id = USER_ID ,
252
- branch_id = expect ,
253
- )
254
-
255
- signed_claims = new_signed_claims (branch_id = actual )
256
- assert publisher .verify_claims (signed_claims = signed_claims ) is valid
257
-
258
- @pytest .mark .parametrize (
259
- ("expect" , "actual" , "valid" ),
260
- [
261
- (PROJECT_ID , PROJECT_ID , True ),
262
- (PROJECT_ID , ORG_ID , False ),
240
+ (PROJECT_NAME , PROJECT_NAME , True ),
241
+ (PROJECT_NAME , ORG_URL_NAME , False ),
263
242
],
264
243
)
265
244
def test_activestate_publisher_project_id_verified (
266
245
self , expect : str , actual : str , valid : bool
267
246
):
268
247
publisher = ActiveStatePublisher (
269
- sub = SUBJECT ,
270
- organization_id = ORG_ID ,
271
- project_id = actual ,
272
- user_id = USER_ID ,
248
+ organization = ORG_URL_NAME ,
249
+ project = actual ,
250
+ actor_id = ACTOR_ID ,
251
+ actor = ACTOR ,
252
+ ingredient = INGREDIENT ,
273
253
)
274
254
275
- signed_claims = new_signed_claims (project_id = expect )
255
+ signed_claims = new_signed_claims (project = expect )
276
256
assert publisher .verify_claims (signed_claims = signed_claims ) is valid
277
257
278
258
@pytest .mark .parametrize (
279
259
("expect" , "actual" , "valid" ),
280
260
[
281
- (USER_ID , USER_ID , True ),
282
- (USER_ID , ORG_ID , False ),
261
+ (ACTOR_ID , ACTOR_ID , True ),
262
+ (ACTOR_ID , ORG_URL_NAME , False ),
283
263
],
284
264
)
285
265
def test_activestate_publisher_user_id_verified (
286
266
self , expect : str , actual : str , valid : bool
287
267
):
288
268
publisher = ActiveStatePublisher (
289
- sub = SUBJECT ,
290
- organization_id = ORG_ID ,
291
- project_id = PROJECT_ID ,
292
- user_id = actual ,
269
+ organization = ORG_URL_NAME ,
270
+ project = PROJECT_NAME ,
271
+ actor_id = actual ,
272
+ actor = ACTOR ,
273
+ ingredient = INGREDIENT ,
293
274
)
294
275
295
- signed_claims = new_signed_claims (user_id = expect )
276
+ signed_claims = new_signed_claims (actor_id = expect )
296
277
assert publisher .verify_claims (signed_claims = signed_claims ) is valid
297
278
298
279
@pytest .mark .parametrize (
299
280
("expected" , "actual" , "valid" ),
300
281
[
301
282
# Both present: must match.
302
283
(
303
- f"org:{ ORG_ID } :project:{ PROJECT_ID } " ,
304
- f"org:{ ORG_ID } :project:{ PROJECT_ID } " ,
305
- True ,
306
- ),
307
- # Both present, with branch id: must match.
308
- (
309
- f"org:{ ORG_ID } :project:{ PROJECT_ID } :branch_id:{ BRANCH_ID } " ,
310
- f"org:{ ORG_ID } :project:{ PROJECT_ID } :branch_id:{ BRANCH_ID } " ,
284
+ f"org:{ ORG_URL_NAME } :project:{ PROJECT_NAME } " ,
285
+ f"org:{ ORG_URL_NAME } :project:{ PROJECT_NAME } " ,
311
286
True ,
312
287
),
313
- # sub configured without branch id, claim has branch id: must fail.
288
+ # Wrong value, project, must fail.
314
289
(
315
- f"org:{ ORG_ID } :project:{ PROJECT_ID } " ,
316
- f"org:{ ORG_ID } :project:{ PROJECT_ID } :branch_id: { BRANCH_ID } " ,
290
+ f"org:{ ORG_URL_NAME } :project:{ PROJECT_NAME } " ,
291
+ f"org:{ ORG_URL_NAME } :project:{ ORG_URL_NAME } " ,
317
292
False ,
318
293
),
319
- # sub configured with branch id, claim missing branch id: must fail.
294
+ # Wrong value, org_id, must fail.
320
295
(
321
- f"org:{ ORG_ID } :project:{ PROJECT_ID } :branch_id: { BRANCH_ID } " ,
322
- f"org:{ ORG_ID } :project:{ PROJECT_ID } " ,
296
+ f"org:{ ORG_URL_NAME } :project:{ PROJECT_NAME } " ,
297
+ f"org:{ PROJECT_NAME } :project:{ PROJECT_NAME } " ,
323
298
False ,
324
299
),
325
- # Wrong format for sub to expect from ActiveState: must fail.
300
+ # Just nonsenes, must fail.
326
301
(
327
- f"org:{ ORG_ID } :project:{ PROJECT_ID } " ,
328
- f"org: { ORG_ID } :project: { ORG_ID } " ,
302
+ f"org:{ ORG_URL_NAME } :project:{ PROJECT_NAME } " ,
303
+ "Nonsense " ,
329
304
False ,
330
305
),
331
306
],
332
307
)
333
308
def test_activestate_publisher_sub (self , expected : str , actual : str , valid : bool ):
334
309
check = ActiveStatePublisher .__required_verifiable_claims__ ["sub" ]
335
- assert check (expected , actual , pretend .stub ()) is valid
310
+ signed_claims = new_signed_claims (sub = actual )
311
+ assert check (expected , actual , signed_claims ) is valid
336
312
337
313
338
314
class TestPendingActiveStatePublisher :
@@ -343,8 +319,10 @@ def test_reify_does_not_exist_yet(self, db_request):
343
319
assert (
344
320
db_request .db .query (ActiveStatePublisher )
345
321
.filter_by (
346
- organization_id = pending_publisher .organization_id ,
347
- sub = pending_publisher .sub ,
322
+ organization = pending_publisher .organization ,
323
+ project = pending_publisher .project ,
324
+ actor_id = pending_publisher .actor_id ,
325
+ actor = pending_publisher .actor ,
348
326
)
349
327
.one_or_none ()
350
328
is None
@@ -353,16 +331,16 @@ def test_reify_does_not_exist_yet(self, db_request):
353
331
354
332
assert isinstance (publisher , ActiveStatePublisher )
355
333
assert pending_publisher in db_request .db .deleted
356
- assert publisher .organization_id == pending_publisher .organization_id
334
+ assert publisher .organization == pending_publisher .organization
357
335
assert publisher .sub == pending_publisher .sub
358
336
359
337
def test_reify_already_exists (self , db_request ):
360
338
existing_publisher : ActiveStatePublisher = ActiveStatePublisherFactory .create ()
361
339
pending_publisher = PendingActiveStatePublisherFactory .create (
362
- organization_id = existing_publisher .organization_id ,
363
- project_id = existing_publisher .project_id ,
364
- branch_id = existing_publisher .branch_id ,
365
- sub = existing_publisher .sub ,
340
+ organization = existing_publisher .organization ,
341
+ project = existing_publisher .project ,
342
+ actor_id = existing_publisher .actor_id ,
343
+ actor = existing_publisher .actor ,
366
344
)
367
345
publisher = pending_publisher .reify (db_request .db )
368
346
0 commit comments