单元测试Firebase存储安全规则时如何上传文件?

发布于 2025-02-12 01:36:39 字数 2175 浏览 0 评论 0原文

我有一个可以上传音频文件的水桶,我的目标是测试它是否按预期工作。但是,当我尝试使用“@firebase/rulet-unit测试”库将测试文件上传到仿真器中时,上传被卡住了,没有取得进展。

规则

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if false;
    }
    match /audio/{audioFileID} {
      allow create: if request.auth != null && request.resource.contentType.matches('audio/(flac|wav)');
    }
  }
}

测试代码

import {
  assertFails,
  assertSucceeds,
  initializeTestEnvironment,
  RulesTestEnvironment,
} from "@firebase/rules-unit-testing";
import { test, beforeAll, beforeEach, afterAll } from "vitest";
import fs from "fs";

const createTestFile = (size: number) => Buffer.alloc(size);
let testEnv: RulesTestEnvironment;

beforeAll(async () => {
  testEnv = await initializeTestEnvironment({
    projectId: "stst-et-interviewer-dev",
    hub: {
      host: "localhost",
      port: 4400,
    },
  });
});

beforeEach(async () => {
  await testEnv.clearStorage();
  await testEnv.clearFirestore();
});

afterAll(async () => await testEnv.cleanup());

const loadStepStoneImage = () =>
  fs.readFileSync("./public/images/stepstoneLogo.svg");

test("Storage does not allow you to read files", async () => {
  const rouge = testEnv.unauthenticatedContext();
  const alice = testEnv.authenticatedContext("alice");
  await assertFails(
    rouge.storage().ref("audio/test-interview.flac").getDownloadURL()
  );
  await assertFails(
    alice.storage().ref("audio/test-interview.flac").getDownloadURL()
  );
  await assertFails(alice.storage().ref("audio/").listAll());
});

test("Storage does not allow you to upload a file if you are not logged in", async () => {
  const rouge = testEnv.unauthenticatedContext();
  const upload = rouge
    .storage("stst-et-interviewer-dev.appspot.com")
    .ref("audio/logo.svg")
    .put(createTestFile(200), { contentType: "audio/flac" });
  
  await assertFails(upload.then()); // <- GETS STUCK HERE
  console.log("this does not happen");
});

发生这种情况的可能原因是什么?有什么简单的调试方法吗?

I have a bucket into which users can upload audio files, and my goal is to test that it is working as expected. But when I try to use the "@firebase/rules-unit-testing" library to upload a test file into the emulator the upload gets stuck and makes no progress.

Rules

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if false;
    }
    match /audio/{audioFileID} {
      allow create: if request.auth != null && request.resource.contentType.matches('audio/(flac|wav)');
    }
  }
}

Test code

import {
  assertFails,
  assertSucceeds,
  initializeTestEnvironment,
  RulesTestEnvironment,
} from "@firebase/rules-unit-testing";
import { test, beforeAll, beforeEach, afterAll } from "vitest";
import fs from "fs";

const createTestFile = (size: number) => Buffer.alloc(size);
let testEnv: RulesTestEnvironment;

beforeAll(async () => {
  testEnv = await initializeTestEnvironment({
    projectId: "stst-et-interviewer-dev",
    hub: {
      host: "localhost",
      port: 4400,
    },
  });
});

beforeEach(async () => {
  await testEnv.clearStorage();
  await testEnv.clearFirestore();
});

afterAll(async () => await testEnv.cleanup());

const loadStepStoneImage = () =>
  fs.readFileSync("./public/images/stepstoneLogo.svg");

test("Storage does not allow you to read files", async () => {
  const rouge = testEnv.unauthenticatedContext();
  const alice = testEnv.authenticatedContext("alice");
  await assertFails(
    rouge.storage().ref("audio/test-interview.flac").getDownloadURL()
  );
  await assertFails(
    alice.storage().ref("audio/test-interview.flac").getDownloadURL()
  );
  await assertFails(alice.storage().ref("audio/").listAll());
});

test("Storage does not allow you to upload a file if you are not logged in", async () => {
  const rouge = testEnv.unauthenticatedContext();
  const upload = rouge
    .storage("stst-et-interviewer-dev.appspot.com")
    .ref("audio/logo.svg")
    .put(createTestFile(200), { contentType: "audio/flac" });
  
  await assertFails(upload.then()); // <- GETS STUCK HERE
  console.log("this does not happen");
});

What are the possible reasons for this happening? Are there any simple ways of debugging?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文