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

Commit

Permalink
fix: pagination for roles (#131)
Browse files Browse the repository at this point in the history
* fix: tear out custom getAllRoles in favor of new global pagination in client

* 5.1.2

Co-authored-by: Maxwell Hammad <[email protected]>
  • Loading branch information
turcottedanny and faroceann authored Mar 10, 2021
1 parent 9819ae4 commit 3d626a2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 96 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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": "5.1.1",
"version": "5.1.2",
"description": "Supporting tools for the Source Control extensions",
"main": "lib/index.js",
"scripts": {
Expand Down
18 changes: 1 addition & 17 deletions src/auth0/handlers/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) {
Expand Down
79 changes: 22 additions & 57 deletions tests/auth0/handlers/roles.tests.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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' }
Expand Down Expand Up @@ -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' }
Expand All @@ -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: {
Expand Down Expand Up @@ -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' }
Expand Down Expand Up @@ -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: () => []
}
Expand Down
20 changes: 0 additions & 20 deletions tests/paged_roles_data.js

This file was deleted.

0 comments on commit 3d626a2

Please sign in to comment.