Skip to content

Fix RUTv2 not setting RTDB namespace by default. #5501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-brooms-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/rules-unit-testing": patch
---

Fix typing issues where Database/Firestore/Storage compat instances returned by RulesTestContext are not compatible with v9 modular APIs.
5 changes: 5 additions & 0 deletions .changeset/little-foxes-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/rules-unit-testing": patch
---

Set RTDB namespace to be same as projectId by default instead of `${projectId}-default-rtdb`. This fixes rules not being applied and other issues related to namespace mismatch.
8 changes: 8 additions & 0 deletions packages/rules-unit-testing/src/impl/test_environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ class RulesTestContextImpl implements RulesTestContext {
}
database(databaseURL?: string): firebase.database.Database {
assertEmulatorRunning(this.emulators, 'database');
if (!databaseURL) {
const url = makeUrl(this.emulators.database, '');
// Make sure to set the namespace equal to projectId -- otherwise the RTDB SDK will by default
// use `${projectId}-default-rtdb`, which is treated as a different DB by the RTDB emulator
// (and thus WON'T apply any rules set for the `projectId` DB during initialization).
url.searchParams.append('ns', this.projectId);
databaseURL = url.toString();
}
const database = this.getApp().database(databaseURL);
database.useEmulator(
this.emulators.database.host,
Expand Down
6 changes: 6 additions & 0 deletions packages/rules-unit-testing/src/public_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
import { FirebaseSignInProvider } from '@firebase/util';
import firebase from 'firebase/compat/app';

// These import statements allow v9 compat instances (created by RulesTestContext) to be used in v9
// modular APIs, e.g. `doc(context.firestore(), 'a/b')` in developer code. (a.k.a. "interop mode".)
import 'firebase/compat/database';
import 'firebase/compat/firestore';
import 'firebase/compat/storage';

/**
* More options for the mock user token to be used for testing, including developer-specfied custom
* claims or optional overrides for Firebase Auth token payloads.
Expand Down