使用经过身份验证的帐户进行 firebase-functions-test

问题描述 投票:0回答:1

我有一些需要用户登录才能调用的 Firebase 函数(例如,您需要登录才能查看数据库中的任何数据):

const exampleEndpoint = onCall(async (request) => {
    if (!request.auth || !request.auth.uid) {
        throw new HttpsError(
            'unauthenticated',
            `You must be logged in to call the API`
        );
    }

    // rest of function ...
});

我正在使用

firebase-functions-test
进行一些单元测试,但找不到任何有关如何以登录用户身份测试功能的文档。我试过:

  • 在调用包装函数之前在测试上下文中使用signInWithEmailAndPassword()登录
  • 将 { auth: ... } 添加到带有身份验证信息的包装函数调用中
  • 将所有输入数据记录到函数中
  • 恢复到 v1 功能

如何使用经过身份验证的用户测试我的函数,例如当您从前端应用程序正常调用函数时如何传入身份验证信息?

firebase unit-testing firebase-authentication google-cloud-functions
1个回答
0
投票

经过大量搜索,似乎在编写 firebase-functions-test 时不支持此功能。

但是,您可以像通常在客户端一样调用这些函数。您可以在管理测试配置旁边拥有一个普通的客户端应用程序:

const firebaseConfig = {
    // ...
};
const clientApp = initializeApp(firebaseConfig);
const functions = getFunctions(clientApp);

httpsCallable(functions, "functionName")(data);

这对于 onCall 函数来说很好,因为您不需要管理员级别访问权限(例如直接测试触发器),尽管您确实需要始终部署函数并且日志记录不会显示在测试控制台中

© www.soinside.com 2019 - 2024. All rights reserved.