From 15f828152424b1c590f7542d56476d3a0a44449b Mon Sep 17 00:00:00 2001 From: Kevin Date: Sat, 21 Dec 2024 14:01:54 +0800 Subject: [PATCH] fix: add tests for replacing dots with underscores --- .../auth/decrypt-form-response.test.ts | 92 +++++++++++++++++++ .../__tests__/compute-parameters.test.ts | 30 ++++++ 2 files changed, 122 insertions(+) diff --git a/packages/backend/src/apps/formsg/__tests__/auth/decrypt-form-response.test.ts b/packages/backend/src/apps/formsg/__tests__/auth/decrypt-form-response.test.ts index a4556dd6c..f7af4be4d 100644 --- a/packages/backend/src/apps/formsg/__tests__/auth/decrypt-form-response.test.ts +++ b/packages/backend/src/apps/formsg/__tests__/auth/decrypt-form-response.test.ts @@ -410,6 +410,98 @@ describe('decrypt form response', () => { }), ) }) + + it('should parse form fields and replace dots with underscores in keys', async () => { + mockDecryptedSubmission({ + responses: [ + { + _id: 'question1.field.answer', + fieldType: 'textarea', + question: 'What do you eat for breakfast?', + answer: 'i eat lorem dimsum for breakfast', + }, + { + _id: 'question2.field.answer', + fieldType: 'mobile', + question: 'What is your mobile number?', + answer: '+6591234567', + }, + ], + }) + await expect(decryptFormResponse($)).resolves.toEqual(true) + expect($.request.body).toEqual( + expect.objectContaining({ + fields: { + question1_field_answer: { + fieldType: 'textarea', + question: 'What do you eat for breakfast?', + answer: 'i eat lorem dimsum for breakfast', + order: 1, + }, + question2_field_answer: { + fieldType: 'mobile', + question: 'What is your mobile number?', + answer: '+6591234567', + order: 2, + }, + }, + }), + ) + expect($.request.headers).toBeUndefined() + expect($.request.query).toBeUndefined() + }) + + it('should parse form fields and replace dots with underscores in keys', async () => { + mockDecryptedSubmission({ + responses: [ + { + _id: 'childrenbirthrecords.abc.childdateofbirth.0', + fieldType: 'children', + question: 'Child Date of birth', + answer: '31/03/2017', + }, + { + _id: 'childrenbirthrecords.abc.childname.0', + fieldType: 'children', + question: 'Child Name', + answer: 'John Doe', + }, + { + _id: 'question2.field.answer', + fieldType: 'mobile', + question: 'What is your mobile number?', + answer: '+6591234567', + }, + ], + }) + await expect(decryptFormResponse($)).resolves.toEqual(true) + expect($.request.body).toEqual( + expect.objectContaining({ + fields: { + childrenbirthrecords_abc_childdateofbirth_0: { + fieldType: 'children', + question: 'Child Date of birth', + answer: '31/03/2017', + order: 1, + }, + childrenbirthrecords_abc_childname_0: { + fieldType: 'children', + question: 'Child Name', + answer: 'John Doe', + order: 2, + }, + question2_field_answer: { + fieldType: 'mobile', + question: 'What is your mobile number?', + answer: '+6591234567', + order: 3, + }, + }, + }), + ) + expect($.request.headers).toBeUndefined() + expect($.request.query).toBeUndefined() + }) }) describe('attachments', () => { diff --git a/packages/backend/src/helpers/__tests__/compute-parameters.test.ts b/packages/backend/src/helpers/__tests__/compute-parameters.test.ts index 0a23df935..0caaf1101 100644 --- a/packages/backend/src/helpers/__tests__/compute-parameters.test.ts +++ b/packages/backend/src/helpers/__tests__/compute-parameters.test.ts @@ -319,4 +319,34 @@ describe('compute parameters', () => { const result = computeParameters(params, executionStep) expect(result).toEqual(expected) }) + + it('should process parameters with underscores in keys', () => { + const executionStep = [ + { + stepId: randomStepID, + dataOut: { + childrenbirthrecords_abc_childdateofbirth_0: { + fieldType: 'children', + question: 'Child Date of birth', + answer: '31/03/2017', + order: 1, + }, + childrenbirthrecords_abc_childname_0: { + fieldType: 'children', + question: 'Child Name', + answer: 'John Doe', + order: 2, + }, + }, + } as unknown as ExecutionStep, + ] + const params = { + toSubstitute: `{{step.${randomStepID}.childrenbirthrecords_abc_childdateofbirth_0.answer}} {{step.${randomStepID}.childrenbirthrecords_abc_childname_0.answer}}`, + } + const expected = { + toSubstitute: `31/03/2017 John Doe`, + } + const result = computeParameters(params, executionStep) + expect(result).toEqual(expected) + }) })