Skip to content

Commit 2ddeb6c

Browse files
committed
[Task] #43 code cleanup
1 parent 4ff8857 commit 2ddeb6c

7 files changed

Lines changed: 37 additions & 16 deletions

File tree

.github/workflows/main.yml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ on:
1010
jobs:
1111
build:
1212
runs-on: ubuntu-latest
13-
13+
env:
14+
NODE_ENV: ${{ vars.NODE_ENV }}
15+
LOCALHOST: ${{ vars.LOCALHOST }}
16+
LOCALHOSTV6: ${{ vars.LOCALHOSTV6 }}
17+
KEYA: ${{ secrets.KEYA }}
18+
KEYB: ${{ secrets.KEYB }}
19+
USER_TEST: ${{ secrets.USER_TEST }}
1420

1521
steps:
1622
- uses: actions/checkout@v3
@@ -19,14 +25,24 @@ jobs:
1925
with:
2026
node-version: '20'
2127
cache: 'npm'
28+
- run: echo "NODE_ENV = $NODE_ENV"
2229
- run: npm ci
2330
- run: npm run build --if-present
2431
- name: Start server
2532
run: |
26-
sudo npm start &
27-
sleep 8 # Give server some time to start
33+
sudo NODE_ENV=$NODE_ENV LOCALHOST=$LOCALHOST LOCALHOSTV6=$LOCALHOSTV6 KEYA=$KEYA KEYB=$KEYB USER_TEST=$USER_TEST npm start &
34+
sleep 15 # Give server some time to start
2835
- name: Check if server is running
2936
run: |
3037
curl --fail http://localhost:80 || exit 1
31-
- name: Run tests
32-
run: npm run test
38+
- name: Run app tests
39+
run: npm run test:app
40+
- name: Run login tests
41+
run: npm run test:login
42+
- name: Run unit tests
43+
run: npm run test:unit
44+
- name: Run integration tests
45+
run: npm run test:integration
46+
47+
48+

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
"dev:static": "nodemon --config nodemon-static.json",
1515
"lint": "eslint . --fix",
1616
"lint:client": "eslint httpdocs/js/ --fix",
17-
"test": "jest"
17+
"test": "jest",
18+
"test:app": "jest src/tests/app.test.ts",
19+
"test:login": "jest src/tests/login.test.ts",
20+
"test:unit": "jest src/tests/unit.test.ts",
21+
"test:integration": "jest src/tests/integration.test.ts"
1822
},
1923
"keywords": [],
2024
"author": "Type-Style",

src/app.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ app.use((req, res, next) => { // limit body for specific http methods
5252
// routes
5353
app.get('/', (req, res) => {
5454
logger.log(req.ip + " - " + res.locals.ip, true);
55-
console.count();
56-
res.send('Hello World, via TypeScript and Node.js! ' + res.locals.ip);
55+
res.send('Hello World, via TypeScript and Node.js! ' + `ENV: ${process.env.NODE_ENV}`);
5756
});
5857

5958
app.use('/write', writeRouter);

src/controller/read.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ router.get("/login/", baseSlowDown, baseRateLimiter, async function login(req: R
4444

4545
router.post("/login/", loginSlowDown, async function postLogin(req: Request, res: Response, next: NextFunction) {
4646
logger.log(req.body);
47-
res.locals.text = "post recieved";
4847
loginLimiter(req, res, async () => {
4948
let validLogin = false;
5049
const user = req.body.user;
@@ -67,7 +66,7 @@ router.post("/login/", loginSlowDown, async function postLogin(req: Request, res
6766
}
6867

6968
// only allow test user in test environment
70-
if (user == "test" && validLogin && process.env.NODE_ENV == "production") {
69+
if (user == "TEST" && validLogin && process.env.NODE_ENV == "production") {
7170
validLogin = false;
7271
}
7372

@@ -115,7 +114,7 @@ function validateToken(req: Request) {
115114
}
116115

117116
// don't allow test user in production environment
118-
if (typeof payload == "object" && payload.user == "test" && process.env.NODE_ENV == "production") {
117+
if (typeof payload == "object" && payload.user == "TEST" && process.env.NODE_ENV == "production") {
119118
return { success: false, status: 403, message: 'test user not allowed on production' };
120119
}
121120

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ async function callServer(timestamp = new Date().getTime(), query: string, expec
1010
params.set("timestamp", timestamp.toString());
1111
url.search = params.toString();
1212

13+
1314
let response;
1415
if (expectStatus == 200) {
1516
if (method == "GET") {
@@ -53,6 +54,8 @@ async function verifiedRequest(url: string, token: string) {
5354
return response;
5455
}
5556

57+
58+
5659
describe('HEAD /write', () => {
5760
// eslint-disable-next-line jest/expect-expect
5861
it('with all parameters correctly set it should succeed', async () => {
@@ -114,7 +117,7 @@ describe("GET /write", () => {
114117
const filePath = path.resolve(dirPath, `data-${formattedDate}.json`);
115118

116119
it('there should a file of the current date', async () => {
117-
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");
120+
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");
118121

119122
fs.access(filePath, fs.constants.F_OK, (err) => {
120123
expect(err).toBeFalsy();
@@ -198,7 +201,7 @@ describe("GET /write", () => {
198201
expect(entry.ignore).toBe(false); // current one to be false allways
199202
expect(lastEntry.ignore).toBe(true); // last one to high hdop to be true
200203

201-
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");
204+
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");
202205
jsonData = getData(filePath);
203206
entry = jsonData.entries[1]; // same data point, but not last now therefore ignore true
204207
expect(entry.ignore).toBe(true);
@@ -229,7 +232,7 @@ describe('API calls', () => {
229232
describe('read and login', () => {
230233
let token = "";
231234
const testData = qs.stringify({
232-
user: "test",
235+
user: "TEST",
233236
password: "test",
234237
});
235238
test(`redirect without logged in`, async () => {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { checkNumber, checkTime } from "../models/entry";
22

33

4-
describe("checkNumber", () => {
4+
describe("entry checkNumber", () => {
55
it("should throw error if value is not provided", () => {
66
expect(() => checkNumber(0, 100)("")).toThrow(new Error('is required'));
77
});
@@ -19,7 +19,7 @@ describe("checkNumber", () => {
1919
});
2020
});
2121

22-
describe("checkTime", () => {
22+
describe("entry checkTime", () => {
2323
it("should throw error if value is not a number", () => {
2424
expect(() => checkTime("abc")).toThrow(new Error('Timestamp should be a number'));
2525
});

0 commit comments

Comments
 (0)