启用应用程序检查的单元测试可调用firebase函数



我正试图根据提供的示例对我的firebase可调用云函数进行单元测试。请参见Firebase示例。它可以归结为类似的东西

const { expect } = require("chai");
const admin = require("firebase-admin");
const test = require("firebase-functions-test")({
projectId: "MYPROJECTID",
});
// Import the exported function definitions from our functions/index.js file
const myFunctions = require("../lib/test");
describe("Unit tests", () => {

after(() => {
test.cleanup();
});
it("tests a simple callable function", async () => {
const wrapped = test.wrap(myFunctions.sayHelloWorld);
const data = {
eventName: "New event"
};
// Call the wrapped function with data and context
const result = await wrapped(data);
// Check that the result looks like we expected.
expect(result).to.eql({
c: 3,
});
});
});

问题是,该功能受到应用程序检查的保护,如果我尝试测试它,它总是无法通过应用程序检查测试:

export const sayHelloWorld = functions.https.onCall(async (data, context) => {
// context.app will be undefined if the request doesn't include a valid
// App Check token.
if (context.app === undefined) {
throw new functions.https.HttpsError(
'failed-precondition',
'The function must be called from an App Check verified app.')
}

如何包含调试应用程序检查令牌,以便测试该功能?为了在iOS上设置AppCheck,我遵循了以下指南:在苹果平台上使用应用程序验证启用应用程序检查。当谈到在云功能上执行AppCheck时,我遵循了这里提到的这些步骤。为云功能启用应用程序检查强制。

经过一番挖掘,我发现您需要为包装的函数提供一个应用程序对象,如下所示:

const result = await wrapped(data, {
auth: {
uid: "someID",
token: "SomeToken",
},
app: { appId: "SomeAppID" },
});

希望这能帮助到别人!

相关内容

最新更新