diff --git a/backend/__tests__/utils/authentication.spec.ts b/backend/__tests__/utils/authentication.spec.ts new file mode 100644 index 000000000..878816be9 --- /dev/null +++ b/backend/__tests__/utils/authentication.spec.ts @@ -0,0 +1,55 @@ +import request from "supertest"; +import app from "../../src/app"; +// eslint-disable-line +import * as Auth from "../../src/utils/auth"; + +const mockApp = request(app); + +describe("Authentication", () => { + describe("requiresFreshToken", () => { + //@ts-ignore + + jest.spyOn(Auth, "verifyIdToken").mockImplementation(() => { + return { + uid: "123456789", + email: "newuser@mail.com", + iat: 0, + }; + }); + + it("should fail if token is not fresh", async () => { + Date.now = jest.fn(() => 60001); + await mockApp + .delete("/users") + .set({ + Accept: "application/json", + Authorization: "Bearer 123456789", + }) + .expect(401); + }); + it("should allow the request if token is fresh", async () => { + Date.now = jest.fn(() => 5); + const newUser = { + name: "NewUser2asdfad", + uid: "123456789", + email: "newuser@mail.com", + }; + + await mockApp + .post("/users/signup") + .send(newUser) + .set({ + Accept: "application/json", + }) + .expect(200); + + await mockApp + .delete("/users") + .set({ + Accept: "application/json", + Authorization: "Bearer 123456789", + }) + .expect(200); + }); + }); +}); diff --git a/backend/jest.config.ts b/backend/jest.config.ts index 9b8ecc3f9..61733ac5a 100644 --- a/backend/jest.config.ts +++ b/backend/jest.config.ts @@ -7,9 +7,9 @@ export default { coverageThreshold: { global: { // These percentages should never decrease - statements: 38, + statements: 39, branches: 38, - functions: 22, + functions: 23, lines: 42, }, }, diff --git a/backend/src/middlewares/auth.ts b/backend/src/middlewares/auth.ts index dab9b21e5..72e8dc9d5 100644 --- a/backend/src/middlewares/auth.ts +++ b/backend/src/middlewares/auth.ts @@ -6,7 +6,6 @@ import { base64UrlDecode } from "../utils/misc"; import { NextFunction, Response, Handler } from "express"; import statuses from "../constants/monkey-status-codes"; import { incrementAuth } from "../utils/prometheus"; -import Logger from "../utils/logger"; interface RequestAuthenticationOptions { isPublic?: boolean;