'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.



Solution 1:[1]

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!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Klo