Skip to content

Enable firestore sdk to talk to emulator #1007

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 16 commits into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions packages/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ export {
assertSucceeds,
initializeAdminApp,
initializeTestApp,
initializeFirestoreTestApp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to remove this to get the CI to pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

loadDatabaseRules
} from './src/api';
42 changes: 38 additions & 4 deletions packages/testing/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
* limitations under the License.
*/

import { firebase } from '@firebase/app';
import * as admin from 'firebase-admin';
import request from 'request-promise';
import * as fs from 'fs';
import { FirebaseApp } from '@firebase/app-types';
import { base64 } from '@firebase/util';

const DBURL = 'http://localhost:9000';

Expand All @@ -36,6 +39,10 @@ export function apps(): (admin.app.App | null)[] {
return admin.apps;
}

export function firestoreApps(): (FirebaseApp | null)[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The distinction between Admin App, Test App and Firestore Test App is not very clear. Would it be possible to add an argument to initializeTestApp that allows us to turn on Firestore support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm combining initializeTestApp and initializeFirestoreTestApp. though the backend support won't land until next week.

return firebase.apps;
}

export function initializeAdminApp(options: any): admin.app.App {
if (!('databaseName' in options)) {
throw new Error('databaseName not specified');
Expand All @@ -49,10 +56,11 @@ export function initializeAdminApp(options: any): admin.app.App {
);
}

export function initializeTestApp(options: any): admin.app.App {
if (!('databaseName' in options)) {
throw new Error('databaseName not specified');
}
export type TestAppOptions = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to make this an interface or to just inline it and use two arguments in initializeTestApp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost :) But it looks fine the way it is.

databaseName: string;
auth: Object;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that Object will only allow empty objects. You should use object.

https://blog.mariusschulz.com/2017/02/24/typescript-2-2-the-object-type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

};
export function initializeTestApp(options: TestAppOptions): admin.app.App {
// if options.auth is not present, we will construct an app with auth == null
return admin.initializeApp(
{
Expand All @@ -64,6 +72,32 @@ export function initializeTestApp(options: any): admin.app.App {
);
}

export type FirestoreTestAppOptions = {
projectId: string;
auth: Object;
};
export function initializeFirestoreTestApp(
options: FirestoreTestAppOptions
): FirebaseApp {
const header = {
alg: 'RS256',
kid: 'fakekid'
};
const fakeToken = [
base64.encodeString(JSON.stringify(header), /*webSafe=*/ false),
base64.encodeString(JSON.stringify(options.auth), /*webSafe=*/ false),
'fakesignature'
].join('.');
const app = firebase.initializeApp(
{ projectId: options.projectId },
'app-' + new Date().getTime() + '-' + Math.random()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make this a little more intuitive if you add a local variable that will help let users know that this is the appName.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we initialize the app here, then immediately hijack the INTERNAL.getToken function?

// hijacking INTERNAL.getToken to bypass FirebaseAuth and allows specifying of auth headers
(app as any).INTERNAL.getToken = () =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment here explaining that this INTERNAL.getToken hijack is to inject the predefined authorization headers into the app (thus bypassing FirebaseAuth)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Promise.resolve({ accessToken: fakeToken });
return app;
}

export type LoadDatabaseRulesOptions = {
databaseName: String;
rules: String;
Expand Down
56 changes: 44 additions & 12 deletions packages/testing/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { expect } from 'chai';
import * as firebase from '../src/api';
import { base64 } from '@firebase/util';

describe('Testing Module Tests', function() {
it('assertSucceeds() iff success', async function() {
Expand Down Expand Up @@ -60,17 +61,8 @@ describe('Testing Module Tests', function() {
expect(app.options).to.not.have.any.keys('databaseAuthVariableOverride');
});

it('initializeTestApp() throws if no databaseName', function() {
expect(firebase.initializeTestApp.bind(null, {})).to.throw(
/databaseName not specified/
);
expect(
firebase.initializeTestApp.bind(null, { databaseName: 'foo' })
).to.not.throw();
});

it('initializeTestApp() uses specified auth.', function() {
let app = firebase.initializeTestApp({ databaseName: 'foo' });
let app = firebase.initializeTestApp({ databaseName: 'foo', auth: {} });
expect(app.options).to.have.any.keys('databaseAuthVariableOverride');

app = firebase.initializeTestApp({
Expand All @@ -84,6 +76,32 @@ describe('Testing Module Tests', function() {
);
});

it('initializeFirestoreTestApp() uses specified auth.', async function() {
let app = firebase.initializeFirestoreTestApp({
projectId: 'foo',
auth: {}
});
let token = await (app as any).INTERNAL.getToken();
expect(token).to.have.any.keys('accessToken');
let claims = base64.decodeString(
token.accessToken.split('.')[1],
/*webSafe=*/ false
);
expect(claims).to.equal('{}');

app = firebase.initializeFirestoreTestApp({
projectId: 'foo',
auth: { uid: 'alice' }
});
token = await (app as any).INTERNAL.getToken();
expect(token).to.have.any.keys('accessToken');
claims = base64.decodeString(
token.accessToken.split('.')[1],
/*webSafe=*/ false
);
expect(claims).to.equal('{"uid":"alice"}');
});

it('loadDatabaseRules() throws if no databaseName or rulesPath', async function() {
expect(firebase.loadDatabaseRules.bind(null, {})).to.throw(
/databaseName not specified/
Expand All @@ -108,13 +126,27 @@ describe('Testing Module Tests', function() {
).to.throw(/Could not find file/);
});

it('apps() returns all created apps', async function() {
it('apps() returns all apps created except by initializeFirestoreTestApp', async function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior is strange and unexpected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleaned it up

const numApps = firebase.apps().length;
await firebase.initializeAdminApp({ databaseName: 'foo' });
expect(firebase.apps().length).to.equal(numApps + 1);
await firebase.initializeAdminApp({ databaseName: 'foo' });
expect(firebase.apps().length).to.equal(numApps + 2);
await firebase.initializeTestApp({ databaseName: 'foo' });
await firebase.initializeTestApp({ databaseName: 'foo', auth: {} });
expect(firebase.apps().length).to.equal(numApps + 3);
await firebase.initializeFirestoreTestApp({ projectId: 'foo', auth: {} });
expect(firebase.apps().length).to.equal(numApps + 3);
});

it('firestoreApps() returns only apps created with initializeFirestoreTestApp', async function() {
const numApps = firebase.firestoreApps().length;
await firebase.initializeAdminApp({ databaseName: 'foo' });
expect(firebase.firestoreApps().length).to.equal(numApps + 0);
await firebase.initializeAdminApp({ databaseName: 'foo' });
expect(firebase.firestoreApps().length).to.equal(numApps + 0);
await firebase.initializeTestApp({ databaseName: 'foo', auth: {} });
expect(firebase.firestoreApps().length).to.equal(numApps + 0);
await firebase.initializeFirestoreTestApp({ projectId: 'foo', auth: {} });
expect(firebase.firestoreApps().length).to.equal(numApps + 1);
});
});