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

mozilla / blurts-server / #11912

pending completion
#11912

push

circleci

web-flow
Merge pull request #2755 from mozilla/MNTOR-1046-User-menu

MNTOR-1046: Add user menu

282 of 1172 branches covered (24.06%)

Branch coverage included in aggregate %.

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

959 of 3117 relevant lines covered (30.77%)

2.5 hits per line

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

0.0
/src/controllers/breaches.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 { mainLayout } from '../views/main.js'
6
import { breaches } from '../views/partials/breaches.js'
7
import { setBreachResolution, updateBreachStats } from '../db/tables/subscribers.js'
8

9
import { appendBreachResolutionChecklist } from '../utils/breach-resolution.js'
10
import { getAllEmailsAndBreaches } from '../utils/breaches.js'
11

12
async function breachesPage (req, res) {
13
  const emailCount = 1 + (req.user.email_addresses?.length || 0) // +1 because user.email_addresses does not include primary
×
14
  // TODO: remove: to test out getBreaches call with JSON returns
15
  const breachesData = await getAllEmailsAndBreaches(req.user, req.app.locals.breaches)
×
16
  appendBreachResolutionChecklist(breachesData)
×
17

18
  const data = {
×
19
    breachesData,
20
    emailCount,
21
    fxaProfile: req.user.fxa_profile_json,
22
    partial: breaches
23
  }
24

25
  res.send(mainLayout(data))
×
26
}
27

28
/**
29
 * Get breaches from the database and return a JSON object
30
 * TODO: Takes in additional query parameters:
31
 *
32
 * status: enum (resolved, unresolved)
33
 * email: string
34
 * @param {object} req
35
 * @param {object} res
36
 */
37
async function getBreaches (req, res) {
38
  const allBreaches = req.app.locals.breaches
×
39
  const sessionUser = req.user
×
40
  const resp = await getAllEmailsAndBreaches(sessionUser, allBreaches)
×
41
  return res.json(resp)
×
42
}
43

44
/**
45
 * Modify breach resolution for a user
46
 * @param {object} req containing {user, body: {affectedEmail, recencyIndex, resolutionsChecked}}
47
 *
48
 * recencyIndex: corresponds to the relevant breach from HIBP
49
 *
50
 * resolutionsChecked: has the following structure [DataTypes]
51
 *
52
 * @param {object} res JSON object containing the updated breach resolution
53
 */
54
async function putBreachResolution (req, res) {
55
  const sessionUser = req.user
×
56
  const { affectedEmail, recencyIndex, resolutionsChecked } = req.body
×
57
  const recencyIndexNumber = Number(recencyIndex)
×
58
  const affectedEmailIsSubscriberRecord = sessionUser.primary_email === affectedEmail
×
59
  const affectedEmailInEmailAddresses = sessionUser.email_addresses.filter(ea => ea.email === affectedEmail)
×
60

61
  // check if current user's emails array contain affectedEmail
62
  if (!affectedEmailIsSubscriberRecord && !affectedEmailInEmailAddresses) {
×
63
    return res.json('Error: affectedEmail is not valid for this subscriber')
×
64
  }
65

66
  // check if recency index is a part of affectEmail's breaches
67
  const allBreaches = req.app.locals.breaches
×
68
  const { verifiedEmails } = await getAllEmailsAndBreaches(req.session.user, allBreaches)
×
69
  const currentEmail = verifiedEmails.find(ve => ve.email === affectedEmailInEmailAddresses[0].email)
×
70
  const currentBreaches = currentEmail.breaches?.filter(b => b.recencyIndex === recencyIndexNumber)
×
71
  if (!currentBreaches) {
×
72
    return res.json('Error: the recencyIndex provided does not exist')
×
73
  }
74

75
  // check if resolutionsChecked array is a subset of the breaches' datatypes
76
  const isSubset = resolutionsChecked.every(val => currentBreaches[0].DataClasses.includes(val))
×
77
  if (!isSubset) {
×
78
    return res.json(`Error: the resolutionChecked param contains more than allowed data types: ${resolutionsChecked}`)
×
79
  }
80

81
  /* new JsonB:
82
  {
83
    email_id: {
84
      recency_index: {
85
        resolutions: ['email', ...],
86
        isResolved: true
87
      }
88
    }
89
  }
90
  */
91

92
  const currentBreachDataTypes = currentBreaches[0].DataClasses // get this from existing breaches
×
93
  const currentBreachResolution = req.user.breach_resolution || {} // get this from existing breach resolution if available
×
94
  const isResolved = resolutionsChecked.length === currentBreachDataTypes.length
×
95
  currentBreachResolution[affectedEmail] = {
×
96
    ...(currentBreachResolution[affectedEmail] || {}),
×
97
    ...{
98
      [recencyIndexNumber]: {
99
        resolutionsChecked,
100
        isResolved
101
      }
102
    }
103
  }
104

105
  const updatedSubscriber = await setBreachResolution(sessionUser, currentBreachResolution)
×
106

107
  req.session.user = updatedSubscriber
×
108

109
  const userBreachStats = breachStatsV1(verifiedEmails)
×
110

111
  await updateBreachStats(sessionUser.id, userBreachStats)
×
112

113
  res.json(updatedSubscriber.breach_resolution)
×
114
}
115

116
// PRIVATE
117

118
/**
119
 * TODO: DEPRECATE
120
 * This utiliy function is maintained to keep backwards compatibility with V1.
121
 * After v2 is launched, we will deprecate this function
122
 * @param {object} verifiedEmails [{breaches: [isResolved: true/false, dataClasses: []]}]
123
 * @returns {object} breachStats
124
 * {
125
 *    monitoredEmails: {
126
      count: 0
127
    },
128
    numBreaches: {
129
      count: 0,
130
      numResolved: 0
131
      numUnresolved: 0
132
    },
133
    passwords: {
134
      count: 0,
135
      numResolved: 0
136
    }
137
  }
138
 */
139
function breachStatsV1 (verifiedEmails) {
140
  const breachStats = {
×
141
    monitoredEmails: {
142
      count: 0
143
    },
144
    numBreaches: {
145
      count: 0,
146
      numResolved: 0
147
    },
148
    passwords: {
149
      count: 0,
150
      numResolved: 0
151
    }
152
  }
153
  let foundBreaches = []
×
154

155
  // combine the breaches for each account, breach duplicates are ok
156
  // since the user may have multiple accounts with different emails
157
  verifiedEmails.forEach(email => {
×
158
    email.breaches.forEach(breach => {
×
159
      if (breach.IsResolved) {
×
160
        breachStats.numBreaches.numResolved++
×
161
      }
162

163
      const dataClasses = breach.DataClasses
×
164
      if (dataClasses.includes('passwords')) {
×
165
        breachStats.passwords.count++
×
166
        if (breach.IsResolved) {
×
167
          breachStats.passwords.numResolved++
×
168
        }
169
      }
170
    })
171
    foundBreaches = [...foundBreaches, ...email.breaches]
×
172
  })
173

174
  // total number of verified emails being monitored
175
  breachStats.monitoredEmails.count = verifiedEmails.length
×
176

177
  // total number of breaches across all emails
178
  breachStats.numBreaches.count = foundBreaches.length
×
179

180
  breachStats.numBreaches.numUnresolved = breachStats.numBreaches.count - breachStats.numBreaches.numResolved
×
181

182
  return breachStats
×
183
}
184

185
export { breachesPage, putBreachResolution, getBreaches }
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