简体   繁体   English

启用 App Check 的单元测试可调用 firebase function

[英]Unit testing callable firebase function with App Check enabled

I am trying to unit test my firebase callable cloud function according to the examples provided.我正在尝试根据提供的示例对我的 firebase 可调用云 function 进行单元测试。 See Firebase Example .请参阅Firebase 示例 It boils down to something like this它归结为这样的事情

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,
    });
  });

});

The problem is that the function is protected by App Check in such a manner that if I try to test it, it always fails the App Check test:问题是 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.')
    }

How do I include a debug App Check Token, so that I can test the function?如何包含调试 App Check Token,以便我可以测试 function? For setting up AppCheck on iOS I followed this guid Enable App Check with App Attest on Apple platforms .为了在 iOS 上设置 AppCheck,我遵循了这个 guid Enable App Check with App Attest on Apple platforms When it comes to enforce AppCheck on cloud functions I followed these steps mentioned here.当涉及到对云功能执行 AppCheck 时,我遵循了此处提到的这些步骤。 Enable App Check enforcement for Cloud Functions . 为 Cloud Functions 启用 App Check 强制执行

After some digging I found out that you need to provide an app object to the wrapped function like this:经过一番挖掘后,我发现您需要像这样向包装的 function 提供一个应用程序 object:

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

Hope this helps someone!希望这对某人有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序检查 Firebase 可调用函数上不需要的强制执行 - App check unwanted enforcement on Firebase callable functions Firebase 可调用 Function + CORS - Firebase Callable Function + CORS 防止 android 可调用时应用程序崩溃 firebase function 抛出异常 - Prevent android app crash when callable firebase function throws exception Firebase 可调用 Function 返回空数据 - Firebase Callable Function returning nill data 如何从 TypeScript 中的可调用 Firebase Cloud Function 向我的 Unity 应用程序发送自定义 object? - How can I send custom object from my callable Firebase Cloud Function in TypeScript to my Unity app? Firebase 可调用函数模拟器配置不起作用 - Firebase Callable Function Emulator configuration not working Firebase 可调用云 Function CORS 错误使用 Z035489FF8D092741943E4A83241F5 - Firebase Callable Cloud Function CORS Error using Firebase Emulator and Vite 从 Firebase 可调用 function(云功能)返回 stream 的正确方法? - Proper way to return stream from Firebase callable function (Cloud function)? Firebase Cloud Functions - 从 https 中的上下文获取来源可调用 function - Firebase Cloud Functions - get origin from context in https callable function 我的 firebase 可调用云 function 被调用了两次(当我只调用一次时),如何避免我的 flutter 应用程序失败? - My firebase callable cloud function is called twice (when I called only once), how can I avoid my flutter app from failure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM