-
Notifications
You must be signed in to change notification settings - Fork 777
[Bug]: return 400 for invalid JSON in profile upload #4362
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
Open
SueJianjian
wants to merge
6
commits into
anurag3407:main
Choose a base branch
from
SueJianjian:codex/fix-invalid-json-400-input-upload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−3
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
566898b
Remove duplicate Clock import from Fake WebOS template
SueJianjian 3dcc1fd
Fix duplicate portfolio update response
SueJianjian 4fca3dd
Fix security scan workflow on fork branches
SueJianjian dfc4d44
fix: return 400 for invalid input upload JSON
SueJianjian a188172
fix: tighten invalid JSON profile upload handling
SueJianjian ce6c34d
docs: add controller docstrings for input upload
SueJianjian 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 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { describe, test, mock } from "node:test"; | ||
|
|
||
| import { inputupload, parseInputDataPayload } from "./input.controller.js"; | ||
| import userModel from "../models/User.model.js"; | ||
|
|
||
| describe("parseInputDataPayload", () => { | ||
| test("returns parsed object for valid JSON strings", () => { | ||
| assert.deepEqual(parseInputDataPayload('{"jobRole":"Engineer"}'), { | ||
| jobRole: "Engineer", | ||
| }); | ||
| }); | ||
|
|
||
| test("returns empty object for missing payloads", () => { | ||
| assert.deepEqual(parseInputDataPayload(undefined), {}); | ||
| assert.deepEqual(parseInputDataPayload(null), {}); | ||
| }); | ||
|
|
||
| test("returns null for malformed JSON strings", () => { | ||
| assert.equal(parseInputDataPayload("{invalid json"), null); | ||
| }); | ||
|
|
||
| test("returns null for empty strings", () => { | ||
| assert.equal(parseInputDataPayload(""), null); | ||
| assert.equal(parseInputDataPayload(" "), null); | ||
| }); | ||
|
|
||
| test("passes through object payloads", () => { | ||
| const payload = { experience: "Mid Level" }; | ||
| assert.equal(parseInputDataPayload(payload), payload); | ||
| }); | ||
|
|
||
| test("returns null for arrays and primitive JSON values", () => { | ||
| assert.equal(parseInputDataPayload([]), null); | ||
| assert.equal(parseInputDataPayload('["React"]'), null); | ||
| assert.equal(parseInputDataPayload('"Engineer"'), null); | ||
| assert.equal(parseInputDataPayload("42"), null); | ||
| assert.equal(parseInputDataPayload("true"), null); | ||
| }); | ||
| }); | ||
|
|
||
| describe("inputupload", () => { | ||
| test("returns 400 for invalid JSON payloads before touching persistence", async () => { | ||
| const findByIdMock = mock.method(userModel, "findById", async () => { | ||
| throw new Error("findById should not be reached"); | ||
| }); | ||
|
|
||
| const req = { | ||
| body: { | ||
| data: "{invalid json", | ||
| }, | ||
| user: { | ||
| id: "user-id", | ||
| }, | ||
| file: null, | ||
| }; | ||
|
|
||
| let statusCode; | ||
| let body; | ||
| const res = { | ||
| status(code) { | ||
| statusCode = code; | ||
| return this; | ||
| }, | ||
| json(payload) { | ||
| body = payload; | ||
| return this; | ||
| }, | ||
| }; | ||
|
|
||
| await inputupload(req, res); | ||
|
|
||
| assert.equal(statusCode, 400); | ||
| assert.deepEqual(body, { | ||
| message: "Invalid JSON format in request body", | ||
| }); | ||
| assert.equal(findByIdMock.mock.callCount(), 0); | ||
|
|
||
| findByIdMock.mock.restore(); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.