Skip to content

Commit 1448931

Browse files
committed
Migrate testing to component framework
1 parent bb43a7e commit 1448931

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

packages/testing/src/api/index.ts

Lines changed: 15 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,18 @@ 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+
getToken: () => Promise.resolve({ accessToken: accessToken }),
136+
getUid: () => null,
137+
addAuthTokenListener: () => { },
138+
removeAuthTokenListener: () => { }
139+
}) as FirebaseAuthInternal,
140+
ComponentType.PRIVATE
141+
);
142+
143+
(app as unknown as _FirebaseApp)._addComponent(mockAuthComponent,/* overwrite */ true);
131144
}
132145
if (databaseName) {
133146
// Toggle network connectivity to force a reauthentication attempt.

packages/testing/test/database.test.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ 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

2526
before(() => {
2627
chai.use(chaiAsPromised);
2728
});
2829

29-
describe('Testing Module Tests', function() {
30-
it('assertSucceeds() iff success', async function() {
30+
describe('Testing Module Tests', function () {
31+
it('assertSucceeds() iff success', async function () {
3132
const success = Promise.resolve('success');
3233
const failure = Promise.reject('failure');
3334
await firebase.assertSucceeds(success).catch(() => {
@@ -38,55 +39,64 @@ describe('Testing Module Tests', function() {
3839
.then(() => {
3940
throw new Error('Expected failure to fail.');
4041
})
41-
.catch(() => {});
42+
.catch(() => { });
4243
});
4344

44-
it('assertFails() iff failure', async function() {
45+
it('assertFails() iff failure', async function () {
4546
const success = Promise.resolve('success');
4647
const failure = Promise.reject('failure');
4748
await firebase
4849
.assertFails(success)
4950
.then(() => {
5051
throw new Error('Expected success to fail.');
5152
})
52-
.catch(() => {});
53+
.catch(() => { });
5354
await firebase.assertFails(failure).catch(() => {
5455
throw new Error('Expected failure to succeed.');
5556
});
5657
});
5758

58-
it('initializeTestApp() with auth=null does not set access token', async function() {
59+
it('initializeTestApp() with auth=null does not set access token', async function () {
5960
const app = firebase.initializeTestApp({
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)
66+
.container.getProvider('auth-internal').getImmediate({ optional: true });
67+
// Auth instance will not be available because no API Key is provided
68+
expect(authInternal).to.be.null;
6569
});
6670

67-
it('initializeTestApp() with auth sets the correct access token', async function() {
71+
it('initializeTestApp() with auth sets the correct access token', async function () {
6872
const auth = { uid: 'alice' };
6973
const app = firebase.initializeTestApp({
7074
projectId: 'foo',
7175
auth: auth
7276
});
73-
const token = await (app as any).INTERNAL.getToken();
77+
const authInternal = (app as unknown as _FirebaseApp)
78+
.container.getProvider('auth-internal').getImmediate();
79+
80+
const token = await authInternal.getToken();
7481
expect(token).to.have.keys('accessToken');
7582
const claims = JSON.parse(
76-
base64.decodeString(token.accessToken.split('.')[1], /*webSafe=*/ false)
83+
base64.decodeString(token!.accessToken.split('.')[1], /*webSafe=*/ false)
7784
);
7885
// We add an 'iat' field.
7986
expect(claims).to.deep.equal({ uid: auth.uid, iat: 0, sub: auth.uid });
8087
});
8188

82-
it('initializeAdminApp() sets the access token to "owner"', async function() {
89+
it('initializeAdminApp() sets the access token to "owner"', async function () {
8390
const app = firebase.initializeAdminApp({ projectId: 'foo' });
84-
const token = await (app as any).INTERNAL.getToken();
91+
const authInternal = (app as unknown as _FirebaseApp)
92+
.container.getProvider('auth-internal').getImmediate();
93+
94+
const token = await authInternal.getToken();
8595
expect(token).to.have.keys('accessToken');
86-
expect(token.accessToken).to.be.string('owner');
96+
expect(token!.accessToken).to.be.string('owner');
8797
});
8898

89-
it('loadDatabaseRules() throws if no databaseName or rules', async function() {
99+
it('loadDatabaseRules() throws if no databaseName or rules', async function () {
90100
// eslint-disable-next-line @typescript-eslint/no-explicit-any
91101
await expect((firebase as any).loadDatabaseRules.bind(null, {})).to.throw(
92102
/databaseName not specified/
@@ -101,13 +111,13 @@ describe('Testing Module Tests', function() {
101111
).to.throw(/databaseName not specified/);
102112
});
103113

104-
it('loadDatabaseRules() tries to make a network request', async function() {
114+
it('loadDatabaseRules() tries to make a network request', async function () {
105115
await expect(
106116
firebase.loadDatabaseRules({ databaseName: 'foo', rules: '{}' })
107117
).to.be.rejectedWith(/ECONNREFUSED/);
108118
});
109119

110-
it('loadFirestoreRules() succeeds on valid input', async function() {
120+
it('loadFirestoreRules() succeeds on valid input', async function () {
111121
let promise = firebase.loadFirestoreRules({
112122
projectId: 'foo',
113123
rules: `service cloud.firestore {
@@ -119,28 +129,28 @@ describe('Testing Module Tests', function() {
119129
await expect(promise).to.be.rejectedWith(/UNAVAILABLE/);
120130
});
121131

122-
it('clearFirestoreData() succeeds on valid input', async function() {
132+
it('clearFirestoreData() succeeds on valid input', async function () {
123133
let promise = firebase.clearFirestoreData({
124134
projectId: 'foo'
125135
});
126136
await expect(promise).to.be.rejectedWith(/UNAVAILABLE/);
127137
});
128138

129-
it('apps() returns apps created with initializeTestApp', async function() {
139+
it('apps() returns apps created with initializeTestApp', async function () {
130140
const numApps = firebase.apps().length;
131141
await firebase.initializeTestApp({ databaseName: 'foo', auth: undefined });
132142
expect(firebase.apps().length).to.equal(numApps + 1);
133143
await firebase.initializeTestApp({ databaseName: 'bar', auth: undefined });
134144
expect(firebase.apps().length).to.equal(numApps + 2);
135145
});
136146

137-
it('there is a way to get database timestamps', function() {
147+
it('there is a way to get database timestamps', function () {
138148
expect(firebase.database.ServerValue.TIMESTAMP).to.deep.equal({
139149
'.sv': 'timestamp'
140150
});
141151
});
142152

143-
it('there is a way to get firestore timestamps', function() {
153+
it('there is a way to get firestore timestamps', function () {
144154
expect(firebase.firestore.FieldValue.serverTimestamp()).not.to.be.null;
145155
});
146156
});

0 commit comments

Comments
 (0)