Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into SPL-499
Browse files Browse the repository at this point in the history
  • Loading branch information
devind-ra committed Nov 25, 2024
2 parents 947fb87 + 8664c94 commit d841cd1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
7 changes: 5 additions & 2 deletions app/emailjs-mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const emailjs = require('@emailjs/nodejs')
const logger = require('./logger')
const plannerText = 'Planner'

const sendMail = async (experience, moreDetails, emailjsIds, options) => {
const sendMail = async (experience, moreDetails, emailjsIds, options, reqHeaders) => {
const currentDateTime = new Date().toLocaleString('en-GB', {
timeZone: 'Europe/London',
year: 'numeric',
Expand All @@ -13,11 +13,14 @@ const sendMail = async (experience, moreDetails, emailjsIds, options) => {
second: '2-digit'
})

const userAgent = reqHeaders?.['user-agent'] || ''

const templateParams = {
experience,
moreDetails,
dateTime: currentDateTime,
plannerOrEligibility: plannerText
plannerOrEligibility: plannerText,
userAgent
}

try {
Expand Down
2 changes: 1 addition & 1 deletion app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ router
}
const experience = req.body.feedback
const moreDetail = req.body['feedback-more-detail']
emailJSEmail(experience, moreDetail, emailjsIds, options).then(() =>
emailJSEmail(experience, moreDetail, emailjsIds, options, req.headers).then(() =>
res.redirect('/feedback/confirmation')
)
})
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function initialisePublic (app) {
app.use('/public', express.static(path.join(__dirname, '/public')))
app.use(
'/',
express.static(path.join(__dirname, '/node_modules/govuk-frontend/govuk/'))
express.static(path.join(__dirname, '/node_modules/govuk-frontend/dist/govuk/'))
)
}

Expand Down
33 changes: 28 additions & 5 deletions test/unit/app/emailjs-mailer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ describe('sendMail', function () {
const plannerText = 'Planner'
const emailjsIds = { serviceID: 'test_service', templateID: 'test_template' }
const options = { publicKey: 'test_public', privateKey: 'test_private' }
const userAgent = { 'user-agent': 'test-agent' }

it('should call emailjs.send with correct parameters', async function () {
await sendMail(experience, moreDetails, emailjsIds, options)
await sendMail(experience, moreDetails, emailjsIds, options, userAgent)

expect(emailjsSendStub.calledOnce).to.equal(true)
const args = emailjsSendStub.getCall(0).args
Expand All @@ -45,33 +46,55 @@ describe('sendMail', function () {
expect(templateParams.experience).to.equal(experience)
expect(templateParams.moreDetails).to.equal(moreDetails)
expect(templateParams.plannerOrEligibility).to.equal(plannerText)
expect(templateParams.userAgent).to.equal(userAgent['user-agent'])
expect(templateParams).to.have.property('dateTime')
expect(args[3]).to.equal(options)
})

it('should log success message when email is sent successfully', async function () {
await sendMail(experience, moreDetails, emailjsIds, options)
await sendMail(experience, moreDetails, emailjsIds, options, userAgent)

expect(loggerInfoStub.calledOnce).to.equal(true)
const logArgs = loggerInfoStub.getCall(0).args[0]
expect(logArgs.message).to.equal('Email sent successfully! 200 OK')
expect(logArgs.eventType).to.equal('MailEvent')
expect(logArgs.eventResult).to.equal('Success')
})

it('should handle undefined userAgent gracefully', async function () {
const experience = 'Great service!'
const moreDetails = 'No additional feedback'
const emailjsIds = { serviceID: 'test_service', templateID: 'test_template' }
const options = { publicKey: 'test_public', privateKey: 'test_private' }
const userAgent = undefined

await sendMail(experience, moreDetails, emailjsIds, options, userAgent)

expect(emailjsSendStub.calledOnce).to.equal(true)
const args = emailjsSendStub.getCall(0).args
expect(args[0]).to.equal(emailjsIds.serviceID)
expect(args[1]).to.equal(emailjsIds.templateID)
const templateParams = args[2]
expect(templateParams.experience).to.equal(experience)
expect(templateParams.moreDetails).to.equal(moreDetails)
expect(templateParams.userAgent).to.equal('')
expect(templateParams).to.have.property('dateTime')
expect(args[3]).to.equal(options)
})
})

describe('failed email sending', function () {
const experience = 'Great service!'
const moreDetails = 'No additional feedback'
const reqHeaders = { 'User-Agent': 'test-agent' }
const emailjsIds = { serviceID: 'test_service', templateID: 'test_template' }
const options = { publicKey: 'test_public', privateKey: 'test_private' }
const userAgent = { 'user-agent': 'test-agent' }

it('should log error message when emailjs.send rejects', async function () {
const error = { text: 'Error sending email' }
emailjsSendStub.rejects(error)

await sendMail(experience, moreDetails, reqHeaders, emailjsIds, options)
await sendMail(experience, moreDetails, emailjsIds, options, userAgent)

expect(loggerErrorStub.calledOnce).to.equal(true)
const logArgs = loggerErrorStub.getCall(0).args[0]
Expand All @@ -85,7 +108,7 @@ describe('sendMail', function () {
const error = { text: 'Unexpected error' }
emailjsSendStub.throws(error)

await sendMail(experience, moreDetails, reqHeaders, emailjsIds, options)
await sendMail(experience, moreDetails, emailjsIds, options)

expect(loggerErrorStub.calledOnce).to.equal(true)
const logArgs = loggerErrorStub.getCall(0).args[0]
Expand Down

0 comments on commit d841cd1

Please sign in to comment.