Home > Software engineering >  Setting up "@firebase/rules-unit-testing" to test security rules locally
Setting up "@firebase/rules-unit-testing" to test security rules locally

Time:10-05

I watched an official tutorial on youtube which explains how to test security rules on the firebase emulator suite. Unfortunately the video is outdated on I couldn`t find a single snippet on the www with an actual solution (even the official documents provided by Firebase dont come up with a working solution):

My Firebase emulator suite is running fine for now, authentification- and functions-emulators works as expected.

The Youtube tutorial towards testing security rules look like this (@firebase/testing):

const assert = require('assert');
const firebase = require('@firebase/testing');

const MY_PROJECT_ID = "my_firebase_project_id";

describe('Firebase testing App', function () {
  it("Can read data from the database", async() => {
    const db = await firebase.initializeTestApp({projectId: MY_PROJECT_ID}).firestore();
    const testDoc = db.collection('readonly').doc('testDoc');
    await firebase.assertSucceeds(testDoc.get());
  })
});

But this code doesnt work with the actual testing packet @firebase/rules-unit-testing.

Never mind Firebase should provide an actual snippet in the official documents which provides the same functionality as the snippet above, can a helpful coder provide me with an actual snippet?

Regarding the official documents the snippet has to start as follows:

const firebase = require("@firebase/rules-unit-testing")

CodePudding user response:

The Emulator Suite codelab includes an example with the rules testing library. Though it could use a refresher to the latest (2.*) version.

https://github.com/firebase/emulators-codelab/blob/062f8daec3c29842f252d3d2599a678a0263ae3b/codelab-final-state/functions/test.js

  • Related