Skip to content

Commit b837c72

Browse files
committed
Address PR comments
1 parent 36dd305 commit b837c72

File tree

3 files changed

+26
-138
lines changed

3 files changed

+26
-138
lines changed

packages/app-check/src/api.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
import '../test/setup';
1818
import { expect } from 'chai';
19-
import { stub, spy, SinonStub } from 'sinon';
19+
import { stub, spy } from 'sinon';
2020
import {
2121
activate,
2222
setTokenAutoRefreshEnabled,
@@ -131,8 +131,6 @@ describe('api', () => {
131131
});
132132
});
133133
describe('onTokenChanged()', () => {
134-
let storageReadStub: SinonStub;
135-
let storageWriteStub: SinonStub;
136134
const fakePlatformLoggingProvider = getFakePlatformLoggingProvider();
137135
const fakeRecaptchaToken = 'fake-recaptcha-token';
138136
const fakeRecaptchaAppCheckToken = {
@@ -142,14 +140,10 @@ describe('api', () => {
142140
};
143141

144142
beforeEach(() => {
145-
storageReadStub = stub(storage, 'readTokenFromStorage').resolves(
146-
undefined
147-
);
148-
storageWriteStub = stub(storage, 'writeTokenToStorage');
143+
stub(storage, 'readTokenFromStorage').resolves(undefined);
144+
stub(storage, 'writeTokenToStorage');
149145
});
150146
afterEach(() => {
151-
storageReadStub.restore();
152-
storageWriteStub.restore();
153147
clearState();
154148
removegreCAPTCHAScriptsOnPage();
155149
});

packages/app-check/src/internal-api.test.ts

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ describe('internal api', () => {
5959
});
6060

6161
afterEach(async () => {
62-
storageReadStub.restore();
63-
storageWriteStub.restore();
6462
clearState();
6563
removegreCAPTCHAScriptsOnPage();
6664
});
@@ -200,9 +198,7 @@ describe('internal api', () => {
200198
activate(app, FAKE_SITE_KEY, false);
201199
stub(reCAPTCHA, 'getToken').resolves(fakeRecaptchaToken);
202200
stub(client, 'exchangeToken').resolves(fakeRecaptchaAppCheckToken);
203-
const listener1 = (): void => {
204-
throw new Error();
205-
};
201+
const listener1 = stub().throws(new Error());
206202
const listener2 = spy();
207203

208204
const errorFn1 = spy();
@@ -213,6 +209,7 @@ describe('internal api', () => {
213209
await getToken(app, fakePlatformLoggingProvider);
214210

215211
expect(errorFn1).not.to.be.called;
212+
expect(listener1).to.be.called;
216213
expect(listener2).to.be.called;
217214
});
218215

@@ -237,7 +234,6 @@ describe('internal api', () => {
237234

238235
stub(reCAPTCHA, 'getToken').resolves(fakeRecaptchaToken);
239236
stub(client, 'exchangeToken').resolves(fakeRecaptchaAppCheckToken);
240-
storageWriteStub.resetHistory();
241237
const result = await getToken(app, fakePlatformLoggingProvider);
242238
expect(result).to.deep.equal({ token: fakeRecaptchaAppCheckToken.token });
243239
expect(storageWriteStub).has.been.calledWith(
@@ -289,58 +285,6 @@ describe('internal api', () => {
289285
);
290286
expect(token).to.deep.equal({ token: fakeRecaptchaAppCheckToken.token });
291287
});
292-
293-
it('reads any memory-cached debug token if in debug mode', async () => {
294-
storageReadStub.resetHistory();
295-
const clientStub = stub(client, 'exchangeToken');
296-
const debugState = getDebugState();
297-
debugState.enabled = true;
298-
debugState.token = new Deferred();
299-
debugState.token.resolve('my-debug-token');
300-
activate(app, FAKE_SITE_KEY);
301-
const state = getState(app);
302-
setState(app, { ...state, token: fakeCachedAppCheckToken });
303-
const token = await getToken(app, fakePlatformLoggingProvider);
304-
expect(token).to.deep.equal({
305-
token: fakeCachedAppCheckToken.token
306-
});
307-
expect(storageReadStub).has.not.been.called;
308-
expect(clientStub).has.not.been.called;
309-
});
310-
311-
it('reads any indexedDB cached debug token if in debug mode and no token in memory', async () => {
312-
storageReadStub.resolves(fakeCachedAppCheckToken);
313-
const clientStub = stub(client, 'exchangeToken');
314-
const debugState = getDebugState();
315-
debugState.enabled = true;
316-
debugState.token = new Deferred();
317-
debugState.token.resolve('my-debug-token');
318-
activate(app, FAKE_SITE_KEY);
319-
expect(getState(app).token).to.equal(undefined);
320-
const result = await getToken(app, fakePlatformLoggingProvider);
321-
expect(result).to.deep.equal({
322-
token: fakeCachedAppCheckToken.token
323-
});
324-
expect(getState(app).token).to.equal(fakeCachedAppCheckToken);
325-
expect(clientStub).has.not.been.called;
326-
});
327-
328-
it('persists debug token to indexedDB storage', async () => {
329-
activate(app, FAKE_SITE_KEY);
330-
331-
stub(client, 'exchangeToken').resolves(fakeRecaptchaAppCheckToken);
332-
storageWriteStub.resetHistory();
333-
const debugState = getDebugState();
334-
debugState.enabled = true;
335-
debugState.token = new Deferred();
336-
debugState.token.resolve('my-debug-token');
337-
const result = await getToken(app, fakePlatformLoggingProvider);
338-
expect(result).to.deep.equal({ token: fakeRecaptchaAppCheckToken.token });
339-
expect(storageWriteStub).has.been.calledWith(
340-
app,
341-
fakeRecaptchaAppCheckToken
342-
);
343-
});
344288
});
345289

346290
describe('addTokenListener', () => {
@@ -413,37 +357,6 @@ describe('internal api', () => {
413357

414358
addTokenListener(app, fakePlatformLoggingProvider, fakeListener);
415359
});
416-
417-
it('notifies the listener with the debug token immediately', async () => {
418-
stub(client, 'exchangeToken').resolves(fakeRecaptchaAppCheckToken);
419-
const listener = stub();
420-
421-
const clock = useFakeTimers();
422-
423-
const debugState = getDebugState();
424-
debugState.enabled = true;
425-
debugState.token = new Deferred();
426-
debugState.token.resolve('my-debug-token');
427-
428-
activate(app, FAKE_SITE_KEY, false);
429-
addTokenListener(app, fakePlatformLoggingProvider, listener);
430-
await clock.runAllAsync();
431-
expect(listener).to.be.calledWith({ token: 'my-debug-token' });
432-
clock.restore();
433-
});
434-
435-
it('does NOT start token refresher in debug mode', () => {
436-
const debugState = getDebugState();
437-
debugState.enabled = true;
438-
debugState.token = new Deferred();
439-
debugState.token.resolve('my-debug-token');
440-
441-
activate(app, FAKE_SITE_KEY, true);
442-
addTokenListener(app, fakePlatformLoggingProvider, () => {});
443-
444-
const state = getState(app);
445-
expect(state.tokenRefresher).is.undefined;
446-
});
447360
});
448361

449362
describe('removeTokenListener', () => {

packages/app-check/src/internal-api.ts

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
import {
2525
AppCheckTokenInternal,
2626
AppCheckTokenObserver,
27-
getDebugState,
2827
getState,
2928
setState
3029
} from './state';
@@ -191,48 +190,30 @@ export function addTokenListener(
191190
};
192191

193192
/**
194-
* DEBUG MODE
195-
*
196-
* invoke the listener once with the debug token.
193+
* Invoke the listener with the valid token, then start the token refresher
197194
*/
198-
if (isDebugMode()) {
199-
const debugState = getDebugState();
200-
if (debugState.enabled && debugState.token) {
201-
debugState.token.promise
202-
.then(token => listener({ token }))
203-
.catch(() => {
204-
/** Ignore errors in listeners. */
205-
});
206-
}
207-
} else {
208-
/**
209-
* PROD MODE
210-
*
211-
* invoke the listener with the valid token, then start the token refresher
212-
*/
213-
if (!newState.tokenRefresher) {
214-
const tokenRefresher = createTokenRefresher(app, platformLoggerProvider);
215-
newState.tokenRefresher = tokenRefresher;
216-
}
195+
if (!newState.tokenRefresher) {
196+
const tokenRefresher = createTokenRefresher(app, platformLoggerProvider);
197+
newState.tokenRefresher = tokenRefresher;
198+
}
217199

218-
// Create the refresher but don't start it if `isTokenAutoRefreshEnabled`
219-
// is not true.
220-
if (
221-
!newState.tokenRefresher.isRunning() &&
222-
state.isTokenAutoRefreshEnabled === true
223-
) {
224-
newState.tokenRefresher.start();
225-
}
200+
// Create the refresher but don't start it if `isTokenAutoRefreshEnabled`
201+
// is not true.
202+
if (
203+
!newState.tokenRefresher.isRunning() &&
204+
state.isTokenAutoRefreshEnabled === true
205+
) {
206+
newState.tokenRefresher.start();
207+
}
226208

227-
// invoke the listener async immediately if there is a valid token
228-
if (state.token && isValid(state.token)) {
229-
const validToken = state.token;
230-
Promise.resolve()
231-
.then(() => listener({ token: validToken.token }))
232-
.catch(() => {
233-
/** Ignore errors in listeners. */
234-
});
235-
}
209+
// invoke the listener async immediately if there is a valid token
210+
if (state.token && isValid(state.token)) {
211+
const validToken = state.token;
212+
Promise.resolve()
213+
.then(() => listener({ token: validToken.token }))
214+
.catch(() => {
215+
/** Ignore errors in listeners. */
216+
});
236217
}
237218

238219
setState(app, newState);

0 commit comments

Comments
 (0)