11import { Test , TestingModule } from '@nestjs/testing' ;
2- import {
3- ExecutionContext ,
4- UnauthorizedException ,
5- ForbiddenException ,
6- } from '@nestjs/common' ;
2+ import { ExecutionContext } from '@nestjs/common' ;
73import { Reflector } from '@nestjs/core' ;
84import { APIKeyAuthGuard } from 'omniboxd/auth' ;
95import { APIKeyService } from 'omniboxd/api-key/api-key.service' ;
@@ -20,7 +16,6 @@ describe('APIKeyAuthGuard', () => {
2016 let guard : APIKeyAuthGuard ;
2117 let apiKeyService : jest . Mocked < APIKeyService > ;
2218 let reflector : jest . Mocked < Reflector > ;
23- let i18nService : jest . Mocked < I18nService > ;
2419
2520 beforeEach ( async ( ) => {
2621 const module : TestingModule = await Test . createTestingModule ( {
@@ -44,11 +39,14 @@ describe('APIKeyAuthGuard', () => {
4439 t : jest . fn ( ( key : string ) => {
4540 // Return mock translations for test purposes
4641 const translations : Record < string , string > = {
47- 'apikey.errors.authorizationHeaderRequired' : 'Authorization header is required' ,
42+ 'apikey.errors.authorizationHeaderRequired' :
43+ 'Authorization header is required' ,
4844 'apikey.errors.invalidApiKeyFormat' : 'Invalid API key format' ,
4945 'apikey.errors.invalidApiKey' : 'Invalid API key' ,
50- 'apikey.errors.noPermissionForTarget' : 'No permission for target {{target}}' ,
51- 'apikey.errors.noSpecificPermission' : 'No {{permission}} permission for target {{target}}' ,
46+ 'apikey.errors.noPermissionForTarget' :
47+ 'No permission for target {{target}}' ,
48+ 'apikey.errors.noSpecificPermission' :
49+ 'No {{permission}} permission for target {{target}}' ,
5250 } ;
5351 return translations [ key ] || key ;
5452 } ) ,
@@ -60,7 +58,6 @@ describe('APIKeyAuthGuard', () => {
6058 guard = module . get < APIKeyAuthGuard > ( APIKeyAuthGuard ) ;
6159 apiKeyService = module . get ( APIKeyService ) ;
6260 reflector = module . get ( Reflector ) ;
63- i18nService = module . get ( I18nService ) ;
6461 } ) ;
6562
6663 const createMockExecutionContext = (
@@ -111,9 +108,7 @@ describe('APIKeyAuthGuard', () => {
111108 . mockReturnValueOnce ( { enabled : true } ) ; // apiKeyAuthOptions = { enabled: true }
112109 const context = createMockExecutionContext ( ) ;
113110
114- await expect ( guard . canActivate ( context ) ) . rejects . toThrow (
115- AppException ,
116- ) ;
111+ await expect ( guard . canActivate ( context ) ) . rejects . toThrow ( AppException ) ;
117112 } ) ;
118113
119114 it ( 'should throw AppException when API key does not start with sk-' , async ( ) => {
@@ -122,9 +117,7 @@ describe('APIKeyAuthGuard', () => {
122117 . mockReturnValueOnce ( { enabled : true } ) ; // apiKeyAuthOptions = { enabled: true }
123118 const context = createMockExecutionContext ( 'Bearer invalid-key' ) ;
124119
125- await expect ( guard . canActivate ( context ) ) . rejects . toThrow (
126- AppException ,
127- ) ;
120+ await expect ( guard . canActivate ( context ) ) . rejects . toThrow ( AppException ) ;
128121 } ) ;
129122
130123 it ( 'should throw AppException when API key is not found' , async ( ) => {
@@ -134,9 +127,7 @@ describe('APIKeyAuthGuard', () => {
134127 const context = createMockExecutionContext ( 'Bearer sk-validformat' ) ;
135128 apiKeyService . findByValue . mockResolvedValue ( null ) ;
136129
137- await expect ( guard . canActivate ( context ) ) . rejects . toThrow (
138- AppException ,
139- ) ;
130+ await expect ( guard . canActivate ( context ) ) . rejects . toThrow ( AppException ) ;
140131 } ) ;
141132
142133 it ( 'should allow valid API key and set apiKey and user on request' , async ( ) => {
@@ -245,9 +236,7 @@ describe('APIKeyAuthGuard', () => {
245236 const context = createMockExecutionContext ( 'Bearer sk-validkey' ) ;
246237 apiKeyService . findByValue . mockResolvedValue ( mockApiKey ) ;
247238
248- await expect ( guard . canActivate ( context ) ) . rejects . toThrow (
249- AppException ,
250- ) ;
239+ await expect ( guard . canActivate ( context ) ) . rejects . toThrow ( AppException ) ;
251240 } ) ;
252241
253242 it ( 'should throw AppException when API key lacks specific permission type' , async ( ) => {
@@ -283,9 +272,7 @@ describe('APIKeyAuthGuard', () => {
283272 const context = createMockExecutionContext ( 'Bearer sk-validkey' ) ;
284273 apiKeyService . findByValue . mockResolvedValue ( mockApiKey ) ;
285274
286- await expect ( guard . canActivate ( context ) ) . rejects . toThrow (
287- AppException ,
288- ) ;
275+ await expect ( guard . canActivate ( context ) ) . rejects . toThrow ( AppException ) ;
289276 } ) ;
290277
291278 it ( 'should allow API key with multiple matching permissions' , async ( ) => {
0 commit comments