Skip to content

Commit c14c950

Browse files
abeisgoatlaurenzlong
authored andcommitted
Add Firestore Integration Tests (#163)
1 parent f807d22 commit c14c950

File tree

11 files changed

+62
-6
lines changed

11 files changed

+62
-6
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

integration_test/firebase.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"database": {
33
"rules": "database.rules.json"
4+
},
5+
"firestore": {
6+
"rules": "firestore.rules",
7+
"indexes": "firestore.indexes.json"
48
}
59
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"indexes": []
3+
}

integration_test/firestore.rules

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
service cloud.firestore {
2+
match /databases/{database}/documents {
3+
match /{document=**} {
4+
allow read, write: if request.auth != null;
5+
}
6+
}
7+
}

integration_test/functions/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

integration_test/functions/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
"@google-cloud/pubsub": "^0.6.0",
99
"@types/lodash": "^4.14.41",
1010
"firebase": ">3.6.9",
11-
"firebase-admin": ">4.1.1",
11+
"firebase-admin": "https://storage.googleapis.com/functions-dev-preview/firebase-admin-5.4.0-rc5.tgz",
1212
"firebase-functions": "./firebase-functions.tgz",
13-
"loadash": "^0.0.1",
1413
"lodash": "^4.17.2"
1514
},
1615
"main": "lib/index.js",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as functions from 'firebase-functions';
2+
import { TestSuite, expectEq, expectDeepEq } from './testing';
3+
4+
const testIdFieldName = 'documentId';
5+
6+
export const firestoreTests: any = functions.firestore.document('tests/{documentId}').onCreate(receivedEvent => {
7+
return new TestSuite('firestore document onWrite')
8+
9+
.it('should not have event.app', event => !event.app)
10+
11+
.it('should give refs write access', event =>
12+
event.data.ref.set({ allowed: 1 }, {merge: true}).then(() => true))
13+
14+
.it('should have well-formatted resource', event => expectEq(
15+
event.resource,
16+
`projects/${process.env.GCLOUD_PROJECT}/databases/(default)/documents/tests/${event.params.documentId}`)
17+
)
18+
19+
.it('should have the right eventType', event => expectEq(
20+
event.eventType, 'providers/cloud.firestore/eventTypes/document.create'))
21+
22+
.it('should have eventId', event => event.eventId)
23+
24+
.it('should have timestamp', event => event.timestamp)
25+
26+
.it('should have the correct data', event => expectDeepEq(event.data.data(), {test: event.params.documentId}))
27+
28+
.it('previous should be null', event => expectEq(event.data.previous, null))
29+
30+
.run(receivedEvent.params[testIdFieldName], receivedEvent);
31+
});

integration_test/functions/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Request, Response } from 'express';
77
export * from './pubsub-tests';
88
export * from './database-tests';
99
export * from './auth-tests';
10+
export * from './firestore-tests';
1011
const numTests = Object.keys(exports).length; // Assumption: every exported function is its own test.
1112

1213
firebase.initializeApp(_.omit(functions.config().firebase, 'credential')); // Explicitly decline admin privileges.
@@ -16,7 +17,6 @@ export const integrationTests: any = functions.https.onRequest((req: Request, re
1617
let pubsub: any = require('@google-cloud/pubsub')();
1718

1819
const testId = firebase.database().ref().push().key;
19-
2020
return Promise.all([
2121
// A database write to trigger the Firebase Realtime Database tests.
2222
// The database write happens without admin privileges, so that the triggered function's "event.data.ref" also
@@ -33,6 +33,8 @@ export const integrationTests: any = functions.https.onRequest((req: Request, re
3333
// A user deletion to trigger the Firebase Auth user deletion tests.
3434
admin.auth().deleteUser(userRecord.uid);
3535
}),
36+
// A firestore write to trigger the Cloud Firestore tests.
37+
admin.firestore().collection('tests').doc(testId).set({test: testId}),
3638
]).then(() => {
3739
// On test completion, check that all tests pass and reply "PASS", or provide further details.
3840
console.log('Waiting for all tests to report they pass...');

integration_test/functions/src/testing.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as firebase from 'firebase-admin';
2+
import * as _ from 'lodash';
23

34
export type TestCase = (event) => any
45
export type TestCaseMap = { [key: string]: TestCase };
@@ -68,6 +69,12 @@ export function expectEq(left, right) {
6869
JSON.stringify(left) + ' does not equal ' + JSON.stringify(right));
6970
}
7071

72+
export function expectDeepEq(left, right) {
73+
return evaluate(
74+
_.isEqual(left, right),
75+
JSON.stringify(left) + ' does not equal ' + JSON.stringify(right));
76+
}
77+
7178
export function expectMatches(input: string, regexp) {
7279
return evaluate(
7380
input.match(regexp),

integration_test/run_tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ function deploy {
4848
announce "Deploying functions..."
4949
cd $DIR
5050
./functions/node_modules/.bin/tsc -p functions/
51-
firebase deploy --project=$PROJECT_ID --only functions,database
51+
# Deploy functions, and security rules for database and Firestore
52+
firebase deploy --project=$PROJECT_ID --only functions,database,firestore
5253
}
5354

5455
function run_tests {

0 commit comments

Comments
 (0)