Skip to content

Commit

Permalink
Adjust external api id middleware for admin routes
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Sherman <[email protected]>
  • Loading branch information
usingtechnology committed Aug 22, 2024
1 parent 0d49e8d commit 3b76182
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 10 additions & 3 deletions app/src/forms/common/middleware/validateParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,19 @@ const validateExternalAPIId = async (req, _res, next, externalAPIId) => {
_validateUuid(externalAPIId, 'externalAPIId');

const externalApi = await externalApiService.readExternalAPI(externalAPIId);
if (!externalApi || externalApi.formId !== req.params.formId) {
if (!externalApi) {
throw new Problem(404, {
detail: 'externalAPIId does not exist on this form',
detail: 'externalAPIId does not exist',
});
}

// perform this check only if there is a formId (admin routes don't have form id)
if (req.params.formId) {
if (!externalApi || externalApi.formId !== req.params.formId) {
throw new Problem(404, {
detail: 'externalAPIId does not exist on this form',
});
}
}
next();
} catch (error) {
next(error);
Expand Down
17 changes: 16 additions & 1 deletion app/tests/unit/forms/common/middleware/validateParameter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ describe('validateExternalApiId', () => {
describe('404 response when', () => {
const expectedStatus = { status: 404 };

test('formId is missing', async () => {
test('externalApiId not found', async () => {
externalApiService.readExternalAPI.mockReturnValueOnce(null);
const req = getMockReq({
params: {
externalAPIId: externalApiId,
Expand Down Expand Up @@ -358,6 +359,20 @@ describe('validateExternalApiId', () => {
expect(externalApiService.readExternalAPI).toBeCalledTimes(1);
expect(next).toBeCalledWith();
});

test('external api id only', async () => {
const req = getMockReq({
params: {
externalAPIId: externalApiId,
},
});
const { res, next } = getMockRes();

await validateParameter.validateExternalAPIId(req, res, next, externalApiId);

expect(externalApiService.readExternalAPI).toBeCalledTimes(1);
expect(next).toBeCalledWith();
});
});
});

Expand Down

0 comments on commit 3b76182

Please sign in to comment.