I am new to testing and I am looking at firebase documentation for testing security rules, but it's very limiting and has no information.
this is my security rule that I want to test:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId}{
allow read: if belongsTo(userId) && isLoggedIn()
}
}
function isLoggedIn() {
return request.auth.uid != null
}
function belongsTo(userId){
return request.auth.uid == resource.data.uid
}
This is my test file:
import {
assertFails,
assertSucceeds,
initializeTestEnvironment,
RulesTestEnvironment,
} from "@firebase/rules-unit-testing";
import { readFileSync } from "fs";
// Assuming a Firestore app and the Firestore emulator for this example
import { setDoc } from "firebase/firestore";
test("this is test, checks that only authenticated user can see their data", async () => {
let testEnv = await initializeTestEnvironment({
projectId: "myproject-ce845",
firestore: {
rules: readFileSync("firestore.rules", "utf8"),
port: 8080,
},
});
const alice = testEnv.authenticatedContext("alice");
await assertSucceeds(setDoc(alice.firestore(), "/users/alice"));
});
this is the error that I'm getting:
{"error":{"code":400,"message":"Error compiling rules:\nL29:5 missing ';' at 'return'\nL33:3 mismatched input '}' expecting {'&&', ',', '.', '==', '>', '>=', '[', '<', '<=', '-', '%', '!=', '||', '+', ']', '\/', '*', '?', 'in', 'is'}","status":"INVALID_ARGUMENT"}}