16
16
17
17
'use strict' ;
18
18
19
+ import * as jwt from 'jsonwebtoken' ;
19
20
import * as _ from 'lodash' ;
20
21
import * as chai from 'chai' ;
21
22
import * as sinon from 'sinon' ;
@@ -28,7 +29,6 @@ import * as mocks from '../../resources/mocks';
28
29
import { Auth , TenantAwareAuth , BaseAuth , DecodedIdToken } from '../../../src/auth/auth' ;
29
30
import { UserRecord } from '../../../src/auth/user-record' ;
30
31
import { FirebaseApp } from '../../../src/firebase-app' ;
31
- import { FirebaseTokenGenerator } from '../../../src/auth/token-generator' ;
32
32
import {
33
33
AuthRequestHandler , TenantAwareAuthRequestHandler , AbstractAuthRequestHandler ,
34
34
} from '../../../src/auth/auth-api-request' ;
@@ -328,63 +328,49 @@ AUTH_CONFIGS.forEach((testConfig) => {
328
328
}
329
329
330
330
describe ( 'createCustomToken()' , ( ) => {
331
- let spy : sinon . SinonSpy ;
332
- beforeEach ( ( ) => {
333
- spy = sinon . spy ( FirebaseTokenGenerator . prototype , 'createCustomToken' ) ;
334
- } ) ;
335
-
336
- afterEach ( ( ) => {
337
- spy . restore ( ) ;
331
+ it ( 'should return a jwt' , async ( ) => {
332
+ const token = await auth . createCustomToken ( 'uid1' ) ;
333
+ const decodedToken = jwt . decode ( token , { complete : true } ) ;
334
+ expect ( decodedToken ) . to . have . property ( 'header' ) . that . has . property ( 'typ' , 'JWT' ) ;
338
335
} ) ;
339
336
340
337
if ( testConfig . Auth === TenantAwareAuth ) {
341
- it ( 'should reject with an unsupported tenant operation error' , ( ) => {
342
- const expectedError = new FirebaseAuthError ( AuthClientErrorCode . UNSUPPORTED_TENANT_OPERATION ) ;
343
- return auth . createCustomToken ( mocks . uid )
344
- . then ( ( ) => {
345
- throw new Error ( 'Unexpected success' ) ;
346
- } )
347
- . catch ( ( error ) => {
348
- expect ( error ) . to . deep . equal ( expectedError ) ;
349
- } ) ;
338
+ it ( 'should contain tenant_id' , async ( ) => {
339
+ const token = await auth . createCustomToken ( 'uid1' ) ;
340
+ expect ( jwt . decode ( token ) ) . to . have . property ( 'tenant_id' , TENANT_ID ) ;
350
341
} ) ;
351
342
} else {
352
- it ( 'should throw if a cert credential is not specified' , ( ) => {
353
- const mockCredentialAuth = testConfig . init ( mocks . mockCredentialApp ( ) ) ;
354
-
355
- expect ( ( ) => {
356
- mockCredentialAuth . createCustomToken ( mocks . uid , mocks . developerClaims ) ;
357
- } ) . not . to . throw ;
343
+ it ( 'should not contain tenant_id' , async ( ) => {
344
+ const token = await auth . createCustomToken ( 'uid1' ) ;
345
+ expect ( jwt . decode ( token ) ) . to . not . have . property ( 'tenant_id' ) ;
358
346
} ) ;
347
+ }
359
348
360
- it ( 'should forward on the call to the token generator\'s createCustomToken() method' , ( ) => {
361
- const developerClaimsCopy = deepCopy ( mocks . developerClaims ) ;
362
- return auth . createCustomToken ( mocks . uid , mocks . developerClaims )
363
- . then ( ( ) => {
364
- expect ( spy )
365
- . to . have . been . calledOnce
366
- . and . calledWith ( mocks . uid , developerClaimsCopy ) ;
367
- } ) ;
368
- } ) ;
349
+ it ( 'should throw if a cert credential is not specified' , ( ) => {
350
+ const mockCredentialAuth = testConfig . init ( mocks . mockCredentialApp ( ) ) ;
369
351
370
- it ( 'should be fulfilled given an app which returns null access tokens' , ( ) => {
371
- // createCustomToken() does not rely on an access token and therefore works in this scenario.
372
- return nullAccessTokenAuth . createCustomToken ( mocks . uid , mocks . developerClaims )
373
- . should . eventually . be . fulfilled ;
374
- } ) ;
352
+ expect ( ( ) => {
353
+ mockCredentialAuth . createCustomToken ( mocks . uid , mocks . developerClaims ) ;
354
+ } ) . not . to . throw ;
355
+ } ) ;
375
356
376
- it ( 'should be fulfilled given an app which returns invalid access tokens' , ( ) => {
377
- // createCustomToken() does not rely on an access token and therefore works in this scenario.
378
- return malformedAccessTokenAuth . createCustomToken ( mocks . uid , mocks . developerClaims )
379
- . should . eventually . be . fulfilled ;
380
- } ) ;
357
+ it ( 'should be fulfilled given an app which returns null access tokens' , ( ) => {
358
+ // createCustomToken() does not rely on an access token and therefore works in this scenario.
359
+ return nullAccessTokenAuth . createCustomToken ( mocks . uid , mocks . developerClaims )
360
+ . should . eventually . be . fulfilled ;
361
+ } ) ;
381
362
382
- it ( 'should be fulfilled given an app which fails to generate access tokens' , ( ) => {
383
- // createCustomToken() does not rely on an access token and therefore works in this scenario.
384
- return rejectedPromiseAccessTokenAuth . createCustomToken ( mocks . uid , mocks . developerClaims )
385
- . should . eventually . be . fulfilled ;
386
- } ) ;
387
- }
363
+ it ( 'should be fulfilled given an app which returns invalid access tokens' , ( ) => {
364
+ // createCustomToken() does not rely on an access token and therefore works in this scenario.
365
+ return malformedAccessTokenAuth . createCustomToken ( mocks . uid , mocks . developerClaims )
366
+ . should . eventually . be . fulfilled ;
367
+ } ) ;
368
+
369
+ it ( 'should be fulfilled given an app which fails to generate access tokens' , ( ) => {
370
+ // createCustomToken() does not rely on an access token and therefore works in this scenario.
371
+ return rejectedPromiseAccessTokenAuth . createCustomToken ( mocks . uid , mocks . developerClaims )
372
+ . should . eventually . be . fulfilled ;
373
+ } ) ;
388
374
} ) ;
389
375
390
376
it ( 'verifyIdToken() should throw when project ID is not specified' , ( ) => {
0 commit comments