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 15 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: 0 additions & 1 deletion packages/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export {
apps,
assertFails,
assertSucceeds,
initializeAdminApp,
initializeTestApp,
loadDatabaseRules
} from './src/api';
3 changes: 1 addition & 2 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@firebase/testing",
"version": "0.1.0",
"version": "1.0.0",
Copy link
Contributor

@jshcrowthe jshcrowthe Jul 16, 2018

Choose a reason for hiding this comment

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

We don't need to do this, even though it is a breaking change major 0s allow for breaking changes between other versions also. Additionally, I update this at publish time so no need to edit.

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

"private": true,
"description": "",
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
Expand All @@ -16,7 +16,6 @@
},
"license": "Apache-2.0",
"dependencies": {
"firebase-admin": "5.12.0",
"request-promise": "4.2.2"
},
"devDependencies": {
Expand Down
63 changes: 36 additions & 27 deletions packages/testing/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* limitations under the License.
*/

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

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

Expand All @@ -32,36 +34,43 @@ class FakeCredentials {
}
}

export function apps(): (admin.app.App | null)[] {
return admin.apps;
export function apps(): (FirebaseApp | null)[] {
return firebase.apps;
}

export function initializeAdminApp(options: any): admin.app.App {
if (!('databaseName' in options)) {
throw new Error('databaseName not specified');
}
return admin.initializeApp(
{
credential: new FakeCredentials(),
export type AppOptions = {
databaseName?: string;
projectId?: string;
auth: object;
};
export function initializeTestApp(options: AppOptions): FirebaseApp {
let appOptions: FirebaseOptions;
if ('databaseName' in options) {
appOptions = {
databaseURL: DBURL + '?ns=' + options.databaseName
},
'app-' + (new Date().getTime() + Math.random())
);
}

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

Choose a reason for hiding this comment

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

Is there a reason to not support both projectId and databseURL? This code might be a little cleaner if you supported both, even if the interface doesn't allow for it.

appOptions = {
projectId: options.projectId
};
} else {
throw new Error('neither databaseName or projectId were specified');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If you want, you could declare the type for options (instead of leaving it as any) and let the compiler construct these assertions for you.

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

// if options.auth is not present, we will construct an app with auth == null
return admin.initializeApp(
{
credential: new FakeCredentials(),
databaseURL: DBURL + '?ns=' + options.databaseName,
databaseAuthVariableOverride: options.auth || null
},
'app-' + (new Date().getTime() + Math.random())
);
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 appName = 'app-' + new Date().getTime() + '-' + Math.random();
const app = firebase.initializeApp(appOptions, appName);
// 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 = {
Expand Down
75 changes: 44 additions & 31 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 @@ -46,42 +47,56 @@ describe('Testing Module Tests', function() {
});
});

it('initializeAdminApp() throws if no databaseName', function() {
expect(firebase.initializeAdminApp.bind(null, {})).to.throw(
/databaseName not specified/
it('initializeTestApp() with DatabaseAppOptions uses specified auth.', async function() {
let app = firebase.initializeTestApp({
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(
firebase.initializeAdminApp.bind(null, { databaseName: 'foo' })
).to.not.throw();
});
expect(claims).to.equal('{}');

it('initializeAdminApp() provides admin', function() {
const app = firebase.initializeAdminApp({ databaseName: 'foo' });
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/
app = firebase.initializeTestApp({
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(
firebase.initializeTestApp.bind(null, { databaseName: 'foo' })
).to.not.throw();
expect(claims).to.equal('{"uid":"alice"}');
});

it('initializeTestApp() uses specified auth.', function() {
let app = firebase.initializeTestApp({ databaseName: 'foo' });
expect(app.options).to.have.any.keys('databaseAuthVariableOverride');
it('initializeTestApp() with FirestoreAppOptions uses specified auth.', async function() {
let app = firebase.initializeTestApp({
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.initializeTestApp({
databaseName: 'foo',
projectId: 'foo',
auth: { uid: 'alice' }
});
expect(app.options).to.have.any.keys('databaseAuthVariableOverride');
expect(app.options.databaseAuthVariableOverride).to.have.all.keys('uid');
expect(app.options.databaseAuthVariableOverride['uid']).to.be.equal(
'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() {
Expand All @@ -108,13 +123,11 @@ describe('Testing Module Tests', function() {
).to.throw(/Could not find file/);
});

it('apps() returns all created apps', async function() {
it('apps() returns apps created with initializeTestApp', async function() {
const numApps = firebase.apps().length;
await firebase.initializeAdminApp({ databaseName: 'foo' });
await firebase.initializeTestApp({ databaseName: 'foo', auth: {} });
expect(firebase.apps().length).to.equal(numApps + 1);
await firebase.initializeAdminApp({ databaseName: 'foo' });
await firebase.initializeTestApp({ databaseName: 'bar', auth: {} });
expect(firebase.apps().length).to.equal(numApps + 2);
await firebase.initializeTestApp({ databaseName: 'foo' });
expect(firebase.apps().length).to.equal(numApps + 3);
});
});