-
Notifications
You must be signed in to change notification settings - Fork 18
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
feat: parse additional namespaces from d2.config.js
and add to manifest.webapp
[LIBS-638]
#860
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes the Application tab crash in Chrome dev tools -- slack thread