Skip to content

Commit

Permalink
[Task] #43 code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-Style committed Mar 20, 2024
1 parent 4ff8857 commit 8302c83
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 318 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ on:
jobs:
build:
runs-on: ubuntu-latest

environment: dev
env:
NODE_ENV: ${{ vars.NODE_ENV }}

steps:
- uses: actions/checkout@v3
Expand All @@ -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

Check notice

Code scanning / devskim

Accessing localhost could indicate debug code, or could hinder scaling. Note

Do not leave debug code in production
- name: Run tests
run: npm run test
- name: Run app tests
run: npm run test:app


6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/controller/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
310 changes: 310 additions & 0 deletions src/tests/integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
import axios, { AxiosError } from 'axios';
import qs from 'qs';

Check failure on line 2 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

'qs' is defined but never used
import fs from "fs";
import path from "path";

Check failure on line 4 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

'path' is defined but never used

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) {

Check failure on line 37 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

'getData' is defined but never used
const data = fs.readFileSync(filePath);
return JSON.parse(data.toString());
}

function isInRange(actual: string | number, expected: number, range: number) {

Check failure on line 42 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

'isInRange' is defined but never used
return Math.abs(Number(actual) - expected) <= range;
}

async function verifiedRequest(url: string, token: string) {

Check failure on line 46 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

'verifiedRequest' is defined but never used
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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=R3Pl4C3&hdop=50.0&altitude=5000.000&speed=150.000&heading=361.0&key=test", 422);
});
});


// describe("GET /write", () => {

Check warning on line 113 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

Some tests seem to be commented
// 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 () => {

Check warning on line 119 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

Some tests seem to be commented
// await await callServer(undefined, "user=xx&lat=52.51451&lon=13.35105&timestamp=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 () => {

Check warning on line 127 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

Some tests seem to be commented
// 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', () => {

Check warning on line 138 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

Some tests seem to be commented
// return new Promise<void>(done => {
// setTimeout(async () => {
// await await callServer(undefined, "user=xx&lat=52.51627&lon=13.37770&timestamp=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', () => {

Check warning on line 151 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

Some tests seem to be commented
// 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', () => {

Check warning on line 171 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

Some tests seem to be commented
// 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', () => {

Check warning on line 180 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

Some tests seem to be commented
// const jsonData = getData(filePath);
// const entry = jsonData.entries.at(-1)

// expect(entry.angle).toBeCloseTo(83.795775);
// });

// it('the speed is correct', () => {

Check warning on line 187 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

Some tests seem to be commented
// 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 () => {

Check warning on line 196 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

Some tests seem to be commented
// 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&timestamp=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', () => {

Check warning on line 212 in src/tests/integration.test.ts

View workflow job for this annotation

GitHub Actions / eslint

Some tests seem to be commented
// 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)}&timestamp=${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);
// });
// });
Loading

0 comments on commit 8302c83

Please sign in to comment.