diff --git a/packages/testing/src/api/index.ts b/packages/testing/src/api/index.ts index 7da634d9cb8..6f7cd64a2a1 100644 --- a/packages/testing/src/api/index.ts +++ b/packages/testing/src/api/index.ts @@ -16,12 +16,15 @@ */ import * as firebase from 'firebase'; +import { _FirebaseApp } from '@firebase/app-types/private'; +import { FirebaseAuthInternal } from '@firebase/auth-interop-types'; import * as request from 'request'; import { base64 } from '@firebase/util'; import { setLogLevel, LogLevel } from '@firebase/logger'; import * as grpc from 'grpc'; import * as protoLoader from '@grpc/proto-loader'; import { resolve } from 'path'; +import { Component, ComponentType } from '@firebase/component'; export { database, firestore } from 'firebase'; @@ -126,8 +129,22 @@ function initializeApp( let app = firebase.initializeApp(appOptions, appName); // hijacking INTERNAL.getToken to bypass FirebaseAuth and allows specifying of auth headers if (accessToken) { - (app as any).INTERNAL.getToken = () => - Promise.resolve({ accessToken: accessToken }); + const mockAuthComponent = new Component( + 'auth-internal', + () => + ({ + getToken: () => Promise.resolve({ accessToken: accessToken }), + getUid: () => null, + addAuthTokenListener: () => {}, + removeAuthTokenListener: () => {} + } as FirebaseAuthInternal), + ComponentType.PRIVATE + ); + + ((app as unknown) as _FirebaseApp)._addComponent( + mockAuthComponent, + /* overwrite */ true + ); } if (databaseName) { // Toggle network connectivity to force a reauthentication attempt. diff --git a/packages/testing/test/database.test.ts b/packages/testing/test/database.test.ts index b265e7df6ad..b04574ec400 100644 --- a/packages/testing/test/database.test.ts +++ b/packages/testing/test/database.test.ts @@ -19,6 +19,7 @@ import * as chai from 'chai'; import * as chaiAsPromised from 'chai-as-promised'; import * as firebase from '../src/api'; import { base64 } from '@firebase/util'; +import { _FirebaseApp } from '@firebase/app-types/private'; const expect = chai.expect; @@ -60,8 +61,12 @@ describe('Testing Module Tests', function() { projectId: 'foo', auth: undefined }); - const token = await (app as any).INTERNAL.getToken(); - expect(token).to.be.null; + + const authInternal = ((app as unknown) as _FirebaseApp).container + .getProvider('auth-internal') + .getImmediate({ optional: true }); + // Auth instance will not be available because no API Key is provided + expect(authInternal).to.be.null; }); it('initializeTestApp() with auth sets the correct access token', async function() { @@ -70,10 +75,14 @@ describe('Testing Module Tests', function() { projectId: 'foo', auth: auth }); - const token = await (app as any).INTERNAL.getToken(); + const authInternal = ((app as unknown) as _FirebaseApp).container + .getProvider('auth-internal') + .getImmediate(); + + const token = await authInternal.getToken(); expect(token).to.have.keys('accessToken'); const claims = JSON.parse( - base64.decodeString(token.accessToken.split('.')[1], /*webSafe=*/ false) + base64.decodeString(token!.accessToken.split('.')[1], /*webSafe=*/ false) ); // We add an 'iat' field. expect(claims).to.deep.equal({ uid: auth.uid, iat: 0, sub: auth.uid }); @@ -81,9 +90,13 @@ describe('Testing Module Tests', function() { it('initializeAdminApp() sets the access token to "owner"', async function() { const app = firebase.initializeAdminApp({ projectId: 'foo' }); - const token = await (app as any).INTERNAL.getToken(); + const authInternal = ((app as unknown) as _FirebaseApp).container + .getProvider('auth-internal') + .getImmediate(); + + const token = await authInternal.getToken(); expect(token).to.have.keys('accessToken'); - expect(token.accessToken).to.be.string('owner'); + expect(token!.accessToken).to.be.string('owner'); }); it('loadDatabaseRules() throws if no databaseName or rules', async function() {