Skip to content

Commit

Permalink
Merge pull request #148 from Type-Style/dev
Browse files Browse the repository at this point in the history
[Task] #138, eta can be 0
  • Loading branch information
Type-Style authored Sep 10, 2024
2 parents 5ce56b5 + 1bfab25 commit 46aa751
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/models/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import logger from '@src/scripts/logger';
export const entry = {
create: async (req: Request, res: Response, next: NextFunction) => {
const fileObj: File.Obj = file.getFile(res, next, "write");
if (!fileObj.content) { return createError(res, 500, "File does not exist: " + fileObj.path, next); }
if (!fileObj.content) { return createError(res, 500, "File does not exist: " + fileObj.path, next); }

fileObj.content = await file.readAsJson(res, fileObj.path, next);

if (!fileObj.content?.entries) {return createError(res, 500, "File Content unavailable: " + fileObj.path, next); }
if (!fileObj.content?.entries) { return createError(res, 500, "File Content unavailable: " + fileObj.path, next); }

const entries = fileObj.content.entries;
const lastEntry = fileObj.content.entries.at(-1);
let previousEntry = fileObj.content.entries.at(-1); // potentially overwritten if entry is set to ignore
Expand Down Expand Up @@ -52,7 +52,7 @@ export const entry = {
break;
}
}

if (previousEntry === fileObj.content.entries.at(-1)) {
logger.error("previousEntry was not replaced");
}
Expand Down Expand Up @@ -85,12 +85,12 @@ export const entry = {
query('user').isLength({ min: 2, max: 2 }),
query('lat').custom(checkNumber(-90, 90)),
query('lon').custom(checkNumber(-180, 180)),
query('timestamp').custom(checkTime),
query('timestamp').custom((value) => checkTime(value)),
query('hdop').custom(checkNumber(0, 100)),
query('altitude').custom(checkNumber(0, 10000)),
query('speed').custom(checkNumber(0, 300)),
query('heading').custom(checkNumber(0, 360, "integer")),
query('eta').optional().custom(checkTime),
query('eta').optional().custom((value) => checkTime(value, { allowZero: true })),
query('eda').optional().custom(checkNumber(0, 10000000)),
query("key").custom(checkKey),
checkExact()
Expand All @@ -115,8 +115,9 @@ export function checkNumber(min: number, max: number, type: string = "float") {
};
}

export function checkTime(value: string) {
export function checkTime(value: string, { allowZero = false } = {}) {
const timestamp = parseFloat(value);
if (allowZero && value === '0') { return true; }

// Check if it's a number
if (isNaN(timestamp)) {
Expand Down
5 changes: 5 additions & 0 deletions src/tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ describe('/write', () => {
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&eta=R3Pl4C3&key=test", 200, undefined, true);
});

// eslint-disable-next-line jest/expect-expect
it('eda & eta can be 0 it sends 200', async () => {
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&eta=0&eda=0key=test", 200, undefined);
});

// eslint-disable-next-line jest/expect-expect
it('with eda correct it sends 200', async () => {
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&eta=R3Pl4C3&eda=1000&key=test", 200, undefined, true);
Expand Down
2 changes: 2 additions & 0 deletions src/tests/login.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import axios, { AxiosError } from 'axios';
import qs from 'qs';

jest.setTimeout(10000);

const userDataLarge = qs.stringify({
user: "user",
password: "pass",
Expand Down

0 comments on commit 46aa751

Please sign in to comment.