firebase - Is it possible to mock an authenticated Firebase user, in order to run full testing with jest, while preserving the database rules?
问题描述
I have a Firebase Realtime Database set up with a bunch of basic rules revolving around basic user authentication:
{
"rules": {
".read": false,
".write": false,
"users": {
"$user_id" : {
".read": "$user_id === auth.uid",
".write": "$user_id === auth.uid",
}
}
}
}
These rules depend on the auth.uid
built-in Firebase variable, which is accessible during development/production as a user is logged in with any given provider.
However, when I am running jest tests on a duplicated database with the same rules, I do not have access to this auth.uid
since there is no actual user logged in.
Is there a way to solve this? A way to perhaps mock some sort of testing user on Firebase itself or through my client-side test code?
I would like the testing to run on a database that is as similar as possible to the regular database. I don't want to have a different rules structure on the test database.
解决方案
我想出的可能解决方案更像是一种解决方法:
- 设置一个测试环境变量,如
FIREBASE_TEST_UID
. - 运行测试时引用此 uid。
- 手动更改与实际存储的比较
$user_id"
规则auth.uid
FIREBASE_TEST_UID
- 现在,数据库受到公共非“经过身份验证”用户的读写保护,而规则结构保持相似并且
FIREBASE_TEST_UID
是私有的(.gitignored env 文件)。
推荐阅读
- python - 在 keras 模型中获取“只有 size-1 数组可以转换为 python 标量”错误
- java - 当 listView 为空时,setEmptyView 不显示图像
- c++ - 将 const 函数引用绑定到 lambda
- r - 如何在不考虑顺序的情况下查找字符串是否包含某些字符?
- google-sheets - Google Sheets api 仅适用于我的浏览器
- sql - PowerApps - 如何从库列表中编辑表单导致一个屏幕
- apache-kafka - Kafka会话超时问题
- node.js - Nginx 上游服务器在其中一个关闭时全部关闭
- r - 如何在有条件的 R 中使用 ColSums
- python - 使用多个分隔符按日期对字符串进行排序