• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / blurts-server / #12688

pending completion
#12688

push

circleci

web-flow
Merge pull request #2888 from mozilla/MNTOR-1254/update-layouts

Mntor 1254/update layouts

282 of 1421 branches covered (19.85%)

Branch coverage included in aggregate %.

5 of 5 new or added lines in 5 files covered. (100.0%)

959 of 3929 relevant lines covered (24.41%)

2.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/controllers/email-preview.js
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4

5
import AppConstants from '../app-constants.js'
6

7
import { notify } from './hibp.js'
8
import { mainLayout } from '../views/mainLayout.js'
9
import { guestLayout } from '../views/guestLayout.js'
10
import { emailPreview } from '../views/partials/email-preview.js'
11
import { getTemplate, getPreviewTemplate } from '../views/emails/email-2022.js'
12
import { breachAlertEmailPartial } from '../views/emails/email-breach-alert.js'
13
import { signupReportEmailPartial } from '../views/emails/email-signup-report.js'
14
import { verifyPartial } from '../views/emails/email-verify.js'
15
import {
16
  monthlyUnresolvedEmailPartial
17
} from '../views/emails/email-monthly-unresolved.js'
18

19
import { getMessage } from '../utils/fluent.js'
20
import { generateToken } from '../utils/csrf.js'
21
import {
22
  EmailTemplateType,
23
  getNotifictionDummyData,
24
  getVerificationDummyData,
25
  getMonthlyDummyData,
26
  getSignupReportDummyData,
27
  sendEmail
28
} from '../utils/email.js'
29

30
const { EMAIL_TEST_RECIPIENT } = AppConstants
×
31

32
function getTemplatesData () {
33
  return {
×
34
    [EmailTemplateType.Verification]: {
35
      label: 'Email verification',
36
      template: getPreviewTemplate(
37
        getVerificationDummyData(EMAIL_TEST_RECIPIENT),
38
        verifyPartial
39
      )
40
    },
41
    [EmailTemplateType.Notification]: {
42
      label: 'Breach notification',
43
      template: getPreviewTemplate(
44
        getNotifictionDummyData(EMAIL_TEST_RECIPIENT),
45
        breachAlertEmailPartial
46
      )
47
    },
48
    [EmailTemplateType.Monthly]: {
49
      label: 'Monthly unresolved breaches',
50
      template: getPreviewTemplate(
51
        getMonthlyDummyData(EMAIL_TEST_RECIPIENT),
52
        monthlyUnresolvedEmailPartial
53
      )
54
    },
55
    [EmailTemplateType.SignupReport]: {
56
      label: 'Signup report',
57
      template: getPreviewTemplate(
58
        getSignupReportDummyData(EMAIL_TEST_RECIPIENT),
59
        signupReportEmailPartial
60
      )
61
    }
62
  }
63
}
64

65
function emailsPage (req, res) {
66
  const { params } = req
×
67
  const template = params.template ?? EmailTemplateType.Verification
×
68

69
  const data = {
×
70
    csrfToken: generateToken(res),
71
    fxaProfile: req.user.fxa_profile_json,
72
    partial: emailPreview,
73
    email: {
74
      data: getTemplatesData(),
75
      recipients: [
76
        req.session.user.primary_email,
77
        AppConstants.EMAIL_TEST_RECIPIENT
78
      ],
79
      template
80
    },
81
    isAdminPreview: true
82
  }
83

84
  res.send(mainLayout(data))
×
85
}
86

87
function emailsPreviewPage (req, res) {
88
  const { params } = req
×
89
  const template = params.template ?? EmailTemplateType.Verification
×
90

91
  const data = {
×
92
    partial: emailPreview,
93
    email: {
94
      data: getTemplatesData(),
95
      template
96
    }
97
  }
98

99
  res.send(guestLayout(data))
×
100
}
101

102
async function sendTestNotification (req, res) {
103
  // The test breach notification can be viewed in the public Mailinator inbox
104
  // as documented in the README:
105
  // https://github.com/mozilla/blurts-server#trigger-breach-alert-email
106
  const breachNotificationData = {
×
107
    breachName: 'Adobe',
108
    // Hash for dummy email `localmonitor20200827@mailinator.com`
109
    hashPrefix: '365050',
110
    hashSuffixes: ['53cbb89874fc738c0512daf12bc4d91765']
111
  }
112

113
  const notifyReq = {
×
114
    app: req.app,
115
    body: {
116
      ...req.body,
117
      ...breachNotificationData
118
    },
119
    token: AppConstants.HIBP_NOTIFY_TOKEN
120
  }
121

122
  await notify(notifyReq, res)
×
123
}
124

125
async function sendTestEmail (req, res) {
126
  const { emailId, recipient } = req.body
×
127

128
  switch (emailId) {
×
129
    case EmailTemplateType.Verification: {
130
      // Send test verification email
131
      const emailTemplate = getTemplate(
×
132
        getVerificationDummyData(recipient),
133
        verifyPartial
134
      )
135
      await sendEmail(
×
136
        recipient,
137
        getMessage('email-subject-verify'),
138
        emailTemplate
139
      )
140
      break
×
141
    }
142
    case EmailTemplateType.Notification: {
143
      // Send test breach notification email
144
      await sendTestNotification(req, res)
×
145
      break
×
146
    }
147
    case EmailTemplateType.Monthly: {
148
      // Send test monthly unresolved breaches email
149
      const emailTemplate = getTemplate(
×
150
        getMonthlyDummyData(EMAIL_TEST_RECIPIENT),
151
        monthlyUnresolvedEmailPartial
152
      )
153
      await sendEmail(
×
154
        recipient,
155
        getMessage('email-unresolved-heading'),
156
        emailTemplate
157
      )
158
      break
×
159
    }
160
    case EmailTemplateType.SignupReport: {
161
      // Send test sign-up report email
162
      const emailTemplate = getTemplate(
×
163
        getSignupReportDummyData(EMAIL_TEST_RECIPIENT),
164
        signupReportEmailPartial
165
      )
166
      await sendEmail(
×
167
        recipient,
168
        getMessage('email-subject-found-breaches'),
169
        emailTemplate
170
      )
171
      break
×
172
    }
173
    default: {
174
      throw new Error(`No test email found for ${emailId}`)
×
175
    }
176
  }
177

178
  console.info(`Sent test email: ${emailId}`)
×
179

180
  // The notify function has its own response
181
  if (emailId !== EmailTemplateType.Notification) {
×
182
    return res.json({
×
183
      success: true,
184
      status: 200,
185
      message: `Sent test ${emailId} email`
186
    })
187
  }
188
}
189

190
export { emailsPage, emailsPreviewPage, sendTestEmail }
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc