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 1 commit
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
13 changes: 11 additions & 2 deletions packages/firestore/src/api/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { User } from '../auth/user';
import { assert, fail } from '../util/assert';
import { Code, FirestoreError } from '../util/error';
import { FirebaseApp } from '@firebase/app-types';
import { _FirebaseApp } from '@firebase/app-types/private';
import { _FirebaseApp, FirebaseAuthTokenData } from '@firebase/app-types/private';

// TODO(mikelehen): This should be split into multiple files and probably
// moved to an auth/ folder to match other platforms.
Expand Down Expand Up @@ -145,6 +145,8 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {

private forceRefresh = false;

private tokenOverride: string = null;

constructor(private readonly app: FirebaseApp) {
// We listen for token changes but all we really care about is knowing when
// the uid may have changed.
Expand All @@ -160,6 +162,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
};

this.userCounter = 0;
this.tokenOverride = app.options.tokenOverride;

// Will fire at least once where we set this.currentUser
(this.app as _FirebaseApp).INTERNAL.addAuthTokenListener(
Expand All @@ -179,7 +182,13 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
const initialUserCounter = this.userCounter;
const forceRefresh = this.forceRefresh;
this.forceRefresh = false;
return (this.app as _FirebaseApp).INTERNAL.getToken(forceRefresh).then(
var token: Promise<FirebaseAuthTokenData>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have to modify the Firestore SDK in order to get this to work? I'd prefer not to introduce this tokenOverride value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AFAICT, we'd have to change app-types to export FirebaseAppImpl or to move INTERNAL into FirebaseApp to give visibility to the testing package. If that's the only way to do this, I'd rather modify the firestore package than app package

if (this.tokenOverride != null) {
token = Promise.resolve({ accessToken: this.tokenOverride });
} else {
token = (this.app as _FirebaseApp).INTERNAL.getToken(forceRefresh);
}
return token.then(
tokenData => {
// Cancel the request since the user changed while the request was
// outstanding so the response is likely for a previous user (which
Expand Down
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';
27 changes: 27 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 * as util from '@firebase/util';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there a better way to import these?


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

Expand Down Expand Up @@ -64,6 +67,30 @@ 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 = [
util.base64.encodeString(JSON.stringify(header),/*webSafe=*/true),
util.base64.encodeString(JSON.stringify(options.auth),/*webSafe=*/true),
"fakesignature"
].join(".");
return 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.

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

}

export type LoadDatabaseRulesOptions = {
databaseName: String;
rules: String;
Expand Down
26 changes: 26 additions & 0 deletions packages/testing/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ 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