From 6707869bdd8994f3b2da7d8169fe1a1285d2017c Mon Sep 17 00:00:00 2001
From: Rosanny <rosanny.sihombing@hft-stuttgart.de>
Date: Mon, 1 Aug 2022 13:43:57 +0200
Subject: [PATCH] update unit test

---
 __tests/db.unit.test.js         | 23 ++++----
 __tests/gitlab.unit.test.js     | 26 ++++-----
 __tests/public.endpoint.test.js | 21 ++++++--
 package.json                    |  2 +-
 src/app.ts                      |  6 +--
 src/index.ts                    | 94 +++++++++++++++++++++++++++++++++
 6 files changed, 135 insertions(+), 37 deletions(-)
 create mode 100644 src/index.ts

diff --git a/__tests/db.unit.test.js b/__tests/db.unit.test.js
index f1d8d697..6b153096 100644
--- a/__tests/db.unit.test.js
+++ b/__tests/db.unit.test.js
@@ -1,50 +1,49 @@
-const dbController = require('../../src/controller/dbController')
+const dbc = require('../src/controller/dbController')
 
 describe('DB methohds test', () => {
   it('returns a user from DB by email', async () => {
-    const user = await dbController.getUserByEmail('litehon958@whipjoy.com')
+    const user = await dbc.dbController.getUserByEmail('litehon958@whipjoy.com')
     expect(user).not.toBeNull()
   })
   it('returns a null user', async () => {
-    const user = await dbController.getUserByEmail('jondoe@nowhere.com') // a non-exist user
+    const user = await dbc.dbController.getUserByEmail('jondoe@nowhere.com') // a non-exist user
     expect(user).toBeNull()
   })
-
   it("returns a user's email", async () => {
-    const email = await dbController.getUserEmailById(1)
+    const email = await dbc.dbController.getUserEmailById(2)
     expect(email).not.toBeNull()
   })
   it("returns null instead of a user's email", async () => {
-    const email = await dbController.getUserEmailById(1005) // no user has this ID
+    const email = await dbc.dbController.getUserEmailById(1005) // no user has this ID
     expect(email).toBeNull()
   })
 
   it('returns null from DB by token', async () => {
-    const user = await dbController.getUserByToken('12345678') // unvalid token
+    const user = await dbc.dbController.getUserByToken('12345678') // unvalid token
     expect(user).toBeNull() // for valid token = expect(user).not.toBeNull()
   })
 
   it("returns a user's verification token, if any", async () => {
-    const token = await dbController.getVerificationTokenByUserId(1)
+    const token = await dbc.dbController.getVerificationTokenByUserId(1)
     expect(token).toBeNull()
   })
 
   it("returns a user's ID, if any", async () => {
-    const token = await dbController.getUserIdByVerificationToken('12345678') // unvalid token
+    const token = await dbc.dbController.getUserIdByVerificationToken('12345678') // unvalid token
     expect(token).toBeNull() // for valid token = expect(user).not.toBeNull()
   })
 
   it("returns a user's GitLab_ID, if any", async () => {
-    const id = await dbController.getGitlabId(1)
+    const id = await dbc.dbController.getGitlabId(2)
     expect(id).not.toBeNull()
   })
 
   it('checks user email', async () => {
-    const user = await dbController.checkUserEmail('litehon958@whipjoy.com')
+    const user = await dbc.dbController.checkUserEmail('litehon958@whipjoy.com')
     expect(user).not.toBeNull()
   })
   it('checks user email and return null', async () => {
-    const user = await dbController.checkUserEmail('jondoe@nowhere.com') // a non-exist user
+    const user = await dbc.dbController.checkUserEmail('jondoe@nowhere.com') // a non-exist user
     expect(user).toBeNull()
   })
 })
