0
0
Firebasecloud~30 mins

Testing rules with emulator in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Testing rules with emulator
📖 Scenario: You are setting up Firebase security rules for a simple app. You want to test these rules locally using the Firebase Emulator to make sure your data is safe before deploying.
🎯 Goal: Build a Firebase security rules file and test it using the Firebase Emulator. You will create a basic rule, configure the emulator, write a test case, and complete the setup to run the emulator.
📋 What You'll Learn
Create a Firebase security rules file named firestore.rules with a simple read rule
Add an emulator configuration in firebase.json to enable Firestore emulator
Write a test script in test.rules.js to check the read rule using the emulator
Complete the firebase.json file with the emulator port and rules path
💡 Why This Matters
🌍 Real World
Testing Firebase security rules locally helps prevent data leaks and unauthorized access before deploying to production.
💼 Career
Firebase developers and cloud engineers use emulators to safely test security rules and app behavior during development.
Progress0 / 4 steps
1
Create basic Firestore security rules
Create a file named firestore.rules with these exact contents: rules_version = '2'; and service cloud.firestore { match /databases/{database}/documents { match /test/{doc} { allow read: if true; } } }
Firebase
Need a hint?

Start with the rules version, then define the service and match paths exactly as shown.

2
Configure Firestore emulator in firebase.json
Add a firestore section inside emulators in firebase.json with port set to 8080 and rules set to firestore.rules
Firebase
Need a hint?

Inside the emulators object, add the firestore key with the port and rules file path.

3
Write a test script to check read access
Create a file named test.rules.js and write code that imports @firebase/rules-unit-testing, initializes a test app with project ID demo-project and Firestore emulator at port 8080, then attempts to read from test/doc1 collection using assertSucceeds
Firebase
Need a hint?

Use initializeTestEnvironment with the project ID and emulator port, then test reading the document with assertSucceeds.

4
Complete firebase.json with emulator settings
Add the emulators section to firebase.json with firestore emulator on port 8080 and rules set to firestore.rules, and add ui emulator on port 4000
Firebase
Need a hint?

Add the UI emulator settings inside the emulators object with enabled true and port 4000.