Skip to content

Commit

Permalink
[temp] test y tests fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-Style committed Feb 2, 2024
1 parent 19aa8eb commit df0711c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 3 additions & 23 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
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";
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);

Expand All @@ -33,22 +22,13 @@ 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);
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`);
});
2 changes: 1 addition & 1 deletion src/tests/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/');

Check notice

Code scanning / devskim

Accessing localhost could indicate debug code, or could hinder scaling. Note test

Do not leave debug code in production
serverStatus = response.status;
} catch (error) {
console.error(error);
Expand Down
41 changes: 20 additions & 21 deletions src/tests/write.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { AxiosError } from 'axios';
import fs from "fs";

Check failure on line 2 in src/tests/write.test.ts

View workflow job for this annotation

GitHub Actions / eslint

'fs' is defined but never used
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?");
Expand All @@ -27,67 +27,67 @@ 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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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()}`;
const dirPath = path.resolve(__dirname, '../../dist/data/');
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&timestamp=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&timestamp=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();
Expand All @@ -109,7 +109,7 @@ describe("GET /write", () => {
return new Promise<void>(done => {
// Increase the timeout for this test
setTimeout(async () => {
await callServer(undefined, "user=xx&lat=52.51627&lon=13.37770&timestamp=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&timestamp=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);
Expand Down Expand Up @@ -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&timestamp=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&timestamp=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)}&timestamp=${new Date().getTime()}&hdop=${(25 * Math.random()).toFixed(3)}&altitude=${i}&speed=88.888&heading=${(360 * Math.random()).toFixed(3)}&key=test`;
Expand All @@ -196,5 +196,4 @@ describe('API calls', () => {
const jsonData = getData(filePath);
expect(jsonData.entries.length).toBeLessThanOrEqual(1000);
});

});
}); */

0 comments on commit df0711c

Please sign in to comment.