From 4e431828cb889c9d5afd52567f69cc66e82eef52 Mon Sep 17 00:00:00 2001 From: Danny Date: Fri, 12 Mar 2021 08:51:38 -0500 Subject: [PATCH] [ESD-7536] fix: export all permissions for a role (v4) (#133) * 4.4.3 --- package-lock.json | 2 +- package.json | 2 +- src/auth0/client.js | 24 ++++++++++++++---------- src/auth0/handlers/roles.js | 2 +- tests/auth0/client.tests.js | 28 ++++++++++++++++++++++++++++ tests/auth0/handlers/roles.tests.js | 29 +++++++++++++---------------- 6 files changed, 58 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0db7a91..05fc947 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "auth0-source-control-extension-tools", - "version": "4.4.2", + "version": "4.4.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 1abc560..31dfb1e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "auth0-source-control-extension-tools", - "version": "4.4.2", + "version": "4.4.3", "description": "Supporting tools for the Source Control extensions", "main": "lib/index.js", "scripts": { diff --git a/src/auth0/client.js b/src/auth0/client.js index e2305a0..32278c6 100644 --- a/src/auth0/client.js +++ b/src/auth0/client.js @@ -41,7 +41,11 @@ function pagedManager(client, manager) { delete newArgs[0].paginate; // Run the first request to get the total number of entity items - const rsp = await target[fnName](...newArgs); + const rsp = await client.pool.addSingleTask({ + data: _.cloneDeep(newArgs), + generator: pageArgs => target[fnName](...pageArgs) + }).promise(); + data.push(...getEntity(rsp)); const total = rsp.total || 0; const pagesLeft = Math.ceil(total / perPage) - 1; @@ -65,11 +69,18 @@ function pagedManager(client, manager) { } return data; } + return target[name](...args); }; } - return Reflect.get(target, name, receiver); + const nestedManager = Reflect.get(target, name, receiver); + + if (typeof nestedManager === 'object' && nestedManager !== null) { + return pagedManager(client, nestedManager); + } + + return nestedManager; } }); } @@ -82,12 +93,5 @@ export default function pagedClient(client) { frequencyWindow: 1000 // 1 sec }); - return new Proxy(client, { - get: function(target, name, receiver) { - if (name in target && target[name].getAll) { - return pagedManager(client, target[name]); - } - return Reflect.get(target, name, receiver); - } - }); + return pagedManager(client, client); } diff --git a/src/auth0/handlers/roles.js b/src/auth0/handlers/roles.js index d61023b..a3308d5 100644 --- a/src/auth0/handlers/roles.js +++ b/src/auth0/handlers/roles.js @@ -128,7 +128,7 @@ export default class RoleHandler extends DefaultHandler { try { const roles = await this.client.roles.getAll({ paginate: true }); for (let index = 0; index < roles.length; index++) { - const permissions = await this.client.roles.permissions.get({ id: roles[index].id }); + const permissions = await this.client.roles.permissions.getAll({ paginate: true, id: roles[index].id }); const strippedPerms = await Promise.all(permissions.map(async (permission) => { delete permission.resource_server_name; delete permission.description; diff --git a/tests/auth0/client.tests.js b/tests/auth0/client.tests.js index 283af71..3aefbd8 100644 --- a/tests/auth0/client.tests.js +++ b/tests/auth0/client.tests.js @@ -33,4 +33,32 @@ describe('#schema validation tests', async () => { expect(allClients.length).to.eq(expectedNbClients); }); + + it('should paginate a nested object with getAll', async () => { + const permissions = []; + const expectedNbItems = 150; + + for (let i = 0; i < expectedNbItems; i++) { + permissions.push({ + name: 'test-' + i + '-' + Math.round(Math.random() * 10000000000) + }); + } + + const mock = { + roles: { + permissions: { + getAll: async localArgs => Promise.resolve({ + start: localArgs.page * localArgs.per_page, + total: expectedNbItems, + permissions: permissions.slice(localArgs.page * localArgs.per_page, (localArgs.page + 1) * localArgs.per_page) + }) + } + } + }; + + const pagedManager = client(mock); + + const rolesPermissions = await pagedManager.roles.permissions.getAll({ paginate: true }); + expect(rolesPermissions.length).to.eq(expectedNbItems); + }); }); diff --git a/tests/auth0/handlers/roles.tests.js b/tests/auth0/handlers/roles.tests.js index 88c5f90..3b3b656 100644 --- a/tests/auth0/handlers/roles.tests.js +++ b/tests/auth0/handlers/roles.tests.js @@ -68,9 +68,9 @@ describe('#roles handler', () => { delete: () => Promise.resolve([]), getAll: () => Promise.resolve([]), permissions: { - get: () => [ + getAll: () => Promise.resolve([ { permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' } - ], + ]), create: (params, data) => { expect(params).to.be.an('object'); expect(params.id).to.equal('myRoleId'); @@ -101,6 +101,10 @@ describe('#roles handler', () => { }); it('should get roles', async () => { + const permissions = new Array(150).fill( + { permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' } + ); + const auth0 = { roles: { getAll: () => Promise.resolve([ @@ -111,9 +115,7 @@ describe('#roles handler', () => { } ]), permissions: { - get: () => [ - { permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' } - ] + getAll: () => Promise.resolve(permissions) } }, pool @@ -126,11 +128,9 @@ describe('#roles handler', () => { name: 'myRole', id: 'myRoleId', description: 'myDescription', - permissions: [ - { - permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' - } - ] + permissions: new Array(150).fill( + { permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' } + ) } ]); }); @@ -216,12 +216,9 @@ describe('#roles handler', () => { } ]), permissions: { - get: () => [ - { permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' } - ], - getAll: () => [ + getAll: () => Promise.resolve([ { permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' } - ], + ]), create: (params, data) => { expect(params).to.be.an('object'); expect(params.id).to.equal('myRoleId'); @@ -281,7 +278,7 @@ describe('#roles handler', () => { } ]), permissions: { - get: () => [] + getAll: () => Promise.resolve([]) } }, pool