server: Add first e2e test, github action, health endpoint (#68)

And a bonus health endpoint to make the simplest test possible
This commit is contained in:
Joaquin "Florius" Azcarate 2022-04-03 21:47:37 +02:00 committed by GitHub
parent 5e9288e9fb
commit ef005ef0b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 2216 additions and 36 deletions

21
.github/workflows/server-CI.yaml vendored Normal file
View file

@ -0,0 +1,21 @@
name: Server CI
on: push
jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: server
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v1
with:
node-version: '16'
- name: Install deps
run: yarn
- name: Run tests
run: yarn test

View file

@ -1 +1,2 @@
node_modules/ node_modules/
__tests__/

4
server/.env.test Normal file
View file

@ -0,0 +1,4 @@
JWT_SECRET=secret-jwt
MEMORY_DB=true
REGISTRATION_PASSWORD=password
SECRET_KEY=secret

10
server/jest.config.js Normal file
View file

@ -0,0 +1,10 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFiles: ["<rootDir>/test/setup-tests.ts"],
moduleNameMapper: {
"@lib/(.*)": "<rootDir>/src/lib/$1",
"@routes/(.*)": "<rootDir>/src/routes/$1"
},
};

View file

@ -11,7 +11,8 @@
"migrate:up": "ts-node migrate up", "migrate:up": "ts-node migrate up",
"migrate:down": "ts-node migrate down", "migrate:down": "ts-node migrate down",
"migrate": "ts-node migrate", "migrate": "ts-node migrate",
"lint": "prettier --config .prettierrc 'src/**/*.ts' 'index.ts' --write" "lint": "prettier --config .prettierrc 'src/**/*.ts' 'index.ts' --write",
"test": "jest"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
@ -41,12 +42,17 @@
"@types/cors": "^2.8.12", "@types/cors": "^2.8.12",
"@types/express": "^4.0.39", "@types/express": "^4.0.39",
"@types/express-jwt": "^6.0.4", "@types/express-jwt": "^6.0.4",
"@types/jest": "^27.4.1",
"@types/jsonwebtoken": "^8.5.8", "@types/jsonwebtoken": "^8.5.8",
"@types/marked": "^4.0.3", "@types/marked": "^4.0.3",
"@types/node": "^17.0.21", "@types/node": "^17.0.21",
"@types/react-dom": "^17.0.14", "@types/react-dom": "^17.0.14",
"@types/supertest": "^2.0.12",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"jest": "^27.5.1",
"prettier": "^2.6.0", "prettier": "^2.6.0",
"supertest": "^6.2.2",
"ts-jest": "^27.1.4",
"ts-node": "^10.6.0", "ts-node": "^10.6.0",
"tsc-alias": "^1.6.5", "tsc-alias": "^1.6.5",
"tsconfig-paths": "^3.14.1", "tsconfig-paths": "^3.14.1",

View file

@ -0,0 +1,16 @@
import * as request from 'supertest'
import { app } from '../app'
describe('GET /health', () => {
it('should return 200 and a status up', (done) => {
request(app)
.get(`/health`)
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) => {
if (err) return done(err)
expect(res.body).toMatchObject({ 'status': 'UP' })
done()
})
})
})

View file

@ -1,7 +1,7 @@
import * as express from "express" import * as express from "express"
import * as bodyParser from "body-parser" import * as bodyParser from "body-parser"
import * as errorhandler from "strong-error-handler" import * as errorhandler from "strong-error-handler"
import { posts, users, auth, files, admin } from "@routes/index" import { posts, users, auth, files, admin, health } from "@routes/index"
import { errors } from "celebrate" import { errors } from "celebrate"
import secretKey from "@lib/middleware/secret-key" import secretKey from "@lib/middleware/secret-key"
import markdown from "@lib/render-markdown" import markdown from "@lib/render-markdown"
@ -17,6 +17,7 @@ app.use("/posts", posts)
app.use("/users", users) app.use("/users", users)
app.use("/files", files) app.use("/files", files)
app.use("/admin", admin) app.use("/admin", admin)
app.use("/health", health)
app.get("/welcome", secretKey, (req, res) => { app.get("/welcome", secretKey, (req, res) => {
const introContent = process.env.WELCOME_CONTENT const introContent = process.env.WELCOME_CONTENT

View file

@ -30,7 +30,7 @@ const config = (): Config => {
} }
const validNodeEnvs = (str: string | undefined) => { const validNodeEnvs = (str: string | undefined) => {
const valid = ["development", "production"] const valid = ["development", "production", "test"]
if (str && !valid.includes(str)) { if (str && !valid.includes(str)) {
throw new Error(`Invalid NODE_ENV set: ${str}`) throw new Error(`Invalid NODE_ENV set: ${str}`)
} else if (!str) { } else if (!str) {

View file

@ -0,0 +1,9 @@
import { Router } from "express"
export const health = Router()
health.get("/", async (req, res) => {
return res.json({
status: "UP"
})
})

View file

@ -3,3 +3,4 @@ export { posts } from "./posts"
export { users } from "./users" export { users } from "./users"
export { files } from "./files" export { files } from "./files"
export { admin } from "./admin" export { admin } from "./admin"
export { health } from "./health"

View file

@ -0,0 +1,4 @@
import * as dotenv from 'dotenv';
import * as path from 'path';
dotenv.config({ path: path.resolve(process.cwd(), '.env.test') });

File diff suppressed because it is too large Load diff