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

Commit

Permalink
[ESD-7536] fix: export all permissions for a role (v4) (#133)
Browse files Browse the repository at this point in the history
* 4.4.3
  • Loading branch information
turcottedanny authored Mar 12, 2021
1 parent f4a3500 commit 4e43182
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 29 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.4.2",
"version": "4.4.3",
"description": "Supporting tools for the Source Control extensions",
"main": "lib/index.js",
"scripts": {
Expand Down
24 changes: 14 additions & 10 deletions src/auth0/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
});
}
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion src/auth0/handlers/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions tests/auth0/client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
29 changes: 13 additions & 16 deletions tests/auth0/handlers/roles.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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([
Expand All @@ -111,9 +115,7 @@ describe('#roles handler', () => {
}
]),
permissions: {
get: () => [
{ permission_name: 'Create:cal_entry', resource_server_identifier: 'organise' }
]
getAll: () => Promise.resolve(permissions)
}
},
pool
Expand All @@ -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' }
)
}
]);
});
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -281,7 +278,7 @@ describe('#roles handler', () => {
}
]),
permissions: {
get: () => []
getAll: () => Promise.resolve([])
}
},
pool
Expand Down

0 comments on commit 4e43182

Please sign in to comment.