From df0711ce64f4d7e72798a62ff5c1ed5622443250 Mon Sep 17 00:00:00 2001 From: Type-Style Date: Fri, 2 Feb 2024 14:25:04 +0100 Subject: [PATCH] [temp] test y tests fail --- .github/workflows/build.yml | 2 +- package.json | 2 +- src/app.ts | 26 +++-------------------- src/tests/app.test.ts | 2 +- src/tests/write.test.ts | 41 ++++++++++++++++++------------------- 5 files changed, 26 insertions(+), 47 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4d0f1e0..f92eb6d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,5 +21,5 @@ jobs: - name: Start server run: | npm start & - sleep 5 # Give server some time to start + sleep 10 # Give server some time to start - run: npm test diff --git a/package.json b/package.json index 7e450e9..86d5cdc 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "start": "node dist/app.js", "dev": "rm -rf dist/* && cp -R httpdocs/ dist/ && nodemon src/app.ts", "lint": "eslint . --fix", - "test": "jest" + "test": "jest --runInBand" }, "keywords": [], "author": "Type-Style", diff --git a/src/app.ts b/src/app.ts index d7fc161..382c255 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,6 @@ require('module-alias/register'); import { config } from 'dotenv'; import express from 'express'; -import helmet from 'helmet'; import hpp from 'hpp'; import cache from './cache'; import * as error from "./error"; @@ -9,20 +8,10 @@ import writeRouter from '@src/controller/write'; import path from 'path'; import logger from '@src/scripts/logger'; + // configurations config(); const app = express(); -app.use( - helmet({ - contentSecurityPolicy: { - directives: { - "default-src": "'self'", - "img-src": "*" - } - } - }) -); - app.use(hpp()); app.use(cache); @@ -33,10 +22,7 @@ app.get('/', (req, res) => { app.use('/write', writeRouter); // use httpdocs as static folder -app.use('/', express.static(path.join(__dirname, 'httpdocs'), { - extensions: ['html', 'txt', "pdf"], - index: "start.html", -})) +app.use('/', express.static(path.join(__dirname, 'httpdocs'))) // error handling app.use(error.notFound); @@ -44,11 +30,5 @@ app.use(error.handler); // init server app.listen(80, () => { - logger.log(`Server running //localhost:80, ENV: ${process.env.NODE_ENV}`, true); -}); - -process.on('uncaughtException', function(err) { - console.error('Caught exception:', err); - logger.error(err); - process.exit(1); + logger.log(`Server running //localhost:80`); }); \ No newline at end of file diff --git a/src/tests/app.test.ts b/src/tests/app.test.ts index 453caf4..b7c01c2 100644 --- a/src/tests/app.test.ts +++ b/src/tests/app.test.ts @@ -4,7 +4,7 @@ describe('Server Status', () => { it('The server is running', async () => { let serverStatus; try { - const response = await axios.get('http://localhost:80'); + const response = await axios.get('http://localhost:80/'); serverStatus = response.status; } catch (error) { console.error(error); diff --git a/src/tests/write.test.ts b/src/tests/write.test.ts index 5db4a87..e5b4a29 100644 --- a/src/tests/write.test.ts +++ b/src/tests/write.test.ts @@ -1,6 +1,6 @@ import axios, { AxiosError } from 'axios'; import fs from "fs"; -import path from "path"; +// import path from "path"; async function callServer(timestamp = new Date().getTime(), query: string, expectStatus: number = 200, method: string = "HEAD") { const url = new URL("http://localhost:80/write?"); @@ -27,59 +27,59 @@ async function callServer(timestamp = new Date().getTime(), query: string, expec } } -function getData(filePath: string) { +/* function getData(filePath: string) { const data = fs.readFileSync(filePath); return JSON.parse(data.toString()); } function isInRange(actual: string | number, expected: number, range: number) { return Math.abs(Number(actual) - expected) <= range; -} +} */ describe('HEAD /write', () => { it('with all parameters correctly set it should succeed', async () => { - callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 200); + await callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 200); }); it('without key it sends 403', async () => { - callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0", 403); + await callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0", 403); }); it('with user length not equal to 2 it sends 422', async () => { - callServer(undefined, "user=x&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 422); + await callServer(undefined, "user=x&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 422); }); it('with lat not between -90 and 90 it sends 422', async () => { - callServer(undefined, "user=xx&lat=91.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 422); + await callServer(undefined, "user=xx&lat=91.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 422); }); it('with lon not between -180 and 180 it sends 422', async () => { - callServer(undefined, "user=xx&lat=45.000&lon=181.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 422); + await callServer(undefined, "user=xx&lat=45.000&lon=181.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 422); }); it('with timestamp to old sends 422', async () => { const timestamp = new Date().getTime() - 24 * 60 * 60 * 1000 * 2; // two days ago - callServer(timestamp, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 422); + await callServer(timestamp, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 422); }) it('with hdop not between 0 and 100 it sends 422', async () => { - callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=101.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 422); + await callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=101.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 422); }); it('with altitude not between 0 and 10000 it sends 422', async () => { - callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=10001.000&speed=150.000&heading=180.0&key=test", 422); + await callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=10001.000&speed=150.000&heading=180.0&key=test", 422); }); it('with speed not between 0 and 300 it sends 422', async () => { - callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=301.000&heading=180.0&key=test", 422); + await callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=301.000&heading=180.0&key=test", 422); }); it('with heading not between 0 and 360 it sends 422', async () => { - callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=361.0&key=test", 422); + await callServer(undefined, "user=xx&lat=45.000&lon=90.000×tamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=361.0&key=test", 422); }); }); - +/* describe("GET /write", () => { const date = new Date(); const formattedDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; @@ -87,7 +87,7 @@ describe("GET /write", () => { const filePath = path.resolve(dirPath, `data-${formattedDate}.json`); it('there should a file of the current date', async () => { - await callServer(undefined, "user=xx&lat=52.51451&lon=13.35105×tamp=R3Pl4C3&hdop=20.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 200, "GET"); + await await callServer(undefined, "user=xx&lat=52.51451&lon=13.35105×tamp=R3Pl4C3&hdop=20.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 200, "GET"); fs.access(filePath, fs.constants.F_OK, (err) => { expect(err).toBeFalsy(); @@ -109,7 +109,7 @@ describe("GET /write", () => { return new Promise(done => { // Increase the timeout for this test setTimeout(async () => { - await callServer(undefined, "user=xx&lat=52.51627&lon=13.37770×tamp=R3Pl4C3&hdop=50&altitude=4000.000&speed=150.000&heading=180.0&key=test", 200, "GET"); + await await callServer(undefined, "user=xx&lat=52.51627&lon=13.37770×tamp=R3Pl4C3&hdop=50&altitude=4000.000&speed=150.000&heading=180.0&key=test", 200, "GET"); const jsonData = getData(filePath); expect(jsonData.entries.length).toBe(2); @@ -172,14 +172,14 @@ describe("GET /write", () => { expect(entry.ignore).toBe(false); // current one to be false allways expect(lastEntry.ignore).toBe(true); // last one to high hdop to be true - await callServer(undefined, "user=xx&lat=52.51627&lon=13.37770×tamp=R3Pl4C3&hdop=50&altitude=4000.000&speed=150.000&heading=180.0&key=test", 200, "GET"); + await await callServer(undefined, "user=xx&lat=52.51627&lon=13.37770×tamp=R3Pl4C3&hdop=50&altitude=4000.000&speed=150.000&heading=180.0&key=test", 200, "GET"); jsonData = getData(filePath); entry = jsonData.entries[1]; // same data point, but not last now therefore ignore true expect(entry.ignore).toBe(true); }); -}); +}); */ -describe('API calls', () => { +/* describe('API calls', () => { test(`1000 api calls`, async () => { for (let i = 0; i < 1000; i++) { const url = `http://localhost:80/write?user=xx&lat=${(52 + Math.random()).toFixed(3)}&lon=${(13 + Math.random()).toFixed(3)}×tamp=${new Date().getTime()}&hdop=${(25 * Math.random()).toFixed(3)}&altitude=${i}&speed=88.888&heading=${(360 * Math.random()).toFixed(3)}&key=test`; @@ -196,5 +196,4 @@ describe('API calls', () => { const jsonData = getData(filePath); expect(jsonData.entries.length).toBeLessThanOrEqual(1000); }); - -}); \ No newline at end of file +}); */ \ No newline at end of file