From 3d626a2b9fe03825772a649d2adbe141cfd35012 Mon Sep 17 00:00:00 2001 From: Danny Date: Wed, 10 Mar 2021 15:42:14 -0500 Subject: [PATCH] fix: pagination for roles (#131) * fix: tear out custom getAllRoles in favor of new global pagination in client * 5.1.2 Co-authored-by: Maxwell Hammad --- package-lock.json | 2 +- package.json | 2 +- src/auth0/handlers/roles.js | 18 +------ tests/auth0/handlers/roles.tests.js | 79 ++++++++--------------------- tests/paged_roles_data.js | 20 -------- 5 files changed, 25 insertions(+), 96 deletions(-) delete mode 100644 tests/paged_roles_data.js diff --git a/package-lock.json b/package-lock.json index 3cb8722..32768c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "auth0-source-control-extension-tools", - "version": "5.1.1", + "version": "5.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 778fcc3..886a44a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "auth0-source-control-extension-tools", - "version": "5.1.1", + "version": "5.1.2", "description": "Supporting tools for the Source Control extensions", "main": "lib/index.js", "scripts": { diff --git a/src/auth0/handlers/roles.js b/src/auth0/handlers/roles.js index 3ec8057..d61023b 100644 --- a/src/auth0/handlers/roles.js +++ b/src/auth0/handlers/roles.js @@ -126,7 +126,7 @@ export default class RoleHandler extends DefaultHandler { } try { - const roles = await this.getAllRoles(); + 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 strippedPerms = await Promise.all(permissions.map(async (permission) => { @@ -146,22 +146,6 @@ export default class RoleHandler extends DefaultHandler { } } - async getAllRoles() { - let result = []; - let initialPage = 0; - let data = await this.client.roles.getAll({ include_totals: true }); // getAll() returns 50 roles as default - if (data && data.total !== 0) { - let pagesLeft = Math.ceil(data.total / data.limit) - 1; - result = data.roles; - while (pagesLeft > 0) { - initialPage += 1; - data = await this.client.roles.getAll({ page: initialPage, include_totals: true }); - result.push(...data.roles); - pagesLeft -= 1; - } - } - return result; - } @order('60') async processChanges(assets) { diff --git a/tests/auth0/handlers/roles.tests.js b/tests/auth0/handlers/roles.tests.js index 4236e7a..88c5f90 100644 --- a/tests/auth0/handlers/roles.tests.js +++ b/tests/auth0/handlers/roles.tests.js @@ -1,6 +1,5 @@ const { expect } = require('chai'); const roles = require('../../../src/auth0/handlers/roles'); -const pagedRoles = require('../../paged_roles_data'); const pool = { addEachTask: (data) => { @@ -67,7 +66,7 @@ describe('#roles handler', () => { }, update: () => Promise.resolve([]), delete: () => Promise.resolve([]), - getAll: () => Promise.resolve({ roles: [], total: 0, limit: 50 }), + getAll: () => Promise.resolve([]), permissions: { get: () => [ { permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' } @@ -104,17 +103,13 @@ describe('#roles handler', () => { it('should get roles', async () => { const auth0 = { roles: { - getAll: () => Promise.resolve({ - roles: [ - { - name: 'myRole', - id: 'myRoleId', - description: 'myDescription' - } - ], - total: 1, - limit: 50 - }), + getAll: () => Promise.resolve([ + { + name: 'myRole', + id: 'myRoleId', + description: 'myDescription' + } + ]), permissions: { get: () => [ { permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' } @@ -140,28 +135,6 @@ describe('#roles handler', () => { ]); }); - it('should get all roles', async () => { - const auth0 = { - roles: { - getAll: data => Promise.resolve({ - roles: data.page ? pagedRoles.roles_page_2 : pagedRoles.roles_page_1, - total: 80, - limit: 50 - }), - permissions: { - get: () => [ - { permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' } - ] - } - }, - pool - }; - - const handler = new roles.default({ client: auth0, config }); - const data = await handler.getType(); - expect(data).to.have.length(80); - }); - it('should return an empty array for 501 status code', async () => { const auth0 = { roles: { @@ -235,17 +208,13 @@ describe('#roles handler', () => { return Promise.resolve(data); }, delete: () => Promise.resolve([]), - getAll: () => Promise.resolve({ - roles: [ - { - name: 'myRole', - id: 'myRoleId', - description: 'myDescription' - } - ], - total: 1, - limit: 50 - }), + getAll: () => Promise.resolve([ + { + name: 'myRole', + id: 'myRoleId', + description: 'myDescription' + } + ]), permissions: { get: () => [ { permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' } @@ -304,17 +273,13 @@ describe('#roles handler', () => { expect(data.id).to.equal('myRoleId'); return Promise.resolve(data); }, - getAll: () => Promise.resolve({ - roles: [ - { - name: 'myRole', - id: 'myRoleId', - description: 'myDescription' - } - ], - total: 1, - limit: 50 - }), + getAll: () => Promise.resolve([ + { + name: 'myRole', + id: 'myRoleId', + description: 'myDescription' + } + ]), permissions: { get: () => [] } diff --git a/tests/paged_roles_data.js b/tests/paged_roles_data.js deleted file mode 100644 index 8a2403f..0000000 --- a/tests/paged_roles_data.js +++ /dev/null @@ -1,20 +0,0 @@ - -let page1 = []; // eslint-disable-line -let page2 = []; // eslint-disable-line -for (let i = 1; i <= 80; i++) { - const role = { - name: 'myRole-' + i, - id: 'myRoleId-' + i, - description: 'myDescription-' + i - }; - if (i <= 50) { - page1.push(role); - } else { - page2.push(role); - } -} - -module.exports = { - roles_page_1: page1, - roles_page_2: page2 -};