From 1f345217f728b13bfd8334089d6c008f8820c7ee Mon Sep 17 00:00:00 2001 From: Type-Style Date: Wed, 24 Jan 2024 11:01:55 +0100 Subject: [PATCH] [Task] #35 add test for protected webhook --- src/controller/write.test.ts | 28 +++++++++++++++++++--------- src/models/entry.ts | 10 +++++++--- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/controller/write.test.ts b/src/controller/write.test.ts index 55b6247..f447d22 100644 --- a/src/controller/write.test.ts +++ b/src/controller/write.test.ts @@ -3,14 +3,24 @@ 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`); + 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`); + 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); @@ -21,7 +31,7 @@ describe('HEAD /write', () => { 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`); + 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); @@ -31,7 +41,7 @@ describe('HEAD /write', () => { 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`); + 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); @@ -41,7 +51,7 @@ describe('HEAD /write', () => { 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`); + 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); @@ -51,7 +61,7 @@ describe('HEAD /write', () => { 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`); + 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); @@ -61,7 +71,7 @@ describe('HEAD /write', () => { 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`); + 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); @@ -71,7 +81,7 @@ describe('HEAD /write', () => { 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`); + 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); @@ -81,7 +91,7 @@ describe('HEAD /write', () => { 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`); + 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/models/entry.ts b/src/models/entry.ts index 97b03bd..9868b73 100644 --- a/src/models/entry.ts +++ b/src/models/entry.ts @@ -68,10 +68,14 @@ export function checkTime(value:string) { } async function checkKey(value:string) { - /* if (process.env.NODE_ENV != "production") { - return true; // dev testing convenience - } */ + if (process.env.NODE_ENV != "production" && value == "test") { + return true; // dev testing convenience + } + if (!value) { + throw new Error('Key required'); + } + const myEncryptPassword = await crypt.cryptPassword(value); console.log("key " + process.env.KEY + " - " + myEncryptPassword);