Skip to content

Commit fe0ae19

Browse files
authored
Fix custom claims (#3915)
1 parent 4f997bc commit fe0ae19

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

.changeset/silent-brooms-search.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/rules-unit-testing': patch
3+
---
4+
5+
Fix custom claims in rules-unit-testing

packages/rules-unit-testing/src/api/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export type FirebaseIdToken = {
105105
};
106106

107107
// Custom claims set by the developer
108-
claims?: object;
108+
[claim: string]: any;
109109
};
110110

111111
// To avoid a breaking change, we accept the 'uid' option here, but

packages/rules-unit-testing/test/database.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,50 @@ describe('Testing Module Tests', function () {
202202
);
203203
});
204204

205+
it('initializeAdminApp() works with custom claims', async function () {
206+
await firebase.loadFirestoreRules({
207+
projectId: 'foo',
208+
rules: `service cloud.firestore {
209+
match /databases/{db}/documents/{doc=**} {
210+
allow read, write: if request.auth.token.custom_claim == 'foo';
211+
}
212+
}`
213+
});
214+
215+
const noClaim = firebase.initializeTestApp({
216+
projectId: 'foo',
217+
auth: {
218+
uid: 'noClaim'
219+
}
220+
});
221+
222+
const hasClaim = firebase.initializeTestApp({
223+
projectId: 'foo',
224+
auth: {
225+
uid: 'hasClaim',
226+
custom_claim: 'foo'
227+
}
228+
});
229+
230+
const wrongClaim = firebase.initializeTestApp({
231+
projectId: 'foo',
232+
auth: {
233+
uid: 'wrongClaim',
234+
custom_claim: 'bar'
235+
}
236+
});
237+
238+
await firebase.assertSucceeds(
239+
hasClaim.firestore().doc('test/test').set({ hello: 'test' })
240+
);
241+
await firebase.assertFails(
242+
noClaim.firestore().doc('test/test').set({ hello: 'test' })
243+
);
244+
await firebase.assertFails(
245+
wrongClaim.firestore().doc('test/test').set({ hello: 'test' })
246+
);
247+
});
248+
205249
it('loadDatabaseRules() throws if no databaseName or rules', async function () {
206250
// eslint-disable-next-line @typescript-eslint/no-explicit-any
207251
await expect((firebase as any).loadDatabaseRules.bind(null, {})).to.throw(

0 commit comments

Comments
 (0)