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 8a681ed
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 20 deletions.
26 changes: 21 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
- 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



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
4 changes: 0 additions & 4 deletions src/models/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 8a681ed

Please sign in to comment.