Skip to content

Commit

Permalink
Merge pull request #591 from auth0/validate-for-unreplaced-keywords
Browse files Browse the repository at this point in the history
Check for unreplaced keywords before import
  • Loading branch information
willvedd authored Jun 27, 2022
2 parents d3fd080 + 7992add commit ac001f8
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/commands/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { deploy as toolsDeploy } from '../tools';
import log from '../logger';
import { setupContext } from '../context';
import { ImportParams } from '../args';
import { Assets, Config } from '../types';

export default async function importCMD(params: ImportParams) {
const {
Expand Down Expand Up @@ -48,8 +49,47 @@ export default async function importCMD(params: ImportParams) {
const config = configFactory();
config.setProvider((key) => nconf.get(key));

//@ts-ignore because context and assets still need to be typed TODO: type assets and type context
findUnreplacedKeywords(context.assets);

await toolsDeploy(context.assets, context.mgmtClient, config);

log.info('Import Successful');
}

export const findUnreplacedKeywords = (assets: Assets) => {
const recursiveFindUnreplacedKeywords = (target): string[] => {
let unreplaced: string[] = [];
if (target === undefined || target === null) return [];
if (Array.isArray(target)) {
target.forEach((child) => {
unreplaced.push(...recursiveFindUnreplacedKeywords(child));
});
} else if (typeof target === 'object') {
Object.values(target).forEach((child) => {
unreplaced.push(...recursiveFindUnreplacedKeywords(child));
});
}

if (typeof target === 'string') {
const arrayMatches = target.match(/(?<=@@).*(?=@@)/g);
if (arrayMatches !== null) {
return arrayMatches;
}
const keywordMatches = target.match(/(?<=##).*(?=##)/g);
if (keywordMatches !== null) {
return keywordMatches;
}
}

return unreplaced;
};

const unreplacedKeywords = recursiveFindUnreplacedKeywords(assets);

if (unreplacedKeywords.length > 0) {
throw `Unreplaced keywords found: ${unreplacedKeywords.join(
', '
)}. Either correct these values or add to AUTH0_KEYWORD_REPLACE_MAPPINGS configuration.`;
}
return;
};
69 changes: 69 additions & 0 deletions test/commands/import.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { expect } from 'chai';
import { findUnreplacedKeywords } from '../../src/commands/import';

describe('#findUnreplacedKeywords function', () => {
it('should not throw if no unreplaced keywords', () => {
const fn = () =>
findUnreplacedKeywords({
actions: [
{
foo: 'foo',
bar: 'bar',
},
{
foo: ['foo1', 'foo2'],
bar: 'bar',
},
{
foo: 'foo',
bar: 'bar',
},
],
tenant: {
foo: 'foo',
bar: {
some: {
nested: { property: 'bar baz' },
},
},
},
//@ts-ignore because we're detecting this
databases: ' database value ', //
});

expect(fn).to.not.throw();
});
it('should throw if unreplaced keywords detected', () => {
const fn = () =>
findUnreplacedKeywords({
actions: [
{
foo: 'foo',
bar: 'bar',
},
{
foo: ['##KEYWORD1##', '##KEYWORD2##'],
bar: 'bar',
},
{
foo: 'foo',
bar: 'bar',
},
],
tenant: {
foo: 'foo',
bar: {
some: {
nested: { property: 'bar ##KEYWORD3##' },
},
},
},
//@ts-ignore because we're detecting this
databases: ' @@KEYWORD4@@ ', //
});

expect(fn).to.throw(
'Unreplaced keywords found: KEYWORD1, KEYWORD2, KEYWORD3, KEYWORD4. Either correct these values or add to AUTH0_KEYWORD_REPLACE_MAPPINGS configuration.'
);
});
});

0 comments on commit ac001f8

Please sign in to comment.