-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parse additional namespaces from
d2.config.js
and add to `man…
…ifest.webapp` [LIBS-638] (#860) * feat: add `additionalNamespaces` config to manifest.webapp * docs: additional namespaces * docs: fix typo
- Loading branch information
1 parent
0d7d4de
commit 62782fe
Showing
5 changed files
with
213 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const { reporter, chalk } = require('@dhis2/cli-helpers-engine') | ||
|
||
const parseAdditionalNamespaces = (additionalNamespaces) => { | ||
if (!additionalNamespaces) { | ||
return undefined | ||
} | ||
if (!Array.isArray(additionalNamespaces)) { | ||
reporter.warn( | ||
`Invalid value ${chalk.bold( | ||
JSON.stringify(additionalNamespaces) | ||
)} specified for ${chalk.bold( | ||
'additionalNamespaces' | ||
)} -- must be an array of objects, skipping.` | ||
) | ||
return undefined | ||
} | ||
|
||
const filteredNamespaces = additionalNamespaces.filter( | ||
(additionalNamespace, index) => { | ||
const msg = `Invalid namespace ${chalk.bold( | ||
JSON.stringify(additionalNamespace) | ||
)} specified at ${chalk.bold(`index ${index}`)} of ${chalk.bold( | ||
'additionalNamespaces' | ||
)} -- see d2.config.js documentation for the correct form. Skipping.` | ||
const { | ||
namespace, | ||
authorities, | ||
readAuthorities, | ||
writeAuthorities, | ||
} = additionalNamespace | ||
|
||
const namespacePropIsString = typeof namespace === 'string' | ||
const definedAuthsProps = [ | ||
authorities, | ||
readAuthorities, | ||
writeAuthorities, | ||
].filter((auths) => auths !== undefined) | ||
const definedAuthsPropsAreValid = | ||
Array.isArray(definedAuthsProps) && | ||
definedAuthsProps.every( | ||
(auths) => | ||
Array.isArray(auths) && | ||
auths.every((auth) => typeof auth === 'string') | ||
) | ||
|
||
const additionalNamespaceIsValid = | ||
namespacePropIsString && | ||
definedAuthsProps.length > 0 && | ||
definedAuthsPropsAreValid | ||
if (!additionalNamespaceIsValid) { | ||
reporter.warn(msg) | ||
return false // skip this additional namespace | ||
} | ||
|
||
return true | ||
} | ||
) | ||
|
||
return filteredNamespaces | ||
} | ||
|
||
exports.parseAdditionalNamespaces = parseAdditionalNamespaces |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const { reporter } = require('@dhis2/cli-helpers-engine') | ||
const { parseAdditionalNamespaces } = require('./parseAdditionalNamespaces') | ||
|
||
jest.mock('@dhis2/cli-helpers-engine', () => ({ | ||
reporter: { warn: jest.fn() }, | ||
chalk: { bold: jest.fn().mockImplementation((input) => input) }, | ||
})) | ||
|
||
test('undefined', () => { | ||
const output = parseAdditionalNamespaces(undefined) | ||
expect(output).toBe(undefined) | ||
expect(reporter.warn).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
test('the happy path', () => { | ||
const additionalNamespaces = [ | ||
{ namespace: 'extra1', authorities: ['M_extra1'] }, | ||
{ namespace: 'extra2', readAuthorities: ['M_extra2read'] }, | ||
{ namespace: 'extra3', writeAuthorities: ['M_extra3write'] }, | ||
{ | ||
namespace: 'extra4', | ||
authorities: ['M_extra4readwrite'], | ||
writeAuthotities: ['M_extra4write'], | ||
}, | ||
] | ||
|
||
const output = parseAdditionalNamespaces(additionalNamespaces) | ||
expect(output).toEqual(additionalNamespaces) | ||
expect(reporter.warn).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
describe('handling faults', () => { | ||
test('additionalNamespaces is not an array', () => { | ||
const additionalNamespaces = { | ||
namespace: 'extra1', | ||
authorities: ['M_extra1'], | ||
} | ||
|
||
const output = parseAdditionalNamespaces(additionalNamespaces) | ||
|
||
expect(output).toBe(undefined) | ||
expect(reporter.warn).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('invalid namespace options get filtered out', () => { | ||
const testNamespaces = [ | ||
{ namespace: 'no-authorities' }, | ||
{ authorities: ['F_MISSING-NAMESPACE-STRING'] }, | ||
{ | ||
namespace: 'valid-namespace-1', | ||
readAuthorities: ['M_extra-read'], | ||
writeAuthorities: ['M_extra-write'], | ||
}, | ||
{ namespace: ['not-a-string'], readAuthorities: ['M_extra2read'] }, | ||
{ | ||
namespace: 'invalid-value-type', | ||
readAuthorities: 'should-be-an-array', | ||
}, | ||
{ | ||
namespace: 'one-correct-auths-prop-one-error', | ||
readAuthorities: ['M_extra-read'], | ||
writeAuthorities: 'should-be-an-array', | ||
}, | ||
{ | ||
namespace: 'valid-namespace-2', | ||
writeAuthorities: ['M_extra-write'], | ||
}, | ||
{ | ||
namespace: 'valid-namespace-3', | ||
authorities: ['M_extra-readwrite'], | ||
writeAuthotities: ['M_extra-write'], | ||
}, | ||
] | ||
|
||
const output = parseAdditionalNamespaces(testNamespaces) | ||
expect(output).toEqual([testNamespaces[2], ...testNamespaces.slice(-2)]) | ||
expect(reporter.warn).toHaveBeenCalledTimes(6) | ||
}) | ||
}) |
Oops, something went wrong.