test for helm-chart, api-docs, backend version endpoints

This commit is contained in:
atakpu ikhide godson 2020-12-21 12:26:24 +01:00
parent c4b2c20e18
commit 3300a629d5
10 changed files with 113 additions and 136 deletions

View File

@ -5,22 +5,6 @@
*
*/
// const Mongoose = require('mongoose');
// const mongoose = require('../config/db');
// const JsonToCsv = require('./jsonToCsv');
// const ObjectID = mongoose.Types.ObjectId;
function isDate(date) {
try {
typeof date === 'object' &&
date !== null &&
new Date(date).toISOString();
return true;
} catch (error) {
return false;
}
}
module.exports = {
sendEmptyResponse(req, res) {
//purge request.
@ -55,95 +39,6 @@ module.exports = {
}
},
sendListResponse: async function(req, res, list, count) {
// remove __v, deleted, deletedAt and deletedById if not Master Admin
if (req.authorizationType !== 'MASTER-ADMIN') {
if (Array.isArray(list)) {
list = list.map(field =>
typeof field === 'object' && field !== null
? filterKeys(field)
: field
);
} else if (typeof list === 'object' && list !== null) {
list = filterKeys(list);
}
}
const response = {};
if (!list) {
list = [];
}
if (list) {
response.data = list;
}
if (count) {
response.count = count;
} else {
if (list) response.count = list.length;
}
if (req.query.skip) {
response.skip = parseInt(req.query.skip);
}
if (req.query.limit) {
response.limit = parseInt(req.query.limit);
}
//purge request.
//req = null;
if (req.query['output-type'] === 'csv') {
if (!Array.isArray(response.data)) {
const properties = Object.keys(response.data);
const newObj = {};
properties.forEach(prop => {
if (
typeof response.data[[prop]] === 'object' &&
response.data[[prop]] !== null
) {
if (response.data[[prop]].name)
response.data[[prop]] = response.data[[prop]].name;
else if (response.data[[prop]].title)
response.data[[prop]] = response.data[[prop]].title;
else if (response.data[[prop]]._id)
response.data[[prop]] = response.data[[prop]]._id;
}
newObj[[prop]] = response.data[[prop]];
});
response.data = JSON.parse(JSON.stringify(newObj));
response.data = [response.data];
} else {
response.data = response.data.map(i => {
i = i._doc ? i._doc : i;
const properties = Object.keys(i);
const newObj = {};
properties.forEach(prop => {
if (
typeof i[[prop]] === 'object' &&
i[[prop]] !== null
) {
if (i[[prop]].name) i[[prop]] = i[[prop]].name;
else if (i[[prop]].title)
i[[prop]] = i[[prop]].title;
else if (i[[prop]]._id) i[[prop]] = i[[prop]]._id;
}
newObj[[prop]] = i[[prop]];
});
return JSON.parse(JSON.stringify(newObj));
});
}
response.data = await JsonToCsv.ToCsv(response.data);
}
res.resBody = response; // To be used in 'auditLog' middleware to log reponse data;
return res.status(200).send(response);
},
async sendItemResponse(req, res, item) {
// remove __v, deleted, deletedAt and deletedById if not Master Admin
if (req.authorizationType !== 'MASTER-ADMIN') {

View File

@ -14,15 +14,20 @@
},
"homepage": "https://github.com/fyipe/api-docs#readme",
"dependencies": {
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"chai-subset": "^1.6.0",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"ejs": "^3.1.3",
"express": "^4.17.1",
"mocha": "^8.2.1",
"path": "^0.12.7"
},
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "echo \"no test required\"",
"test": "cross-env NODE_ENV=development mocha --exit test/index.js",
"audit": "npm audit --audit-level=low",
"dep-check": "depcheck ./ --skip-missing=true --ignores='babel-*,browserslist,ejs,path,loadable-components,js-uuid,acorn,@beam-australia/react-env'"
},

View File

@ -6,6 +6,25 @@ const cors = require('cors');
app.use(cors());
process.on('exit', () => {
/* eslint-disable no-console */
console.log('Server Shutting Shutdown');
});
process.on('unhandledRejection', err => {
/* eslint-disable no-console */
console.error('Unhandled rejection in server process occurred');
/* eslint-disable no-console */
console.error(err);
});
process.on('uncaughtException', err => {
/* eslint-disable no-console */
console.error('Uncaught exception in server process occurred');
/* eslint-disable no-console */
console.error(err);
});
app.use(function(req, res, next) {
if (typeof req.body === 'string') {
req.body = JSON.parse(req.body);
@ -86,3 +105,8 @@ app.listen(app.get('port'), function() {
// eslint-disable-next-line no-console
console.log('API Reference started on PORT:' + app.get('port'));
});
module.exports = app;
module.exports.close = function() {
server.close();
};

7
api-docs/test/index.js Normal file
View File

@ -0,0 +1,7 @@
try {
require('./version.test');
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
throw error;
}

View File

@ -0,0 +1,19 @@
/* eslint-disable linebreak-style */
process.env.PORT = 1446;
const expect = require('chai').expect;
const chai = require('chai');
chai.use(require('chai-http'));
const app = require('../server');
const request = chai.request.agent(app);
describe('Version API', function() {
this.timeout(20000);
it('should get the current api-docs version', function(done) {
request.get('/version').end(function(err, res) {
expect(res).to.have.status(200);
expect(res.body.docs).to.be.equal(process.env.npm_package_version);
done();
});
});
});

View File

@ -6,10 +6,6 @@ chai.use(require('chai-http'));
const app = require('../server');
const request = chai.request.agent(app);
const dashboardVersion = require('../../dashboard/package.json').version;
const helmVersion = require('../../helm-chart/package.json').version;
const docsVersion = require('../../api-docs/package.json').version;
describe('Version API', function() {
this.timeout(20000);
@ -22,28 +18,4 @@ describe('Version API', function() {
done();
});
});
it('should get the current dashboard version', function(done) {
request.get('/version').end(function(err, res) {
expect(res).to.have.status(200);
expect(res.body.dashboard).to.be.equal(dashboardVersion);
done();
});
});
it('should get the current docs version', function(done) {
request.get('/version').end(function(err, res) {
expect(res).to.have.status(200);
expect(res.body.docs).to.be.equal(docsVersion);
done();
});
});
it('should get the current helm chart version', function(done) {
request.get('/version').end(function(err, res) {
expect(res).to.have.status(200);
expect(res.body.helm).to.be.equal(helmVersion);
done();
});
});
});

View File

@ -6,15 +6,20 @@
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "echo \"no test required\"",
"test": "cross-env NODE_ENV=development mocha --exit test/index.js",
"audit": "npm audit --audit-level=low",
"dep-check": "depcheck ./ --skip-missing=true --ignores='ejs'"
},
"author": "",
"license": "ISC",
"dependencies": {
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"chai-subset": "^1.6.0",
"cors": "^2.8.5",
"express": "^4.17.1"
"cross-env": "^7.0.3",
"express": "^4.17.1",
"mocha": "^8.2.1"
},
"devDependencies": {
"depcheck": "^0.9.2",

View File

@ -4,6 +4,25 @@ const path = require('path');
const version = require('./api/version');
const cors = require('cors');
process.on('exit', () => {
/* eslint-disable no-console */
console.log('Server Shutting Shutdown');
});
process.on('unhandledRejection', err => {
/* eslint-disable no-console */
console.error('Unhandled rejection in server process occurred');
/* eslint-disable no-console */
console.error(err);
});
process.on('uncaughtException', err => {
/* eslint-disable no-console */
console.error('Uncaught exception in server process occurred');
/* eslint-disable no-console */
console.error(err);
});
app.use(cors());
app.use(function(req, res, next) {
@ -79,3 +98,8 @@ app.listen(app.get('port'), function() {
// eslint-disable-next-line no-console
console.log('API Reference started on PORT:' + app.get('port'));
});
module.exports = app;
module.exports.close = function() {
server.close();
};

7
helm-chart/test/index.js Normal file
View File

@ -0,0 +1,7 @@
try {
require('./version.test');
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
throw error;
}

View File

@ -0,0 +1,19 @@
/* eslint-disable linebreak-style */
process.env.PORT = 3424;
const expect = require('chai').expect;
const chai = require('chai');
chai.use(require('chai-http'));
const app = require('../server');
const request = chai.request.agent(app);
describe('Version API', function() {
this.timeout(20000);
it('should get the current helm-chart version', function(done) {
request.get('/version').end(function(err, res) {
expect(res).to.have.status(200);
expect(res.body.helm).to.be.equal(process.env.npm_package_version);
done();
});
});
});