From ab8892888ae35f0e17b51a25bd8c0dd38c077e5a Mon Sep 17 00:00:00 2001 From: Luke Crouch Date: Mon, 11 May 2020 15:03:30 -0500 Subject: [PATCH] fix #1685: add resolved stats to breach-stats --- controllers/user.js | 10 ++++++++-- tests/controllers/user.test.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) 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(), + }); +});