diff --git a/infra/controller.js b/infra/controller.js index 5bf86cc..426cb86 100644 --- a/infra/controller.js +++ b/infra/controller.js @@ -15,11 +15,12 @@ function onNoMatchHandler(request, response) { } function onErrorHandler(error, request, response) { - if ( - error instanceof ValidationError || - error instanceof NotFoundError || - error instanceof UnauthorizedError - ) { + if (error instanceof ValidationError || error instanceof NotFoundError) { + return response.status(error.statusCode).json(error); + } + + if (error instanceof UnauthorizedError) { + clearSessionCookie(response); return response.status(error.statusCode).json(error); } diff --git a/test/integration/api/v1/user/get.test.js b/test/integration/api/v1/user/get.test.js index dc96b4b..6246404 100644 --- a/test/integration/api/v1/user/get.test.js +++ b/test/integration/api/v1/user/get.test.js @@ -90,6 +90,17 @@ describe("GET /api/v1/user", () => { action: "Check if user is logged in and try again.", status_code: 401, }); + + // Set-Cookie assertions + + const parsedCookie = setCookieParser(response, { map: true }); + expect(parsedCookie.session_id).toEqual({ + name: "session_id", + value: "invalid", + maxAge: -1, + path: "/", + httpOnly: true, + }); }); test("With expired session", async () => { @@ -118,6 +129,17 @@ describe("GET /api/v1/user", () => { action: "Check if user is logged in and try again.", status_code: 401, }); + + // Set-Cookie assertions + + const parsedCookie = setCookieParser(response, { map: true }); + expect(parsedCookie.session_id).toEqual({ + name: "session_id", + value: "invalid", + maxAge: -1, + path: "/", + httpOnly: true, + }); }); test("With valid session about to expire", async () => {