@@ -55,6 +55,7 @@ import {
55
55
import { BaseExternalAccountClient } from '../src/auth/baseexternalclient' ;
56
56
import { AuthClient , DEFAULT_UNIVERSE } from '../src/auth/authclient' ;
57
57
import { ExternalAccountAuthorizedUserClient } from '../src/auth/externalAccountAuthorizedUserClient' ;
58
+ import { stringify } from 'querystring' ;
58
59
59
60
nock . disableNetConnect ( ) ;
60
61
@@ -1520,37 +1521,80 @@ describe('googleauth', () => {
1520
1521
assert ( client . idTokenProvider instanceof JWT ) ;
1521
1522
} ) ;
1522
1523
1523
- it ( 'should call getClient for getIdTokenClient' , async ( ) => {
1524
+ it ( 'should return a UserRefreshClient client for getIdTokenClient' , async ( ) => {
1524
1525
// Set up a mock to return path to a valid credentials file.
1525
1526
mockEnvVar (
1526
1527
'GOOGLE_APPLICATION_CREDENTIALS' ,
1527
- './test/fixtures/private .json'
1528
+ './test/fixtures/refresh .json'
1528
1529
) ;
1530
+ mockEnvVar ( 'GOOGLE_CLOUD_PROJECT' , 'some-project-id' ) ;
1529
1531
1530
- const spy = sinon . spy ( auth , 'getClient' ) ;
1531
1532
const client = await auth . getIdTokenClient ( 'a-target-audience' ) ;
1532
1533
assert ( client instanceof IdTokenClient ) ;
1533
- assert ( spy . calledOnce ) ;
1534
+ assert ( client . idTokenProvider instanceof UserRefreshClient ) ;
1534
1535
} ) ;
1535
1536
1536
- it ( 'should fail when using UserRefreshClient' , async ( ) => {
1537
+ it ( 'should properly use ` UserRefreshClient` client for `getIdTokenClient` ' , async ( ) => {
1537
1538
// Set up a mock to return path to a valid credentials file.
1538
1539
mockEnvVar (
1539
1540
'GOOGLE_APPLICATION_CREDENTIALS' ,
1540
1541
'./test/fixtures/refresh.json'
1541
1542
) ;
1542
1543
mockEnvVar ( 'GOOGLE_CLOUD_PROJECT' , 'some-project-id' ) ;
1543
1544
1544
- try {
1545
- await auth . getIdTokenClient ( 'a-target-audience' ) ;
1546
- } catch ( e ) {
1547
- assert ( e instanceof Error ) ;
1548
- assert (
1549
- e . message . startsWith ( 'Cannot fetch ID token in this environment' )
1550
- ) ;
1551
- return ;
1552
- }
1553
- assert . fail ( 'failed to throw' ) ;
1545
+ // Assert `UserRefreshClient`
1546
+ const baseClient = await auth . getClient ( ) ;
1547
+ assert ( baseClient instanceof UserRefreshClient ) ;
1548
+
1549
+ // Setup variables
1550
+ const idTokenPayload = Buffer . from ( JSON . stringify ( { exp : 100 } ) ) . toString (
1551
+ 'base64'
1552
+ ) ;
1553
+ const testIdToken = `TEST.${ idTokenPayload } .TOKEN` ;
1554
+ const targetAudience = 'a-target-audience' ;
1555
+ const tokenEndpoint = new URL ( baseClient . endpoints . oauth2TokenUrl ) ;
1556
+ const expectedTokenRequestBody = stringify ( {
1557
+ client_id : baseClient . _clientId ,
1558
+ client_secret : baseClient . _clientSecret ,
1559
+ grant_type : 'refresh_token' ,
1560
+ refresh_token : baseClient . _refreshToken ,
1561
+ target_audience : targetAudience ,
1562
+ } ) ;
1563
+ const url = new URL ( 'https://my-protected-endpoint.a.app' ) ;
1564
+ const expectedRes = { hello : true } ;
1565
+
1566
+ // Setup mock endpoints
1567
+ nock ( tokenEndpoint . origin )
1568
+ . post ( tokenEndpoint . pathname , expectedTokenRequestBody )
1569
+ . reply ( 200 , { id_token : testIdToken } ) ;
1570
+ nock ( url . origin , {
1571
+ reqheaders : {
1572
+ authorization : `Bearer ${ testIdToken } ` ,
1573
+ } ,
1574
+ } )
1575
+ . get ( url . pathname )
1576
+ . reply ( 200 , expectedRes ) ;
1577
+
1578
+ // Make assertions
1579
+ const client = await auth . getIdTokenClient ( targetAudience ) ;
1580
+ assert ( client instanceof IdTokenClient ) ;
1581
+ assert ( client . idTokenProvider instanceof UserRefreshClient ) ;
1582
+
1583
+ const res = await client . request ( { url} ) ;
1584
+ assert . deepStrictEqual ( res . data , expectedRes ) ;
1585
+ } ) ;
1586
+
1587
+ it ( 'should call getClient for getIdTokenClient' , async ( ) => {
1588
+ // Set up a mock to return path to a valid credentials file.
1589
+ mockEnvVar (
1590
+ 'GOOGLE_APPLICATION_CREDENTIALS' ,
1591
+ './test/fixtures/private.json'
1592
+ ) ;
1593
+
1594
+ const spy = sinon . spy ( auth , 'getClient' ) ;
1595
+ const client = await auth . getIdTokenClient ( 'a-target-audience' ) ;
1596
+ assert ( client instanceof IdTokenClient ) ;
1597
+ assert ( spy . calledOnce ) ;
1554
1598
} ) ;
1555
1599
1556
1600
describe ( 'getUniverseDomain' , ( ) => {
0 commit comments