mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-24 06:43:57 +08:00
added authentication tests
This commit is contained in:
parent
8d17f699df
commit
8ec307c837
3 changed files with 57 additions and 3 deletions
55
backend/__tests__/utils/authentication.spec.ts
Normal file
55
backend/__tests__/utils/authentication.spec.ts
Normal file
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue