Skip to content

Commit 70f69cb

Browse files
committed
feat: add tc for workflow outcome pdf generation
1 parent c1c17a8 commit 70f69cb

File tree

1 file changed

+121
-1
lines changed

1 file changed

+121
-1
lines changed

src/app/modules/submission/multirespondent-submission/__tests__/multirespondent-submission.service.spec.ts

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ jest.mock('src/app/modules/datadog/datadog.utils')
3434
jest.mock('src/app/services/mail/mail.utils')
3535

3636
const MockMailUtils = jest.mocked(MailUtils)
37+
const MOCK_PDF_ATTACHMENT_BUFFER = Buffer.from('mock pdf buffer')
38+
const EXPECTED_MOCK_PDF_ATTACHMENT = {
39+
filename: 'response.pdf',
40+
content: MOCK_PDF_ATTACHMENT_BUFFER,
41+
}
42+
const MOCK_SUBMISSION_ATTACHMENTS = [
43+
{
44+
filename: 'attachment_1.pdf',
45+
content: Buffer.from('mock pdf buffer'),
46+
fieldId: new ObjectId().toHexString(),
47+
},
48+
]
3749
MockMailUtils.generateAutoreplyPdf.mockReturnValue(
3850
okAsync(Buffer.from('mock pdf buffer')),
3951
)
@@ -55,6 +67,34 @@ describe('multirespondent-submission.service', () => {
5567
const mockFormId = new ObjectId().toHexString()
5668
const mockSubmissionId = new ObjectId().toHexString()
5769

70+
describe('pdf attachment', () => {
71+
describe('pdf attachment is not generated when not needed', () => {
72+
describe('first step', () => {
73+
it('should not generate pdf when there is no active form summary included email field and workflow is incomplete', () => {})
74+
it('should not generate pdf when there is no active form summary included email field and workflow is complete but has no emails to notify for outcome', () => {})
75+
})
76+
77+
describe('subsequent steps', () => {
78+
it('should not generate pdf when there is no active form summary included email field and workflow is incomplete', () => {})
79+
it('should not generate pdf when there is no active form summary included email field and workflow is complete but has no emails to notify for outcome', () => {})
80+
})
81+
})
82+
})
83+
84+
describe('respondent copy emails are sent', () => {
85+
describe('first step', () => {
86+
it('sends respondent copy without pdf when email field auto reply enabled but form summary is not included', () => {})
87+
it('sends respondent copy emails with pdf when email field auto reply enabled and form summary is included', () => {})
88+
it('does not send respondent copy emails when email field auto reply is not enabled', () => {})
89+
})
90+
91+
describe('subsequent steps', () => {
92+
it('sends respondent copy without pdf when email field auto reply enabled but form summary is not included', () => {})
93+
it('sends respondent copy emails with pdf when email field auto reply enabled and form summary is included', () => {})
94+
it('does not send respondent copy emails when email field auto reply is not enabled', () => {})
95+
})
96+
})
97+
5898
describe('mrf approval email notification when approval step exists', () => {
5999
it('workflow continues and does not send approved outcome email when mrf is approved for mid step of multiple step MRF', async () => {
60100
// Arrange
@@ -310,6 +350,7 @@ describe('multirespondent-submission.service', () => {
310350
] as FormFieldDto[],
311351
} as SnapshottedFormDef,
312352
currentStepNumber: currentWorkflowStep,
353+
attachments: MOCK_SUBMISSION_ATTACHMENTS,
313354
encryptedPayload: {
314355
encryptedContent: 'encryptedContent',
315356
version: 1,
@@ -325,6 +366,13 @@ describe('multirespondent-submission.service', () => {
325366
expect(sendMrfApprovalEmailSpy).toHaveBeenCalledTimes(1)
326367
expect(sendMrfWorkflowCompletionEmailSpy).not.toHaveBeenCalled()
327368
expect(sendMRFWorkflowStepEmailSpy).not.toHaveBeenCalled()
369+
// pdf generation is invoked
370+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
371+
// pdf attachment is included and submission attachments are correct
372+
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].attachments).toEqual([
373+
...MOCK_SUBMISSION_ATTACHMENTS,
374+
EXPECTED_MOCK_PDF_ATTACHMENT,
375+
])
328376
// is approve email and destination emails are correct
329377
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].isRejected).toBeFalse()
330378
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].emails).toContainValues(
@@ -471,6 +519,12 @@ describe('multirespondent-submission.service', () => {
471519
expect(sendMrfApprovalEmailSpy).toHaveBeenCalledTimes(1)
472520
expect(sendMrfWorkflowCompletionEmailSpy).not.toHaveBeenCalled()
473521
expect(sendMRFWorkflowStepEmailSpy).not.toHaveBeenCalled()
522+
// pdf generation is invoked
523+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
524+
// pdf attachment is included and submission attachments are correct
525+
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].attachments).toEqual([
526+
EXPECTED_MOCK_PDF_ATTACHMENT,
527+
])
474528
// is approve email and destination emails are correct
475529
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].isRejected).toBeFalse()
476530
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].emails).toContainValues(
@@ -611,6 +665,12 @@ describe('multirespondent-submission.service', () => {
611665
expect(sendMrfApprovalEmailSpy).toHaveBeenCalledTimes(1)
612666
expect(sendMrfWorkflowCompletionEmailSpy).not.toHaveBeenCalled()
613667
expect(sendMRFWorkflowStepEmailSpy).not.toHaveBeenCalled()
668+
// pdf generation is invoked
669+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
670+
// pdf attachment is included and submission attachments are correct
671+
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].attachments).toEqual([
672+
EXPECTED_MOCK_PDF_ATTACHMENT,
673+
])
614674
// is approve email and destination emails are correct
615675
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].isRejected).toBeFalse()
616676
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].emails).toContainValues(
@@ -732,6 +792,7 @@ describe('multirespondent-submission.service', () => {
732792
] as FormFieldDto[],
733793
} as SnapshottedFormDef,
734794
currentStepNumber: currentStepNumber,
795+
attachments: MOCK_SUBMISSION_ATTACHMENTS,
735796
encryptedPayload: {
736797
encryptedContent: 'encryptedContent',
737798
version: 1,
@@ -747,6 +808,13 @@ describe('multirespondent-submission.service', () => {
747808
expect(sendMrfApprovalEmailSpy).toHaveBeenCalledTimes(1)
748809
expect(sendMrfWorkflowCompletionEmailSpy).not.toHaveBeenCalled()
749810
expect(sendMRFWorkflowStepEmailSpy).not.toHaveBeenCalled()
811+
// pdf generation is invoked
812+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
813+
// pdf attachment is included and submission attachments are correct
814+
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].attachments).toEqual([
815+
...MOCK_SUBMISSION_ATTACHMENTS,
816+
EXPECTED_MOCK_PDF_ATTACHMENT,
817+
])
750818
// is rejected email and destination emails are correct
751819
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].isRejected).toBeTrue()
752820
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].emails).toContainValues(
@@ -902,6 +970,12 @@ describe('multirespondent-submission.service', () => {
902970
expect(sendMrfApprovalEmailSpy).toHaveBeenCalledTimes(1)
903971
expect(sendMrfWorkflowCompletionEmailSpy).not.toHaveBeenCalled()
904972
expect(sendMRFWorkflowStepEmailSpy).not.toHaveBeenCalled()
973+
// pdf generation is invoked
974+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
975+
// pdf attachment is included and submission attachments are correct
976+
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].attachments).toEqual([
977+
EXPECTED_MOCK_PDF_ATTACHMENT,
978+
])
905979
// is rejected email and destination emails are correct
906980
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].isRejected).toBeTrue()
907981
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].emails).toContainValues(
@@ -1042,6 +1116,12 @@ describe('multirespondent-submission.service', () => {
10421116
expect(sendMrfApprovalEmailSpy).toHaveBeenCalledTimes(1)
10431117
expect(sendMrfWorkflowCompletionEmailSpy).not.toHaveBeenCalled()
10441118
expect(sendMRFWorkflowStepEmailSpy).not.toHaveBeenCalled()
1119+
// pdf generation is invoked
1120+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
1121+
// pdf attachment is included and submission attachments are correct
1122+
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].attachments).toEqual([
1123+
EXPECTED_MOCK_PDF_ATTACHMENT,
1124+
])
10451125
// is rejected email and destination emails are correct
10461126
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].isRejected).toBeTrue()
10471127
expect(sendMrfApprovalEmailSpy.mock.calls[0][0].emails).toContainValues(
@@ -1054,7 +1134,7 @@ describe('multirespondent-submission.service', () => {
10541134
})
10551135

10561136
describe('mrf completion email notification when no approval step exists', () => {
1057-
it('sends completion email when single step mrf is completed', async () => {
1137+
it('sends completion email with pdf attachment when single step mrf is completed', async () => {
10581138
// Arrange
10591139
const sendMrfWorkflowCompletionEmailSpy = jest.spyOn(
10601140
MailService,
@@ -1087,11 +1167,19 @@ describe('multirespondent-submission.service', () => {
10871167
submissionPublicKey: 'submissionPublicKey',
10881168
encryptedSubmissionSecretKey: 'encryptedSubmissionSecretKey',
10891169
} as MultirespondentSubmissionDto,
1170+
attachments: MOCK_SUBMISSION_ATTACHMENTS,
10901171
logMeta: {} as any,
10911172
})
10921173

10931174
// Assert
10941175
expect(sendMrfWorkflowCompletionEmailSpy).toHaveBeenCalledTimes(1)
1176+
// pdf generation is invoked
1177+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
1178+
// pdf attachment is included and submission attachments are correct
1179+
expect(
1180+
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].attachments,
1181+
).toEqual([...MOCK_SUBMISSION_ATTACHMENTS, EXPECTED_MOCK_PDF_ATTACHMENT])
1182+
// the correct destination emails are included
10951183
expect(
10961184
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].emails,
10971185
).toContainValues(['[email protected]'])
@@ -1193,6 +1281,12 @@ describe('multirespondent-submission.service', () => {
11931281

11941282
// Assert
11951283
expect(sendMrfWorkflowCompletionEmailSpy).toHaveBeenCalledTimes(1)
1284+
// pdf generation is invoked
1285+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
1286+
// pdf attachment is included and submission attachments are correct
1287+
expect(
1288+
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].attachments,
1289+
).toEqual([EXPECTED_MOCK_PDF_ATTACHMENT])
11961290
// The emails sent to should only be the expected emails exactly
11971291
expect(
11981292
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].emails,
@@ -1384,6 +1478,12 @@ describe('multirespondent-submission.service', () => {
13841478

13851479
// Assert
13861480
expect(sendMrfWorkflowCompletionEmailSpy).toHaveBeenCalledTimes(1)
1481+
// pdf generation is invoked
1482+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
1483+
// pdf attachment is included and submission attachments are correct
1484+
expect(
1485+
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].attachments,
1486+
).toEqual([EXPECTED_MOCK_PDF_ATTACHMENT])
13871487
expect(
13881488
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].emails,
13891489
).toContainValues(expectedEmails)
@@ -1448,10 +1548,17 @@ describe('multirespondent-submission.service', () => {
14481548
workflowStep: workflow.length - 1,
14491549
} as MultirespondentSubmissionDto,
14501550
logMeta: {} as any,
1551+
attachments: MOCK_SUBMISSION_ATTACHMENTS,
14511552
})
14521553

14531554
// Assert
14541555
expect(sendMrfWorkflowCompletionEmailSpy).toHaveBeenCalledTimes(1)
1556+
// pdf generation is invoked
1557+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
1558+
// pdf attachment is included and submission attachments are correct
1559+
expect(
1560+
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].attachments,
1561+
).toEqual([...MOCK_SUBMISSION_ATTACHMENTS, EXPECTED_MOCK_PDF_ATTACHMENT])
14551562
expect(
14561563
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].emails,
14571564
).toContainValues(expectedEmails)
@@ -1522,6 +1629,12 @@ describe('multirespondent-submission.service', () => {
15221629

15231630
// Assert
15241631
expect(sendMrfWorkflowCompletionEmailSpy).toHaveBeenCalledTimes(1)
1632+
// pdf generation is invoked
1633+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
1634+
// pdf attachment is included and submission attachments are correct
1635+
expect(
1636+
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].attachments,
1637+
).toEqual([EXPECTED_MOCK_PDF_ATTACHMENT])
15251638
expect(
15261639
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].emails,
15271640
).toContainValues(expectedEmails)
@@ -1612,6 +1725,13 @@ describe('multirespondent-submission.service', () => {
16121725

16131726
// Assert
16141727
expect(sendMrfWorkflowCompletionEmailSpy).toHaveBeenCalledTimes(1)
1728+
// pdf generation is invoked
1729+
expect(MockMailUtils.generateAutoreplyPdf).toHaveBeenCalledTimes(1)
1730+
// pdf attachment is included and submission attachments are correct
1731+
expect(
1732+
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].attachments,
1733+
).toEqual([EXPECTED_MOCK_PDF_ATTACHMENT])
1734+
// the correct destination emails are included
16151735
expect(
16161736
sendMrfWorkflowCompletionEmailSpy.mock.calls[0][0].emails,
16171737
).toContainValues(expectedEmails)

0 commit comments

Comments
 (0)