@@ -18,11 +18,13 @@ describe('/api/v1/gravity-index/search POST endpoint', () => {
1818 let mockTrackEvent : TrackEventFn
1919 let mockGetUserInfoFromApiKey : GetUserInfoFromApiKeyFn
2020 let mockFetch : typeof globalThis . fetch
21+ let mockWarn : ReturnType < typeof mock >
2122
2223 beforeEach ( ( ) => {
24+ mockWarn = mock ( ( ) => { } )
2325 mockLogger = {
2426 error : mock ( ( ) => { } ) ,
25- warn : mock ( ( ) => { } ) ,
27+ warn : mockWarn ,
2628 info : mock ( ( ) => { } ) ,
2729 debug : mock ( ( ) => { } ) ,
2830 }
@@ -194,4 +196,48 @@ describe('/api/v1/gravity-index/search POST endpoint', () => {
194196 expect ( res . status ) . toBe ( 502 )
195197 expect ( await res . json ( ) ) . toEqual ( { error : 'bad request' } )
196198 } )
199+
200+ test ( 'redacts Gravity API key from upstream error responses and logs' , async ( ) => {
201+ mockFetch = Object . assign (
202+ mock ( async ( ) =>
203+ new Response (
204+ JSON . stringify ( {
205+ detail : [
206+ {
207+ input : {
208+ query : '' ,
209+ platform_api_key : 'gravity-key' ,
210+ } ,
211+ } ,
212+ ] ,
213+ } ) ,
214+ { status : 422 , headers : { 'Content-Type' : 'application/json' } } ,
215+ ) ,
216+ ) ,
217+ { preconnect : ( ) => { } } ,
218+ ) as typeof fetch
219+ const req = new NextRequest (
220+ 'http://localhost:3000/api/v1/gravity-index/search' ,
221+ {
222+ method : 'POST' ,
223+ headers : { Authorization : 'Bearer valid' } ,
224+ body : JSON . stringify ( { query : 'transactional email' } ) ,
225+ } ,
226+ )
227+
228+ const res = await postGravityIndexSearch ( {
229+ req,
230+ getUserInfoFromApiKey : mockGetUserInfoFromApiKey ,
231+ logger : mockLogger ,
232+ loggerWithContext : mockLoggerWithContext ,
233+ trackEvent : mockTrackEvent ,
234+ fetch : mockFetch ,
235+ serverEnv : testServerEnv ,
236+ } )
237+
238+ expect ( res . status ) . toBe ( 502 )
239+ expect ( JSON . stringify ( await res . json ( ) ) ) . not . toContain ( 'gravity-key' )
240+ expect ( JSON . stringify ( mockWarn . mock . calls ) ) . not . toContain ( 'gravity-key' )
241+ expect ( JSON . stringify ( mockWarn . mock . calls ) ) . toContain ( '[redacted]' )
242+ } )
197243} )
0 commit comments