Skip to content

Commit 6ad7c8f

Browse files
committed
Adding unit test
1 parent 2a79406 commit 6ad7c8f

File tree

5 files changed

+83
-28
lines changed

5 files changed

+83
-28
lines changed

packages/auth/src/core/auth/auth_impl.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ describe('core/auth/auth_impl', () => {
353353
it('should return the existing token if it is valid', async () => {
354354
persistenceStub._get.withArgs(tokenKey).resolves(mockToken as any);
355355
const token = await auth.getFirebaseAccessToken();
356-
expect(token).to.eql(mockToken);
356+
expect(token).to.eql('test-token');
357357
expect(exchangeTokenStub).not.to.have.been.called;
358358
});
359359

@@ -381,7 +381,7 @@ describe('core/auth/auth_impl', () => {
381381
'test-idp',
382382
'new-id-token'
383383
);
384-
expect(token).to.eql(mockToken);
384+
expect(token).to.eql('test-token');
385385
});
386386

387387
it('should force refresh the token when forceRefresh is true', async () => {
@@ -449,7 +449,7 @@ describe('core/auth/auth_impl', () => {
449449
await regionalAuth._updateFirebaseToken(token);
450450
await regionalAuth.signOut();
451451
expect(persistenceStub._remove).to.have.been.called;
452-
expect(regionalAuth.getFirebaseAccessToken()).to.be.null;
452+
expect(await regionalAuth.getFirebaseAccessToken()).to.be.null;
453453
});
454454
it('is blocked if a beforeAuthStateChanged callback throws', async () => {
455455
await auth._updateCurrentUser(testUser(auth, 'test'));

packages/auth/src/core/auth/firebase_internal.test.ts

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
import { FirebaseError } from '@firebase/util';
1919
import { expect, use } from 'chai';
2020
import * as sinon from 'sinon';
21+
import * as mockFetch from '../../../test/helpers/mock_fetch';
2122
import sinonChai from 'sinon-chai';
2223
import chaiAsPromised from 'chai-as-promised';
2324

2425
import {
2526
regionalTestAuth,
27+
regionalTestAuthWithTokenRefreshHandler,
2628
testAuth,
2729
testUser
2830
} from '../../../test/helpers/mock_auth';
31+
import { RegionalEndpoint } from '../../api';
32+
import { mockRegionalEndpointWithParent } from '../../../test/helpers/api/helper';
2933
import { AuthInternal } from '../../model/auth';
3034
import { UserInternal } from '../../model/user';
3135
import { AuthInterop } from './firebase_internal';
@@ -227,17 +231,32 @@ describe('core/auth/firebase_internal', () => {
227231

228232
describe('core/auth/firebase_internal - Regional Firebase Auth', () => {
229233
let regionalAuth: AuthInternal;
234+
let regionalAuthWithRefreshToken: AuthInternal;
230235
let regionalAuthInternal: AuthInterop;
236+
let regionalAuthWithRefreshTokenInternal: AuthInterop;
231237
let now: number;
232238
beforeEach(async () => {
233239
regionalAuth = await regionalTestAuth();
234240
regionalAuthInternal = new AuthInterop(regionalAuth);
241+
regionalAuthWithRefreshToken =
242+
await regionalTestAuthWithTokenRefreshHandler();
243+
regionalAuthWithRefreshTokenInternal = new AuthInterop(
244+
regionalAuthWithRefreshToken
245+
);
235246
now = Date.now();
236247
sinon.stub(Date, 'now').returns(now);
248+
mockFetch.setUp();
249+
mockRegionalEndpointWithParent(
250+
RegionalEndpoint.EXCHANGE_TOKEN,
251+
'projects/test-project-id/locations/us/tenants/tenant-1/idpConfigs/idp-config',
252+
'test-api-key',
253+
{ accessToken: 'access-token-new', expiresIn: 10_000 }
254+
);
237255
});
238256

239257
afterEach(() => {
240258
sinon.restore();
259+
mockFetch.tearDown();
241260
});
242261

243262
context('getFirebaseToken', () => {
@@ -265,7 +284,8 @@ describe('core/auth/firebase_internal - Regional Firebase Auth', () => {
265284
expirationTime: now - 5_000
266285
});
267286
expect(await regionalAuthInternal.getToken()).to.null;
268-
expect(regionalAuth.firebaseToken).to.null;
287+
const firebaseToken = await regionalAuth.getFirebaseAccessToken();
288+
expect(firebaseToken).to.null;
269289
});
270290

271291
it('logs out if token is expiring in next 5 seconds', async () => {
@@ -274,24 +294,36 @@ describe('core/auth/firebase_internal - Regional Firebase Auth', () => {
274294
expirationTime: now + 5_000
275295
});
276296
expect(await regionalAuthInternal.getToken()).to.null;
277-
expect(regionalAuth.firebaseToken).to.null;
297+
const firebaseToken = await regionalAuth.getFirebaseAccessToken();
298+
expect(firebaseToken).to.null;
278299
});
279300

280-
it('logs warning if getToken is called with forceRefresh true', async () => {
281-
sinon.stub(console, 'warn');
282-
await regionalAuth._updateFirebaseToken({
301+
it('returns refreshIdToken if getToken is called with forceRefresh true', async () => {
302+
await regionalAuthWithRefreshToken._updateFirebaseToken({
283303
token: 'access-token',
284-
expirationTime: now + 300_000
304+
expirationTime: now + 30_000
285305
});
286-
expect(await regionalAuthInternal.getToken(true)).to.eql({
287-
accessToken: 'access-token'
306+
expect(await regionalAuthWithRefreshTokenInternal.getToken(true)).to.eql({
307+
accessToken: 'access-token-new'
288308
});
289-
expect(console.warn).to.have.been.calledWith(
290-
sinon.match.string,
291-
sinon.match(
292-
/Refresh token is not a valid operation for Regional Auth instance initialized\./
293-
)
294-
);
309+
});
310+
311+
it('returns refreshIdToken if current idToken is expired', async () => {
312+
await regionalAuthWithRefreshToken._updateFirebaseToken({
313+
token: 'access-token',
314+
expirationTime: now - 5_000
315+
});
316+
expect(await regionalAuthWithRefreshTokenInternal.getToken()).to.eql({
317+
accessToken: 'access-token-new'
318+
});
319+
});
320+
321+
it('returns null if current idToken is expired and tokenRefreshHandler is not implemented', async () => {
322+
await regionalAuth._updateFirebaseToken({
323+
token: 'access-token',
324+
expirationTime: now - 5_000
325+
});
326+
expect(await regionalAuthInternal.getToken()).to.eql(null);
295327
});
296328
});
297329
});

packages/auth/src/core/auth/firebase_internal.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { AuthInternal } from '../../model/auth';
2222
import { UserInternal } from '../../model/user';
2323
import { _assert } from '../util/assert';
2424
import { AuthErrorCode } from '../errors';
25-
import { _logWarn } from '../util/log';
2625

2726
interface TokenListener {
2827
(tok: string | null): unknown;
@@ -46,11 +45,6 @@ export class AuthInterop implements FirebaseAuthInternal {
4645
this.assertAuthConfigured();
4746
await this.auth._initializationPromise;
4847
if (this.auth.tenantConfig) {
49-
if (forceRefresh) {
50-
_logWarn(
51-
'Refresh token is not a valid operation for Regional Auth instance initialized.'
52-
);
53-
}
5448
const accessToken = await this.getTokenForRegionalAuth(forceRefresh);
5549
return accessToken ? { accessToken } : null;
5650
}

packages/auth/src/core/strategies/exchange_token.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('core/strategies/exchangeToken', () => {
5353
RegionalEndpoint.EXCHANGE_TOKEN,
5454
'projects/test-project-id/locations/us/tenants/tenant-1/idpConfigs/idp-config',
5555
'test-api-key',
56-
{ accessToken: 'outbound-token', expiresIn: 10 }
56+
{ accessToken: 'outbound-token', expiresIn: 10_000 }
5757
);
5858

5959
const accessToken = await exchangeToken(
@@ -72,8 +72,8 @@ describe('core/strategies/exchangeToken', () => {
7272
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(
7373
'application/json'
7474
);
75-
expect(regionalAuth.firebaseToken?.token).to.equal('outbound-token');
76-
expect(regionalAuth.firebaseToken?.expirationTime).to.equal(now + 10_000);
75+
const firebaseToken = await regionalAuth.getFirebaseAccessToken();
76+
expect(firebaseToken).to.equal('outbound-token');
7777
});
7878

7979
it('throws exception for default Auth', async () => {
@@ -117,6 +117,7 @@ describe('core/strategies/exchangeToken', () => {
117117
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(
118118
'application/json'
119119
);
120-
expect(regionalAuth.firebaseToken).is.null;
120+
const accessToken = await regionalAuth.getFirebaseAccessToken();
121+
expect(accessToken).is.null;
121122
});
122123
});

packages/auth/test/helpers/mock_auth.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
import { FirebaseApp } from '@firebase/app';
1919
import { Provider } from '@firebase/component';
2020
import { AppCheckTokenResult } from '@firebase/app-check-interop-types';
21-
import { PopupRedirectResolver } from '../../src/model/public_types';
21+
import {
22+
PopupRedirectResolver,
23+
TokenRefreshHandler,
24+
RefreshIdpTokenResult
25+
} from '../../src/model/public_types';
2226
import { debugErrorMap } from '../../src';
2327

2428
import { AuthImpl } from '../../src/core/auth/auth_impl';
@@ -118,6 +122,30 @@ export async function testAuth(
118122
return auth;
119123
}
120124

125+
class TokenRefreshHandlerImpl implements TokenRefreshHandler {
126+
async refreshIdpToken(): Promise<RefreshIdpTokenResult> {
127+
// Fetch the token for their custom Idp configured...
128+
return {
129+
idToken: 'new-token', // The new token string
130+
idpConfigId: 'idp-config' // The ID for your IdP
131+
};
132+
}
133+
}
134+
135+
export async function regionalTestAuthWithTokenRefreshHandler(
136+
popupRedirectResolver?: PopupRedirectResolver,
137+
persistence = new MockPersistenceLayer(),
138+
skipAwaitOnInit?: boolean
139+
): Promise<TestAuth> {
140+
const regionalAuth = await regionalTestAuth(
141+
popupRedirectResolver,
142+
persistence,
143+
skipAwaitOnInit
144+
);
145+
regionalAuth.setTokenRefreshHandler(new TokenRefreshHandlerImpl());
146+
return regionalAuth;
147+
}
148+
121149
export async function regionalTestAuth(
122150
popupRedirectResolver?: PopupRedirectResolver,
123151
persistence = new MockPersistenceLayer(),

0 commit comments

Comments
 (0)