diff --git a/api-docs/middlewares/response.js b/api-docs/middlewares/response.js index fb86d77f94..012fa011ca 100644 --- a/api-docs/middlewares/response.js +++ b/api-docs/middlewares/response.js @@ -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') { diff --git a/api-docs/package.json b/api-docs/package.json index b83a2b3338..886a4dff82 100755 --- a/api-docs/package.json +++ b/api-docs/package.json @@ -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'" }, diff --git a/api-docs/server.js b/api-docs/server.js index adfbf1ce10..301a2eb2f8 100755 --- a/api-docs/server.js +++ b/api-docs/server.js @@ -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(); +}; diff --git a/api-docs/test/index.js b/api-docs/test/index.js new file mode 100644 index 0000000000..c8d584e29e --- /dev/null +++ b/api-docs/test/index.js @@ -0,0 +1,7 @@ +try { + require('./version.test'); +} catch (error) { + // eslint-disable-next-line no-console + console.error(error); + throw error; +} diff --git a/api-docs/test/version.test.js b/api-docs/test/version.test.js new file mode 100644 index 0000000000..1a0c5c50a9 --- /dev/null +++ b/api-docs/test/version.test.js @@ -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(); + }); + }); +}); diff --git a/backend/test/version.test.js b/backend/test/version.test.js index 04db79438f..9d6b6b1f96 100644 --- a/backend/test/version.test.js +++ b/backend/test/version.test.js @@ -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(); - }); - }); }); diff --git a/helm-chart/package.json b/helm-chart/package.json index a5e09c2b72..70311156ff 100644 --- a/helm-chart/package.json +++ b/helm-chart/package.json @@ -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", diff --git a/helm-chart/server.js b/helm-chart/server.js index bd97b28c02..92438ef410 100644 --- a/helm-chart/server.js +++ b/helm-chart/server.js @@ -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(); +}; diff --git a/helm-chart/test/index.js b/helm-chart/test/index.js new file mode 100644 index 0000000000..c8d584e29e --- /dev/null +++ b/helm-chart/test/index.js @@ -0,0 +1,7 @@ +try { + require('./version.test'); +} catch (error) { + // eslint-disable-next-line no-console + console.error(error); + throw error; +} diff --git a/helm-chart/test/version.test.js b/helm-chart/test/version.test.js new file mode 100644 index 0000000000..5cd9198e00 --- /dev/null +++ b/helm-chart/test/version.test.js @@ -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(); + }); + }); +});