Skip to content

Update @firebase/testing to use component framework #2340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/testing/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.
Expand Down
25 changes: 19 additions & 6 deletions packages/testing/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand All @@ -70,20 +75,28 @@ 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 });
});

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() {
Expand Down