Step24 Firestore Security Rules Testing
Goal of this step
- Learn how to write test for Firestore Security Rules
Refs
- https://firebase.google.com/docs/firestore/security/test-rules-emulator
- https://medium.com/@adityadroid/60-days-of-flutter-building-a-messenger-day-55-56-deploying-firestore-security-rules-using-d8d78fd1eeea
Setup
Install java
If you haven't installed java...
- https://chocolatey.org/packages/openjdk#individual
- https://firebase.google.com/docs/rules/emulator-setup
*You may need to restart your pc after install!
Install firebase cli
https://firebase.google.com/docs/cli/
Check it works correctly
server
folder
Make Imitate Quickstart
https://firebase.google.com/docs/firestore/security/test-rules-emulator#quickstart
package.json
Make npm install
run npm install
in sever directory.
test/test.js
Make Copy quickstart rules.
Run Test
Let's run example test.
Test case for our Flutter app
So, let's write test for out security rules by imitating quickstart example.
Check test reports
https://firebase.google.com/docs/rules/emulator-reports
Start emulator
Then, in another terminal tab
And visit generated url
Test Coverage
https://firebase.google.com/docs/rules/emulator-reports
If test is not applied to the rules, it shows message like this.
Deploy Rules
Seed data
This is just a memo links.
https://github.com/firebase/firebase-tools/issues/1167#issuecomment-545641337 https://stackoverflow.com/questions/56268092/how-to-setup-test-data-when-testing-firestore-rules-with-emulator https://github.com/sgr-ksmt/firestore-emulator-rules-test/blob/master/test/test.ts https://techlife.cookpad.com/entry/2018/11/05/143000