Skip to content

Commit

Permalink
[Task] #35 add test for protected webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-Style committed Jan 24, 2024
1 parent 2363c06 commit 1f34521
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
28 changes: 19 additions & 9 deletions src/controller/write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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&timestamp=${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&timestamp=${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&timestamp=${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&timestamp=${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&timestamp=${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);
Expand All @@ -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&timestamp=${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&timestamp=${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);
Expand All @@ -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&timestamp=${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&timestamp=${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);
Expand All @@ -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&timestamp=${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&timestamp=${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);
Expand All @@ -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&timestamp=${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&timestamp=${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);
Expand All @@ -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&timestamp=${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&timestamp=${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);
Expand All @@ -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&timestamp=${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&timestamp=${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);
Expand All @@ -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&timestamp=${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&timestamp=${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);
Expand Down
10 changes: 7 additions & 3 deletions src/models/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 1f34521

Please sign in to comment.