From 8873654b0394929394400f62b4eb16514997ee98 Mon Sep 17 00:00:00 2001 From: Type-Style Date: Fri, 26 Jan 2024 16:46:25 +0100 Subject: [PATCH] [Change] #35 relocated tests and refactor write, also added file check --- src/controller/write.test.ts | 100 ---------------------------- src/controller/write.ts | 2 - src/scripts/file.ts | 1 - src/{ => tests}/app.test.ts | 2 +- src/{models => tests}/entry.test.ts | 2 +- src/tests/write.test.ts | 87 ++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 105 deletions(-) delete mode 100644 src/controller/write.test.ts rename src/{ => tests}/app.test.ts (81%) rename src/{models => tests}/entry.test.ts (97%) create mode 100644 src/tests/write.test.ts diff --git a/src/controller/write.test.ts b/src/controller/write.test.ts deleted file mode 100644 index f447d22..0000000 --- a/src/controller/write.test.ts +++ /dev/null @@ -1,100 +0,0 @@ -import axios, { AxiosError } from 'axios'; - -describe('HEAD /write', () => { - it('with all parameters correctly set it should succeed', async () => { - const timestamp = new Date().getTime(); - const response = await axios.head(`http://localhost/write?user=xx&lat=45.000&lon=90.000×tamp=${timestamp}&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test`); - expect(response.status).toBe(200); - }); - - it('without key it sends 403', async () => { - try { - const timestamp = new Date().getTime(); - await axios.head(`http://localhost/write?user=xx&lat=45.000&lon=90.000×tamp=${timestamp}&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0`); - } catch (error) { - const axiosError = error as AxiosError; - expect(axiosError.response!.status).toBe(403); - } - }); - - it('with user length not equal to 2 it sends 422', async () => { - try { - const timestamp = new Date().getTime(); - await axios.head(`http://localhost/write?user=x&lat=45.000&lon=90.000×tamp=${timestamp}&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test`); - } catch (error) { - const axiosError = error as AxiosError; - expect(axiosError.response!.status).toBe(422); - } - }); - - - it('with lat not between -90 and 90 it sends 422', async () => { - try { - const timestamp = new Date().getTime(); - await axios.head(`http://localhost/write?user=xx&lat=91.000&lon=90.000×tamp=${timestamp}&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test`); - } catch (error) { - const axiosError = error as AxiosError; - expect(axiosError.response!.status).toBe(422); - } - }); - - it('with lon not between -180 and 180 it sends 422', async () => { - try { - const timestamp = new Date().getTime(); - await axios.head(`http://localhost/write?user=xx&lat=45.000&lon=181.000×tamp=${timestamp}&hdop=50.0&altitude=5000.000&speed=150.000&heading=180.0&key=test`); - } catch (error) { - const axiosError = error as AxiosError; - expect(axiosError.response!.status).toBe(422); - } - }); - - it('with timestamp to old sends 422', async () => { - try { - const timestamp = new Date().getTime() - 24 * 60 * 60 * 1000 * 2; // two days ago - await axios.head(`http://localhost/write?user=xx&lat=45.000&lon=90.000×tamp=${timestamp}&hdop=101.0&altitude=5000.000&speed=150.000&heading=180.0&key=test`); - } catch (error) { - const axiosError = error as AxiosError; - expect(axiosError.response!.status).toBe(422); - } - }) - - it('with hdop not between 0 and 100 it sends 422', async () => { - try { - const timestamp = new Date().getTime(); - await axios.head(`http://localhost/write?user=xx&lat=45.000&lon=90.000×tamp=${timestamp}&hdop=101.0&altitude=5000.000&speed=150.000&heading=180.0&key=test`); - } catch (error) { - const axiosError = error as AxiosError; - expect(axiosError.response!.status).toBe(422); - } - }); - - it('with altitude not between 0 and 10000 it sends 422', async () => { - try { - const timestamp = new Date().getTime(); - await axios.head(`http://localhost/write?user=xx&lat=45.000&lon=90.000×tamp=${timestamp}&hdop=50.0&altitude=10001.000&speed=150.000&heading=180.0&key=test`); - } catch (error) { - const axiosError = error as AxiosError; - expect(axiosError.response!.status).toBe(422); - } - }); - - it('with speed not between 0 and 300 it sends 422', async () => { - try { - const timestamp = new Date().getTime(); - await axios.head(`http://localhost/write?user=xx&lat=45.000&lon=90.000×tamp=${timestamp}&hdop=50.0&altitude=5000.000&speed=301.000&heading=180.0&key=test`); - } catch (error) { - const axiosError = error as AxiosError; - expect(axiosError.response!.status).toBe(422); - } - }); - - it('with heading not between 0 and 360 it sends 422', async () => { - try { - const timestamp = new Date().getTime(); - await axios.head(`http://localhost/write?user=xx&lat=45.000&lon=90.000×tamp=${timestamp}&hdop=50.0&altitude=5000.000&speed=150.000&heading=361.0&key=test`); - } catch (error) { - const axiosError = error as AxiosError; - expect(axiosError.response!.status).toBe(422); - } - }); -}); diff --git a/src/controller/write.ts b/src/controller/write.ts index 4bf9a09..4b61d30 100644 --- a/src/controller/write.ts +++ b/src/controller/write.ts @@ -24,8 +24,6 @@ function errorChecking (req:Request, res:Response, next:NextFunction) { // Regular Save logic from here entry.create(req, res, next); - - res.send(req.query); } diff --git a/src/scripts/file.ts b/src/scripts/file.ts index a7ba192..75040a0 100644 --- a/src/scripts/file.ts +++ b/src/scripts/file.ts @@ -20,7 +20,6 @@ export const createFile = (res: Response, next: NextFunction): File.Obj => { if (!fs.existsSync(filePath)) { // check if file exist fileExisted = false; try { - // fs.appendFileSync(filePath, 'test'); fs.writeFileSync(filePath, ''); } catch (err) { createError(res, 500, "File cannot be written to", next); diff --git a/src/app.test.ts b/src/tests/app.test.ts similarity index 81% rename from src/app.test.ts rename to src/tests/app.test.ts index 0682f2d..453caf4 100644 --- a/src/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'); + const response = await axios.get('http://localhost:80'); serverStatus = response.status; } catch (error) { console.error(error); diff --git a/src/models/entry.test.ts b/src/tests/entry.test.ts similarity index 97% rename from src/models/entry.test.ts rename to src/tests/entry.test.ts index bd2c0b4..48058e7 100644 --- a/src/models/entry.test.ts +++ b/src/tests/entry.test.ts @@ -1,4 +1,4 @@ -import { checkNumber, checkTime } from "./entry"; +import { checkNumber, checkTime } from "../models/entry"; describe("checkNumber", () => { diff --git a/src/tests/write.test.ts b/src/tests/write.test.ts new file mode 100644 index 0000000..2a61ef8 --- /dev/null +++ b/src/tests/write.test.ts @@ -0,0 +1,87 @@ +import axios, { AxiosError } from 'axios'; +import fs from "fs"; +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?"); + url.search = "?" + query; + const params = new URLSearchParams(url.search); + params.set("timestamp", timestamp.toString()); + url.search = params.toString(); + + let response; + if (expectStatus == 200) { + if (method == "GET") { + response = await axios.get(url.toString()); + } else { + response = await axios.head(url.toString()); + } + expect(response.status).toBe(expectStatus); + } else { + try { + await axios.head(url.toString()); + } catch (error) { + const axiosError = error as AxiosError; + expect(axiosError.response!.status).toBe(expectStatus); + } + } +} + +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); + }); + + 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); + }); + + 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); + }); + + 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); + }); + + 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); + }); + + 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); + }) + + 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); + }); + + 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); + }); + + 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); + }); + + 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); + }); +}); + + +describe("GET /write", () => { + it('there should a file of the current date', async () => { + 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, "GET"); + + 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`); + console.log(filePath); + fs.access(filePath, fs.constants.F_OK, (err) => { + expect(err).toBeFalsy(); + }); + }); +}); \ No newline at end of file