单元测试Firebase存储安全规则时如何上传文件?
我有一个可以上传音频文件的水桶,我的目标是测试它是否按预期工作。但是,当我尝试使用“@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论