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

mozilla / blurts-server / 27dbd455-244c-48ab-8e37-1ece9db5723e

pending completion
27dbd455-244c-48ab-8e37-1ece9db5723e

push

circleci

GitHub
MNTOR-983: Settings page frontend (#2765)

282 of 1281 branches covered (22.01%)

Branch coverage included in aggregate %.

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

959 of 3457 relevant lines covered (27.74%)

4.5 hits per line

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

0.0
/src/controllers/settings.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 {
8
  getUserEmails,
9
  resetUnverifiedEmailAddress,
10
  addSubscriberUnverifiedEmailHash,
11
  removeOneSecondaryEmail,
12
  getEmailById,
13
  verifyEmailHash
14
} from '../db/tables/email_addresses.js'
15

16
import { setAllEmailsToPrimary } from '../db/tables/subscribers.js'
17

18
import { fluentError, getMessage } from '../utils/fluent.js'
19
import { sendEmail, getVerificationUrl, getUnsubscribeUrl } from '../utils/email.js'
20

21
import { getBreachesForEmail } from '../utils/hibp.js'
22
import { generateToken } from '../utils/csrf.js'
23

24
import { mainLayout } from '../views/main.js'
25
import { settings } from '../views/partials/settings.js'
26
import { getTemplate } from '../views/email-2022.js'
27
import { verifyPartial } from '../views/partials/email-verify.js'
28

29
async function settingsPage (req, res) {
30
  const emails = await getUserEmails(req.session.user.id)
×
31
  // Add primary subscriber email to the list
32
  emails.push({
×
33
    email: req.session.user.primary_email,
34
    sha1: req.session.user.primary_sha1,
35
    primary: true,
36
    verified: true
37
  })
38

39
  const breachCounts = new Map()
×
40

41
  const allBreaches = req.app.locals.breaches
×
42
  for (const email of emails) {
×
43
    const breaches = await getBreachesForEmail(
×
44
      email.sha1,
45
      allBreaches,
46
      true,
47
      false
48
    )
49
    breachCounts.set(email.email, breaches?.length || 0)
×
50
  }
51

52
  const {
53
    all_emails_to_primary: allEmailsToPrimary,
54
    fxa_profile_json: fxaProfile
55
  } = req.user
×
56

57
  const data = {
×
58
    allEmailsToPrimary,
59
    fxaProfile,
60
    partial: settings,
61
    emails,
62
    breachCounts,
63
    limit: AppConstants.MAX_NUM_ADDRESSES,
64
    csrfToken: generateToken(res)
65
  }
66

67
  res.send(mainLayout(data))
×
68
}
69

70
async function addEmail (req, res) {
71
  const sessionUser = req.user
×
72
  const email = req.body.email
×
73
  // Use the same regex as HTML5 email input type
74
  // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email#basic_validation
75
  const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
×
76

77
  if (!email || !emailRegex.test(email)) {
×
78
    throw fluentError('user-add-invalid-email')
×
79
  }
80

81
  if (sessionUser.email_addresses.length >= AppConstants.MAX_NUM_ADDRESSES) {
×
82
    throw fluentError('user-add-too-many-emails')
×
83
  }
84

85
  checkForDuplicateEmail(sessionUser, email)
×
86

87
  const unverifiedSubscriber = await addSubscriberUnverifiedEmailHash(
×
88
    req.session.user,
89
    email
90
  )
91

92
  await sendVerificationEmail(unverifiedSubscriber.id)
×
93

94
  return res.json({
×
95
    success: true,
96
    status: 200,
97
    message: 'Sent the verification email'
98
  })
99
}
100

101
function checkForDuplicateEmail (sessionUser, email) {
102
  const emailLowerCase = email.toLowerCase()
×
103
  if (emailLowerCase === sessionUser.primary_email.toLowerCase()) {
×
104
    throw fluentError('user-add-duplicate-email')
×
105
  }
106

107
  for (const secondaryEmail of sessionUser.email_addresses) {
×
108
    if (emailLowerCase === secondaryEmail.email.toLowerCase()) {
×
109
      throw fluentError('user-add-duplicate-email')
×
110
    }
111
  }
112
}
113

114
async function removeEmail (req, res) {
115
  const emailId = req.body.emailId
×
116
  const sessionUser = req.user
×
117
  const existingEmail = await getEmailById(emailId)
×
118

119
  if (existingEmail.subscriber_id !== sessionUser.id) {
×
120
    throw fluentError('error-not-subscribed')
×
121
  }
122

123
  removeOneSecondaryEmail(emailId)
×
124
  res.redirect('/user/settings')
×
125
}
126

127
async function resendEmail (req, res) {
128
  const emailId = req.body.emailId
×
129
  const sessionUser = req.user
×
130
  const existingEmail = await getUserEmails(sessionUser.id)
×
131

132
  const filteredEmail = existingEmail.filter(
×
133
    (a) => a.email === emailId && a.subscriber_id === sessionUser.id
×
134
  )
135

136
  if (!filteredEmail) {
×
137
    throw fluentError('user-verify-token-error')
×
138
  }
139

140
  await sendVerificationEmail(emailId)
×
141

142
  return res.json({
×
143
    success: true,
144
    status: 200,
145
    message: 'Sent the verification email'
146
  })
147
}
148

149
async function sendVerificationEmail (emailId) {
150
  const unverifiedEmailAddressRecord = await resetUnverifiedEmailAddress(
×
151
    emailId
152
  )
153
  const recipientEmail = unverifiedEmailAddressRecord.email
×
154
  const data = {
×
155
    recipientEmail,
156
    ctaHref: getVerificationUrl(unverifiedEmailAddressRecord),
157
    utmCampaign: 'email_verify',
158
    unsubscribeUrl: getUnsubscribeUrl(
159
      unverifiedEmailAddressRecord,
160
      'account-verification-email'
161
    ),
162
    heading: getMessage('email-verify-heading'),
163
    subheading: getMessage('email-verify-subhead'),
164
    partial: { name: 'verify' }
165
  }
166
  await sendEmail(
×
167
    recipientEmail,
168
    getMessage('email-subject-verify'),
169
    getTemplate(data, verifyPartial(data))
170
  )
171
}
172

173
async function verifyEmail (req, res) {
174
  const token = req.query.token
×
175
  await verifyEmailHash(token)
×
176

177
  return res.redirect('/user/settings')
×
178
}
179

180
async function updateCommunicationOptions (req, res) {
181
  const sessionUser = req.user
×
182
  // 0 = Send breach alerts to the email address found in brew breach.
183
  // 1 = Send all breach alerts to user's primary email address.
184
  const allEmailsToPrimary = Number(req.body.communicationOption) === 1
×
185
  const updatedSubscriber = await setAllEmailsToPrimary(
×
186
    sessionUser,
187
    allEmailsToPrimary
188
  )
189
  req.session.user = updatedSubscriber
×
190

191
  return res.json({
×
192
    success: true,
193
    status: 200,
194
    message: 'Communications options updated'
195
  })
196
}
197

198
export {
199
  settingsPage,
200
  resendEmail,
201
  addEmail,
202
  removeEmail,
203
  verifyEmail,
204
  updateCommunicationOptions
205
}
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