added authentication tests

This commit is contained in:
Miodec 2022-06-30 01:15:06 +02:00
parent 8d17f699df
commit 8ec307c837
3 changed files with 57 additions and 3 deletions

View 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);
});
});
});

View file

@ -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,
},
},

View file

@ -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;