1- import {
2- authenticate ,
3- GITHUB_AUTH_API_KEY ,
4- GITHUB_AUTH_SERVICE_URL
5- } from '../src/auth'
1+ import { authenticate , PRODUCTION_SERVICE , STAGING_SERVICE } from '../src/auth'
62import { jest } from '@jest/globals'
73
84describe ( 'authenticate' , ( ) => {
@@ -13,24 +9,54 @@ describe('authenticate', () => {
139 mockFetch . mockClear ( )
1410 } )
1511
16- it ( 'should use GitHub App authentication when app is installed' , async ( ) => {
12+ it ( 'should use production GitHub App when installed' , async ( ) => {
1713 mockFetch . mockResolvedValue ( {
1814 ok : true ,
1915 status : 200 ,
20- json : async ( ) => Promise . resolve ( { token : 'ghs_app_123 ' } )
16+ json : async ( ) => Promise . resolve ( { token : 'ghs_prod_token ' } )
2117 } as Response )
2218
2319 const result = await authenticate (
2420 { owner : 'dunder-mifflin' , repo : 'website' } ,
2521 'ghp_default_token'
2622 )
2723
28- expect ( result ) . toBe ( 'ghs_app_123' )
24+ expect ( result ) . toBe ( 'ghs_prod_token' )
25+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 )
2926 expect ( mockFetch ) . toHaveBeenCalledWith (
30- `${ GITHUB_AUTH_SERVICE_URL } /github/dunder-mifflin/website/installation-token` ,
27+ `${ PRODUCTION_SERVICE . url } /github/dunder-mifflin/website/installation-token` ,
3128 expect . objectContaining ( {
3229 method : 'POST' ,
33- headers : { Authorization : `Bearer ${ GITHUB_AUTH_API_KEY } ` }
30+ headers : { Authorization : `Bearer ${ PRODUCTION_SERVICE . key } ` }
31+ } )
32+ )
33+ } )
34+
35+ it ( 'should use staging GitHub App when production not installed' , async ( ) => {
36+ mockFetch
37+ . mockResolvedValueOnce ( {
38+ ok : false ,
39+ status : 404 ,
40+ json : async ( ) => ( { error : 'Not installed' } )
41+ } as Response )
42+ . mockResolvedValueOnce ( {
43+ ok : true ,
44+ status : 200 ,
45+ json : async ( ) => ( { token : 'ghs_staging_token' } )
46+ } as Response )
47+
48+ const result = await authenticate (
49+ { owner : 'dunder-mifflin' , repo : 'website' } ,
50+ 'ghp_default_token'
51+ )
52+
53+ expect ( result ) . toBe ( 'ghs_staging_token' )
54+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 )
55+ expect ( mockFetch ) . toHaveBeenCalledWith (
56+ `${ STAGING_SERVICE . url } /github/dunder-mifflin/website/installation-token` ,
57+ expect . objectContaining ( {
58+ method : 'POST' ,
59+ headers : { Authorization : `Bearer ${ STAGING_SERVICE . key } ` }
3460 } )
3561 )
3662 } )
@@ -48,6 +74,7 @@ describe('authenticate', () => {
4874 )
4975
5076 expect ( result ) . toBe ( 'ghp_default_token' )
77+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 )
5178 } )
5279
5380 it ( 'should fall back to standard authentication when service is unavailable' , async ( ) => {
@@ -65,11 +92,54 @@ describe('authenticate', () => {
6592 const customPAT = 'ghp_custom_pat'
6693
6794 const result = await authenticate (
68- { owner : 'owner ' , repo : 'repo ' } ,
95+ { owner : 'dunder-mifflin ' , repo : 'website ' } ,
6996 customPAT
7097 )
7198
7299 expect ( result ) . toBe ( customPAT )
73100 expect ( mockFetch ) . not . toHaveBeenCalled ( )
74101 } )
102+
103+ it ( 'should handle all services returning errors' , async ( ) => {
104+ mockFetch
105+ . mockResolvedValueOnce ( {
106+ ok : false ,
107+ status : 401 ,
108+ json : async ( ) => ( { error : 'Unauthorized' } )
109+ } as Response )
110+ . mockResolvedValueOnce ( {
111+ ok : false ,
112+ status : 500 ,
113+ json : async ( ) => ( { error : 'Server error' } )
114+ } as Response )
115+
116+ const result = await authenticate (
117+ { owner : 'dunder-mifflin' , repo : 'website' } ,
118+ 'ghp_default_token'
119+ )
120+
121+ expect ( result ) . toBe ( 'ghp_default_token' )
122+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 )
123+ } )
124+
125+ it ( 'should handle unexpected status codes' , async ( ) => {
126+ mockFetch
127+ . mockResolvedValueOnce ( {
128+ ok : false ,
129+ status : 403 ,
130+ json : async ( ) => ( { error : 'Forbidden' } )
131+ } as Response )
132+ . mockResolvedValueOnce ( {
133+ ok : false ,
134+ status : 403 ,
135+ json : async ( ) => ( { error : 'Forbidden' } )
136+ } as Response )
137+
138+ const result = await authenticate (
139+ { owner : 'dunder-mifflin' , repo : 'website' } ,
140+ 'ghp_default_token'
141+ )
142+
143+ expect ( result ) . toBe ( 'ghp_default_token' )
144+ } )
75145} )
0 commit comments