Skip to content

Commit

Permalink
Fix assertFails not recognising database permission denied error (#4093)
Browse files Browse the repository at this point in the history
* Fix assertFails not recognising database permission denied error

* Add rules-unit-testing changeset

* Add assertFails test when code is PERMISSION_DENIED
  • Loading branch information
mikkelvp authored Nov 20, 2020
1 parent bab4e19 commit 3a19f9e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-dots-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/rules-unit-testing": patch
---

Fix assertFails not recognising database permission denied error
9 changes: 7 additions & 2 deletions packages/rules-unit-testing/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,14 @@ export function assertFails(pr: Promise<any>): any {
);
},
(err: any) => {
const errCode = (err && err.code && err.code.toLowerCase()) || '';
const errMessage =
(err && err.message && err.message.toLowerCase()) || '';
const isPermissionDenied =
(err && err.message && err.message.indexOf('PERMISSION_DENIED') >= 0) ||
(err && err.code === 'permission-denied');
errCode === 'permission-denied' ||
errCode === 'permission_denied' ||
errMessage.indexOf('permission_denied') >= 0;

if (!isPermissionDenied) {
return Promise.reject(
new Error(
Expand Down
25 changes: 25 additions & 0 deletions packages/rules-unit-testing/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ describe('Testing Module Tests', function () {
.catch(() => {});
});

it('assertFails() if code is PERMISSION_DENIED', async function () {
const success = Promise.resolve('success');
const permissionDenied = Promise.reject({
code: 'PERMISSION_DENIED'
});
const otherFailure = Promise.reject('failure');
await firebase
.assertFails(success)
.then(() => {
throw new Error('Expected success to fail.');
})
.catch(() => {});

await firebase.assertFails(permissionDenied).catch(() => {
throw new Error('Expected permissionDenied to succeed.');
});

await firebase
.assertFails(otherFailure)
.then(() => {
throw new Error('Expected otherFailure to fail.');
})
.catch(() => {});
});

it('initializeTestApp() with auth=null does not set access token', async function () {
const app = firebase.initializeTestApp({
projectId: 'foo',
Expand Down

0 comments on commit 3a19f9e

Please sign in to comment.