-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e915863
commit 90fa990
Showing
8 changed files
with
2,377 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.