diff --git a/__tests/gitlab.unit.test.js b/__tests/gitlab.unit.test.js
index babb5689..297c8d73 100644
--- a/__tests/gitlab.unit.test.js
+++ b/__tests/gitlab.unit.test.js
@@ -1,36 +1,30 @@
-const gitlabController = require('../src/controller/gitlabController')
-const axios = require('axios')
-
-jest.mock('axios')
+const gc = require('../src/controller/gitlabController')
 
 describe('GitLab API', () => {
   test('returns an existing gitlab user by an email address', async () => {
-    axios.get.mockResolvedValue({
-
-    })
-    const user = await gitlabController.getUserByEmail('litehon958@whipjoy.com')
+    const user = await gc.gitlabController.getUserByEmail('litehon958@whipjoy.com')
     expect(user).not.toBeNull()
   })
   test('returns an undefined user', async () => {
-    const user = await gitlabController.getUserByEmail('johndoe@nowhere.com')
+    const user = await gc.gitlabController.getUserByEmail('johndoe@nowhere.com')
     expect(user).toBeUndefined()
   })
 
   test('returns users project', async () => {
-    const userProjects = await gitlabController.getUserProjects(136)
+    const userProjects = await gc.gitlabController.getUserProjects(136)
     expect(userProjects).toBeDefined()
   })
-  test('returns undefined projects, due to non-existing gitlab user ID', async () => {
-    const userProjects = await gitlabController.getUserProjects(0)
-    expect(userProjects).toBeUndefined()
+  test('returns null, due to non-existing gitlab user ID', async () => {
+    const userProjects = await gc.gitlabController.getUserProjects(0)
+    expect(userProjects).toBeNull()
   })
 
   test('returns a project by ID', async () => {
-    const project = await gitlabController.getProjectById(13) // m4lab_landing_page
+    const project = await gc.gitlabController.getProjectById(13) // m4lab_landing_page
     expect(project).toBeDefined()
   })
   test('returns undefined, due to invalid project ID', async () => {
-    const project = await gitlabController.getProjectById(0)
-    expect(project).toBeUndefined()
+    const project = await gc.gitlabController.getProjectById(0)
+    expect(project).toBeNull()
   })
 })
diff --git a/__tests/public.endpoint.test.js b/__tests/public.endpoint.test.js
index e9f52bab..1a097b5e 100644
--- a/__tests/public.endpoint.test.js
+++ b/__tests/public.endpoint.test.js
@@ -1,10 +1,23 @@
 const request = require('supertest')
-const express = require('express')
-
-const app = express()
-app.set('port', 9989)
+const app = require('../src/app')
 
 describe('Test endpoint(s)', () => {
+  it('should return a 200 status code', () => {
+    request(app)
+      .get('/registration')
+      .expect(200)
+      .end(function (err, res) {
+        if (err) throw err
+      })
+  })
+  it('should return a 200 status code', () => {
+    request(app)
+      .get('/forgotPwd')
+      .expect(200)
+      .end(function (err, res) {
+        if (err) throw err
+      })
+  })
   it('should return a 200 status code', () => {
     request(app)
       .get('/contact')
diff --git a/package.json b/package.json
index ec6fdbd7..1028bbe4 100644
--- a/package.json
+++ b/package.json
@@ -16,7 +16,7 @@
     "url": "https://transfer.hft-stuttgart.de/gitlab/m4lab_tv1/user-account.git"
   },
   "scripts": {
-    "start:dev": "nodemon ./src/app.ts",
+    "start:dev": "nodemon ./src/index.ts",
     "start": "nodemon ./built/app.js",
     "test": "jest",
     "clean-code": "ts-standard --fix",
diff --git a/src/app.ts b/src/app.ts
index 7d830539..59f673c1 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -16,7 +16,6 @@ const config = require('./config/config')[env]
 const lang = 'DE'
 
 const app = express()
-app.set('port', config.app.port)
 app.set('views', path.join(path.join(__dirname, '/views')))
 app.set('view engine', 'pug')
 
@@ -85,6 +84,5 @@ app.use(function (err: any, req: any, res: any, next: any) {
   })
 })
 
-app.listen(app.get('port'), function () {
-  console.log('Express server listening on port ' + app.get('port'))
-})
+// export { app }
+module.exports = app
\ No newline at end of file
diff --git a/src/index.ts b/src/index.ts
new file mode 100644
index 00000000..22f384c5
--- /dev/null
+++ b/src/index.ts
@@ -0,0 +1,94 @@
+#!/usr/bin/env node
+
+/**
+ * Module dependencies.
+ */
+
+//import {app} from './app';
+const app = require('./app')
+var debug = require('debug')('MLAB-Account:server');
+import http from 'http'
+
+const env = process.env.NODE_ENV ?? 'testing'
+const config = require('./config/config')[env]
+
+/**
+ * Get port from environment and store in Express.
+ */
+
+var port = normalizePort(process.env.PORT || config.app.port);
+app.set('port', port);
+
+/**
+ * Create HTTP server.
+ */
+
+var server = http.createServer(app);
+
+/**
+ * Listen on provided port, on all network interfaces.
+ */
+
+server.listen(port);
+server.on('error', onError);
+server.on('listening', onListening);
+
+/**
+ * Normalize a port into a number, string, or false.
+ */
+
+function normalizePort(val: any) {
+  var port = parseInt(val, 10);
+
+  if (isNaN(port)) {
+    // named pipe
+    return val;
+  }
+
+  if (port >= 0) {
+    // port number
+    return port;
+  }
+
+  return false;
+}
+
+/**
+ * Event listener for HTTP server "error" event.
+ */
+
+function onError(error: any) {
+  if (error.syscall !== 'listen') {
+    throw error;
+  }
+
+  var bind = typeof port === 'string'
+    ? 'Pipe ' + port
+    : 'Port ' + port;
+
+  // handle specific listen errors with friendly messages
+  switch (error.code) {
+    case 'EACCES':
+      console.error(bind + ' requires elevated privileges');
+      process.exit(1);
+      break;
+    case 'EADDRINUSE':
+      console.error(bind + ' is already in use');
+      process.exit(1);
+      break;
+    default:
+      throw error;
+  }
+}
+
+/**
+ * Event listener for HTTP server "listening" event.
+ */
+
+function onListening() {
+  var addr = server.address();
+  var bind = typeof addr === 'string'
+    ? 'pipe ' + addr
+    : 'port ' + addr!.port;
+  debug('Listening on ' + bind);
+}
-- 
GitLab