From 97bdbb594299db5c4014593da26d2d2cd8aed6c9 Mon Sep 17 00:00:00 2001 From: Patrick Dillon Date: Wed, 5 Feb 2020 13:24:10 -0500 Subject: [PATCH] Continue processing if hooks or roles are not enabled (#80) --- package.json | 2 +- src/auth0/handlers/hooks.js | 21 ++++++--- src/auth0/handlers/roles.js | 7 +-- tests/auth0/handlers/hooks.tests.js | 72 +++++++++++++++++++++++++++++ tests/auth0/handlers/roles.tests.js | 55 ++++++++++++++++++++++ 5 files changed, 146 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 056f1b3..5944071 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "auth0-source-control-extension-tools", - "version": "4.0.0", + "version": "4.0.1", "description": "Supporting tools for the Source Control extensions", "main": "lib/index.js", "scripts": { diff --git a/src/auth0/handlers/hooks.js b/src/auth0/handlers/hooks.js index 44712f4..a6bc3bc 100644 --- a/src/auth0/handlers/hooks.js +++ b/src/auth0/handlers/hooks.js @@ -141,17 +141,24 @@ export default class HooksHandler extends DefaultHandler { // in case client version does not support hooks if (!this.client.hooks || typeof this.client.hooks.getAll !== 'function') { - return null; + return []; } - const hooks = await this.client.hooks.getAll(); + try { + const hooks = await this.client.hooks.getAll(); - // hooks.getAll does not return code and secrets, we have to fetch hooks one-by-one - this.existing = await Promise.all(hooks.map(hook => this.client.hooks.get({ id: hook.id }) - .then(hookWithCode => this.client.hooks.getSecrets({ id: hook.id }) - .then(secrets => ({ ...hookWithCode, secrets }))))); + // hooks.getAll does not return code and secrets, we have to fetch hooks one-by-one + this.existing = await Promise.all(hooks.map(hook => this.client.hooks.get({ id: hook.id }) + .then(hookWithCode => this.client.hooks.getSecrets({ id: hook.id }) + .then(secrets => ({ ...hookWithCode, secrets }))))); - return this.existing; + return this.existing; + } catch (err) { + if (err.statusCode === 404 || err.statusCode === 403 || err.statusCode === 501) { + return []; + } + throw err; + } } async calcChanges(assets) { diff --git a/src/auth0/handlers/roles.js b/src/auth0/handlers/roles.js index 2a006fb..10b4e78 100644 --- a/src/auth0/handlers/roles.js +++ b/src/auth0/handlers/roles.js @@ -83,7 +83,7 @@ export default class RoleHandler extends DefaultHandler { // in case client version does not support roles if (!this.client.roles || typeof this.client.roles.getAll !== 'function') { - return {}; + return []; } try { @@ -100,8 +100,9 @@ export default class RoleHandler extends DefaultHandler { this.existing = roles; return this.existing; } catch (err) { - if (err.statusCode === 404) return {}; - if (err.statusCode === 501) return {}; + if (err.statusCode === 404 || err.statusCode === 501) { + return []; + } throw err; } } diff --git a/tests/auth0/handlers/hooks.tests.js b/tests/auth0/handlers/hooks.tests.js index 2c6ef71..eb1c7ab 100644 --- a/tests/auth0/handlers/hooks.tests.js +++ b/tests/auth0/handlers/hooks.tests.js @@ -208,6 +208,78 @@ describe('#hooks handler', () => { expect(data).to.deep.equal(hooksData.map(hook => ({ ...hook, code, secrets: { SECRET: `hook-${hook.id}-secret` } }))); }); + it('should return an empty array for 501 status code', async () => { + const auth0 = { + hooks: { + getAll: () => { + const error = new Error('Feature is not yet implemented'); + error.statusCode = 501; + throw error; + } + }, + pool + }; + + const handler = new hooks.default({ client: auth0, config }); + const data = await handler.getType(); + expect(data).to.deep.equal([]); + }); + + it('should return an empty array for 404 status code', async () => { + const auth0 = { + hooks: { + getAll: () => { + const error = new Error('Not found'); + error.statusCode = 404; + throw error; + } + }, + pool + }; + + const handler = new hooks.default({ client: auth0, config }); + const data = await handler.getType(); + expect(data).to.deep.equal([]); + }); + + it('should return an empty array for 403 status code', async () => { + const auth0 = { + hooks: { + getAll: () => { + const error = new Error('This endpoint is disabled for your tenant.'); + error.statusCode = 403; + throw error; + } + }, + pool + }; + + const handler = new hooks.default({ client: auth0, config }); + const data = await handler.getType(); + expect(data).to.deep.equal([]); + }); + + it('should throw an error for all other failed requests', async () => { + const auth0 = { + hooks: { + getAll: () => { + const error = new Error('Bad request'); + error.statusCode = 500; + throw error; + } + }, + pool + }; + + const handler = new hooks.default({ client: auth0, config }); + try { + await handler.getType(); + } catch (error) { + expect(error).to.be.an.instanceOf(Error); + } + }); + + it('should update hook', async () => { const auth0 = { hooks: { diff --git a/tests/auth0/handlers/roles.tests.js b/tests/auth0/handlers/roles.tests.js index 43d44f6..5d693a3 100644 --- a/tests/auth0/handlers/roles.tests.js +++ b/tests/auth0/handlers/roles.tests.js @@ -131,6 +131,61 @@ describe('#roles handler', () => { ]); }); + it('should return an empty array for 501 status code', async () => { + const auth0 = { + roles: { + getAll: () => { + const error = new Error('Feature is not yet implemented'); + error.statusCode = 501; + throw error; + } + }, + pool + }; + + const handler = new roles.default({ client: auth0, config }); + const data = await handler.getType(); + expect(data).to.deep.equal([]); + }); + + it('should return an empty array for 404 status code', async () => { + const auth0 = { + roles: { + getAll: () => { + const error = new Error('Not found'); + error.statusCode = 404; + throw error; + } + }, + pool + }; + + const handler = new roles.default({ client: auth0, config }); + const data = await handler.getType(); + expect(data).to.deep.equal([]); + }); + + + it('should throw an error for all other failed requests', async () => { + const auth0 = { + roles: { + getAll: () => { + const error = new Error('Bad request'); + error.statusCode = 500; + throw error; + } + }, + pool + }; + + const handler = new roles.default({ client: auth0, config }); + try { + await handler.getType(); + } catch (error) { + expect(error).to.be.an.instanceOf(Error); + } + }); + it('should update role', async () => { const auth0 = { roles: {