diff --git a/controllers/user.js b/controllers/user.js index 4acbc5724..8caac3d37 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -559,11 +559,17 @@ async function getBreachStats(req, res) { const allBreaches = req.app.locals.breaches; const { verifiedEmails } = await getAllEmailsAndBreaches(user, allBreaches); const breachStats = resultsSummary(verifiedEmails); - return res.json({ + const baseStats = { monitoredEmails: breachStats.monitoredEmails.count, numBreaches: breachStats.numBreaches.count, passwords: breachStats.passwords.count, - }); + }; + const resolvedStats = { + numBreachesResolved: breachStats.numBreaches.numResolved, + passwordsResolved: breachStats.passwords.numResolved, + }; + const returnStats = (req.query.includeResolved === "true") ? Object.assign(baseStats, resolvedStats) : baseStats; + return res.json(returnStats); } diff --git a/tests/controllers/user.test.js b/tests/controllers/user.test.js index 1ef5b1e65..5ce44e82a 100644 --- a/tests/controllers/user.test.js +++ b/tests/controllers/user.test.js @@ -631,6 +631,7 @@ test("user breach-stats POST request with FXA response for Monitor user returns const req = { token: "test-token", app: { locals: { breaches: testBreaches } }, + query: {}, }; FXA.verifyOAuthToken = jest.fn(); FXA.verifyOAuthToken.mockReturnValueOnce({ @@ -654,3 +655,36 @@ test("user breach-stats POST request with FXA response for Monitor user returns passwords: expect.anything(), }); }); + + +test("user breach-stats POST request with includeResolved returns breach stats json with resolved", async () => { + const testSubscriberFxAUID = TEST_SUBSCRIBERS.firefox_account.fxa_uid; + const req = { + token: "test-token", + app: { locals: { breaches: testBreaches } }, + query: {includeResolved: "true"}, + }; + FXA.verifyOAuthToken = jest.fn(); + FXA.verifyOAuthToken.mockReturnValueOnce({ + body: { + scope: [user.FXA_MONITOR_SCOPE], + user: testSubscriberFxAUID, + }, + }); + HIBP.getBreachesForEmail = jest.fn(); + HIBP.getBreachesForEmail.mockReturnValue([]); + + const resp = { json: jest.fn() }; + + await user.getBreachStats(req, resp); + + const jsonCallArgs = resp.json.mock.calls[0]; + + expect(jsonCallArgs[0]).toMatchObject({ + monitoredEmails: expect.anything(), + numBreaches: expect.anything(), + passwords: expect.anything(), + numBreachesResolved: expect.anything(), + passwordsResolved: expect.anything(), + }); +});