@@ -116,21 +116,30 @@ describe('Cloudflare Access middleware', async () => {
116
116
const keyPair2 = await generateJWTKeyPair ( ) ;
117
117
const keyPair3 = await generateJWTKeyPair ( ) ;
118
118
119
- vi . stubGlobal ( 'fetch' , async ( ) => {
120
- return Response . json ( {
121
- keys : [
122
- publicKeyToJWK ( keyPair1 . publicKey ) ,
123
- publicKeyToJWK ( keyPair2 . publicKey ) ,
124
- ] ,
119
+ beforeEach ( ( ) => {
120
+ vi . clearAllMocks ( ) ;
121
+ vi . stubGlobal ( 'fetch' , async ( ) => {
122
+ return Response . json ( {
123
+ keys : [
124
+ publicKeyToJWK ( keyPair1 . publicKey ) ,
125
+ publicKeyToJWK ( keyPair2 . publicKey ) ,
126
+ ] ,
127
+ } )
125
128
} )
126
- } )
129
+ } ) ;
127
130
128
131
const app = new Hono ( )
129
132
130
133
app . use ( '/*' , cloudflareAccess ( 'my-cool-team-name' ) )
131
134
app . get ( '/hello-behind-access' , ( c ) => c . text ( 'foo' ) )
132
135
app . get ( '/access-payload' , ( c ) => c . json ( c . get ( 'accessPayload' ) ) )
133
136
137
+ app . onError ( ( err , c ) => {
138
+ return c . json ( {
139
+ err : err . toString ( ) ,
140
+ } , 500 )
141
+ } )
142
+
134
143
it ( 'Should be throw Missing bearer token when nothing is sent' , async ( ) => {
135
144
const res = await app . request ( 'http://localhost/hello-behind-access' )
136
145
expect ( res ) . not . toBeNull ( )
@@ -248,4 +257,34 @@ describe('Cloudflare Access middleware', async () => {
248
257
"exp" :expect . any ( Number )
249
258
} )
250
259
} )
260
+
261
+ it ( 'Should throw an error, if the access organization does not exist' , async ( ) => {
262
+ vi . stubGlobal ( 'fetch' , async ( ) => {
263
+ return Response . json ( { success : false } , { status : 404 } )
264
+ } )
265
+
266
+ const res = await app . request ( 'http://localhost/hello-behind-access' , {
267
+ headers : {
268
+ 'cf-access-jwt-assertion' : 'asdads'
269
+ }
270
+ } )
271
+ expect ( res ) . not . toBeNull ( )
272
+ expect ( res . status ) . toBe ( 500 )
273
+ expect ( await res . json ( ) ) . toEqual ( { "err" :"Error: Authentication error: The Access Organization 'my-cool-team-name' does not exist" } )
274
+ } )
275
+
276
+ it ( 'Should throw an error, if the access certs url is unavailable' , async ( ) => {
277
+ vi . stubGlobal ( 'fetch' , async ( ) => {
278
+ return Response . json ( { success : false } , { status : 500 } )
279
+ } )
280
+
281
+ const res = await app . request ( 'http://localhost/hello-behind-access' , {
282
+ headers : {
283
+ 'cf-access-jwt-assertion' : 'asdads'
284
+ }
285
+ } )
286
+ expect ( res ) . not . toBeNull ( )
287
+ expect ( res . status ) . toBe ( 500 )
288
+ expect ( await res . json ( ) ) . toEqual ( { "err" :"Error: Authentication error: Received unexpected HTTP code 500 from Cloudflare Access" } )
289
+ } )
251
290
} )
0 commit comments