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 #112 from auth0-extensions/client-grants-fix
Browse files Browse the repository at this point in the history
Client grants fix
  • Loading branch information
faroceann authored Sep 23, 2020
2 parents ec75d80 + 959ae78 commit aee8639
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 5 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.7",
"version": "4.1.8",
"description": "Supporting tools for the Source Control extensions",
"main": "lib/index.js",
"scripts": {
Expand Down
12 changes: 11 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ export function convertClientNameToId(name, clients) {
}

export function convertClientNamesToIds(names, clients) {
return names.map(name => convertClientNameToId(name, clients));
const resolvedNames = names.map(name => ({ name, resolved: false }));
const result = clients.reduce((acc, client) => {
if (names.includes(client.name)) {
const index = resolvedNames.findIndex(item => item.name === client.name);
resolvedNames[index].resolved = true;
acc.push(client.client_id);
}
return acc;
}, []);
const unresolved = resolvedNames.filter(item => !item.resolved).map(item => item.name);
return [ ...unresolved, ...result ];
}

export function loadFile(file, mappings) {
Expand Down
109 changes: 109 additions & 0 deletions tests/auth0/handlers/clientGrants.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,113 @@ describe('#clientGrants handler', () => {
await stageFn.apply(handler, [ assets ]);
});
});
it('should not delete client grants of excluded clients with multiple instances', async () => {
config.data = {
AUTH0_CLIENT_ID: 'client_id',
AUTH0_ALLOW_DELETE: true
};

const auth0 = {
clientGrants: {
create: (params) => {
expect(params).to.be.an('undefined');

return Promise.resolve([]);
},
update: (params) => {
expect(params).to.be.an('undefined');

return Promise.resolve([]);
},
delete: (params) => {
expect(params).to.be.an('undefined');

return Promise.resolve([]);
},
getAll: () => [
{
client_id: '123',
audience: 'a',
id: '1'
},
{
client_id: '123',
audience: 'a',
id: '2'
},
{
client_id: '123',
audience: 'a',
id: '3'
},
{
client_id: '456',
audience: 'a',
id: '4'
},
{
client_id: '456',
audience: 'a',
id: '5'
}
]
},
clients: {
getAll: () => [
{
name: 'abc',
client_id: 'abc'
},
{
name: 'foo_client',
client_id: '123'
},
{
name: 'foo_client',
client_id: '456'
}
]
},
pool
};

const handler = new clientGrants.default({ client: auth0, config });
const stageFn = Object.getPrototypeOf(handler).processChanges;

const assets = {
clients: [
{
name: 'foo_client'
},
{
name: 'foo_client'
}
],
clientGrants: [
{
client_id: 'foo_client',
audience: 'https://example.com'
},
{
client_id: 'foo_client',
audience: 'https://example.com'
},
{
client_id: 'foo_client',
audience: 'https://example.com'
},
{
client_id: 'foo_client',
audience: 'https://example.com'
},
{
client_id: 'foo_client',
audience: 'https://example.com'
}
],
exclude: { clients: [ 'foo_client' ] }
};

await stageFn.apply(handler, [ assets ]);
});
});
4 changes: 2 additions & 2 deletions tests/utils.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ describe('#utils', function() {

const names = [ 'dd', 'cc', 'aa' ];

const expected = [ 'dd', '3', '1' ];
const expected = [ '1', '3', 'dd' ];

expect(utils.convertClientNamesToIds(names, clients)).to.deep.equal(expected);
expect(utils.convertClientNamesToIds(names, clients).sort()).to.deep.equal(expected);
});
});

Expand Down

0 comments on commit aee8639

Please sign in to comment.