Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch clients if not defined in client grants YAML handler #865

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/context/yaml/handlers/clientGrants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ async function parse(context: YAMLContext): Promise<ParsedClientGrants> {
}

async function dump(context: YAMLContext): Promise<ParsedClientGrants> {
const { clientGrants, clients } = context.assets;
let { clientGrants, clients } = context.assets;

if (!clientGrants) return { clientGrants: null };

if (clients === undefined) {
clients = await context.mgmtClient.clients.getAll({
paginate: true,
include_totals: true,
});
}

// Convert client_id to the client name for readability
return {
clientGrants: clientGrants.map((grant) => {
Expand Down
3 changes: 2 additions & 1 deletion src/context/yaml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
wrapArrayReplaceMarkersInQuotes,
Auth0,
} from '../../tools';
import pagedClient from '../../tools/auth0/client';

import log from '../../logger';
import { isFile, toConfigFn, stripIdentifiers, formatResults, recordsSorter } from '../../utils';
Expand All @@ -29,7 +30,7 @@ export default class YAMLContext {
this.configFile = config.AUTH0_INPUT_FILE;
this.config = config;
this.mappings = config.AUTH0_KEYWORD_REPLACE_MAPPINGS || {};
this.mgmtClient = mgmtClient;
this.mgmtClient = pagedClient(mgmtClient);
this.disableKeywordReplacement = false;

//@ts-ignore because the assets property gets filled out throughout
Expand Down
49 changes: 48 additions & 1 deletion test/context/yaml/clientGrants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,58 @@ describe('#YAML context client grants', () => {
it('should dump client grants', async () => {
const context = new Context({ AUTH0_INPUT_FILE: './test.yml' }, mockMgmtClient());
const clientGrants = [
{ audience: 'https://test.myapp.com/api/v1', client_id: 'My M2M', scope: ['update:account'] },
{
audience: 'https://test.myapp.com/api/v1',
client_id: 'client-id',
scope: ['update:account'],
},
];
context.assets.clientGrants = clientGrants;

const dumped = await handler.dump(context);
expect(dumped).to.deep.equal({ clientGrants });
});

it('should dump client grants and replace client ID with client name if clients in assets', async () => {
const context = new Context({ AUTH0_INPUT_FILE: './test.yml' }, mockMgmtClient());
const clientGrants = [
{
audience: 'https://test.myapp.com/api/v1',
client_id: 'client-id-1',
scope: ['update:account'],
},
];
context.assets.clientGrants = clientGrants;
context.assets.clients = [{ client_id: 'client-id-1', name: 'Client 1' }];

const dumped = await handler.dump(context);
const expected = (() => {
const ret = clientGrants;
ret[0].client_id = 'Client 1';
return { clientGrants: ret };
})();
expect(dumped).to.deep.equal(expected);
});

it('should dump client grants and replace client ID with client name even if clients not in assets', async () => {
const mockMgmt = mockMgmtClient();
mockMgmt.clients.getAll = () => [[{ client_id: 'client-id-1', name: 'Client 1' }]];
const context = new Context({ AUTH0_INPUT_FILE: './test.yml' }, mockMgmt);
const clientGrants = [
{
audience: 'https://test.myapp.com/api/v1',
client_id: 'client-id-1',
scope: ['update:account'],
},
];
context.assets.clientGrants = clientGrants;

const dumped = await handler.dump(context);
const expected = (() => {
const ret = clientGrants;
ret[0].client_id = 'Client 1';
return { clientGrants: ret };
})();
expect(dumped).to.deep.equal(expected);
});
});