Skip to content

Commit

Permalink
feat: Attachment support in email responses for storage mode forms (#…
Browse files Browse the repository at this point in the history
…8053)

* feat: send attachment to mail service

* test: add test case for submitEncryptModeForm

* test: add attachments to mail service tests

* test: add test for case where there are no attachments
  • Loading branch information
g-tejas authored Jan 24, 2025
1 parent fe12170 commit 00f2848
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import {
SpVerifiedContent,
} from 'src/app/modules/verified-content/verified-content.types'
import MailService from 'src/app/services/mail/mail.service'
import { FormFieldSchema, IPopulatedEncryptedForm } from 'src/types'
import {
FormFieldSchema,
IAttachmentInfo,
IPopulatedEncryptedForm,
} from 'src/types'
import { EncryptSubmissionDto, FormCompleteDto } from 'src/types/api'

import { SubmissionEmailObj } from '../../email-submission/email-submission.util'
Expand Down Expand Up @@ -1330,4 +1334,121 @@ describe('encrypt-submission.controller', () => {
)
})
})

describe('submitEncryptModeForm', () => {
beforeEach(() => {
jest.clearAllMocks()
MockMailService.sendSubmissionToAdmin.mockReturnValue(okAsync(true))
})

it('should call sendSubmissionToAdmin with no attachments', async () => {
// Arrange
jest
.spyOn(MailService, 'sendSubmissionToAdmin')
.mockReturnValue(okAsync(true))

const mockFormId = new ObjectId()
const mockForm = {
_id: mockFormId,
title: 'Test Form',
authType: FormAuthType.NIL,
form_fields: [] as FormFieldSchema[],
emails: ['[email protected]'],
getUniqueMyInfoAttrs: () => [] as MyInfoAttribute[],
} as IPopulatedEncryptedForm

const mockAttachments: IAttachmentInfo[] = []

const mockReq = merge(
expressHandler.mockRequest({
params: { formId: mockFormId.toHexString() },
body: {
responses: [],
attachments: mockAttachments,
},
}),
{
formsg: {
encryptedPayload: {
encryptedContent: 'mockEncryptedContent',
version: 1,
},
formDef: {},
encryptedFormDef: mockForm,
unencryptedAttachments: mockAttachments,
} as unknown as EncryptSubmissionDto,
} as unknown as FormCompleteDto,
) as unknown as SubmitEncryptModeFormHandlerRequest

const mockRes = expressHandler.mockResponse()

// Act
await submitEncryptModeFormForTest(mockReq, mockRes)

// Assert (done)
expect(MockMailService.sendSubmissionToAdmin).toHaveBeenCalledWith(
expect.objectContaining({
attachments: mockAttachments,
}),
)
})

it('should call sendSubmissionToAdmin with the attachments', async () => {
// Arrange
jest
.spyOn(MailService, 'sendSubmissionToAdmin')
.mockReturnValue(okAsync(true))

const mockFormId = new ObjectId()
const mockForm = {
_id: mockFormId,
title: 'Test Form',
authType: FormAuthType.NIL,
form_fields: [] as FormFieldSchema[],
emails: ['[email protected]'],
getUniqueMyInfoAttrs: () => [] as MyInfoAttribute[],
} as IPopulatedEncryptedForm

const mockAttachments: IAttachmentInfo[] = [
{
filename: 'test.pdf',
content: Buffer.from('this is a test file'),
fieldId: 'test-field-id',
},
]

const mockReq = merge(
expressHandler.mockRequest({
params: { formId: mockFormId.toHexString() },
body: {
responses: [],
attachments: mockAttachments,
},
}),
{
formsg: {
encryptedPayload: {
encryptedContent: 'mockEncryptedContent',
version: 1,
},
formDef: {},
encryptedFormDef: mockForm,
unencryptedAttachments: mockAttachments,
} as unknown as EncryptSubmissionDto,
} as unknown as FormCompleteDto,
) as unknown as SubmitEncryptModeFormHandlerRequest

const mockRes = expressHandler.mockResponse()

// Act
await submitEncryptModeFormForTest(mockReq, mockRes)

// Assert (done)
expect(MockMailService.sendSubmissionToAdmin).toHaveBeenCalledWith(
expect.objectContaining({
attachments: mockAttachments,
}),
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ const _createSubmission = async ({
created: createdTime,
id: submission.id,
},
attachments: undefined, // Don't send attachments in the email notifications
attachments: unencryptedAttachments,
formData: emailData.formData,
})
}
Expand Down
8 changes: 7 additions & 1 deletion src/app/services/mail/__tests__/mail.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,13 @@ describe('mail.service', () => {
id: 'mockSubmissionId',
created: new Date(),
},
attachments: [],
attachments: [
{
filename: 'test.pdf',
content: Buffer.from('this is a test file'),
fieldId: 'test-field-id',
},
],
dataCollationData: [
{
question: 'some question',
Expand Down

0 comments on commit 00f2848

Please sign in to comment.