From 8a681ed314ce3542bd080bc13901138d29e1ae96 Mon Sep 17 00:00:00 2001 From: Type-Style Date: Tue, 19 Mar 2024 15:17:20 +0100 Subject: [PATCH] [Task] #43 code cleanup --- .github/workflows/main.yml | 26 +++++++++++++++---- package.json | 6 ++++- src/app.ts | 3 +-- src/controller/read.ts | 5 ++-- src/models/entry.ts | 4 --- ...tegration.test_.ts => integration.test.ts} | 9 ++++--- src/tests/{login.test_.ts => login.test.ts} | 0 src/tests/{entry.test.ts => unit.test.ts} | 4 +-- 8 files changed, 37 insertions(+), 20 deletions(-) rename src/tests/{integration.test_.ts => integration.test.ts} (96%) rename src/tests/{login.test_.ts => login.test.ts} (100%) rename src/tests/{entry.test.ts => unit.test.ts} (96%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 09c7f0c..e31f256 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,7 +10,13 @@ on: jobs: build: runs-on: ubuntu-latest - + env: + NODE_ENV: ${{ vars.NODE_ENV }} + LOCALHOST: ${{ vars.LOCALHOST }} + LOCALHOSTV6: ${{ vars.LOCALHOSTV6 }} + KEYA: ${{ secrets.KEYA }} + KEYB: ${{ secrets.KEYB }} + USER_TEST: ${{ secrets.USER_TEST }} steps: - uses: actions/checkout@v3 @@ -19,14 +25,24 @@ 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 LOCALHOST=$LOCALHOST LOCALHOSTV6=$LOCALHOSTV6 KEYA=$KEYA KEYB=$KEYB USER_TEST=$USER_TEST npm start & + sleep 15 # Give server some time to start - name: Check if server is running run: | curl --fail http://localhost:80 || exit 1 - - name: Run tests - run: npm run test \ No newline at end of file + - name: Run app tests + run: npm run test:app + - name: Run login tests + run: npm run test:login + - name: Run unit tests + run: npm run test:unit + - name: Run integration tests + run: npm run test:integration + + + \ No newline at end of file diff --git a/package.json b/package.json index 6d5ba9d..d42439d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app.ts b/src/app.ts index 307d4c2..ce1edff 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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); diff --git a/src/controller/read.ts b/src/controller/read.ts index 62ea931..b04893d 100644 --- a/src/controller/read.ts +++ b/src/controller/read.ts @@ -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; @@ -67,7 +66,7 @@ router.post("/login/", loginSlowDown, async function postLogin(req: Request, res } // only allow test user in test environment - if (user == "test" && validLogin && process.env.NODE_ENV == "production") { + if (user == "TEST" && validLogin && process.env.NODE_ENV == "production") { validLogin = false; } @@ -115,7 +114,7 @@ function validateToken(req: Request) { } // don't allow test user in production environment - if (typeof payload == "object" && payload.user == "test" && process.env.NODE_ENV == "production") { + if (typeof payload == "object" && payload.user == "TEST" && process.env.NODE_ENV == "production") { return { success: false, status: 403, message: 'test user not allowed on production' }; } diff --git a/src/models/entry.ts b/src/models/entry.ts index 444a997..e7fc107 100644 --- a/src/models/entry.ts +++ b/src/models/entry.ts @@ -102,10 +102,6 @@ export function checkTime(value: string) { throw new Error('Timestamp should represent a valid date'); } - if (process.env.NODE_ENV == "development") { - return true; // dev testing convenience - } - const now = new Date(); const difference = now.getTime() - date.getTime(); const oneDayInMilliseconds = 24 * 60 * 60 * 1000; diff --git a/src/tests/integration.test_.ts b/src/tests/integration.test.ts similarity index 96% rename from src/tests/integration.test_.ts rename to src/tests/integration.test.ts index 5bb4f57..2e189e0 100644 --- a/src/tests/integration.test_.ts +++ b/src/tests/integration.test.ts @@ -10,6 +10,7 @@ async function callServer(timestamp = new Date().getTime(), query: string, expec params.set("timestamp", timestamp.toString()); url.search = params.toString(); + let response; if (expectStatus == 200) { if (method == "GET") { @@ -53,6 +54,8 @@ async function verifiedRequest(url: string, token: string) { return response; } + + describe('HEAD /write', () => { // eslint-disable-next-line jest/expect-expect it('with all parameters correctly set it should succeed', async () => { @@ -114,7 +117,7 @@ describe("GET /write", () => { const filePath = path.resolve(dirPath, `data-${formattedDate}.json`); it('there should a file of the current date', async () => { - await await callServer(undefined, "user=xx&lat=52.51451&lon=13.35105×tamp=R3Pl4C3&hdop=20.0&altitude=5000.000&speed=150.000&heading=180.0&key=test", 200, "GET"); + await callServer(undefined, "user=xx&lat=52.51451&lon=13.35105×tamp=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(); @@ -198,7 +201,7 @@ describe("GET /write", () => { 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×tamp=R3Pl4C3&hdop=50&altitude=4000.000&speed=150.000&heading=180.0&key=test", 200, "GET"); + await callServer(undefined, "user=xx&lat=52.51627&lon=13.37770×tamp=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); @@ -229,7 +232,7 @@ describe('API calls', () => { describe('read and login', () => { let token = ""; const testData = qs.stringify({ - user: "test", + user: "TEST", password: "test", }); test(`redirect without logged in`, async () => { diff --git a/src/tests/login.test_.ts b/src/tests/login.test.ts similarity index 100% rename from src/tests/login.test_.ts rename to src/tests/login.test.ts diff --git a/src/tests/entry.test.ts b/src/tests/unit.test.ts similarity index 96% rename from src/tests/entry.test.ts rename to src/tests/unit.test.ts index 48058e7..013a1b4 100644 --- a/src/tests/entry.test.ts +++ b/src/tests/unit.test.ts @@ -1,7 +1,7 @@ import { checkNumber, checkTime } from "../models/entry"; -describe("checkNumber", () => { +describe("entry checkNumber", () => { it("should throw error if value is not provided", () => { expect(() => checkNumber(0, 100)("")).toThrow(new Error('is required')); }); @@ -19,7 +19,7 @@ describe("checkNumber", () => { }); }); -describe("checkTime", () => { +describe("entry checkTime", () => { it("should throw error if value is not a number", () => { expect(() => checkTime("abc")).toThrow(new Error('Timestamp should be a number')); });