@@ -325,6 +325,7 @@ describe('List API Keys - GET /auth/key/all', () => {
325325 const response = await request ( app . getHttpServer ( ) )
326326 . get ( '/auth/key/all' )
327327 . set ( 'Authorization' , `Bearer ${ apiKey } ` )
328+ . set ( 'x-user-jwt' , regularUserJwt )
328329 . expect ( 200 ) ;
329330
330331 expect ( response . body . keys ) . toBeDefined ( ) ;
@@ -393,6 +394,7 @@ describe('List API Keys - GET /auth/key/all', () => {
393394 const response = await request ( app . getHttpServer ( ) )
394395 . get ( `/auth/key/all` )
395396 . set ( 'Authorization' , `Bearer ${ apiKey } ` )
397+ . set ( 'x-user-jwt' , adminJwt )
396398 . expect ( 200 ) ;
397399
398400 expect ( response . body . keys ) . toBeDefined ( ) ;
@@ -402,10 +404,10 @@ describe('List API Keys - GET /auth/key/all', () => {
402404
403405 it ( 'should support offset and limit query parameters' , async ( ) => {
404406 const adminJwt = await getJwtForUser ( ADMIN_EMAIL , ADMIN_PASSWORD ) ;
405-
407+ const keys = [ ] ;
406408 // Create multiple API keys
407409 for ( let i = 0 ; i < 3 ; i ++ ) {
408- await request ( app . getHttpServer ( ) )
410+ const key = await request ( app . getHttpServer ( ) )
409411 . post ( '/auth/key' )
410412 . set ( 'Authorization' , `Bearer ${ adminJwt } ` )
411413 . send ( {
@@ -414,24 +416,27 @@ describe('List API Keys - GET /auth/key/all', () => {
414416 description : `pagination-key-${ i } ` ,
415417 } )
416418 . expect ( 201 ) ;
419+ keys . push ( key ) ;
417420 }
418421
422+ const apiKey = keys . at ( 0 ) . body [ 'apiKey' ] ;
419423 const response = await request ( app . getHttpServer ( ) )
420424 . get ( '/auth/key/all?offset=0&limit=2' )
421- . set ( 'Authorization' , `Bearer ${ adminJwt } ` )
425+ . set ( 'Authorization' , `Bearer ${ apiKey } ` )
426+ . set ( 'x-user-jwt' , adminJwt )
422427 . expect ( 200 ) ;
423428
424429 expect ( response . body . keys ) . toBeDefined ( ) ;
425430 expect ( response . body . keys . length ) . toBeLessThanOrEqual ( 2 ) ;
426431 } ) ;
427432} ) ;
428433
429- describe ( 'Delete API Key by ID - DELETE /auth/key/:id ' , ( ) => {
430- it ( 'should delete an existing API key by its ID ' , async ( ) => {
434+ describe ( 'Delete API Key - DELETE /auth/key' , ( ) => {
435+ it ( 'should delete an existing API key' , async ( ) => {
431436 const adminJwt = await getJwtForUser ( ADMIN_EMAIL , ADMIN_PASSWORD ) ;
432437
433438 // Create an API key
434- const createResp = await request ( app . getHttpServer ( ) )
439+ const key = await request ( app . getHttpServer ( ) )
435440 . post ( '/auth/key' )
436441 . set ( 'Authorization' , `Bearer ${ adminJwt } ` )
437442 . send ( {
@@ -440,46 +445,19 @@ describe('Delete API Key by ID - DELETE /auth/key/:id', () => {
440445 description : 'delete-by-id-test' ,
441446 } )
442447 . expect ( 201 ) ;
448+ const apiKey = key . body [ 'apiKey' ] ;
449+ expect ( apiKey ) . toBeDefined ( ) ;
443450
444- expect ( createResp . body . apiKey ) . toBeDefined ( ) ;
445-
446- // List keys to find the created key's ID
447- const listResp = await request ( app . getHttpServer ( ) )
448- . get ( '/auth/key/all?projectId=0' )
449- . set ( 'Authorization' , `Bearer ${ adminJwt } ` )
450- . expect ( 200 ) ;
451-
452- const createdKey = listResp . body . keys . find (
453- ( k : any ) => k . description === 'delete-by-id-test' ,
454- ) ;
455- expect ( createdKey ) . toBeDefined ( ) ;
456- const keyId = createdKey . id ;
457-
458- // Delete the key by ID
451+ // Delete the key
459452 await request ( app . getHttpServer ( ) )
460- . delete ( `/auth/key/${ keyId } ` )
461- . set ( 'Authorization' , `Bearer ${ adminJwt } ` )
462- . expect ( 200 ) ;
463- } ) ;
464-
465- it ( 'should return 400 for a non-numeric ID' , async ( ) => {
466- const adminJwt = await getJwtForUser ( ADMIN_EMAIL , ADMIN_PASSWORD ) ;
467- return request ( app . getHttpServer ( ) )
468- . delete ( '/auth/key/abc' )
469- . set ( 'Authorization' , `Bearer ${ adminJwt } ` )
470- . expect ( 400 ) ;
471- } ) ;
472-
473- it ( 'should return an error for a non-existent API key ID' , async ( ) => {
474- const adminJwt = await getJwtForUser ( ADMIN_EMAIL , ADMIN_PASSWORD ) ;
475- return request ( app . getHttpServer ( ) )
476- . delete ( '/auth/key/999999' )
477- . set ( 'Authorization' , `Bearer ${ adminJwt } ` )
478- . expect ( 404 ) ;
453+ . delete ( `/auth/key` )
454+ . set ( 'Authorization' , `Bearer ${ apiKey } ` )
455+ . set ( 'x-user-jwt' , adminJwt )
456+ . expect ( 204 ) ;
479457 } ) ;
480458
481459 it ( 'should not be accessible without authentication' , ( ) => {
482- return request ( app . getHttpServer ( ) ) . delete ( '/auth/key/1 ' ) . expect ( 401 ) ;
460+ return request ( app . getHttpServer ( ) ) . delete ( '/auth/key' ) . expect ( 401 ) ;
483461 } ) ;
484462} ) ;
485463
0 commit comments