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 5 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';
31 changes: 31 additions & 0 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 Down Expand Up @@ -64,6 +67,34 @@ export function initializeTestApp(options: any): admin.app.App {
);
}

export function initializeFirestoreTestApp(options: any): FirebaseApp {
if (!('projectId' in options)) {
throw new Error('projectId not specified');
}
if (typeof options.auth != 'object') {
throw new Error('auth must be an object');
}
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

var header = {
alg: 'RS256',
kid: 'fakekid'
};
var fakeToken = [
base64.encodeString(JSON.stringify(header), /*webSafe=*/ true),
base64.encodeString(JSON.stringify(options.auth), /*webSafe=*/ true),
'fakesignature'
].join('.');
let app = firebase.initializeApp(
{
projectId: options.projectId,
tokenOverride: fakeToken
},
'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?

(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
41 changes: 41 additions & 0 deletions packages/testing/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,47 @@ describe('Testing Module Tests', function() {
);
});

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

it('initializeFirestoreTestApp() throws if auth is not an object', function() {
expect(
firebase.initializeFirestoreTestApp.bind(null, {
projectId: 'a',
auth: 'b'
})
).to.throw(/auth must be an object/);
expect(
firebase.initializeFirestoreTestApp.bind(null, {
projectId: 'a',
auth: {}
})
).to.not.throw();
});

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

app = firebase.initializeFirestoreTestApp({
projectId: 'foo',
auth: { uid: 'alice' }
});
expect(app.options).to.have.any.keys('tokenOverride');
});

it('loadDatabaseRules() throws if no databaseName or rulesPath', async function() {
expect(firebase.loadDatabaseRules.bind(null, {})).to.throw(
/databaseName not specified/
Expand Down