-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: allow single CC/BCC recipient to be sent via form-data (#2286)
* fix cc and bcc to allow stringified array * add tests for cc * abstract email validator, update test cases * revert to 2d array for test inputs
- Loading branch information
1 parent
3ccb15a
commit f7d3ecb
Showing
2 changed files
with
204 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import request from 'supertest' | ||
import { Op } from 'sequelize' | ||
import { Sequelize } from 'sequelize-typescript' | ||
|
||
import { User } from '@core/models' | ||
|
@@ -15,6 +16,7 @@ import { | |
import { | ||
EmailFromAddress, | ||
EmailMessageTransactional, | ||
EmailMessageTransactionalCc, | ||
TransactionalEmailMessageStatus, | ||
} from '@email/models' | ||
import { | ||
|
@@ -863,6 +865,166 @@ describe(`${emailTransactionalRoute}/send`, () => { | |
]) | ||
}) | ||
|
||
const ccValidTests = [ | ||
['[email protected]'], | ||
['[email protected]', '[email protected]'], | ||
JSON.stringify(['[email protected]']), | ||
JSON.stringify([ | ||
'[email protected]', | ||
'[email protected]', | ||
]), | ||
] | ||
test.each(ccValidTests)( | ||
'Should send email with cc from valid array or stringified array - JSON payload', | ||
async (cc) => { | ||
mockSendEmail = jest | ||
.spyOn(EmailService, 'sendEmail') | ||
.mockResolvedValue(true) | ||
const res = await request(app) | ||
.post(endpoint) | ||
.set('Authorization', `Bearer ${apiKey}`) | ||
.send({ | ||
...validApiCall, | ||
cc, | ||
reply_to: user.email, | ||
}) | ||
|
||
const arrayToCheck = Array.isArray(cc) ? cc : JSON.parse(cc) | ||
|
||
expect(res.status).toBe(201) | ||
expect(res.body.cc.sort()).toStrictEqual(arrayToCheck.sort()) | ||
expect(mockSendEmail).toBeCalledTimes(1) | ||
const transactionalEmail = await EmailMessageTransactional.findOne({ | ||
where: { id: res.body.id }, | ||
}) | ||
expect(transactionalEmail).not.toBeNull() | ||
expect(transactionalEmail).toMatchObject({ | ||
recipient: validApiCall.recipient, | ||
from: validApiCall.from, | ||
status: TransactionalEmailMessageStatus.Accepted, | ||
errorCode: null, | ||
}) | ||
} | ||
) | ||
|
||
test.each(ccValidTests)( | ||
'Should send email with cc from valid array or stringified array - form-data', | ||
async (cc) => { | ||
mockSendEmail = jest | ||
.spyOn(EmailService, 'sendEmail') | ||
.mockResolvedValue(true) | ||
// in the case where single cc is sent, stringify the cc list | ||
const ccSend = | ||
Array.isArray(cc) && cc.length === 1 ? JSON.stringify(cc) : cc | ||
|
||
const res = await request(app) | ||
.post(endpoint) | ||
.set('Authorization', `Bearer ${apiKey}`) | ||
.field('recipient', validApiCall.recipient) | ||
.field('subject', validApiCall.subject) | ||
.field('body', validApiCall.body) | ||
.field('from', 'Postman <[email protected]>') | ||
.field('reply_to', validApiCall.reply_to) | ||
.field('cc', ccSend) | ||
|
||
const arrayToCheck = Array.isArray(cc) ? cc : JSON.parse(cc) | ||
|
||
expect(res.status).toBe(201) | ||
expect(res.body.cc.sort()).toStrictEqual(arrayToCheck.sort()) | ||
expect(mockSendEmail).toBeCalledTimes(1) | ||
const transactionalEmail = await EmailMessageTransactional.findOne({ | ||
where: { id: res.body.id }, | ||
include: [ | ||
{ | ||
model: EmailMessageTransactionalCc, | ||
attributes: ['email', 'ccType'], | ||
where: { errorCode: { [Op.eq]: null } }, | ||
required: false, | ||
}, | ||
], | ||
}) | ||
|
||
expect(transactionalEmail).not.toBeNull() | ||
expect(transactionalEmail).toMatchObject({ | ||
recipient: validApiCall.recipient, | ||
from: validApiCall.from, | ||
status: TransactionalEmailMessageStatus.Accepted, | ||
errorCode: null, | ||
}) | ||
const transactionalCcEmails = | ||
transactionalEmail?.emailMessageTransactionalCc.map( | ||
(item) => item.email | ||
) | ||
expect(transactionalCcEmails?.sort()).toStrictEqual(arrayToCheck.sort()) | ||
} | ||
) | ||
|
||
const ccInvalidTests = [ | ||
{ | ||
cc: '[email protected]', | ||
errMsg: | ||
'"cc" failed custom validation because cc must be a valid array or stringified array.', | ||
}, | ||
{ | ||
cc: JSON.stringify('[email protected]'), | ||
errMsg: | ||
'"cc" failed custom validation because cc must be a valid stringified array', | ||
}, | ||
{ | ||
cc: JSON.stringify({ key: 'cc', email: '[email protected]' }), | ||
errMsg: | ||
'"cc" failed custom validation because cc must be a valid stringified array', | ||
}, | ||
] | ||
test.each(ccInvalidTests)( | ||
'Should throw api validation error if cc is not valid array or stringified array - JSON payload', | ||
async ({ cc, errMsg }) => { | ||
mockSendEmail = jest.spyOn(EmailService, 'sendEmail') | ||
|
||
const res = await request(app) | ||
.post(endpoint) | ||
.set('Authorization', `Bearer ${apiKey}`) | ||
.send({ | ||
...validApiCall, | ||
cc, | ||
reply_to: user.email, | ||
}) | ||
|
||
expect(res.status).toBe(400) | ||
expect(mockSendEmail).not.toBeCalled() | ||
|
||
expect(res.body).toStrictEqual({ | ||
code: 'api_validation', | ||
message: errMsg, | ||
}) | ||
} | ||
) | ||
|
||
test.each(ccInvalidTests)( | ||
'Should throw api validation error if cc is not valid array or stringified array - form-data', | ||
async ({ cc, errMsg }) => { | ||
mockSendEmail = jest.spyOn(EmailService, 'sendEmail') | ||
|
||
const res = await request(app) | ||
.post(endpoint) | ||
.set('Authorization', `Bearer ${apiKey}`) | ||
.field('recipient', validApiCall.recipient) | ||
.field('subject', validApiCall.subject) | ||
.field('body', validApiCall.body) | ||
.field('from', 'Postman <[email protected]>') | ||
.field('reply_to', validApiCall.reply_to) | ||
.field('cc', cc) | ||
|
||
expect(res.status).toBe(400) | ||
expect(mockSendEmail).not.toBeCalled() | ||
|
||
expect(res.body).toStrictEqual({ | ||
code: 'api_validation', | ||
message: errMsg, | ||
}) | ||
} | ||
) | ||
|
||
test('Requests should be rate limited and metadata and error code is saved correctly in db', async () => { | ||
mockSendEmail = jest | ||
.spyOn(EmailService, 'sendEmail') | ||
|