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

mozilla / blurts-server / 4040bc25-7f30-464b-958e-746a756a4e44

pending completion
4040bc25-7f30-464b-958e-746a756a4e44

push

circleci

GitHub
Merge pull request #2790 from mozilla/MNTOR-1056-Migrate-breach-alert-email

282 of 1375 branches covered (20.51%)

Branch coverage included in aggregate %.

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

959 of 3709 relevant lines covered (25.86%)

4.2 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/main.js'
9
import { emailPreview } from '../views/partials/email-preview.js'
10
import { getTemplate, getPreviewTemplate } from '../views/email-2022.js'
11
import { breachAlertEmailPartial } from '../views/partials/email-breach-alert.js'
12
import { signupReportEmailPartial } from '../views/partials/email-signup-report.js'
13
import { verifyPartial } from '../views/partials/email-verify.js'
14
import {
15
  monthlyUnresolvedEmailPartial
16
} from '../views/partials/email-monthly-unresolved.js'
17

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

29
const { EMAIL_TEST_RECIPIENT } = AppConstants
×
30

31
function emailsPage (req, res) {
32
  const { params } = req
×
33
  const template = params.template ?? EmailTemplateType.Verification
×
34

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

66
  const data = {
×
67
    csrfToken: generateToken(res),
68
    fxaProfile: req.user.fxa_profile_json,
69
    partial: emailPreview,
70
    email: {
71
      data: emailTemplates,
72
      recipients: [
73
        req.session.user.primary_email,
74
        AppConstants.EMAIL_TEST_RECIPIENT
75
      ],
76
      template
77
    }
78
  }
79

80
  res.send(mainLayout(data))
×
81
}
82

83
async function sendTestNotification (req, res) {
84
  // The test breach notification can be viewed in the public Mailinator inbox
85
  // as documented in the README:
86
  // https://github.com/mozilla/blurts-server#trigger-breach-alert-email
87
  const breachNotificationData = {
×
88
    breachName: 'Adobe',
89
    // Hash for dummy email `localmonitor20200827@mailinator.com`
90
    hashPrefix: '365050',
91
    hashSuffixes: ['53cbb89874fc738c0512daf12bc4d91765']
92
  }
93

94
  const notifyReq = {
×
95
    app: req.app,
96
    body: {
97
      ...req.body,
98
      ...breachNotificationData
99
    },
100
    token: AppConstants.HIBP_NOTIFY_TOKEN
101
  }
102

103
  await notify(notifyReq, res)
×
104
}
105

106
async function sendTestEmail (req, res) {
107
  const { emailId, recipient } = req.body
×
108

109
  switch (emailId) {
×
110
    case EmailTemplateType.Verification: {
111
      // Send test verification email
112
      const emailTemplate = getTemplate(
×
113
        getVerificationDummyData(recipient),
114
        verifyPartial
115
      )
116
      await sendEmail(
×
117
        recipient,
118
        getMessage('email-subject-verify'),
119
        emailTemplate
120
      )
121
      break
×
122
    }
123
    case EmailTemplateType.Notification: {
124
      // Send test breach notification email
125
      await sendTestNotification(req, res)
×
126
      break
×
127
    }
128
    case EmailTemplateType.Monthly: {
129
      // Send test monthly unresolved breaches email
130
      const emailTemplate = getTemplate(
×
131
        getMonthlyDummyData(EMAIL_TEST_RECIPIENT),
132
        monthlyUnresolvedEmailPartial
133
      )
134
      await sendEmail(
×
135
        recipient,
136
        getMessage('email-unresolved-heading'),
137
        emailTemplate
138
      )
139
      break
×
140
    }
141
    case EmailTemplateType.SignupReport: {
142
      // Send test sign-up report email
143
      const emailTemplate = getTemplate(
×
144
        getSignupReportDummyData(EMAIL_TEST_RECIPIENT),
145
        signupReportEmailPartial
146
      )
147
      await sendEmail(
×
148
        recipient,
149
        getMessage('email-subject-found-breaches'),
150
        emailTemplate
151
      )
152
      break
×
153
    }
154
    default: {
155
      throw new Error(`No test email found for ${emailId}`)
×
156
    }
157
  }
158

159
  console.info(`Sent test email: ${emailId}`)
×
160

161
  // The notify function has its own response
162
  if (emailId !== EmailTemplateType.Notification) {
×
163
    return res.json({
×
164
      success: true,
165
      status: 200,
166
      message: `Sent test ${emailId} email`
167
    })
168
  }
169
}
170

171
export { emailsPage, 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