16
16
*/
17
17
import '../test/setup' ;
18
18
import { expect } from 'chai' ;
19
- import { spy , stub } from 'sinon' ;
19
+ import { match , spy , stub } from 'sinon' ;
20
20
import {
21
21
setTokenAutoRefreshEnabled ,
22
22
initializeAppCheck ,
@@ -38,10 +38,12 @@ import * as logger from './logger';
38
38
import * as client from './client' ;
39
39
import * as storage from './storage' ;
40
40
import * as internalApi from './internal-api' ;
41
+ import * as indexeddb from './indexeddb' ;
41
42
import { deleteApp , FirebaseApp } from '@firebase/app' ;
42
43
import { CustomProvider , ReCaptchaV3Provider } from './providers' ;
43
44
import { AppCheckService } from './factory' ;
44
45
import { AppCheckToken } from './public-types' ;
46
+ import { getDebugToken } from './debug' ;
45
47
46
48
describe ( 'api' , ( ) => {
47
49
let app : FirebaseApp ;
@@ -118,6 +120,42 @@ describe('api', () => {
118
120
} )
119
121
) . to . equal ( appCheckInstance ) ;
120
122
} ) ;
123
+ it ( 'starts debug mode on first call' , async ( ) => {
124
+ const fakeWrite = ( ) :Promise < void > => Promise . resolve ( ) ;
125
+ const writeToIndexedDBStub = stub (
126
+ indexeddb ,
127
+ 'writeDebugTokenToIndexedDB'
128
+ ) . callsFake ( fakeWrite ) ;
129
+ stub (
130
+ indexeddb ,
131
+ 'readDebugTokenFromIndexedDB'
132
+ ) . callsFake ( ( ) => Promise . resolve ( undefined ) ) ;
133
+ const logStub = stub ( logger . logger , 'warn' ) ;
134
+ const consoleStub = stub ( console , 'log' ) ;
135
+ self . FIREBASE_APPCHECK_DEBUG_TOKEN = true ;
136
+ initializeAppCheck ( app , {
137
+ provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
138
+ } ) ;
139
+ await fakeWrite ( ) ;
140
+ const token = writeToIndexedDBStub . args [ 0 ] ;
141
+ expect ( logStub ) . to . not . be . called ;
142
+ expect ( consoleStub . args [ 0 ] [ 0 ] ) . to . include ( token ) ;
143
+ self . FIREBASE_APPCHECK_DEBUG_TOKEN = undefined ;
144
+ } ) ;
145
+ it ( 'warns about debug mode on second call' , async ( ) => {
146
+ const logStub = stub ( logger . logger , 'warn' ) ;
147
+ self . FIREBASE_APPCHECK_DEBUG_TOKEN = 'abcdefg' ;
148
+ initializeAppCheck ( app , {
149
+ provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
150
+ } ) ;
151
+ initializeAppCheck ( app , {
152
+ provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
153
+ } ) ;
154
+ const token = await getDebugToken ( ) ;
155
+ expect ( token ) . to . equal ( 'abcdefg' ) ;
156
+ expect ( logStub ) . to . be . calledWith ( match ( 'abcdefg' ) ) ;
157
+ self . FIREBASE_APPCHECK_DEBUG_TOKEN = undefined ;
158
+ } ) ;
121
159
122
160
it ( 'initialize reCAPTCHA when a ReCaptchaV3Provider is provided' , ( ) => {
123
161
const initReCAPTCHAStub = stub ( reCAPTCHA , 'initialize' ) . returns (
0 commit comments