diff --git a/controllers/user.js b/controllers/user.js index 7211588f3..991f1b42a 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -1,5 +1,6 @@ "use strict"; +const crypto = require("crypto"); const isemail = require("isemail"); const HIBP = require("../hibp"); const DB = require("../db/DB"); @@ -8,7 +9,6 @@ const HBSHelpers = require("../hbs-helpers"); const UNSUB_REASONS = require("../unsubscribe_reasons"); const sha1 = require("../sha1-utils"); const TIPS = require("../tips"); -const crypto = require("crypto"); async function add(req, res) { @@ -84,7 +84,7 @@ async function postUnsubscribe(req, res) { const unsubscribedUser = await DB.removeSubscriberByToken(req.body.token, req.body.emailHash); // if user backs into unsubscribe page and clicks "unsubscribe" again if (!unsubscribedUser) { - throw new Error("This email address is not subscribed to Firefox Monitor"); + throw new Error("This email address is not subscribed to Firefox Monitor."); } const surveyTicket = crypto.randomBytes(40).toString("hex"); @@ -94,7 +94,7 @@ async function postUnsubscribe(req, res) { } -async function getUnsubSurvey(req, res) { +function getUnsubSurvey(req, res) { //throws error if user refreshes unsubscribe survey page after they have submitted an answer if(!req.session.unsub) { throw new Error("This email address is not subscribed to Firefox Monitor."); @@ -106,7 +106,7 @@ async function getUnsubSurvey(req, res) { } -async function postUnsubSurvey(req, res) { +function postUnsubSurvey(req, res) { //clear session in case a user subscribes / unsubscribes multiple times or with multiple email addresses. req.session.reset(); res.send({ diff --git a/routes/user.js b/routes/user.js index c68948084..5067435c5 100644 --- a/routes/user.js +++ b/routes/user.js @@ -16,7 +16,7 @@ router.get("/verify", jsonParser, asyncMiddleware(verify)); router.use("/unsubscribe", urlEncodedParser); router.get("/unsubscribe", asyncMiddleware(getUnsubscribe)); router.post("/unsubscribe", asyncMiddleware(postUnsubscribe)); -router.get("/unsubscribe_survey", asyncMiddleware(getUnsubSurvey)); -router.post("/unsubscribe_survey", jsonParser, asyncMiddleware(postUnsubSurvey)); +router.get("/unsubscribe_survey", getUnsubSurvey); +router.post("/unsubscribe_survey", jsonParser, postUnsubSurvey); module.exports = router; diff --git a/tests/controllers/user.test.js b/tests/controllers/user.test.js index 1b0e452c0..e3ed77fbe 100644 --- a/tests/controllers/user.test.js +++ b/tests/controllers/user.test.js @@ -109,13 +109,13 @@ test("user unsubscribe POST request with valid hash and token unsubscribes user" const validToken = "0e2cb147-2041-4e5b-8ca9-494e773b2cf0"; const validHash = getSha1("unverifiedemail@test.com"); // Set up mocks - const req = { body: { token: validToken, emailHash: validHash } }; + const req = { body: { token: validToken, emailHash: validHash }, session: {}}; const resp = httpMocks.createResponse(); // Call code-under-test await user.postUnsubscribe(req, resp); - expect(resp.statusCode).toEqual(200); + expect(resp.statusCode).toEqual(302); const subscriber = await DB.getSubscriberByToken(validToken); expect(subscriber).toBeUndefined(); }); @@ -134,15 +134,12 @@ test("user unsubscribe GET request with invalid token returns error", async () = }); -test("user unsubscribe POST request with invalid token and hash redirects home", async () => { +test("user unsubscribe POST request with invalid token and throws error", async () => { const invalidToken = "123456789"; const invalidHash = "0123456789abcdef"; const req = { body: { token: invalidToken, emailHash: invalidHash } }; const resp = { redirect: jest.fn() }; - await user.postUnsubscribe(req, resp); - - const mockRedirectCallArgs = resp.redirect.mock.calls[0]; - expect(mockRedirectCallArgs[0]).toBe("/"); + await expect(user.postUnsubscribe(req, resp)).rejects.toThrow("This email address is not subscribed to Firefox Monitor."); });