From 3a19f9e6987360dbded076937723acb3d1de82c9 Mon Sep 17 00:00:00 2001 From: Mikkel Vester Petersen Date: Fri, 20 Nov 2020 17:00:51 +0100 Subject: [PATCH] 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 --- .changeset/early-dots-peel.md | 5 ++++ packages/rules-unit-testing/src/api/index.ts | 9 +++++-- .../rules-unit-testing/test/database.test.ts | 25 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 .changeset/early-dots-peel.md diff --git a/.changeset/early-dots-peel.md b/.changeset/early-dots-peel.md new file mode 100644 index 00000000000..2b059fbabff --- /dev/null +++ b/.changeset/early-dots-peel.md @@ -0,0 +1,5 @@ +--- +"@firebase/rules-unit-testing": patch +--- + +Fix assertFails not recognising database permission denied error diff --git a/packages/rules-unit-testing/src/api/index.ts b/packages/rules-unit-testing/src/api/index.ts index 680b195b09f..28626127f52 100644 --- a/packages/rules-unit-testing/src/api/index.ts +++ b/packages/rules-unit-testing/src/api/index.ts @@ -454,9 +454,14 @@ export function assertFails(pr: Promise): 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( diff --git a/packages/rules-unit-testing/test/database.test.ts b/packages/rules-unit-testing/test/database.test.ts index d5929ee8666..95bb16a8db7 100644 --- a/packages/rules-unit-testing/test/database.test.ts +++ b/packages/rules-unit-testing/test/database.test.ts @@ -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',