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 21, 2024
1 parent 4ff8857 commit d7da614
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 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
5 changes: 2 additions & 3 deletions 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 All @@ -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;
}

Expand Down Expand Up @@ -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' };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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&timestamp=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&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();
Expand Down Expand Up @@ -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&timestamp=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&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);
Expand Down Expand Up @@ -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 () => {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/tests/entry.test.ts → src/tests/unit.test.ts
Original file line number Diff line number Diff line change
@@ -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'));
});
Expand All @@ -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'));
});
Expand Down

0 comments on commit d7da614

Please sign in to comment.