1
votes

summary

I'm getting an error with the firebase emulator trying to parse the simplest of firestore rules.

Given it's so simple I can't help think it might be some sort of versioning issue? but not really sure. I've tried to update firebase tools. I can't really find any similar errors on stackoverflow and elsewhere.

Hoping someone might have some ideas on this?

firestore.rules

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userID} {
      allow read, write: if request.auth != null;
    }
  }
}

error by emulator parsing firestore.rules when trying to run 'npm test' / mocha test (see test below)

L5:11 Missing 'match' keyword before path.
L6:7 Unexpected 'allow'.
L6:7 mismatched input 'allow' expecting {'{', '/', PATH_SEGMENT}
L8:11 Missing 'match' keyword before path.
L9:7 Unexpected 'allow'.
L9:7 mismatched input 'allow' expecting {'{', '/', PATH_SEGMENT}
L9:28 Missing 'match' keyword before path.
L9:38 Forward slash '/' found where identifier or binding expected.
L9:39 mismatched input '$' expecting {'{', '/', PATH_SEGMENT}
L9:50 Missing 'match' keyword before path.
L9:66 Forward slash '/' found where identifier or binding expected.
L9:67 Unexpected '$'.
L9:68 Unexpected '{'.
L9:69 mismatched input 'request' expecting {'{', '/', PATH_SEGMENT}
L9:86 Unexpected ')'.
L15:1 Unexpected '}'.
      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 C:\files\react-learning\app-02\test\node_modules\@firebase\firestore\dist\index.node.cjs.js:15676:30
      at C:\files\react-learning\app-02\test\node_modules\@firebase\firestore\dist\index.node.cjs.js:15712:28
      at C:\files\react-learning\app-02\test\node_modules\@firebase\firestore\dist\index.node.cjs.js:14143:20
      at processTicksAndRejections (node:internal/process/task_queues:94:5)

unit test being run

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

const MY_PROJECT_ID = 'my-app-id'

describe('my app', () => {

    it('can read users', async () => {

        const db = firebase.initializeTestApp(
            {
                projectId: MY_PROJECT_ID,
                auth: {
                    uid: '1231123', 
                    email: '[email protected]'
                }
            }
        ).firestore()

        const USER_ID = 'qnWc3u3UjowlF1PCVLSn'
        const testDoc = db.collection('users').doc(USER_ID)
        await firebase.assertSucceeds(testDoc.get())
    })
})

It fails on the 'await firebase...' line.

what i'm expecting

The above firestore.rules to be parsed without any issues, so that the mocha test can run.

versions

firebase 9.5.0

1

1 Answers

1
votes

Found the answer.

I needed to read in the firestore rules.

Was missing.

  1. const fs = require('fs'); at the top, and
  2. firebase.loadFirestoreRules({ projectId: MY_PROJECT_ID, rules: fs.readFileSync('../firestore.rules', 'utf8') }) ...before I try to call any rules.

Pretty sure this wasn't in the YouTube video from Google on testing rules...but is in the docs => https://firebase.google.com/docs/rules/unit-tests about 2/3rds of the way down the page.