【问题标题】:Using Firebase Emulators and the @firebase/testing library, how can you update a user's custom claims?使用 Firebase 模拟器和 @firebase/testing 库,如何更新用户的自定义声明?
【发布时间】:2022-01-07 14:42:42
【问题描述】:

我有一个云功能,可以在某个字段更改时更改用户的自定义声明。

我正在使用 firebase 模拟器为我的应用编写单元测试。我需要手动更改用户声明(因为函数不会在测试模式下运行)。我想测试在云功能更改自定义声明后数据库拒绝用户访问

谢谢

【问题讨论】:

  • @firebase/testing 已弃用,请使用@firebase/rules-unit-testing。它的文档are here.
  • 我使用的是 firebase sdk 版本 8
  • 我链接的文档还包含您将用于 v8 的步骤。
  • v8的步骤使用“initializeTestApp”,这是来自@firebase/testing的函数。

标签: firebase firebase-realtime-database firebase-authentication


【解决方案1】:

@firebase/testing 已弃用,上次更新时间为 2020 年 10 月。

改用@firebase/rules-unit-testing

  • 对于 Web SDK 的 v8.10.0,请使用 npm install @firebase/rules-unit-testing@1.3.15(自 2021 年 8 月起,Firebase v9 之前的最后一次更新)。
  • 对于 v9.0.0 及更高版本,在模块化和命名空间模式下,请使用 npm install @firebase/rules-unit-testing

从技术上讲,它们曾经是同一个软件包,但一个正在获得新的更新,而另一个没有。因为它们是从相同的代码库构建的,所以在处理旧的命名空间 Firebase SDK 时,它们的 API 是相似的。对于所有新代码,您应该在模块化 SDK 和 modular test methods 旁边使用 @firebase/rules-unit-testing

基于documentation

import * as firebase from "@firebase/rules-unit-testing"; // note this "firebase" is not the normal firebase namespace!

const testApp = firebase.initializeTestApp({
  projectId: "my-test-project",
  auth: {
    uid: "alice",
    email: "alice@example.com",
    isAdmin: true,               // custom claim
    isModerator: true            // custom claim
  }
});

如果在您的代码中使用普通的 Firebase 命名空间,请使用以下代码导入测试库:

import * as firebase from "firebase"; // for v8 or older.
import * as firebaseTest from "@firebase/rules-unit-testing";

【讨论】:

    猜你喜欢
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 2020-06-17
    • 2019-05-04
    • 2021-12-31
    • 2020-01-20
    • 2019-07-02
    相关资源
    最近更新 更多