Skip to content

Commit

Permalink
#28 Add first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JabobKrauskopf committed Mar 5, 2023
1 parent e915863 commit 90fa990
Show file tree
Hide file tree
Showing 8 changed files with 2,377 additions and 46 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Run Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install modules
run: yarn
- name: Run tests
run: yarn test
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
setupFiles: ["./test/setEnv.js"],
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "ts-node-dev --respawn --exit-child --transpile-only server/index.ts",
"build": "node ./config/build-config.js",
"start": "node ./build/index.js",
"test": "jest",
"prisma:generate": "prisma generate",
"prisma:db:push": "prisma db push",
"prisma:db:deploy": "prisma migrate deploy",
Expand All @@ -25,13 +26,15 @@
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/express": "^4.17.17",
"@types/jest": "^29.4.0",
"@types/jsonwebtoken": "^9.0.1",
"@types/node": "^18.6.1",
"@types/ssh2": "^1.11.5",
"esbuild": "^0.16.17",
"jest": "^29.4.3",
"prisma": "^4.11.0",
"supertest": "^6.3.3",
"ts-jest": "^29.0.5",
"ts-node-dev": "^2.0.0",
"typescript": "^4.9.5"
},
Expand Down
2 changes: 2 additions & 0 deletions test/setEnv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
process.env.JWT_SECRET = "secret";
process.env.JWT_EXPIRES_IN = "900";
9 changes: 8 additions & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@
"extends": "../config/shared-tsconfig.json",
"include": [
"./**/*"
]
],
"compilerOptions": {
"paths": {
"~/*": [
"../server/*"
]
}
}
}
171 changes: 171 additions & 0 deletions test/utils/env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
describe("Env Utils", () => {
const cacheEnv = process.env;

beforeEach(() => {
jest.resetModules();
process.env = { ...cacheEnv };
});

afterEach(() => {
process.env = cacheEnv;
});

describe("PORT Env Variable", () => {
test("PORT can be correctly set", () => {
process.env.PORT = "3000";

const { env } = require("../../server/utils/env");

expect(env.PORT).toBe(3000);
});

test("PORT has correct default value", () => {
process.env.PORT = undefined;

const { env } = require("../../server/utils/env");

expect(env.PORT).toBe(8000);
});

test("PORT throws error if PORT is not a number", () => {
process.env.PORT = "undefined";

expect(() => {
require("../../server/utils/env");
}).toThrow();
});
});

describe("HOST Env Variable", () => {
test("HOST can be correctly set", () => {
process.env.HOST = "127.0.0.1";

const { env } = require("../../server/utils/env");

expect(env.HOST).toBe("127.0.0.1");
});

test("HOST has correct default value", () => {
process.env.HOST = undefined;

const { env } = require("../../server/utils/env");

expect(env.HOST).toBe("localhost");
});
});

describe("PROTOCOL Env Variable", () => {
test("PROTOCOL can be set to http", () => {
process.env.PROTOCOL = "http";

const { env } = require("../../server/utils/env");

expect(env.PROTOCOL).toBe("http");
});
test("PROTOCOL can be set to https", () => {
process.env.PROTOCOL = "https";

const { env } = require("../../server/utils/env");

expect(env.PROTOCOL).toBe("https");
});

test("PROTOCOL has correct default value", () => {
process.env.PROTOCOL = undefined;

const { env } = require("../../server/utils/env");

expect(env.PROTOCOL).toBe("http");
});

test("PROTOCOL throws error if PROTOCOL is not http or https", () => {
process.env.PROTOCOL = "undefined";

expect(() => {
require("../../server/utils/env");
}).toThrow();
});
});

describe("DATA_FOLDER Env Variable", () => {
test("DATA_FOLDER can be correctly set", () => {
process.env.DATA_FOLDER = "/data";

const { env } = require("../../server/utils/env");

expect(env.DATA_FOLDER).toBe("/data");
});

test("DATA_FOLDER is undefined if DATA_FOLDER not set", () => {
process.env.DATA_FOLDER = undefined;

const { env } = require("../../server/utils/env");

expect(env.DATA_FOLDER).toBe(undefined);
});
});

describe("API_KEY Env Variable", () => {
test("API_KEY can be correctly set", () => {
process.env.API_KEY = "api_key";

const { env } = require("../../server/utils/env");

expect(env.API_KEY).toBe("api_key");
});

test("API_KEY throws error if API_KEY is not set", () => {
process.env.API_KEY = undefined;

expect(() => {
require("../../server/utils/env");
}).toThrow();
});
});

describe("JWT_SECRET Env Variable", () => {
test("JWT_SECRET can be correctly set", () => {
process.env.JWT_SECRET = "jwt_secret";

const { env } = require("../../server/utils/env");

expect(env.JWT_SECRET).toBe("jwt_secret");
});

test("JWT_SECRET throws error if JWT_SECRET is not set", () => {
process.env.JWT_SECRET = undefined;

expect(() => {
require("../../server/utils/env");
}).toThrow();
});
});

describe("JWT_EXPIRES_IN Env Variable", () => {
test("JWT_EXPIRES_IN can be correctly set", () => {
process.env.JWT_EXPIRES_IN = "800";

const { env } = require("../../server/utils/env");

expect(env.JWT_EXPIRES_IN).toBe(800);
});

test("JWT_EXPIRES_IN has correct default value", () => {
process.env.JWT_EXPIRES_IN = undefined;

const { env } = require("../../server/utils/env");

expect(env.JWT_EXPIRES_IN).toBe(900);
});

test("JWT_EXPIRES_IN throws error if JWT_EXPIRES_IN is not a number", () => {
process.env.JWT_EXPIRES_IN = "undefined";

expect(() => {
require("../../server/utils/env");
}).toThrow();
});
});
});

export {};
40 changes: 40 additions & 0 deletions test/utils/jwt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { generateJWT, verifyJWT } from "../../server/utils/jwt";

describe("JWT Utils", () => {
beforeAll(() => {
Date.now = jest.fn(() => 1677970800);
});

describe("Genearting JWT", () => {
test("Successfully generate JWT", async () => {
const payload = { action: "test" };

const token = generateJWT(payload);

expect(token).toBe(
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY3Rpb24iOiJ0ZXN0IiwiaWF0IjoxNjc3OTcwLCJleHAiOjE2Nzg4NzB9.0IxQyH2aDIP9P4r6fNIreWjCpyF7l0pHxvhosz-ck3Q"
);
});
});

describe("Verifying JWT", () => {
test("Successfully verify JWT", async () => {
const payload = { action: "test" };

const token = generateJWT(payload);

const result = verifyJWT(token);

expect(result).toStrictEqual({
isVerified: true,
payload: { ...payload, exp: 1678870, iat: 1677970 },
});
});

test("Reject malformed JWT", async () => {
const result = verifyJWT("token");

expect(result).toStrictEqual({ isVerified: false });
});
});
});
Loading

0 comments on commit 90fa990

Please sign in to comment.