gpt4 book ai didi

javascript - Firebase 模拟器上的 Firebase 权限被拒绝错误

转载 作者:行者123 更新时间:2023-12-04 00:56:39 25 4
gpt4 key购买 nike

我引用这个 tutorial用于 Firestore 安全规则。我已经从存储库中提取了代码,它与视频的代码相匹配。
我更改了 setup运行 firestore.rules 的代码而不是 firestore-test.rules ,并尝试运行 firebase emulators:startjest ./spec遵循相同的目录结构,我未通过 "should allow delete when user is admin" 的测试和 "should not allow delete for normal user"它失败的原因是通配符中的写入规则。有谁知道出了什么问题?

collections.spec.js

const { setup, teardown } = require("./helpers");


describe("General Safety Rules", () => {
afterEach(async () => {
await teardown();
});

test("should deny a read to the posts collection", async () => {
const db = await setup();
const postsRef = db.collection("posts");
await expect(postsRef.get()).toDeny();
});

test("should deny a write to users even when logged in", async () => {
const db = await setup({
uid: "danefilled"
});

const usersRef = db.collection("users");
await expect(usersRef.add({ data: "something" })).toDeny();
});
});

describe("Posts Rules", () => {
afterEach(async () => {
await teardown();
});

test("should allow update when user owns post", async () => {
const mockData = {
"posts/id1": {
userId: "danefilled"
},
"posts/id2": {
userId: "not_filledstacks"
}
};

const mockUser = {
uid: "danefilled"
};

const db = await setup(mockUser, mockData);

const postsRef = db.collection("posts");

await expect(
postsRef.doc("id1").update({ updated: "new_value" })
).toAllow();

await expect(postsRef.doc("id2").update({ updated: "new_value" })).toDeny();
});

test("should allow delete when user owns post", async () => {
const mockData = {
"posts/id1": {
userId: "danefilled"
},
"posts/id2": {
userId: "not_filledstacks"
}
};

const mockUser = {
uid: "danefilled"
};

const db = await setup(mockUser, mockData);

const postsRef = db.collection("posts");

await expect(postsRef.doc("id1").delete()).toAllow();

await expect(postsRef.doc("id2").delete()).toDeny();
});

test("should allow delete when user is admin", async () => {
const mockData = {
"users/filledstacks": {
userRole: "Admin"
},
"posts/id1": {
userId: "not_matching1"
},
"posts/id2": {
userId: "not_matching2"
}
};

const mockUser = {
uid: "filledstacks"
};

const db = await setup(mockUser, mockData);

const postsRef = db.collection("posts");

await expect(postsRef.doc("id1").delete()).toAllow();
});

test("should not allow delete for normal user", async () => {
const mockData = {
"users/filledstacks": {
userRole: "User"
},
"posts/id1": {
userId: "not_matching1"
},
"posts/id2": {
userId: "not_matching2"
}
};

const mockUser = {
uid: "filledstacks"
};

const db = await setup(mockUser, mockData);

const postsRef = db.collection("posts");

await expect(postsRef.doc("id1").delete()).toDeny();
});

test("should allow adding a post when logged in", async () => {
const db = await setup({
uid: "userId"
});

const postsRef = db.collection("posts");
await expect(postsRef.add({ title: "new_post" })).toAllow();
});

test("should deny adding a post when not logged in", async () => {
const db = await setup();
const postsRef = db.collection("posts");
await expect(postsRef.add({ title: "new post" })).toDeny();
});
});

firestore.rules
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {



// lock down the db
match /{document=**} {
allow read: if false;
allow write: if false;
}

match /posts/{postId} {
allow update: if userOwnsPost();
allow delete: if userOwnsPost() || userIsAdmin();
allow create: if loggedIn();
}

function loggedIn() {
return request.auth.uid != null;
}

function userIsAdmin() {
return getUserData().userRole == 'Admin';
}

function getUserData() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data
}

function userOwnsPost() {
return resource.data.userId == request.auth.uid;
}
}
}

来自终端的错误跟踪
FirebaseError: 7 PERMISSION_DENIED: 
false for 'create' @ L10


● Posts Rules › should not allow delete for normal user

FirebaseError: 7 PERMISSION_DENIED:
false for 'create' @ L10

at new FirestoreError (/Users/../../../../../../../../../Resources/rules/node_modules/@firebase/firestore/src/util/error.ts:166:5)
at ClientDuplexStream.<anonymous> (/Users/../../../../../../../../../Resources/rules/node_modules/@firebase/firestore/src/platform_node/grpc_connection.ts:240:13)
at ClientDuplexStream._emitStatusIfDone (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client.js:234:12)
at ClientDuplexStream._receiveStatus (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client.js:211:8)
at Object.onReceiveStatus (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:1311:15)
at InterceptingListener._callNext (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:568:42)
at InterceptingListener.onReceiveStatus (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:618:8)
at /Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:1127:18

最佳答案

我实际上是按照相同的教程开始使用 firebase 模拟器并收到相同类型的错误消息。我的问题是当你启动模拟器时它会自动寻找你的firestore.rules。文件并加载规则。因此,当您添加 mockData规则已经适用。

为了使您的测试代码正常工作,请更改您的 firebase.json 中的 Firestore 规则文件的设置。到一个不存在的文件(或允许所有读/写的规则文件)或添加 mockData作为您的 setup 中的管理员功能,例如:

module.exports.setup = async (auth, data) => {
const projectId = `rules-spec-${Date.now()}`;
const app = firebase.initializeTestApp({
projectId,
auth
});

const db = app.firestore();

// Initialize admin app
const adminApp = firebase.initializeAdminApp({
projectId
});

const adminDB = adminApp.firestore();

// Write mock documents before rules using adminApp
if (data) {
for (const key in data) {
const ref = adminDB.doc(key);
await ref.set(data[key]);
}
}

// Apply rules
await firebase.loadFirestoreRules({
projectId,
rules: fs.readFileSync('firestore.rules', 'utf8')
});

return db;
};

希望这可以帮助。

另见 this question

关于javascript - Firebase 模拟器上的 Firebase 权限被拒绝错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61984553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com