mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2024-09-20 15:26:15 +08:00
Rewriting middleware tests (#3266)
* aoaoaooao * different attempts * figured out the test * added another test * redefining next before each function, checking next call count * removed eslint ignore
This commit is contained in:
parent
b813dd5aa0
commit
db3b6755b8
93
backend/__tests__/middlewares/auth.spec.ts
Normal file
93
backend/__tests__/middlewares/auth.spec.ts
Normal file
|
@ -0,0 +1,93 @@
|
|||
import * as AuthUtils from "../../src/utils/auth";
|
||||
import * as Auth from "../../src/middlewares/auth";
|
||||
import { DecodedIdToken } from "firebase-admin/lib/auth/token-verifier";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { getCachedConfiguration } from "../../src/init/configuration";
|
||||
|
||||
const mockDecodedToken: DecodedIdToken = {
|
||||
uid: "123456789",
|
||||
email: "newuser@mail.com",
|
||||
iat: 0,
|
||||
} as DecodedIdToken;
|
||||
|
||||
jest.spyOn(AuthUtils, "verifyIdToken").mockImplementation(async (_token) => {
|
||||
return mockDecodedToken;
|
||||
});
|
||||
|
||||
describe("middlewares/auth", () => {
|
||||
let mockRequest: Partial<MonkeyTypes.Request>;
|
||||
let mockResponse: Partial<Response>;
|
||||
let nextFunction: NextFunction;
|
||||
|
||||
beforeEach(async () => {
|
||||
mockRequest = {
|
||||
headers: {
|
||||
authorization: "Bearer 123456789",
|
||||
},
|
||||
ctx: {
|
||||
configuration: await getCachedConfiguration(true),
|
||||
decodedToken: {
|
||||
type: "None",
|
||||
uid: "",
|
||||
email: "",
|
||||
},
|
||||
},
|
||||
};
|
||||
mockResponse = {
|
||||
json: jest.fn(),
|
||||
};
|
||||
nextFunction = jest.fn((error) => {
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
return "Next function called";
|
||||
}) as unknown as NextFunction;
|
||||
});
|
||||
|
||||
describe("authenticateRequest", () => {
|
||||
it("should fail if token is not fresh", async () => {
|
||||
Date.now = jest.fn(() => 60001);
|
||||
|
||||
const authenticateRequest = Auth.authenticateRequest({
|
||||
requireFreshToken: true,
|
||||
});
|
||||
|
||||
let result;
|
||||
|
||||
try {
|
||||
result = await authenticateRequest(
|
||||
mockRequest as Request,
|
||||
mockResponse as Response,
|
||||
nextFunction
|
||||
);
|
||||
} catch (e) {
|
||||
result = e;
|
||||
}
|
||||
|
||||
expect(result.message).toBe(
|
||||
"Unauthorized\nStack: This endpoint requires a fresh token"
|
||||
);
|
||||
expect(nextFunction).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
it("should allow the request if token is fresh", async () => {
|
||||
Date.now = jest.fn(() => 10000);
|
||||
|
||||
const authenticateRequest = Auth.authenticateRequest({
|
||||
requireFreshToken: true,
|
||||
});
|
||||
|
||||
await authenticateRequest(
|
||||
mockRequest as Request,
|
||||
mockResponse as Response,
|
||||
nextFunction
|
||||
);
|
||||
|
||||
const decodedToken = mockRequest?.ctx?.decodedToken;
|
||||
|
||||
expect(decodedToken?.type).toBe("Bearer");
|
||||
expect(decodedToken?.email).toBe(mockDecodedToken.email);
|
||||
expect(decodedToken?.uid).toBe(mockDecodedToken.uid);
|
||||
expect(nextFunction).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,55 +0,0 @@
|
|||
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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue