Fix custom claims (#3915)

This commit is contained in:
Sam Stern
2020-10-09 13:15:21 -04:00
committed by GitHub
parent 9007e00f5f
commit 153ee400fb
3 changed files with 50 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@firebase/rules-unit-testing': patch
---
Fix custom claims in rules-unit-testing
+1 -1
View File
@@ -105,7 +105,7 @@ export type FirebaseIdToken = {
};
// Custom claims set by the developer
claims?: object;
[claim: string]: any;
};
// To avoid a breaking change, we accept the 'uid' option here, but
@@ -202,6 +202,50 @@ describe('Testing Module Tests', function () {
);
});
it('initializeAdminApp() works with custom claims', async function () {
await firebase.loadFirestoreRules({
projectId: 'foo',
rules: `service cloud.firestore {
match /databases/{db}/documents/{doc=**} {
allow read, write: if request.auth.token.custom_claim == 'foo';
}
}`
});
const noClaim = firebase.initializeTestApp({
projectId: 'foo',
auth: {
uid: 'noClaim'
}
});
const hasClaim = firebase.initializeTestApp({
projectId: 'foo',
auth: {
uid: 'hasClaim',
custom_claim: 'foo'
}
});
const wrongClaim = firebase.initializeTestApp({
projectId: 'foo',
auth: {
uid: 'wrongClaim',
custom_claim: 'bar'
}
});
await firebase.assertSucceeds(
hasClaim.firestore().doc('test/test').set({ hello: 'test' })
);
await firebase.assertFails(
noClaim.firestore().doc('test/test').set({ hello: 'test' })
);
await firebase.assertFails(
wrongClaim.firestore().doc('test/test').set({ hello: 'test' })
);
});
it('loadDatabaseRules() throws if no databaseName or rules', async function () {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await expect((firebase as any).loadDatabaseRules.bind(null, {})).to.throw(