Commit 6707869b authored by Rosanny Sihombing's avatar Rosanny Sihombing
Browse files

update unit test

parent 65fa21bf
const dbController = require('../../src/controller/dbController') const dbc = require('../src/controller/dbController')
describe('DB methohds test', () => { describe('DB methohds test', () => {
it('returns a user from DB by email', async () => { 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() expect(user).not.toBeNull()
}) })
it('returns a null user', async () => { 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() expect(user).toBeNull()
}) })
it("returns a user's email", async () => { it("returns a user's email", async () => {
const email = await dbController.getUserEmailById(1) const email = await dbc.dbController.getUserEmailById(2)
expect(email).not.toBeNull() expect(email).not.toBeNull()
}) })
it("returns null instead of a user's email", async () => { 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() expect(email).toBeNull()
}) })
it('returns null from DB by token', async () => { 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() expect(user).toBeNull() // for valid token = expect(user).not.toBeNull()
}) })
it("returns a user's verification token, if any", async () => { 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() expect(token).toBeNull()
}) })
it("returns a user's ID, if any", async () => { 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() expect(token).toBeNull() // for valid token = expect(user).not.toBeNull()
}) })
it("returns a user's GitLab_ID, if any", async () => { 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() expect(id).not.toBeNull()
}) })
it('checks user email', async () => { 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() expect(user).not.toBeNull()
}) })
it('checks user email and return null', async () => { 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() expect(user).toBeNull()
}) })
}) })
const gitlabController = require('../src/controller/gitlabController') const gc = require('../src/controller/gitlabController')
const axios = require('axios')
jest.mock('axios')
describe('GitLab API', () => { describe('GitLab API', () => {
test('returns an existing gitlab user by an email address', async () => { test('returns an existing gitlab user by an email address', async () => {
axios.get.mockResolvedValue({ const user = await gc.gitlabController.getUserByEmail('litehon958@whipjoy.com')
})
const user = await gitlabController.getUserByEmail('litehon958@whipjoy.com')
expect(user).not.toBeNull() expect(user).not.toBeNull()
}) })
test('returns an undefined user', async () => { 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() expect(user).toBeUndefined()
}) })
test('returns users project', async () => { test('returns users project', async () => {
const userProjects = await gitlabController.getUserProjects(136) const userProjects = await gc.gitlabController.getUserProjects(136)
expect(userProjects).toBeDefined() expect(userProjects).toBeDefined()
}) })
test('returns undefined projects, due to non-existing gitlab user ID', async () => { test('returns null, due to non-existing gitlab user ID', async () => {
const userProjects = await gitlabController.getUserProjects(0) const userProjects = await gc.gitlabController.getUserProjects(0)
expect(userProjects).toBeUndefined() expect(userProjects).toBeNull()
}) })
test('returns a project by ID', async () => { 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() expect(project).toBeDefined()
}) })
test('returns undefined, due to invalid project ID', async () => { test('returns undefined, due to invalid project ID', async () => {
const project = await gitlabController.getProjectById(0) const project = await gc.gitlabController.getProjectById(0)
expect(project).toBeUndefined() expect(project).toBeNull()
}) })
}) })
const request = require('supertest') const request = require('supertest')
const express = require('express') const app = require('../src/app')
const app = express()
app.set('port', 9989)
describe('Test endpoint(s)', () => { 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', () => { it('should return a 200 status code', () => {
request(app) request(app)
.get('/contact') .get('/contact')
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
"url": "https://transfer.hft-stuttgart.de/gitlab/m4lab_tv1/user-account.git" "url": "https://transfer.hft-stuttgart.de/gitlab/m4lab_tv1/user-account.git"
}, },
"scripts": { "scripts": {
"start:dev": "nodemon ./src/app.ts", "start:dev": "nodemon ./src/index.ts",
"start": "nodemon ./built/app.js", "start": "nodemon ./built/app.js",
"test": "jest", "test": "jest",
"clean-code": "ts-standard --fix", "clean-code": "ts-standard --fix",
......
...@@ -16,7 +16,6 @@ const config = require('./config/config')[env] ...@@ -16,7 +16,6 @@ const config = require('./config/config')[env]
const lang = 'DE' const lang = 'DE'
const app = express() const app = express()
app.set('port', config.app.port)
app.set('views', path.join(path.join(__dirname, '/views'))) app.set('views', path.join(path.join(__dirname, '/views')))
app.set('view engine', 'pug') app.set('view engine', 'pug')
...@@ -85,6 +84,5 @@ app.use(function (err: any, req: any, res: any, next: any) { ...@@ -85,6 +84,5 @@ app.use(function (err: any, req: any, res: any, next: any) {
}) })
}) })
app.listen(app.get('port'), function () { // export { app }
console.log('Express server listening on port ' + app.get('port')) module.exports = app
}) \ No newline at end of file
#!/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);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment