简体   繁体   中英

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. See Firebase Example . 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:

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? For setting up AppCheck on iOS I followed this 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. Enable App Check enforcement for Cloud Functions .

After some digging I found out that you need to provide an app object to the wrapped function like this:

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

Hope this helps someone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM