Skip to content

Commit 3a19f9e

Browse files
authored
Fix assertFails not recognising database permission denied error (#4093)
* Fix assertFails not recognising database permission denied error * Add rules-unit-testing changeset * Add assertFails test when code is PERMISSION_DENIED
1 parent bab4e19 commit 3a19f9e

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

.changeset/early-dots-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/rules-unit-testing": patch
3+
---
4+
5+
Fix assertFails not recognising database permission denied error

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,14 @@ export function assertFails(pr: Promise<any>): any {
454454
);
455455
},
456456
(err: any) => {
457+
const errCode = (err && err.code && err.code.toLowerCase()) || '';
458+
const errMessage =
459+
(err && err.message && err.message.toLowerCase()) || '';
457460
const isPermissionDenied =
458-
(err && err.message && err.message.indexOf('PERMISSION_DENIED') >= 0) ||
459-
(err && err.code === 'permission-denied');
461+
errCode === 'permission-denied' ||
462+
errCode === 'permission_denied' ||
463+
errMessage.indexOf('permission_denied') >= 0;
464+
460465
if (!isPermissionDenied) {
461466
return Promise.reject(
462467
new Error(

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,31 @@ describe('Testing Module Tests', function () {
103103
.catch(() => {});
104104
});
105105

106+
it('assertFails() if code is PERMISSION_DENIED', async function () {
107+
const success = Promise.resolve('success');
108+
const permissionDenied = Promise.reject({
109+
code: 'PERMISSION_DENIED'
110+
});
111+
const otherFailure = Promise.reject('failure');
112+
await firebase
113+
.assertFails(success)
114+
.then(() => {
115+
throw new Error('Expected success to fail.');
116+
})
117+
.catch(() => {});
118+
119+
await firebase.assertFails(permissionDenied).catch(() => {
120+
throw new Error('Expected permissionDenied to succeed.');
121+
});
122+
123+
await firebase
124+
.assertFails(otherFailure)
125+
.then(() => {
126+
throw new Error('Expected otherFailure to fail.');
127+
})
128+
.catch(() => {});
129+
});
130+
106131
it('initializeTestApp() with auth=null does not set access token', async function () {
107132
const app = firebase.initializeTestApp({
108133
projectId: 'foo',

0 commit comments

Comments
 (0)