Skip to content
This repository has been archived by the owner on Jul 31, 2021. It is now read-only.

Commit

Permalink
Continue processing if hooks or roles are not enabled (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdillon authored Feb 5, 2020
1 parent 764ccd1 commit 97bdbb5
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
21 changes: 14 additions & 7 deletions src/auth0/handlers/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions src/auth0/handlers/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
}
Expand Down
72 changes: 72 additions & 0 deletions tests/auth0/handlers/hooks.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
55 changes: 55 additions & 0 deletions tests/auth0/handlers/roles.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 97bdbb5

Please sign in to comment.