Skip to content

Commit 292b858

Browse files
authored
Update @firebase/testing to use component framework (#2340)
* Make tests work again * [AUTOMATED]: Prettier Code Styling * Migrate testing to component framework * [AUTOMATED]: Prettier Code Styling
1 parent 5e4f80b commit 292b858

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

packages/testing/src/api/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
*/
1717

1818
import * as firebase from 'firebase';
19+
import { _FirebaseApp } from '@firebase/app-types/private';
20+
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
1921
import * as request from 'request';
2022
import { base64 } from '@firebase/util';
2123
import { setLogLevel, LogLevel } from '@firebase/logger';
2224
import * as grpc from 'grpc';
2325
import * as protoLoader from '@grpc/proto-loader';
2426
import { resolve } from 'path';
27+
import { Component, ComponentType } from '@firebase/component';
2528

2629
export { database, firestore } from 'firebase';
2730

@@ -126,8 +129,22 @@ function initializeApp(
126129
let app = firebase.initializeApp(appOptions, appName);
127130
// hijacking INTERNAL.getToken to bypass FirebaseAuth and allows specifying of auth headers
128131
if (accessToken) {
129-
(app as any).INTERNAL.getToken = () =>
130-
Promise.resolve({ accessToken: accessToken });
132+
const mockAuthComponent = new Component(
133+
'auth-internal',
134+
() =>
135+
({
136+
getToken: () => Promise.resolve({ accessToken: accessToken }),
137+
getUid: () => null,
138+
addAuthTokenListener: () => {},
139+
removeAuthTokenListener: () => {}
140+
} as FirebaseAuthInternal),
141+
ComponentType.PRIVATE
142+
);
143+
144+
((app as unknown) as _FirebaseApp)._addComponent(
145+
mockAuthComponent,
146+
/* overwrite */ true
147+
);
131148
}
132149
if (databaseName) {
133150
// Toggle network connectivity to force a reauthentication attempt.

packages/testing/test/database.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as chai from 'chai';
1919
import * as chaiAsPromised from 'chai-as-promised';
2020
import * as firebase from '../src/api';
2121
import { base64 } from '@firebase/util';
22+
import { _FirebaseApp } from '@firebase/app-types/private';
2223

2324
const expect = chai.expect;
2425

@@ -60,8 +61,12 @@ describe('Testing Module Tests', function() {
6061
projectId: 'foo',
6162
auth: undefined
6263
});
63-
const token = await (app as any).INTERNAL.getToken();
64-
expect(token).to.be.null;
64+
65+
const authInternal = ((app as unknown) as _FirebaseApp).container
66+
.getProvider('auth-internal')
67+
.getImmediate({ optional: true });
68+
// Auth instance will not be available because no API Key is provided
69+
expect(authInternal).to.be.null;
6570
});
6671

6772
it('initializeTestApp() with auth sets the correct access token', async function() {
@@ -70,20 +75,28 @@ describe('Testing Module Tests', function() {
7075
projectId: 'foo',
7176
auth: auth
7277
});
73-
const token = await (app as any).INTERNAL.getToken();
78+
const authInternal = ((app as unknown) as _FirebaseApp).container
79+
.getProvider('auth-internal')
80+
.getImmediate();
81+
82+
const token = await authInternal.getToken();
7483
expect(token).to.have.keys('accessToken');
7584
const claims = JSON.parse(
76-
base64.decodeString(token.accessToken.split('.')[1], /*webSafe=*/ false)
85+
base64.decodeString(token!.accessToken.split('.')[1], /*webSafe=*/ false)
7786
);
7887
// We add an 'iat' field.
7988
expect(claims).to.deep.equal({ uid: auth.uid, iat: 0, sub: auth.uid });
8089
});
8190

8291
it('initializeAdminApp() sets the access token to "owner"', async function() {
8392
const app = firebase.initializeAdminApp({ projectId: 'foo' });
84-
const token = await (app as any).INTERNAL.getToken();
93+
const authInternal = ((app as unknown) as _FirebaseApp).container
94+
.getProvider('auth-internal')
95+
.getImmediate();
96+
97+
const token = await authInternal.getToken();
8598
expect(token).to.have.keys('accessToken');
86-
expect(token.accessToken).to.be.string('owner');
99+
expect(token!.accessToken).to.be.string('owner');
87100
});
88101

89102
it('loadDatabaseRules() throws if no databaseName or rules', async function() {

0 commit comments

Comments
 (0)