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

Commit

Permalink
Merge pull request #103 from luisbritos/ESD-7536_getAllRoles
Browse files Browse the repository at this point in the history
[ESD-7536] fix exporting roles
  • Loading branch information
luisbritos authored Jul 21, 2020
2 parents a8d5697 + 3836de7 commit b954da7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 26 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": "4.1.3",
"version": "4.1.4",
"description": "Supporting tools for the Source Control extensions",
"main": "lib/index.js",
"scripts": {
Expand Down
19 changes: 18 additions & 1 deletion src/auth0/handlers/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class RoleHandler extends DefaultHandler {
}

try {
const roles = await this.client.roles.getAll();
const roles = await this.getAllRoles();
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 @@ -107,6 +107,23 @@ 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) {
const { roles } = assets;
Expand Down
80 changes: 57 additions & 23 deletions tests/auth0/handlers/roles.tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 @@ -66,7 +67,7 @@ describe('#roles handler', () => {
},
update: () => Promise.resolve([]),
delete: () => Promise.resolve([]),
getAll: () => [],
getAll: () => Promise.resolve({ roles: [], total: 0, limit: 50 }),
permissions: {
get: () => [
{ permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' }
Expand Down Expand Up @@ -103,9 +104,17 @@ describe('#roles handler', () => {
it('should get roles', async () => {
const auth0 = {
roles: {
getAll: () => [
{ name: 'myRole', id: 'myRoleId', description: 'myDescription' }
],
getAll: () => Promise.resolve({
roles: [
{
name: 'myRole',
id: 'myRoleId',
description: 'myDescription'
}
],
total: 1,
limit: 50
}),
permissions: {
get: () => [
{ permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' }
Expand All @@ -131,6 +140,28 @@ 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 @@ -204,13 +235,17 @@ describe('#roles handler', () => {
return Promise.resolve(data);
},
delete: () => Promise.resolve([]),
getAll: () => [
{
name: 'myRole',
id: 'myRoleId',
description: 'myDescription'
}
],
getAll: () => Promise.resolve({
roles: [
{
name: 'myRole',
id: 'myRoleId',
description: 'myDescription'
}
],
total: 1,
limit: 50
}),
permissions: {
get: () => [
{ permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' }
Expand Down Expand Up @@ -269,18 +304,17 @@ describe('#roles handler', () => {
expect(data.id).to.equal('myRoleId');
return Promise.resolve(data);
},
getAll: () => [
{
name: 'myRole',
id: 'myRoleId',
description: 'myDescription',
permissions: [
{
permission_name: 'Create:cal_entry', resource_server_identifier: 'organise'
}
]
}
],
getAll: () => Promise.resolve({
roles: [
{
name: 'myRole',
id: 'myRoleId',
description: 'myDescription'
}
],
total: 1,
limit: 50
}),
permissions: {
get: () => []
}
Expand Down
20 changes: 20 additions & 0 deletions tests/paged_roles_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

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
};

0 comments on commit b954da7

Please sign in to comment.