From 8302c83cac7dda43d49dffe33d5d3ba8e9349d19 Mon Sep 17 00:00:00 2001 From: Type-Style Date: Tue, 19 Mar 2024 15:17:20 +0100 Subject: [PATCH] [Task] #43 code cleanup --- .github/workflows/main.yml | 15 +- package.json | 6 +- src/app.ts | 3 +- src/controller/read.ts | 1 - src/tests/integration.test.ts | 310 ++++++++++++++++++++ src/tests/integration.test_.ts | 307 ------------------- src/tests/{login.test_.ts => login.test.ts} | 0 src/tests/{entry.test.ts => unit.test.ts} | 4 +- 8 files changed, 328 insertions(+), 318 deletions(-) create mode 100644 src/tests/integration.test.ts delete mode 100644 src/tests/integration.test_.ts rename src/tests/{login.test_.ts => login.test.ts} (100%) rename src/tests/{entry.test.ts => unit.test.ts} (96%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 09c7f0c..af2410e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,7 +10,9 @@ on: jobs: build: runs-on: ubuntu-latest - + environment: dev + env: + NODE_ENV: ${{ vars.NODE_ENV }} steps: - uses: actions/checkout@v3 @@ -19,14 +21,17 @@ jobs: with: node-version: '20' cache: 'npm' + - run: echo "NODE_ENV = $NODE_ENV" - run: npm ci - run: npm run build --if-present - name: Start server run: | - sudo npm start & - sleep 8 # Give server some time to start + sudo NODE_ENV=$NODE_ENV npm start & + sleep 15 # Give server some time to start - name: Check if server is running run: | curl --fail http://localhost:80 || exit 1 - - name: Run tests - run: npm run test \ No newline at end of file + - name: Run app tests + run: npm run test:app + + \ No newline at end of file diff --git a/package.json b/package.json index 6d5ba9d..d42439d 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,11 @@ "dev:static": "nodemon --config nodemon-static.json", "lint": "eslint . --fix", "lint:client": "eslint httpdocs/js/ --fix", - "test": "jest" + "test": "jest", + "test:app": "jest src/tests/app.test.ts", + "test:login": "jest src/tests/login.test.ts", + "test:unit": "jest src/tests/unit.test.ts", + "test:integration": "jest src/tests/integration.test.ts" }, "keywords": [], "author": "Type-Style", diff --git a/src/app.ts b/src/app.ts index 307d4c2..ce1edff 100644 --- a/src/app.ts +++ b/src/app.ts @@ -52,8 +52,7 @@ app.use((req, res, next) => { // limit body for specific http methods // routes app.get('/', (req, res) => { logger.log(req.ip + " - " + res.locals.ip, true); - console.count(); - res.send('Hello World, via TypeScript and Node.js! ' + res.locals.ip); + res.send('Hello World, via TypeScript and Node.js! ' + `ENV: ${process.env.NODE_ENV}`); }); app.use('/write', writeRouter); diff --git a/src/controller/read.ts b/src/controller/read.ts index 62ea931..a5e7a6e 100644 --- a/src/controller/read.ts +++ b/src/controller/read.ts @@ -44,7 +44,6 @@ router.get("/login/", baseSlowDown, baseRateLimiter, async function login(req: R router.post("/login/", loginSlowDown, async function postLogin(req: Request, res: Response, next: NextFunction) { logger.log(req.body); - res.locals.text = "post recieved"; loginLimiter(req, res, async () => { let validLogin = false; const user = req.body.user; diff --git a/src/tests/integration.test.ts b/src/tests/integration.test.ts new file mode 100644 index 0000000..4656558 --- /dev/null +++ b/src/tests/integration.test.ts @@ -0,0 +1,310 @@ +import axios, { AxiosError } from 'axios'; +import qs from 'qs'; +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; + if (axiosError.response) { + expect(axiosError.response.status).toBe(expectStatus); + } else { + console.error(axiosError); + } + } + } +} + + +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; +} + +async function verifiedRequest(url: string, token: string) { + const response = await axios({ + method: 'get', + url: url, + headers: { + 'Authorization': `Bearer ${token}`, + } + }); + return response; +} + + + +describe('HEAD /write', () => { + // eslint-disable-next-line jest/expect-expect + it('with all parameters correctly set it should succeed', 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); + }); + + // eslint-disable-next-line jest/expect-expect + it('without key it sends 403', 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", 403); + }); + + // eslint-disable-next-line jest/expect-expect + it('with user length not equal to 2 it sends 422', async () => { + 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); + }); + + // eslint-disable-next-line jest/expect-expect + it('with lat not between -90 and 90 it sends 422', async () => { + 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); + }); + + // eslint-disable-next-line jest/expect-expect + it('with lon not between -180 and 180 it sends 422', async () => { + 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); + }); + + // eslint-disable-next-line jest/expect-expect + it('with timestamp to old sends 422', async () => { + const timestamp = new Date().getTime() - 24 * 60 * 60 * 1000 * 2; // two days ago + 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); + }) + + // eslint-disable-next-line jest/expect-expect + it('with hdop not between 0 and 100 it sends 422', async () => { + 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); + }); + + // eslint-disable-next-line jest/expect-expect + it('with altitude not between 0 and 10000 it sends 422', async () => { + 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); + }); + + // eslint-disable-next-line jest/expect-expect + it('with speed not between 0 and 300 it sends 422', async () => { + 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); + }); + + // eslint-disable-next-line jest/expect-expect + it('with heading not between 0 and 360 it sends 422', async () => { + 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()}`; +// 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 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(); +// }); +// }); + +// it('the file contains valid JSON', async () => { +// fs.readFile(filePath, 'utf8', (err, data) => { +// expect(err).toBeFalsy(); +// try { +// JSON.parse(data); +// } catch (e) { +// expect(e).toBeFalsy(); +// } +// }); +// }); + +// it('after second call and the JSON entries length is 2', () => { +// return new Promise(done => { +// setTimeout(async () => { +// 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); + +// done(); +// }, 3500); +// }) +// }); + +// it('the time is correct', () => { +// const jsonData = getData(filePath); +// const entry = jsonData.entries.at(-1) + +// expect(entry.time.created).toBeGreaterThan(date.getTime()); +// expect(entry.time.diff).toBeGreaterThan(3.5); +// expect(entry.time.diff).toBeLessThan(4.6); + + +// const germanDayPattern = "(Montag|Dienstag|Mittwoch|Donnerstag|Freitag|Samstag|Sonntag)"; +// const dayOfMonthPattern = "(0?[1-9]|[12][0-9]|3[01])"; +// const germanMonthPattern = "(Januar|Februar|März|April|Mai|Juni|Juli|August|September|Oktober|November|Dezember)"; +// const yearPattern = "(\\d{4})"; +// const timePattern = "([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]"; +// const pattern = new RegExp(`^${germanDayPattern}, ${dayOfMonthPattern}. ${germanMonthPattern} ${yearPattern} um ${timePattern}$`); +// const string = entry.time.createdString; +// expect(pattern.test(string)).toBeTruthy(); + +// }); + +// it('the distance is correct', () => { +// const jsonData = getData(filePath); +// const entry = jsonData.entries.at(-1) + +// expect(entry.distance.horizontal).toBeCloseTo(1813.926); +// expect(entry.distance.vertical).toBe(-1000); +// expect(entry.distance.total).toBeCloseTo(2071.311); +// }); + +// it('the angle is correct', () => { +// const jsonData = getData(filePath); +// const entry = jsonData.entries.at(-1) + +// expect(entry.angle).toBeCloseTo(83.795775); +// }); + +// it('the speed is correct', () => { +// const jsonData = getData(filePath); +// const entry = jsonData.entries.at(-1) + +// expect(isInRange(entry.speed.horizontal, 515, 10)).toBe(true); +// expect(isInRange(entry.speed.vertical, -284, 10)).toBe(true); +// expect(isInRange(entry.speed.total, 588, 15)).toBe(true); +// }); + +// it('check ignore', async () => { +// let jsonData = getData(filePath); +// let entry = jsonData.entries[1]; +// const lastEntry = jsonData.entries[0]; + +// 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 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', () => { +// 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`; +// const response = await axios.get(url); +// expect(response.status).toBe(200); +// } +// }, 20000); // adjust this to to fit your setup + +// test(`length of json should not exceed 1000`, async () => { +// 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`); +// const jsonData = getData(filePath); +// expect(jsonData.entries.length).toBeLessThanOrEqual(1000); +// }); +// }); + + +// describe('read and login', () => { +// let token = ""; +// const testData = qs.stringify({ +// user: "test", +// password: "test", +// }); +// test(`redirect without logged in`, async () => { +// try { +// await axios.get("http://localhost:80/read/"); +// } catch (error) { +// const axiosError = error as AxiosError; +// if (axiosError.response) { +// expect(axiosError.response.status).toBe(401); +// } else { +// console.error(axiosError); +// } +// } +// }); + +// it('test user can login', async () => { +// const response = await axios.post('http://localhost:80/read/login', testData); + +// expect(response.status).toBe(200); +// expect(response.headers['content-type']).toEqual(expect.stringContaining('application/json')); +// expect(response).toHaveProperty('data.token'); +// expect(response.data.token).not.toBeNull(); +// token = response.data.token; +// }) + +// test('wrong token get error', async () => { +// try { +// await verifiedRequest("http://localhost:80/read?index=0", "justWrongValue"); +// } catch (error) { +// const axiosError = error as AxiosError; +// if (axiosError.response) { +// expect(axiosError.response.status).toBe(403); +// } else { +// console.error(axiosError); +// } +// } +// }); + +// test('verified request returns json', async () => { +// const response = await verifiedRequest("http://localhost:80/read?index=0", token); +// expect(response.status).toBe(200); +// expect(response.headers['content-type']).toEqual(expect.stringContaining('application/json')); +// }); + +// test(`index parameter to long`, async () => { +// try { +// await verifiedRequest("http://localhost:80/read?index=1234", token); +// } catch (error) { +// const axiosError = error as AxiosError; +// if (axiosError.response) { +// expect(axiosError.response.status).toBe(400); +// } else { +// console.error(axiosError); +// } +// } +// }); + +// test(`index parameter to be a number`, async () => { +// try { +// await verifiedRequest("http://localhost:80/read?index=a9", token); +// } catch (error) { +// const axiosError = error as AxiosError; +// if (axiosError.response) { +// expect(axiosError.response.status).toBe(400); +// } else { +// console.error(axiosError); +// } +// } +// }); +// test(`index parameter reduces length of json`, async () => { +// const response = await verifiedRequest("http://localhost:80/read?index=999", token); +// expect(response.status).toBe(200); +// expect(response.data.entries.length).toBe(1); +// }); +// }); \ No newline at end of file diff --git a/src/tests/integration.test_.ts b/src/tests/integration.test_.ts deleted file mode 100644 index 5bb4f57..0000000 --- a/src/tests/integration.test_.ts +++ /dev/null @@ -1,307 +0,0 @@ -import axios, { AxiosError } from 'axios'; -import qs from 'qs'; -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; - if (axiosError.response) { - expect(axiosError.response.status).toBe(expectStatus); - } else { - console.error(axiosError); - } - } - } -} - - -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; -} - -async function verifiedRequest(url: string, token: string) { - const response = await axios({ - method: 'get', - url: url, - headers: { - 'Authorization': `Bearer ${token}`, - } - }); - return response; -} - -describe('HEAD /write', () => { - // eslint-disable-next-line jest/expect-expect - it('with all parameters correctly set it should succeed', 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); - }); - - // eslint-disable-next-line jest/expect-expect - it('without key it sends 403', 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", 403); - }); - - // eslint-disable-next-line jest/expect-expect - it('with user length not equal to 2 it sends 422', async () => { - 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); - }); - - // eslint-disable-next-line jest/expect-expect - it('with lat not between -90 and 90 it sends 422', async () => { - 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); - }); - - // eslint-disable-next-line jest/expect-expect - it('with lon not between -180 and 180 it sends 422', async () => { - 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); - }); - - // eslint-disable-next-line jest/expect-expect - it('with timestamp to old sends 422', async () => { - const timestamp = new Date().getTime() - 24 * 60 * 60 * 1000 * 2; // two days ago - 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); - }) - - // eslint-disable-next-line jest/expect-expect - it('with hdop not between 0 and 100 it sends 422', async () => { - 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); - }); - - // eslint-disable-next-line jest/expect-expect - it('with altitude not between 0 and 10000 it sends 422', async () => { - 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); - }); - - // eslint-disable-next-line jest/expect-expect - it('with speed not between 0 and 300 it sends 422', async () => { - 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); - }); - - // eslint-disable-next-line jest/expect-expect - it('with heading not between 0 and 360 it sends 422', async () => { - 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()}`; - 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 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(); - }); - }); - - it('the file contains valid JSON', async () => { - fs.readFile(filePath, 'utf8', (err, data) => { - expect(err).toBeFalsy(); - try { - JSON.parse(data); - } catch (e) { - expect(e).toBeFalsy(); - } - }); - }); - - it('after second call and the JSON entries length is 2', () => { - return new Promise(done => { - setTimeout(async () => { - 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); - - done(); - }, 3500); - }) - }); - - it('the time is correct', () => { - const jsonData = getData(filePath); - const entry = jsonData.entries.at(-1) - - expect(entry.time.created).toBeGreaterThan(date.getTime()); - expect(entry.time.diff).toBeGreaterThan(3.5); - expect(entry.time.diff).toBeLessThan(4.6); - - - const germanDayPattern = "(Montag|Dienstag|Mittwoch|Donnerstag|Freitag|Samstag|Sonntag)"; - const dayOfMonthPattern = "(0?[1-9]|[12][0-9]|3[01])"; - const germanMonthPattern = "(Januar|Februar|März|April|Mai|Juni|Juli|August|September|Oktober|November|Dezember)"; - const yearPattern = "(\\d{4})"; - const timePattern = "([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]"; - const pattern = new RegExp(`^${germanDayPattern}, ${dayOfMonthPattern}. ${germanMonthPattern} ${yearPattern} um ${timePattern}$`); - const string = entry.time.createdString; - expect(pattern.test(string)).toBeTruthy(); - - }); - - it('the distance is correct', () => { - const jsonData = getData(filePath); - const entry = jsonData.entries.at(-1) - - expect(entry.distance.horizontal).toBeCloseTo(1813.926); - expect(entry.distance.vertical).toBe(-1000); - expect(entry.distance.total).toBeCloseTo(2071.311); - }); - - it('the angle is correct', () => { - const jsonData = getData(filePath); - const entry = jsonData.entries.at(-1) - - expect(entry.angle).toBeCloseTo(83.795775); - }); - - it('the speed is correct', () => { - const jsonData = getData(filePath); - const entry = jsonData.entries.at(-1) - - expect(isInRange(entry.speed.horizontal, 515, 10)).toBe(true); - expect(isInRange(entry.speed.vertical, -284, 10)).toBe(true); - expect(isInRange(entry.speed.total, 588, 15)).toBe(true); - }); - - it('check ignore', async () => { - let jsonData = getData(filePath); - let entry = jsonData.entries[1]; - const lastEntry = jsonData.entries[0]; - - 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 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', () => { - 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`; - const response = await axios.get(url); - expect(response.status).toBe(200); - } - }, 20000); // adjust this to to fit your setup - - test(`length of json should not exceed 1000`, async () => { - 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`); - const jsonData = getData(filePath); - expect(jsonData.entries.length).toBeLessThanOrEqual(1000); - }); -}); - - -describe('read and login', () => { - let token = ""; - const testData = qs.stringify({ - user: "test", - password: "test", - }); - test(`redirect without logged in`, async () => { - try { - await axios.get("http://localhost:80/read/"); - } catch (error) { - const axiosError = error as AxiosError; - if (axiosError.response) { - expect(axiosError.response.status).toBe(401); - } else { - console.error(axiosError); - } - } - }); - - it('test user can login', async () => { - const response = await axios.post('http://localhost:80/read/login', testData); - - expect(response.status).toBe(200); - expect(response.headers['content-type']).toEqual(expect.stringContaining('application/json')); - expect(response).toHaveProperty('data.token'); - expect(response.data.token).not.toBeNull(); - token = response.data.token; - }) - - test('wrong token get error', async () => { - try { - await verifiedRequest("http://localhost:80/read?index=0", "justWrongValue"); - } catch (error) { - const axiosError = error as AxiosError; - if (axiosError.response) { - expect(axiosError.response.status).toBe(403); - } else { - console.error(axiosError); - } - } - }); - - test('verified request returns json', async () => { - const response = await verifiedRequest("http://localhost:80/read?index=0", token); - expect(response.status).toBe(200); - expect(response.headers['content-type']).toEqual(expect.stringContaining('application/json')); - }); - - test(`index parameter to long`, async () => { - try { - await verifiedRequest("http://localhost:80/read?index=1234", token); - } catch (error) { - const axiosError = error as AxiosError; - if (axiosError.response) { - expect(axiosError.response.status).toBe(400); - } else { - console.error(axiosError); - } - } - }); - - test(`index parameter to be a number`, async () => { - try { - await verifiedRequest("http://localhost:80/read?index=a9", token); - } catch (error) { - const axiosError = error as AxiosError; - if (axiosError.response) { - expect(axiosError.response.status).toBe(400); - } else { - console.error(axiosError); - } - } - }); - test(`index parameter reduces length of json`, async () => { - const response = await verifiedRequest("http://localhost:80/read?index=999", token); - expect(response.status).toBe(200); - expect(response.data.entries.length).toBe(1); - }); -}); \ No newline at end of file diff --git a/src/tests/login.test_.ts b/src/tests/login.test.ts similarity index 100% rename from src/tests/login.test_.ts rename to src/tests/login.test.ts diff --git a/src/tests/entry.test.ts b/src/tests/unit.test.ts similarity index 96% rename from src/tests/entry.test.ts rename to src/tests/unit.test.ts index 48058e7..013a1b4 100644 --- a/src/tests/entry.test.ts +++ b/src/tests/unit.test.ts @@ -1,7 +1,7 @@ import { checkNumber, checkTime } from "../models/entry"; -describe("checkNumber", () => { +describe("entry checkNumber", () => { it("should throw error if value is not provided", () => { expect(() => checkNumber(0, 100)("")).toThrow(new Error('is required')); }); @@ -19,7 +19,7 @@ describe("checkNumber", () => { }); }); -describe("checkTime", () => { +describe("entry checkTime", () => { it("should throw error if value is not a number", () => { expect(() => checkTime("abc")).toThrow(new Error('Timestamp should be a number')); });