Skip to content

Fix assertFails not recognising database permission denied error #4093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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