-
-
Save Adrek/293c9229445acccd93d69b5dd8a2dae4 to your computer and use it in GitHub Desktop.
Example of Firebase emulator unit tests and seed Firestore data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"firestore": { | |
"rules": "firestore.rules", | |
"indexes": "firestore.indexes.json" | |
}, | |
"emulators": { | |
"firestore": { | |
"port": 8080 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rules_version = '2'; | |
service cloud.firestore { | |
match /databases/{database}/documents { | |
match rows/{rowId} { | |
allow read: if request.auth.uid == resource.data.owner; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"rows/foo": { | |
"owner": "kato", "score": 10 | |
}, | |
"rows/bar": { | |
"owner": "puf", "score": 20 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference path='../node_modules/mocha-typescript/globals.d.ts' /> | |
import * as firebase from "@firebase/testing"; | |
/* | |
* ============ | |
* Setup | |
* ============ | |
*/ | |
const port = require("./firebase.json").emulators.firestore.port; | |
const projectId = "gvcassistant-staging"; | |
const coverageUrl = `http://localhost:${port}/emulator/v1/projects/${projectId}:ruleCoverage.html`; | |
const fs = require("fs"); | |
const rules = fs.readFileSync("./firestore.rules", "utf8"); | |
const seed = require('./seed.json'); | |
/** | |
* Creates a new app with authentication data matching the input. | |
* | |
* @param {object} auth the object to use for authentication (typically {uid: some-uid}) | |
* @return {object} the app. | |
*/ | |
function authedApp(auth: object) { | |
return firebase | |
.initializeTestApp({ projectId, auth }) | |
.firestore(); | |
} | |
async function resetData() { | |
await firebase.clearFirestoreData({ projectId }); | |
const db = firebase.initializeAdminApp({projectId}).firestore(); | |
const promises = Object.entries(seed).map(entry => db.doc(entry[0]).set(entry[1])); | |
await Promise.all(promises); | |
} | |
/* | |
* ============ | |
* Test Cases | |
* ============ | |
*/ | |
before(async () => { | |
await firebase.loadFirestoreRules({ projectId, rules }); | |
}); | |
beforeEach(resetData); | |
after(async () => { | |
await Promise.all(firebase.apps().map(app => app.delete())); | |
console.log(`View rule coverage information at ${coverageUrl}\n`); | |
}); | |
@suite | |
class MyApp { | |
@test | |
async "can access rows I own"() { | |
const db = authedApp({uid: "kato"}); | |
const doc = db.doc("rows/foo"); | |
await firebase.assertSucceeds(doc.get()); | |
} | |
@test | |
async "cannot access rows I do not own"() { | |
const db = authedApp({uid: "kato"}); | |
const doc = db.doc("rows/bar"); | |
await firebase.assertFails(doc.get()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment