Created
June 30, 2022 08:01
-
-
Save honungsburk/c78f40bfb41b5027eef3c4af1c8d6ea3 to your computer and use it in GitHub Desktop.
Testing firebase rules with vitest
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
import { | |
assertFails, | |
initializeTestEnvironment, | |
} from "@firebase/rules-unit-testing"; | |
import { test } from "vitest"; | |
import fs from "fs"; | |
import * as firstore from "firebase/firestore"; | |
type Interview = { | |
transcript?: string; | |
feedback?: string; | |
error?: string; | |
userID: string; | |
question: string; | |
questionID: string; | |
audioID: string; | |
extention: string; | |
}; | |
/** | |
* NOTE: The firebase emulator must be running for the test to work. I recomend putting you rules test in a different folder | |
* from the rest of your code and use `vitest --dir path-to-test-dir` to seperate them. | |
* you can use the `firebase emulators:exec --only firestore` to create a firestore instance before running the tests and | |
* it will also shut down automatically, | |
* | |
*/ | |
let testEnv = await initializeTestEnvironment({ | |
projectId: "stst-et-interviewer-dev", | |
// Specifing the hub allows firebase to automagically find all other emulators | |
hub: { | |
host: "localhost", | |
port: 4400, | |
}, | |
firestore: { | |
rules: fs.readFileSync("rules/firestore.rules", "utf8"), | |
}, | |
}); | |
test(" not allow you to create a document if you are not authenticated", async () => { | |
const rouge = testEnv.unauthenticatedContext(); | |
const doc = firstore.doc(rouge.firestore(), "interviews/my-interview"); | |
const data: Interview = { | |
userID: "I have no id", | |
question: "My question", | |
questionID: "A question id", | |
audioID: "lol-an-audio-id", | |
extention: ".flac", | |
}; | |
await assertFails(firstore.setDoc(doc, data)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment