firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.01k stars 929 forks source link

Error message improvement for testing security rules #2577

Open rdinkel opened 4 years ago

rdinkel commented 4 years ago

Environment info

Test case

package.json

{
  "name": "security_rules",
  "version": "1.0.0",
  "description": "Unit testing for security rules",
  "main": "test.js",
  "scripts": {
    "test": "mocha --exit"
  },
  "author": "me",
  "license": "ISC",
  "devDependencies": {
    "@firebase/testing": "^0.20.11",
    "mocha": "^8.1.1"
  }
}

firestore_rules.json

rules_version = '1';
service cloud.firestore {
  match /databases/{database}/documents {
      match /{document=**} {
        allow read: if false;
        allow write: if false;
      }
}

test,js

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

const PROJECT_ID = "asd";
const myId = "user_abc";
const theirId = "user_xyz";
const myAuth = {uid: myId, email: "abc@example.com"};

function getFireStore(myAuth){
    return firebase.initializeTestApp({projectId: PROJECT_ID, auth: myAuth}).firestore();
}

function getFireStoreAdmin(){
    return firebase.initializeAdminApp({projectId: PROJECT_ID}).firestore();
}

beforeEach(async() => {
    await firebase.clearFirestoreData({projectId: PROJECT_ID});
});

describe("asd app", () => {

    it("get doc from firestore", async() => {
        const db = getFireStore(null);
        const testDoc = db.collection("readonly").doc("testDoc");
        await firebase.assertSucceeds(testDoc.get());
    });
})

Steps to reproduce

firebase emulators:start

npm test

Expected behavior

A clear error message what is wrong. Something like FirebaseError: PERMISSION_DENIED because security rules returned false for 'get' @ L5

Actual behavior

Running Security Rule Test fails with following unclear error:

FirebaseError: false for 'get' @ L5 at new FirestoreError (node_modules/@firebase/firestore/dist/index.node.cjs.js:1205:28) at fromRpcStatus (node_modules/@firebase/firestore/dist/index.node.cjs.js:5240:12) at fromWatchChange (node_modules/@firebase/firestore/dist/index.node.cjs.js:5476:35) at PersistentListenStream.onMessage (node_modules/@firebase/firestore/dist/index.node.cjs.js:15743:27) at /home/roman/projects/asd/test/security_rules/node_modules/@firebase/firestore/dist/index.node.cjs.js:15676:30 at /home/roman/projects/asd/test/security_rules/node_modules/@firebase/firestore/dist/index.node.cjs.js:15712:28 at /home/roman/projects/asd/test/security_rules/node_modules/@firebase/firestore/dist/index.node.cjs.js:14143:20 at processTicksAndRejections (internal/process/task_queues.js:93:5)

yuchenshi commented 4 years ago

Thanks for filing the feature request. I've tracked this internally as b/166499006 for further triaging.

yuchenshi commented 4 years ago

Re-opening to make it easier for others to find and subscribe to this feature request.

bofeiw commented 3 years ago

This will save new users a lot of time like me, who spent quite some time figuring why that error message was occuring.

trullock commented 3 years ago

+1 for this, just spent ages trying to understand what false for get meant..

BossBele commented 3 years ago

+1

AlaeddineMessadi commented 2 years ago

+1

aezur commented 2 years ago

+1

jaredforth commented 4 months ago

+1 This would be very helpful