diff --git a/.eslintrc.json b/.eslintrc.json index 85ef0ab..30be6b6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -6,15 +6,22 @@ "sourceType": "module", "project": "tsconfig.json" }, - "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended", + "plugin:jest/recommended" + ], "env": { "node": true, "jest/globals": true }, + "plugins": ["jest", "@typescript-eslint"], "rules": { - //'no-console': 'off', - //'import/prefer-default-export': 'off', - "@typescript-eslint/no-unused-vars": "warn" + //"no-console": "off", + //"import/prefer-default-export": "off", + //"@typescript-eslint/no-unused-vars": "warn" + "jest/no-conditional-expect": "off" }, - "ignorePatterns": ["dist"] + "ignorePatterns": ["dist", "jest.config.js"] + } diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 016b1ae..a464e73 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,5 +18,9 @@ jobs: - uses: actions/checkout@v3 - run: npm ci - run: npm run build:prod --if-present - - run: npm start + - name: Start server + run: | + npm start & + sleep 5 # Give server some time to start + - run: npm test diff --git a/src/app.test.ts b/src/app.test.ts index 836d785..0682f2d 100644 --- a/src/app.test.ts +++ b/src/app.test.ts @@ -2,7 +2,7 @@ import axios from 'axios'; describe('Server Status', () => { it('The server is running', async () => { - var serverStatus; + let serverStatus; try { const response = await axios.get('http://localhost'); serverStatus = response.status; diff --git a/src/controller/write.ts b/src/controller/write.ts index 458fe4a..b62fd57 100644 --- a/src/controller/write.ts +++ b/src/controller/write.ts @@ -10,7 +10,7 @@ function errorChecking (req:Request, res:Response, next:NextFunction) { const errorAsString = new Error(JSON.stringify(errorAsJson)); const hasKeyErrors = errors.array().some(error => error.msg.includes("Key")); - res.status(hasKeyErrors ? 403 : 422); // send forbidden or + res.status(hasKeyErrors ? 403 : 422); // send forbidden or unprocessable content return next(errorAsString); } @@ -23,7 +23,7 @@ function errorChecking (req:Request, res:Response, next:NextFunction) { //entry.create(req, res); //const test = process.env.TEST; - res.send(req.query); + // res.send(req.query); } diff --git a/src/error.ts b/src/error.ts index 086caff..0b0bc04 100644 --- a/src/error.ts +++ b/src/error.ts @@ -1,6 +1,4 @@ import { Request, Response, NextFunction } from "express"; -import logger from '@src/scripts/logger'; - export function notFound(req: Request, res: Response, next: NextFunction) { res.status(404); @@ -11,11 +9,14 @@ export function notFound(req: Request, res: Response, next: NextFunction) { export function handler(err: Error, req: Request, res: Response, next: NextFunction) { const statusCode = res.statusCode !== 200 ? res.statusCode : 500; res.status(statusCode); - let message = err.message; + + let message; try { - let jsonMessage = JSON.parse(message); + const jsonMessage = JSON.parse(err.message); message = jsonMessage; - } catch (e) {} + } catch (e) { + message = err.message; + } const responseBody = { status: statusCode, diff --git a/src/scripts/crypt.ts b/src/scripts/crypt.ts index 6be4dce..b14ef42 100644 --- a/src/scripts/crypt.ts +++ b/src/scripts/crypt.ts @@ -1,5 +1,9 @@ -const crypto = require('crypto'); +import * as crypto from 'crypto'; -export const crypt = function (value:string) { - return crypto.createHmac('sha256', process.env.KEYA).update(value).digest("base64"); +export const crypt = function (value:string) { + const key = process.env.KEYA; + if (!key) { + throw new Error('KEYA is not defined in the environment variables'); + } + return crypto.createHmac('sha256', key).update(value).digest("base64"); }; \ No newline at end of file