不允许任何客户端用户写入 - Firebase 和 Mocha

Not allow any client side user to write - Firebase and Mocha

我正在尝试使用 Firebase 安全规则创建单元测试。我不断收到以下错误:

> test@1.0.0 test C:\Users\simeon.ramjit\Documents\projects\plant-order-form\test
> mocha --exit

  My cool app
Warning: FIRESTORE_EMULATOR_HOST not set, using default value localhost:8080    
    √ Understands basic addition
    1) Not allow any client side user to write to GLOBAL_DATA/DASH_DATA

  1 passing (2s)
  1 failing

  1) My cool app
       Not allow any client side user to write to GLOBAL_DATA/DASH_DATA:    
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\simeon.ramjit\Documents\projects\plant-order-form\test\test.js)
      at listOnTimeout (internal/timers.js:554:17)
      at processTimers (internal/timers.js:497:7)

npm ERR! Test failed.  See above for more details.

使用 done() 无法解决问题,设置更长的超时期限也无法解决。下面是我的 test.js

const assert = require("assert");
const firebase = require("@firebase/rules-unit-testing");

const MY_PROJECT_ID = "example";
const idAdmin = "user_admin";
const adminAuth = { uid: idAdmin, email: "admin@example.com", admin: true };

function getFirestore(auth) {
  return firebase
    .initializeTestApp({ projectId: MY_PROJECT_ID, auth: auth })
    .firestore();
}

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

describe("My cool app", () => {
  it("Understands basic addition", () => {
    assert.strictEqual(2 + 2, 4);
  });

  it("Not allow any client side user to write to GLOBAL_DATA/DASH_DATA",async () => {
    const db = getFirestore(adminAuth);
    const testDoc = db.collection('GLOBAL_DATA').doc('DASH_DATA');

    await firebase.assertFails(testDoc.get());
    

  })
});

after(async () => {
  await firebase.clearFirestoreData({ projectId: MY_PROJECT_ID });
});

下面是firebase emulators:start

的结果
PS C:\Users\simeon.ramjit\Documents\projects\plant-order-form> firebase emulators:start
i  emulators: Starting emulators: auth, functions, firestore
!  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: database, hosting, pubsub
!  Your requested "node" version "12" doesn't match your global version "14"
i  firestore: Firestore Emulator logging to firestore-debug.log
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "C:\Users\simeon.ramjit\Documents\projects\plant-order-form\functions" for Cloud Functions...
+  functions[changeUserAuthorization]: http function initialized (http://localhost:5001/plant-order-form/us-central1/changeUserAuthorization).
+  functions[createUser]: http function initialized (http://localhost:5001/plant-order-form/us-central1/createUser).
+  functions[deleteUser]: http function initialized (http://localhost:5001/plant-order-form/us-central1/deleteUser).
+  functions[downloadItemPdf]: http function initialized (http://localhost:5001/plant-order-form/us-central1/downloadItemPdf).
+  functions[downloadReqsSplit]: http function initialized (http://localhost:5001/plant-order-form/us-central1/downloadReqsSplit).
+  functions[sendRepeatOrderStatusChangeEmail]: firestore function initialized.
+  functions[stripGeneralDataFromItemUpload]: firestore function initialized.
+  functions[sendEmailReciept]: firestore function initialized.
i  functions[updateItemDatabase]: function ignored because the storage emulator does not exist or is not running.
i  functions[scheduledFirestoreExport]: function ignored because the pubsub emulator does not exist or is not running.
+  functions[firestoreImport]: http function initialized (http://localhost:5001/plant-order-form/us-central1/firestoreImport).
+  functions[updateBlynkSevenC]: http function initialized (http://localhost:5001/plant-order-form/us-central1/updateBlynkSevenC).
+  functions[updateBlynkNineC]: http function initialized (http://localhost:5001/plant-order-form/us-central1/updateBlynkNineC).
+  functions[updateMachineStatus]: http function initialized (http://localhost:5001/plant-order-form/us-central1/updateMachineStatus).
+  functions[resetConversionMachineOrderDetails]: http function initialized (http://localhost:5001/plant-order-form/us-central1/resetConversionMachineOrderDetails).
+  functions[updateTimeStatusChanged]: http function initialized (http://localhost:5001/plant-order-form/us-central1/updateTimeStatusChanged).
i  functions[sendSixAmOrderSummary]: function ignored because the pubsub emulator does not exist or is not running.
i  functions[sendTwelvePmOrderSummary]: function ignored because the pubsub emulator does not exist or is not running.
+  functions[downloadBatchRequisition]: http function initialized (http://localhost:5001/plant-order-form/us-central1/downloadBatchRequisition).
+  functions[stripBatchNumber]: firestore function initialized.
i  functions[sendOrderScheduledSummary]: function ignored because the pubsub emulator does not exist or is not running.
+  functions[updateBlynkExtrusion]: firestore function initialized.
+  functions[downloadBtReq]: http function initialized (http://localhost:5001/plant-order-form/us-central1/downloadBtReq).
i  functions[sendScheduledBatchConfirmation]: function ignored because the pubsub emulator does not exist or is not running.
i  functions[updateMouldingItemDatabase]: function ignored because the storage emulator does not exist or is not running.
+  functions[sendMouldingSlackMessage]: firestore function initialized.
+  functions[sendDailyMouldingProductionUpdate]: http function initialized (http://localhost:5001/plant-order-form/us-central1/sendDailyMouldingProductionUpdate).
i  functions[uploadPriceList]: function ignored because the storage emulator does not exist or is not running.

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://localhost:4000                │
└─────────────────────────────────────────────────────────────┘

┌────────────────┬────────────────┬─────────────────────────────────┐
│ Emulator       │ Host:Port      │ View in Emulator UI             │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Authentication │ localhost:9099 │ http://localhost:4000/auth      │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Functions      │ localhost:5001 │ http://localhost:4000/functions │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Firestore      │ localhost:8080 │ http://localhost:4000/firestore │
└────────────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at localhost:4400
  Other reserved ports: 4500

Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.

您可以在此处阅读更深入的讨论:https://github.com/firebase/firebase-js-sdk/issues/4674

我通过在我的机器上启动一个 VM 和 运行 那里的测试套件来解决这个问题。我记得看到 post 关于卡巴斯基导致同样的问题。所以也许只是尝试禁用您的防病毒软件。

Firebase 测试没有任何问题,它在虚拟机上按预期工作。