@@ -144,7 +144,7 @@ func Test_GenerateJWT(t *testing.T) {
144
144
},
145
145
}
146
146
147
- ip := NewInstallation (httptest .NewRequest (http .MethodGet , "http://localhost" , strings .NewReader ("" )), run , & v1alpha1.Repository {}, & github.Provider {}, tt .namespace .GetName ())
147
+ ip := NewInstallation (httptest .NewRequest (http .MethodGet , "http://localhost" , strings .NewReader ("" )), run , & v1alpha1.Repository {}, & github.Provider {}, tt .namespace .GetName (), nil )
148
148
token , err := ip .GenerateJWT (ctx )
149
149
if tt .wantErr {
150
150
assert .Assert (t , err != nil )
@@ -206,7 +206,7 @@ func Test_GetAndUpdateInstallationID(t *testing.T) {
206
206
ctx = info .StoreCurrentControllerName (ctx , "default" )
207
207
ctx = info .StoreNS (ctx , testNamespace .GetName ())
208
208
209
- ip := NewInstallation (httptest .NewRequest (http .MethodGet , "http://localhost" , strings .NewReader ("" )), run , & v1alpha1.Repository {}, & github.Provider {}, testNamespace .GetName ())
209
+ ip := NewInstallation (httptest .NewRequest (http .MethodGet , "http://localhost" , strings .NewReader ("" )), run , & v1alpha1.Repository {}, & github.Provider {}, testNamespace .GetName (), nil )
210
210
jwtToken , err := ip .GenerateJWT (ctx )
211
211
assert .NilError (t , err )
212
212
req := httptest .NewRequest (http .MethodGet , "http://localhost" , strings .NewReader ("" ))
@@ -256,7 +256,7 @@ func Test_GetAndUpdateInstallationID(t *testing.T) {
256
256
`{"total_count": 2,"repositories": [{"id":1,"html_url": "https://matched/%s/incoming"},{"id":2,"html_url": "https://anotherrepo/that/would/failit"}]}` ,
257
257
orgName )
258
258
})
259
- ip = NewInstallation (req , run , repo , gprovider , testNamespace .GetName ())
259
+ ip = NewInstallation (req , run , repo , gprovider , testNamespace .GetName (), nil )
260
260
_ , token , installationID , err := ip .GetAndUpdateInstallationID (ctx )
261
261
assert .NilError (t , err )
262
262
assert .Equal (t , installationID , int64 (wantID ))
@@ -294,6 +294,24 @@ func TestGetAndUpdateInstallationID_Fallbacks(t *testing.T) {
294
294
skip bool
295
295
expectedErrorString string
296
296
}{
297
+ {
298
+ name : "repo installation succeeds directly" ,
299
+ repoURL : fmt .Sprintf ("https://matched/%s/%s" , orgName , repoName ),
300
+ setupMux : func (mux * http.ServeMux , jwtToken string ) {
301
+ mux .HandleFunc (fmt .Sprintf ("/repos/%s/%s/installation" , orgName , repoName ), func (w http.ResponseWriter , _ * http.Request ) {
302
+ _ , _ = fmt .Fprintf (w , `{"id": %d}` , orgID )
303
+ })
304
+ mux .HandleFunc (fmt .Sprintf ("/app/installations/%d/access_tokens" , orgID ), func (w http.ResponseWriter , r * http.Request ) {
305
+ testMethod (t , r )
306
+ w .Header ().Set ("Authorization" , "Bearer " + jwtToken )
307
+ w .Header ().Set ("Accept" , "application/vnd.github+json" )
308
+ _ , _ = fmt .Fprintf (w , `{"token": "%s"}` , wantToken )
309
+ })
310
+ },
311
+ wantErr : false ,
312
+ wantInstallationID : orgID ,
313
+ wantToken : wantToken ,
314
+ },
297
315
{
298
316
name : "repo installation fails, org installation succeeds" ,
299
317
repoURL : fmt .Sprintf ("https://matched/%s/%s" , orgName , repoName ),
@@ -357,13 +375,40 @@ func TestGetAndUpdateInstallationID_Fallbacks(t *testing.T) {
357
375
expectedErrorString : "could not find repository, organization or user installation" ,
358
376
},
359
377
{
360
- name : "invalid repo url" ,
361
- repoURL : "https://invalid/url" ,
362
- setupMux : func (_ * http.ServeMux , _ string ) {
378
+ name : "repo installation succeeds but token generation fails" ,
379
+ repoURL : fmt .Sprintf ("https://matched/%s/%s" , orgName , repoName ),
380
+ setupMux : func (mux * http.ServeMux , jwtToken string ) {
381
+ mux .HandleFunc (fmt .Sprintf ("/repos/%s/%s/installation" , orgName , repoName ), func (w http.ResponseWriter , _ * http.Request ) {
382
+ _ , _ = fmt .Fprintf (w , `{"id": %d}` , orgID )
383
+ })
384
+ mux .HandleFunc (fmt .Sprintf ("/app/installations/%d/access_tokens" , orgID ), func (w http.ResponseWriter , r * http.Request ) {
385
+ testMethod (t , r )
386
+ w .Header ().Set ("Authorization" , "Bearer " + jwtToken )
387
+ w .Header ().Set ("Accept" , "application/vnd.github+json" )
388
+ w .WriteHeader (http .StatusForbidden )
389
+ _ , _ = fmt .Fprintf (w , `{"message": "Forbidden"}` )
390
+ })
363
391
},
364
- wantErr : true ,
365
- expectedErrorString : "invalid repository URL path" ,
392
+ wantErr : false , // Should not error, but return empty token
393
+ wantInstallationID : orgID ,
394
+ wantToken : "" , // Empty token when generation fails
366
395
},
396
+ {
397
+ name : "invalid repo url - too few path segments" ,
398
+ repoURL : "https://invalid/url" ,
399
+ setupMux : func (_ * http.ServeMux , _ string ) {
400
+ },
401
+ wantErr : true ,
402
+ expectedErrorString : "invalid repository URL path" ,
403
+ },
404
+ {
405
+ name : "invalid GitHub repo url - too many path segments" ,
406
+ repoURL : "https://github.com/group/subgroup/repo" ,
407
+ setupMux : func (_ * http.ServeMux , _ string ) {
408
+ },
409
+ wantErr : true ,
410
+ expectedErrorString : "invalid repository URL path" ,
411
+ },
367
412
}
368
413
369
414
for _ , tt := range tests {
@@ -393,7 +438,7 @@ func TestGetAndUpdateInstallationID_Fallbacks(t *testing.T) {
393
438
ctx = info .StoreCurrentControllerName (ctx , "default" )
394
439
ctx = info .StoreNS (ctx , testNamespace .GetName ())
395
440
396
- ip := NewInstallation (httptest .NewRequest (http .MethodGet , "http://localhost" , strings .NewReader ("" )), run , & v1alpha1.Repository {}, & github.Provider {}, testNamespace .GetName ())
441
+ ip := NewInstallation (httptest .NewRequest (http .MethodGet , "http://localhost" , strings .NewReader ("" )), run , & v1alpha1.Repository {}, & github.Provider {}, testNamespace .GetName (), nil )
397
442
jwtToken , err := ip .GenerateJWT (ctx )
398
443
assert .NilError (t , err )
399
444
@@ -412,7 +457,7 @@ func TestGetAndUpdateInstallationID_Fallbacks(t *testing.T) {
412
457
gprovider .SetGithubClient (fakeghclient )
413
458
t .Setenv ("PAC_GIT_PROVIDER_TOKEN_APIURL" , serverURL )
414
459
415
- ip = NewInstallation (httptest .NewRequest (http .MethodGet , "http://localhost" , strings .NewReader ("" )), run , repo , gprovider , testNamespace .GetName ())
460
+ ip = NewInstallation (httptest .NewRequest (http .MethodGet , "http://localhost" , strings .NewReader ("" )), run , repo , gprovider , testNamespace .GetName (), nil )
416
461
_ , token , installationID , err := ip .GetAndUpdateInstallationID (ctx )
417
462
418
463
if tt .wantErr {
0 commit comments