feat(#528): validate branding and partners configuration before upload - #836
Open
AchrafReyani wants to merge 3 commits into
Open
feat(#528): validate branding and partners configuration before upload#836AchrafReyani wants to merge 3 commits into
AchrafReyani wants to merge 3 commits into
Conversation
…upload upload-branding and upload-partners accepted any JSON and uploaded it as the branding/partners doc, even though cht-core assumes a specific structure (a string title, a resources object mapping to attachment names). A malformed doc breaks the admin images pages without any indication of the cause. Validate branding.json and partners.json with joi before uploading and also check that every resources entry references a file that exists in the branding/ or partners/ directory. Invalid configuration now fails fast with a descriptive error and nothing is uploaded.
- pass processJson/validate to upload-configuration-docs as an options object instead of a fifth positional parameter - name the exported function and extract assertValidConfiguration and uploadDoc helpers to bring cognitive complexity under the threshold - use optional chaining in validate-configuration-docs
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
upload-brandingandupload-partnerscurrently accept whatever is inbranding.json/partners.jsonand upload it verbatim as thebranding/partnersdoc. cht-core assumes a specific structure for these docs (api/src/services/branding.js, the adminimages-branding/images-partnerscontrollers): a stringtitle, and aresourcesobject whose values are attachment names. A malformed doc breaks the admin images pages without any indication of the cause (see the issue).This PR validates both configuration files before anything is uploaded:
validate-app-settings.js):branding.json:titleis a required non-empty string;resources(optional) is an object whoselogo/favicon/iconvalues are non-empty strings — the three keys cht-core reads.partners.json:resourcesis a required object mapping partner name → non-empty string.resourcesvalue must reference a file that exists in thebranding/orpartners/directory, otherwise cht-core would look up an attachment that was never uploaded.On failure the action throws a descriptive error and nothing is written, e.g.
Implementation: new
src/lib/validate-configuration-docs.js;upload-configuration-docstakes an optionalvalidate(settings, attachments)callback (alongside the existing optionalprocessJson), and the two actions pass their validator. Other callers ofupload-configuration-docsare unaffected.#528
Code review items
test/lib/validate-configuration-docs.spec.jsplus new cases intest/lib/upload-configuration-docs.spec.js(validate accepted / rejected → throws, no upload); action specs assert the validator is wired in.Verification
mainwith the upload module stubbed:{ "title": 42, "resources": "logo.png" }is uploaded as-is. After this change the same input throws the first error above andinsertOrReplaceis never called; a valid config still uploads the same doc as before.npm run eslintclean.npm testin a cleannode:22container withFORCE_COLOR=1(same as CI): 899 passing, 1 pending (the pending one is pre-existing), 0 failing.Written with assistance from Claude Code; every change and test result above was run and checked locally.