Learn how to test Firestore security rules step-by-step. Set up Firebase, write rules, use Firebase CLI and testing tools, and run secure test cases for your data.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Your Firebase Project
Before you begin testing Firestore security rules, ensure you have an existing Firebase project. If not, create one via the Firebase Console.
Create a Firebase project:
Head to Firebase Console, click on "Add project", and follow the instructions to create a new project.
Add Firestore to your project:
Once your project is created, navigate to Firestore under the "Build" section, and click "Create Database". Choose the test mode (be mindful that this allows open access to your database initially).
Step 2: Write Firestore Security Rules
Develop rules to govern access to your Firestore database.
Example Rule:
This example allows only authenticated users to read and write their own user data, assuming documents are stored under users/{userId}.
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
}
Write these rules in the Firestore Rules tab in the Firebase Console.
Step 3: Install Firebase CLI
To test security rules locally, set up Firebase CLI.
Installation Command:
Use npm to install Firebase CLI globally:
npm install -g firebase-tools
After installation, verify using:
firebase --version
Step 4: Initialize Firebase in Your Project
Prepare your local project to use Firebase features, including Firestore.
Initialize Firebase in the project directory:
firebase init
Step 5: Import Firebase Testing Library
To test Firestore rules, utilize the Firebase testing library for Node.js.
Add the library to your testing environment:
npm install --save-dev @firebase/rules-unit-testing
Step 6: Write Test Cases for Firestore Security Rules
Develop test cases using the Firebase Test SDK that test your Firestore rules.
Example Test Code:
const { initializeTestEnvironment, assertFails, assertSucceeds } = require('@firebase/rules-unit-testing');
const { getFirestore, doc, setDoc } = require('firebase/firestore');
const projectId = "my-test-project";
let testEnv;
beforeAll(async () => {
testEnv = await initializeTestEnvironment({
projectId: projectId,
firestore: {
rules: 'firestore.rules', // Assumed location of your rules file.
}
});
});
afterAll(async () => {
await testEnv.cleanup();
});
describe('Firestore security rules', () => {
it('allows authenticated user to write to their own document', async () => {
const auth = { uid: 'user\_abc' };
const firestore = testEnv.authenticatedContext('user\_abc').firestore();
const testDoc = doc(firestore, 'users/user\_abc');
await assertSucceeds(setDoc(testDoc, { foo: 'bar' }));
});
it('denies write to other user document', async () => {
const firestore = testEnv.authenticatedContext('user\_abc').firestore();
const testDoc = doc(firestore, 'users/other\_user');
await assertFails(setDoc(testDoc, { foo: 'bar' }));
});
});
Step 7: Run Your Tests
Test your Firestore security rules via a Node.js test runner like Jest or Mocha.
Example Command with Jest:
npm run test
Ensure you have configured Jest or Mocha in your project to recognize Firebase testing properly.
By following these steps, you can effectively test your Firebase Firestore security rules, ensuring they properly enforce data access according to your specifications.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.