Skip to content

Instantly share code, notes, and snippets.

@katowulf

katowulf/firebase.json

Last active Sep 11, 2020
Embed
What would you like to do?
Example of Firebase emulator unit tests and seed Firestore data
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"emulators": {
"firestore": {
"port": 8080
}
}
}
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match rows/{rowId} {
allow read: if request.auth.uid == resource.data.owner;
}
}
}
{
"rows/foo": {
"owner": "kato", "score": 10
},
"rows/bar": {
"owner": "puf", "score": 20
}
}
/// <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());
}
}
@markgoho

This comment has been minimized.

Copy link

@markgoho markgoho commented Sep 9, 2020

@katowulf are you missing the import statement for the seed.json file?

@katowulf

This comment has been minimized.

Copy link
Owner Author

@katowulf katowulf commented Sep 11, 2020

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.