-
Notifications
You must be signed in to change notification settings - Fork 930
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
Changes from 8 commits
808659f
b7d185b
0c7999c
254bf54
e7a155e
3b03a9c
d8865f7
92977e8
3a2ff76
8452e3f
b2259f5
bf748c3
5497fba
d335a60
585366f
85ca2e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -36,6 +39,10 @@ export function apps(): (admin.app.App | null)[] { | |
return admin.apps; | ||
} | ||
|
||
export function firestoreApps(): (FirebaseApp | null)[] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The distinction between There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
|
@@ -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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that Object will only allow empty objects. You should use https://blog.mariusschulz.com/2017/02/24/typescript-2-2-the-object-type There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
{ | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we initialize the app here, then immediately hijack the |
||
// hijacking INTERNAL.getToken to bypass FirebaseAuth and allows specifying of auth headers | ||
(app as any).INTERNAL.getToken = () => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a comment here explaining that this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
|
@@ -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({ | ||
|
@@ -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/ | ||
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This behavior is strange and unexpected. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
}); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done