Skip to content

Commit 4e14c21

Browse files
committed
Add support for keyword markers in database enabled_clients
- Add hasKeywordMarkers() utility to detect @@Keyword@@ and ##KEYWORD## patterns - Modify mapClientID2NameSorted() to preserve strings with keyword markers - Handle both string and array inputs for enabled_clients configuration - Add comprehensive test coverage for keyword marker detection and handling - Fix TypeError when processing databases with template variables
1 parent 9b57999 commit 4e14c21

3 files changed

Lines changed: 118 additions & 1 deletion

File tree

src/utils.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,27 @@ export function convertClientIdToName(clientId: string, knownClients: Asset[] =
201201
}
202202
}
203203

204-
export function mapClientID2NameSorted(enabledClients: string[], knownClients: Asset[]): string[] {
204+
export function hasKeywordMarkers(value: any): boolean {
205+
if (typeof value !== 'string') return false;
206+
return /@@[A-Z_]+@@/.test(value) || /##[A-Z_]+##/.test(value);
207+
}
208+
209+
export function mapClientID2NameSorted(enabledClients: string[] | string, knownClients: Asset[]): string[] | string {
210+
// If enabledClients is a string (likely contains keyword markers), return as-is
211+
if (typeof enabledClients === 'string') {
212+
return enabledClients;
213+
}
214+
215+
// If enabledClients is null or undefined, return empty array
216+
if (!enabledClients) {
217+
return [];
218+
}
219+
220+
// If any element in the array contains keyword markers, return the array as-is
221+
if (Array.isArray(enabledClients) && enabledClients.some(client => hasKeywordMarkers(client))) {
222+
return enabledClients;
223+
}
224+
205225
return [
206226
...(enabledClients || []).map((clientId) => convertClientIdToName(clientId, knownClients)),
207227
].sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));

test/context/directory/databases.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,30 @@ describe('#directory context databases', () => {
354354
strategy: 'auth0',
355355
});
356356
});
357+
358+
it('should dump databases with keyword replacement in enabled_clients', async () => {
359+
cleanThenMkdir(dbDumpDir);
360+
const context = new Context({ AUTH0_INPUT_FILE: dbDumpDir }, mockMgmtClient());
361+
362+
context.assets.databases = [
363+
{
364+
name: 'users',
365+
enabled_clients: '@@DATABASE_ENABLED_CLIENTS@@', // String with keyword marker
366+
options: {
367+
requires_username: true,
368+
},
369+
strategy: 'auth0',
370+
},
371+
];
372+
373+
// This should not throw a TypeError anymore
374+
await handler.dump(context);
375+
const scriptsFolder = path.join(dbDumpDir, constants.DATABASE_CONNECTIONS_DIRECTORY, 'users');
376+
const dumpedDB = loadJSON(path.join(scriptsFolder, 'database.json'));
377+
378+
// The keyword marker should be preserved as-is
379+
expect(dumpedDB.enabled_clients).to.equal('@@DATABASE_ENABLED_CLIENTS@@');
380+
expect(dumpedDB.name).to.equal('users');
381+
expect(dumpedDB.strategy).to.equal('auth0');
382+
});
357383
});

test/tools/utils.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from 'chai';
22
import { getEnabledClients } from '../../src/tools/utils';
3+
import { hasKeywordMarkers, mapClientID2NameSorted } from '../../src/utils';
34

45
describe('#getEnabledClients', () => {
56
const mockExclusions = {};
@@ -62,3 +63,73 @@ describe('#getEnabledClients', () => {
6263
expect(enabledClients).to.equal(undefined);
6364
});
6465
});
66+
67+
describe('#hasKeywordMarkers', () => {
68+
it('should detect array keyword markers', () => {
69+
const testString = '@@SOME_KEYWORD@@';
70+
const result = hasKeywordMarkers(testString);
71+
expect(result).to.be.true;
72+
});
73+
74+
it('should detect string keyword markers', () => {
75+
const testString = '##SOME_KEYWORD##';
76+
const result = hasKeywordMarkers(testString);
77+
expect(result).to.be.true;
78+
});
79+
80+
it('should return false for non-string values', () => {
81+
const testArray = ['client1', 'client2'];
82+
const result = hasKeywordMarkers(testArray);
83+
expect(result).to.be.false;
84+
});
85+
86+
it('should return false for strings without markers', () => {
87+
const testString = 'regular string';
88+
const result = hasKeywordMarkers(testString);
89+
expect(result).to.be.false;
90+
});
91+
92+
it('should return false for empty strings', () => {
93+
const testString = '';
94+
const result = hasKeywordMarkers(testString);
95+
expect(result).to.be.false;
96+
});
97+
});
98+
99+
describe('#mapClientID2NameSorted', () => {
100+
const mockClients = [
101+
{ name: 'Client B', client_id: 'client-b-id' },
102+
{ name: 'Client A', client_id: 'client-a-id' },
103+
{ name: 'Client C', client_id: 'client-c-id' },
104+
];
105+
106+
it('should handle array input normally', () => {
107+
const enabledClients = ['client-a-id', 'client-b-id'];
108+
const result = mapClientID2NameSorted(enabledClients, mockClients);
109+
expect(result).to.deep.equal(['Client A', 'Client B']);
110+
});
111+
112+
it('should return string unchanged when input is a string', () => {
113+
const enabledClients = '@@DATABASE_ENABLED_CLIENTS@@';
114+
const result = mapClientID2NameSorted(enabledClients, mockClients);
115+
expect(result).to.equal('@@DATABASE_ENABLED_CLIENTS@@');
116+
});
117+
118+
it('should handle empty array', () => {
119+
const enabledClients = [];
120+
const result = mapClientID2NameSorted(enabledClients, mockClients);
121+
expect(result).to.deep.equal([]);
122+
});
123+
124+
it('should handle null/undefined enabled clients', () => {
125+
const result = mapClientID2NameSorted(null, mockClients);
126+
expect(result).to.deep.equal([]);
127+
});
128+
129+
it('should preserve keyword markers in arrays', () => {
130+
const enabledClientsWithKeywords = ['@@CLIENT_KEYWORD@@', 'client-a-id'];
131+
const result = mapClientID2NameSorted(enabledClientsWithKeywords, mockClients);
132+
// When an array contains keyword markers, the entire array should be returned as-is
133+
expect(result).to.deep.equal(enabledClientsWithKeywords);
134+
});
135+
});

0 commit comments

Comments
 (0)