@@ -58,7 +58,7 @@ test("README example for app auth", async () => {
58
58
} ) ;
59
59
60
60
test ( "README example for installation auth" , async ( ) => {
61
- const matchCreateAccessToken : MockMatcherFunction = (
61
+ const matchCreateInstallationAccessToken : MockMatcherFunction = (
62
62
url ,
63
63
{ body, headers }
64
64
) => {
@@ -75,7 +75,7 @@ test("README example for installation auth", async () => {
75
75
return true ;
76
76
} ;
77
77
78
- const createAccessTokenResponseData = {
78
+ const createInstallationAccessTokenResponseData = {
79
79
token : "secret123" ,
80
80
expires_at : "1970-01-01T01:00:00.000Z" ,
81
81
permissions : {
@@ -94,7 +94,10 @@ test("README example for installation auth", async () => {
94
94
request : {
95
95
fetch : fetchMock
96
96
. sandbox ( )
97
- . postOnce ( matchCreateAccessToken , createAccessTokenResponseData )
97
+ . postOnce (
98
+ matchCreateInstallationAccessToken ,
99
+ createInstallationAccessTokenResponseData
100
+ )
98
101
}
99
102
} )
100
103
} ) ;
@@ -117,6 +120,64 @@ test("README example for installation auth", async () => {
117
120
} ) ;
118
121
} ) ;
119
122
123
+ test ( "README example for oauth" , async ( ) => {
124
+ const matchCreateOAuthAccessToken : MockMatcherFunction = (
125
+ url ,
126
+ { body, headers }
127
+ ) => {
128
+ expect ( url ) . toEqual ( "https://github.com/login/oauth/access_token" ) ;
129
+ expect ( headers ) . toStrictEqual ( {
130
+ accept : "application/json" ,
131
+ "user-agent" : "test" ,
132
+ "content-type" : "application/json; charset=utf-8"
133
+ } ) ;
134
+ expect ( JSON . parse ( String ( body ) ) ) . toStrictEqual ( {
135
+ client_id : "12345678901234567890" ,
136
+ client_secret : "1234567890123456789012345678901234567890" ,
137
+ code : "123456"
138
+ } ) ;
139
+ return true ;
140
+ } ;
141
+
142
+ const createOAuthAccessTokenResponseData = {
143
+ access_token : "secret123" ,
144
+ scope : "" ,
145
+ token_type : "bearer"
146
+ } ;
147
+
148
+ const auth = createAppAuth ( {
149
+ id : APP_ID ,
150
+ privateKey : PRIVATE_KEY ,
151
+ clientId : "12345678901234567890" ,
152
+ clientSecret : "1234567890123456789012345678901234567890" ,
153
+ request : request . defaults ( {
154
+ headers : {
155
+ "user-agent" : "test"
156
+ } ,
157
+ request : {
158
+ fetch : fetchMock
159
+ . sandbox ( )
160
+ . postOnce (
161
+ matchCreateOAuthAccessToken ,
162
+ createOAuthAccessTokenResponseData
163
+ )
164
+ }
165
+ } )
166
+ } ) ;
167
+
168
+ const authentication = await auth ( {
169
+ type : "oauth" ,
170
+ code : "123456"
171
+ } ) ;
172
+
173
+ expect ( authentication ) . toEqual ( {
174
+ type : "token" ,
175
+ token : "secret123" ,
176
+ tokenType : "oauth" ,
177
+ scopes : [ ]
178
+ } ) ;
179
+ } ) ;
180
+
120
181
test ( "installationId strategy option" , async ( ) => {
121
182
const requestMock = request . defaults ( {
122
183
headers : {
@@ -164,14 +225,17 @@ test("installationId strategy option", async () => {
164
225
} ) ;
165
226
166
227
test ( "repositoryIds auth option" , async ( ) => {
167
- const matchCreateAccessToken : MockMatcherFunction = ( url , { body } ) => {
228
+ const matchCreateInstallationAccessToken : MockMatcherFunction = (
229
+ url ,
230
+ { body }
231
+ ) => {
168
232
expect ( JSON . parse ( String ( body ) ) ) . toStrictEqual ( {
169
233
repository_ids : [ 1 , 2 , 3 ]
170
234
} ) ;
171
235
return true ;
172
236
} ;
173
237
174
- const createAccessTokenResponseData = {
238
+ const createInstallationAccessTokenResponseData = {
175
239
token : "secret123" ,
176
240
expires_at : "1970-01-01T01:00:00.000Z" ,
177
241
permissions : {
@@ -191,7 +255,10 @@ test("repositoryIds auth option", async () => {
191
255
request : {
192
256
fetch : fetchMock
193
257
. sandbox ( )
194
- . postOnce ( matchCreateAccessToken , createAccessTokenResponseData )
258
+ . postOnce (
259
+ matchCreateInstallationAccessToken ,
260
+ createInstallationAccessTokenResponseData
261
+ )
195
262
}
196
263
} )
197
264
} ) ;
@@ -217,7 +284,10 @@ test("repositoryIds auth option", async () => {
217
284
} ) ;
218
285
219
286
test ( "permissions auth option" , async ( ) => {
220
- const matchCreateAccessToken : MockMatcherFunction = ( url , { body } ) => {
287
+ const matchCreateInstallationAccessToken : MockMatcherFunction = (
288
+ url ,
289
+ { body }
290
+ ) => {
221
291
expect ( JSON . parse ( String ( body ) ) ) . toStrictEqual ( {
222
292
permissions : {
223
293
single_file : "write"
@@ -226,7 +296,7 @@ test("permissions auth option", async () => {
226
296
return true ;
227
297
} ;
228
298
229
- const createAccessTokenResponseData = {
299
+ const createInstallationAccessTokenResponseData = {
230
300
token : "secret123" ,
231
301
expires_at : "1970-01-01T01:00:00.000Z" ,
232
302
permissions : {
@@ -246,7 +316,10 @@ test("permissions auth option", async () => {
246
316
request : {
247
317
fetch : fetchMock
248
318
. sandbox ( )
249
- . postOnce ( matchCreateAccessToken , createAccessTokenResponseData )
319
+ . postOnce (
320
+ matchCreateInstallationAccessToken ,
321
+ createInstallationAccessTokenResponseData
322
+ )
250
323
}
251
324
} )
252
325
} ) ;
@@ -449,7 +522,7 @@ test("installation cache with different options", async () => {
449
522
return true ;
450
523
} ;
451
524
452
- const createAccessTokenResponseData = {
525
+ const createInstallationAccessTokenResponseData = {
453
526
token : "secret123" ,
454
527
expires_at : "1970-01-01T01:00:00.000Z" ,
455
528
permissions : {
@@ -460,8 +533,14 @@ test("installation cache with different options", async () => {
460
533
461
534
const mock = fetchMock
462
535
. sandbox ( )
463
- . postOnce ( matchCreateAccessToken1 , createAccessTokenResponseData )
464
- . postOnce ( matchCreateAccessToken2 , createAccessTokenResponseData ) ;
536
+ . postOnce (
537
+ matchCreateAccessToken1 ,
538
+ createInstallationAccessTokenResponseData
539
+ )
540
+ . postOnce (
541
+ matchCreateAccessToken2 ,
542
+ createInstallationAccessTokenResponseData
543
+ ) ;
465
544
466
545
const requestMock = request . defaults ( {
467
546
headers : {
@@ -563,8 +642,114 @@ test("refresh option", async () => {
563
642
expect ( authentication2 ) . toEqual ( EXPECTED ) ;
564
643
} ) ;
565
644
645
+ test ( "oauth with `code`, `redirectUrl` and `state`" , async ( ) => {
646
+ const matchCreateOAuthAccessToken : MockMatcherFunction = (
647
+ url ,
648
+ { body, headers }
649
+ ) => {
650
+ expect ( url ) . toEqual ( "https://github.com/login/oauth/access_token" ) ;
651
+ expect ( headers ) . toStrictEqual ( {
652
+ accept : "application/json" ,
653
+ "user-agent" : "test" ,
654
+ "content-type" : "application/json; charset=utf-8"
655
+ } ) ;
656
+ expect ( JSON . parse ( String ( body ) ) ) . toStrictEqual ( {
657
+ client_id : "12345678901234567890" ,
658
+ client_secret : "1234567890123456789012345678901234567890" ,
659
+ code : "123456" ,
660
+ redirect_uri : "https://example.com/login" ,
661
+ state : "mystate123"
662
+ } ) ;
663
+
664
+ return true ;
665
+ } ;
666
+
667
+ const createOAuthAccessTokenResponseData = {
668
+ access_token : "secret123" ,
669
+ scope : "" ,
670
+ token_type : "bearer"
671
+ } ;
672
+
673
+ const auth = createAppAuth ( {
674
+ id : APP_ID ,
675
+ privateKey : PRIVATE_KEY ,
676
+ clientId : "12345678901234567890" ,
677
+ clientSecret : "1234567890123456789012345678901234567890" ,
678
+ request : request . defaults ( {
679
+ headers : {
680
+ "user-agent" : "test"
681
+ } ,
682
+ request : {
683
+ fetch : fetchMock
684
+ . sandbox ( )
685
+ . postOnce (
686
+ matchCreateOAuthAccessToken ,
687
+ createOAuthAccessTokenResponseData
688
+ )
689
+ }
690
+ } )
691
+ } ) ;
692
+
693
+ const authentication = await auth ( {
694
+ type : "oauth" ,
695
+ code : "123456" ,
696
+ state : "mystate123" ,
697
+ redirectUrl : "https://example.com/login"
698
+ } ) ;
699
+
700
+ expect ( authentication ) . toEqual ( {
701
+ type : "token" ,
702
+ token : "secret123" ,
703
+ tokenType : "oauth" ,
704
+ scopes : [ ]
705
+ } ) ;
706
+ } ) ;
707
+
708
+ test ( "oauth with custom baseUrl (GHE)" , async ( ) => {
709
+ const createOAuthAccessTokenResponseData = {
710
+ access_token : "secret123" ,
711
+ scope : "" ,
712
+ token_type : "bearer"
713
+ } ;
714
+
715
+ const auth = createAppAuth ( {
716
+ id : APP_ID ,
717
+ privateKey : PRIVATE_KEY ,
718
+ clientId : "12345678901234567890" ,
719
+ clientSecret : "1234567890123456789012345678901234567890" ,
720
+ request : request . defaults ( {
721
+ baseUrl : "https://github.acme-inc.com/api/v3" ,
722
+ headers : {
723
+ "user-agent" : "test"
724
+ } ,
725
+ request : {
726
+ fetch : fetchMock
727
+ . sandbox ( )
728
+ . postOnce (
729
+ "https://github.acme-inc.com/login/oauth/access_token" ,
730
+ createOAuthAccessTokenResponseData
731
+ )
732
+ }
733
+ } )
734
+ } ) ;
735
+
736
+ const authentication = await auth ( {
737
+ type : "oauth" ,
738
+ code : "123456" ,
739
+ state : "mystate123" ,
740
+ redirectUrl : "https://example.com/login"
741
+ } ) ;
742
+
743
+ expect ( authentication ) . toEqual ( {
744
+ type : "token" ,
745
+ token : "secret123" ,
746
+ tokenType : "oauth" ,
747
+ scopes : [ ]
748
+ } ) ;
749
+ } ) ;
750
+
566
751
test ( "caches based on installation id" , async ( ) => {
567
- const createAccessTokenResponseData = {
752
+ const createInstallationAccessTokenResponseData = {
568
753
token : "secret123" ,
569
754
expires_at : "1970-01-01T01:00:00.000Z" ,
570
755
permissions : {
@@ -582,11 +767,11 @@ test("caches based on installation id", async () => {
582
767
. sandbox ( )
583
768
. postOnce (
584
769
"path:/app/installations/123/access_tokens" ,
585
- createAccessTokenResponseData
770
+ createInstallationAccessTokenResponseData
586
771
)
587
772
. postOnce (
588
773
"path:/app/installations/456/access_tokens" ,
589
- Object . assign ( { } , createAccessTokenResponseData , {
774
+ Object . assign ( { } , createInstallationAccessTokenResponseData , {
590
775
token : "secret456"
591
776
} )
592
777
)
0 commit comments