[英]Unit testing callable firebase function with App Check enabled
我正在嘗試根據提供的示例對我的 firebase 可調用雲 function 進行單元測試。 請參閱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,
});
});
});
問題是 function 受到 App Check 的保護,如果我嘗試測試它,它總是無法通過 App Check 測試:
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.')
}
如何包含調試 App Check Token,以便我可以測試 function? 為了在 iOS 上設置 AppCheck,我遵循了這個 guid Enable App Check with App Attest on Apple platforms 。 當涉及到對雲功能執行 AppCheck 時,我遵循了此處提到的這些步驟。 為 Cloud Functions 啟用 App Check 強制執行。
經過一番挖掘后,我發現您需要像這樣向包裝的 function 提供一個應用程序 object:
const result = await wrapped(data, {
auth: {
uid: "someID",
token: "SomeToken",
},
app: { appId: "SomeAppID" },
});
希望這對某人有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.