【问题标题】:Why an error "connect ECONNREFUSED 127.0.0.1:8080" occurs when testing firestore rules with npm为什么使用 npm 测试 firestore 规则时出现错误“connect ECONNREFUSED 127.0.0.1:8080”
【发布时间】:2023-02-16 08:21:28
【问题描述】:

我想测试 Firestore 规则。下面是 firestore.rules。我想检查这些安全规则是否有效。然后我尝试使用 jest 和 firebase 测试。但是,在执行“npm test”时,出现错误“connect ECONNREFUSED 127.0.0.1:8080”。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /clubUsers/{uid} {
      allow read: if request.auth != null
        && request.auth.uid == uid;
      allow create: if request.auth != null
        && request.auth.uid == uid;   
      allow update: if request.auth != null
        && request.auth.uid == uid;  
    }
  }
}

我的测试脚本在这里。

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

const project_id = "PROJECT ID";

describe("testing firestore rules", () => {

    beforeAll(
        async () => {
            await firebase.loadFirestoreRules({
                projectId: project_id,
                rules: fs.readFileSync('../../firestore.rules', 'utf8'),
            });
        }
    );

    afterEach(
        async () => {
            await firebase.clearFirestoreData({ projectId: project_id });
        }
    );

    afterAll(
        async () => {
            await Promise.all(
                firebase.apps().map((app) => app.delete())
            );
        }
    );

    function authedApp(auth) {
        return firebase.initializeTestApp({
            projectId: project_id,
            auth: auth,
        }).firestore();
    }

    describe("testing get and write", () => {

        test("testing get", async () => {
            const db = authedApp({ uid: 'UID' });
            const message = db.collection("clubUsers").doc("UID");
            await firebase.assertSucceeds(message.get());
        })

        test("testing write", async () => {
            const db = authedApp({ uid: "UID" });
            const message = db.collection("clubUsers").doc("UID");
            await firebase.assertSucceeds(
                message.set({ text: "hoge" })
            );
        })
    })

})

我在打开 firebase 模拟器时尝试了测试。

我通过在终端上执行 sudo lsof -P -i:8080 来检查什么正在使用端口 8080。 但是,没有任何东西使用端口 8080。

【问题讨论】:

    标签: javascript firebase testing jestjs firebase-security


    【解决方案1】:

    今天也遇到了这个……几个来源(GitHub 和here)导致了一个解决方案。这是调用 firebase.clearFirestoreData() 时出现的问题,当在默认主机上找不到模拟器时会发生:它期望的端口。

    解决方案建议设置一个环境变量来定义适用于您的设置的主机和端口。例如:

    process.env.FIRESTORE_EMULATOR_HOST = '127.0.0.1:5002';
    

    在使用firebase emulators:start 启动模拟器后,您可以找到要使用的正确主机:端口组合

    这已在新的模块化 v9 JS SDK 中得到解决,但确实需要对新 API 进行一些重构(如 docs 中所定义)。您现在可以指定主持人和港口初始化时:

    testEnv = await initializeTestEnvironment({
        projectId: projectId,
        firestore: {
            host: '127.0.0.1',
            port: 5002,
        }
    });
    

    附言。不要像我一样被愚弄,假设 127.0.0.1 和本地主机总是相同的!

    【讨论】:

      猜你喜欢
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 2023-02-24
      • 1970-01-01
      • 2021-09-03
      • 2022-11-23
      • 2023-04-10
      相关资源
      最近更新 更多