Skip to content

Commit 0f7032f

Browse files
committed
Adding Unit test
1 parent ea7b605 commit 0f7032f

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
460460
async _updateFirebaseToken(
461461
firebaseToken: FirebaseToken | null
462462
): Promise<void> {
463-
if (firebaseToken) {
464463
this.firebaseToken = firebaseToken;
465-
}
466464
}
467465

468466
async signOut(): Promise<void> {

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import { expect, use } from 'chai';
2020
import * as sinon from 'sinon';
2121
import chaiAsPromised from 'chai-as-promised';
2222

23-
import { regionalTestAuth, testAuth, testUser } from '../../../test/helpers/mock_auth';
23+
import {
24+
regionalTestAuth,
25+
testAuth,
26+
testUser
27+
} from '../../../test/helpers/mock_auth';
2428
import { AuthInternal } from '../../model/auth';
2529
import { UserInternal } from '../../model/user';
2630
import { AuthInterop } from './firebase_internal';
@@ -38,8 +42,8 @@ describe('core/auth/firebase_internal', () => {
3842
afterEach(() => {
3943
sinon.restore();
4044
delete (auth as unknown as Record<string, unknown>)[
41-
'_initializationPromise'
42-
];
45+
'_initializationPromise'
46+
];
4347
});
4448

4549
context('getUid', () => {
@@ -222,9 +226,12 @@ describe('core/auth/firebase_internal', () => {
222226
describe('core/auth/firebase_internal - Regional Firebase Auth', () => {
223227
let regionalAuth: AuthInternal;
224228
let regionalAuthInternal: AuthInterop;
229+
let now: number;
225230
beforeEach(async () => {
226231
regionalAuth = await regionalTestAuth();
227232
regionalAuthInternal = new AuthInterop(regionalAuth);
233+
now = Date.now();
234+
sinon.stub(Date, 'now').returns(now);
228235
});
229236

230237
afterEach(() => {
@@ -235,5 +242,37 @@ describe('core/auth/firebase_internal - Regional Firebase Auth', () => {
235242
it('returns null if firebase token is undefined', async () => {
236243
expect(await regionalAuthInternal.getFirebaseToken()).to.be.null;
237244
});
245+
246+
it('returns the id token correctly', async () => {
247+
await regionalAuth._updateFirebaseToken({
248+
token: 'access-token',
249+
expirationTime: now + 300_000
250+
});
251+
expect(await regionalAuthInternal.getFirebaseToken()).to.eql({
252+
accessToken: 'access-token'
253+
});
254+
});
255+
256+
it('logs out the the id token expires in next 30 seconds', async () => {
257+
expect(await regionalAuthInternal.getFirebaseToken()).to.be.null;
258+
});
259+
260+
it('logs out if token has expired', async () => {
261+
await regionalAuth._updateFirebaseToken({
262+
token: 'access-token',
263+
expirationTime: now - 5_000
264+
});
265+
expect(await regionalAuthInternal.getFirebaseToken()).to.null;
266+
expect(regionalAuth.firebaseToken).to.null;
267+
});
268+
269+
it('logs out if token is expiring in next 5 seconds', async () => {
270+
await regionalAuth._updateFirebaseToken({
271+
token: 'access-token',
272+
expirationTime: now + 5_000
273+
});
274+
expect(await regionalAuthInternal.getFirebaseToken()).to.null;
275+
expect(regionalAuth.firebaseToken).to.null;
276+
});
238277
});
239278
});

packages/auth/test/helpers/mock_auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ export async function testAuth(
121121
export async function regionalTestAuth(
122122
popupRedirectResolver?: PopupRedirectResolver,
123123
persistence = new MockPersistenceLayer(),
124-
skipAwaitOnInit?: boolean): Promise<TestAuth> {
124+
skipAwaitOnInit?: boolean
125+
): Promise<TestAuth> {
125126
const tenantConfig = { 'location': 'us', 'tenantId': 'tenant-1' };
126127
const auth: TestAuth = new AuthImpl(
127128
FAKE_APP,

0 commit comments

Comments
 (0)