3

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.

Klo
  • 145
  • 8
  • I found that this [quickstart](https://firebase.google.com/docs/firestore/security/test-rules-emulator#quickstart) you are following is missing some important parameters and possibly steps from the [Unit testing of Cloud Functions](https://firebase.google.com/docs/functions/unit-testing) page. It would be great for the community if you could share how you have configured [AppCheck](https://firebase.google.com/docs/app-check) or which documentation you are following; in the code you provided, the initialization of this is not visible. – Alex Mar 30 '22 at 19:48
  • 1
    Thanks for the hint. I added some more links to clarify. Hope this helps. – Klo Mar 31 '22 at 13:59
  • I can see you have possibly not made the necessary changes from the quickstart version you have from the documentation I provided. Could you please take a second look at the differences and update with the missing information and parameters like the `databaseURL`, `storageBucket`. Did you follow the Unit testing link provided in the previous comment? Please be aware the quickstart will only give a starting point and might not provide the full setup you need. – Alex Apr 13 '22 at 17:40

1 Answers1

5

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!

Klo
  • 145
  • 8