我有一些需要用户登录才能调用的 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
进行一些单元测试,但找不到任何有关如何以登录用户身份测试功能的文档。我试过:
如何使用经过身份验证的用户测试我的函数,例如当您从前端应用程序正常调用函数时如何传入身份验证信息?
经过大量搜索,似乎在编写 firebase-functions-test 时不支持此功能。
但是,您可以像通常在客户端一样调用这些函数。您可以在管理测试配置旁边拥有一个普通的客户端应用程序:
const firebaseConfig = {
// ...
};
const clientApp = initializeApp(firebaseConfig);
const functions = getFunctions(clientApp);
httpsCallable(functions, "functionName")(data);
这对于 onCall 函数来说很好,因为您不需要管理员级别访问权限(例如直接测试触发器),尽管您确实需要始终部署函数并且日志记录不会显示在测试控制台中