@@ -234,7 +234,9 @@ describe('DataConnectApiClient', () => {
234
234
235
235
describe ( 'impersonateQuery' , ( ) => {
236
236
const unauthenticatedOptions : GraphqlOptions < unknown > =
237
- { operationName : 'operationName' , impersonate : { unauthenticated : true } } ;
237
+ { operationName : 'unauthenticatedQuery' , impersonate : { unauthenticated : true } } ;
238
+ const authenticatedOptions : GraphqlOptions < unknown > =
239
+ { operationName : 'authenticatedQuery' , impersonate : { authClaims : { sub : 'authenticated-UUID' } } } ;
238
240
239
241
it ( 'should reject when no operationName is provided' , ( ) => {
240
242
apiClient . impersonateQuery ( { impersonate : { unauthenticated : true } } )
@@ -300,7 +302,7 @@ describe('DataConnectApiClient', () => {
300
302
. should . eventually . be . rejected . and . deep . include ( expected ) ;
301
303
} ) ;
302
304
303
- it ( 'should resolve with the GraphQL response on success' , ( ) => {
305
+ describe ( 'should resolve with the GraphQL response on success' , ( ) => {
304
306
interface UsersResponse {
305
307
users : [
306
308
user : {
@@ -310,11 +312,11 @@ describe('DataConnectApiClient', () => {
310
312
}
311
313
] ;
312
314
}
313
- const stub = sandbox
314
- . stub ( HttpClient . prototype , 'send' )
315
- . resolves ( utils . responseFrom ( TEST_RESPONSE , 200 ) ) ;
316
- return apiClient . impersonateQuery < UsersResponse , unknown > ( unauthenticatedOptions )
317
- . then ( ( resp ) => {
315
+ it ( 'for an unauthenticated request' , ( ) => {
316
+ const stub = sandbox
317
+ . stub ( HttpClient . prototype , 'send' )
318
+ . resolves ( utils . responseFrom ( TEST_RESPONSE , 200 ) ) ;
319
+ return apiClient . impersonateQuery < UsersResponse , unknown > ( unauthenticatedOptions ) . then ( ( resp ) => {
318
320
expect ( resp . data . users ) . to . be . not . empty ;
319
321
expect ( resp . data . users [ 0 ] . name ) . to . be . not . undefined ;
320
322
expect ( resp . data . users [ 0 ] . address ) . to . be . not . undefined ;
@@ -329,6 +331,27 @@ describe('DataConnectApiClient', () => {
329
331
}
330
332
} ) ;
331
333
} ) ;
334
+ } ) ;
335
+ it ( 'for an authenticated request' , ( ) => {
336
+ const stub = sandbox
337
+ . stub ( HttpClient . prototype , 'send' )
338
+ . resolves ( utils . responseFrom ( TEST_RESPONSE , 200 ) ) ;
339
+ return apiClient . impersonateQuery < UsersResponse , unknown > ( authenticatedOptions ) . then ( ( resp ) => {
340
+ expect ( resp . data . users ) . to . be . not . empty ;
341
+ expect ( resp . data . users [ 0 ] . name ) . to . be . not . undefined ;
342
+ expect ( resp . data . users [ 0 ] . address ) . to . be . not . undefined ;
343
+ expect ( resp . data . users ) . to . deep . equal ( TEST_RESPONSE . data . users ) ;
344
+ expect ( stub ) . to . have . been . calledOnce . and . calledWith ( {
345
+ method : 'POST' ,
346
+ url : `https://firebasedataconnect.googleapis.com/v1alpha/projects/test-project/locations/${ connectorConfig . location } /services/${ connectorConfig . serviceId } /connectors/${ connectorConfig . connector } :impersonateQuery` ,
347
+ headers : EXPECTED_HEADERS ,
348
+ data : {
349
+ operationName : authenticatedOptions . operationName ,
350
+ extensions : { impersonate : authenticatedOptions . impersonate }
351
+ }
352
+ } ) ;
353
+ } ) ;
354
+ } ) ;
332
355
} ) ;
333
356
334
357
it ( 'should use DATA_CONNECT_EMULATOR_HOST if set' , ( ) => {
@@ -351,10 +374,12 @@ describe('DataConnectApiClient', () => {
351
374
} ) ;
352
375
} ) ;
353
376
354
- describe ( 'impersonateMutation' , ( ) => {
355
- const unauthenticatedOptions : GraphqlOptions < unknown > =
356
- { operationName : 'operationName' , impersonate : { unauthenticated : true } } ;
377
+ const unauthenticatedOptions : GraphqlOptions < unknown > =
378
+ { operationName : 'operationName' , impersonate : { unauthenticated : true } } ;
379
+ const authenticatedOptions : GraphqlOptions < unknown > =
380
+ { operationName : 'operationName' , impersonate : { unauthenticated : true } } ;
357
381
382
+ describe ( 'impersonateMutation' , ( ) => {
358
383
it ( 'should reject when no operationName is provided' , ( ) => {
359
384
apiClient . impersonateMutation ( { impersonate : { unauthenticated : true } } )
360
385
. should . eventually . be . rejectedWith ( '`query` must be a non-empty string.' ) ;
@@ -419,7 +444,61 @@ describe('DataConnectApiClient', () => {
419
444
. should . eventually . be . rejected . and . deep . include ( expected ) ;
420
445
} ) ;
421
446
422
- it ( 'should resolve with the GraphQL response on success' , ( ) => {
447
+ describe ( 'should resolve with the GraphQL response on success' , ( ) => {
448
+ interface UsersResponse {
449
+ users : [
450
+ user : {
451
+ id : string ;
452
+ name : string ;
453
+ address : string ;
454
+ }
455
+ ] ;
456
+ }
457
+ it ( 'for an unauthenticated request' , ( ) => {
458
+ const stub = sandbox
459
+ . stub ( HttpClient . prototype , 'send' )
460
+ . resolves ( utils . responseFrom ( TEST_RESPONSE , 200 ) ) ;
461
+ return apiClient . impersonateMutation < UsersResponse , unknown > ( unauthenticatedOptions )
462
+ . then ( ( resp ) => {
463
+ expect ( resp . data . users ) . to . be . not . empty ;
464
+ expect ( resp . data . users [ 0 ] . name ) . to . be . not . undefined ;
465
+ expect ( resp . data . users [ 0 ] . address ) . to . be . not . undefined ;
466
+ expect ( resp . data . users ) . to . deep . equal ( TEST_RESPONSE . data . users ) ;
467
+ expect ( stub ) . to . have . been . calledOnce . and . calledWith ( {
468
+ method : 'POST' ,
469
+ url : `https://firebasedataconnect.googleapis.com/v1alpha/projects/test-project/locations/${ connectorConfig . location } /services/${ connectorConfig . serviceId } /connectors/${ connectorConfig . connector } :impersonateMutation` ,
470
+ headers : EXPECTED_HEADERS ,
471
+ data : {
472
+ operationName : unauthenticatedOptions . operationName ,
473
+ extensions : { impersonate : unauthenticatedOptions . impersonate }
474
+ }
475
+ } ) ;
476
+ } ) ;
477
+ } ) ;
478
+ it ( 'for an authenticated request' , ( ) => {
479
+ const stub = sandbox
480
+ . stub ( HttpClient . prototype , 'send' )
481
+ . resolves ( utils . responseFrom ( TEST_RESPONSE , 200 ) ) ;
482
+ return apiClient . impersonateMutation < UsersResponse , unknown > ( authenticatedOptions )
483
+ . then ( ( resp ) => {
484
+ expect ( resp . data . users ) . to . be . not . empty ;
485
+ expect ( resp . data . users [ 0 ] . name ) . to . be . not . undefined ;
486
+ expect ( resp . data . users [ 0 ] . address ) . to . be . not . undefined ;
487
+ expect ( resp . data . users ) . to . deep . equal ( TEST_RESPONSE . data . users ) ;
488
+ expect ( stub ) . to . have . been . calledOnce . and . calledWith ( {
489
+ method : 'POST' ,
490
+ url : `https://firebasedataconnect.googleapis.com/v1alpha/projects/test-project/locations/${ connectorConfig . location } /services/${ connectorConfig . serviceId } /connectors/${ connectorConfig . connector } :impersonateMutation` ,
491
+ headers : EXPECTED_HEADERS ,
492
+ data : {
493
+ operationName : authenticatedOptions . operationName ,
494
+ extensions : { impersonate : authenticatedOptions . impersonate }
495
+ }
496
+ } ) ;
497
+ } ) ;
498
+ } ) ;
499
+ } ) ;
500
+
501
+ it ( 'should resolve with the GraphQL response on success for an authenticated request' , ( ) => {
423
502
interface UsersResponse {
424
503
users : [
425
504
user : {
0 commit comments