Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API versions list #5169

Merged
merged 1 commit into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ function create (env, ctx) {
///////////////////////////////////////////////////
// api and json object variables
///////////////////////////////////////////////////
const apiRoot = require('./lib/api/root')(env, ctx);
var api = require('./lib/api/')(env, ctx);
var api3 = require('./lib/api3/')(env, ctx);
var ddata = require('./lib/data/endpoints')(env, ctx);
var notificationsV2 = require('./lib/api/notifications-v2')(app, ctx)
var notificationsV2 = require('./lib/api/notifications-v2')(app, ctx);

app.use(compression({
filter: function shouldCompress (req, res) {
Expand Down Expand Up @@ -166,6 +167,10 @@ function create (env, ctx) {
});
});

app.use('/api', bodyParser({
limit: 1048576 * 50
}), apiRoot);

app.use('/api/v1', bodyParser({
limit: 1048576 * 50
}), api);
Expand Down Expand Up @@ -233,7 +238,7 @@ function create (env, ctx) {

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

app.use('/swagger-ui-dist', (req, res, next) => {
app.use('/swagger-ui-dist', (req, res) => {
res.redirect(307, '/api-docs');
});

Expand Down
4 changes: 4 additions & 0 deletions lib/api/const.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"API1_VERSION": "1.0.0",
"API2_VERSION": "2.0.0"
}
23 changes: 23 additions & 0 deletions lib/api/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

function configure () {
const express = require('express')
, api = express.Router( )
, apiConst = require('./const')
, api3Const = require('../api3/const')
;

api.get('/versions', function getVersion (req, res) {

const versions = [
{ version: apiConst.API1_VERSION, url: '/api/v1' },
{ version: apiConst.API2_VERSION, url: '/api/v2' },
{ version: api3Const.API3_VERSION, url: '/api/v3' }
];

res.json(versions);
});

return api;
}
module.exports = configure;
42 changes: 42 additions & 0 deletions tests/api.root.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const request = require('supertest');
require('should');

describe('Root REST API', function() {
const self = this
, instance = require('./fixtures/api/instance')
, semver = require('semver')
;

this.timeout(15000);

before(async () => {
self.instance = await instance.create({});
self.app = self.instance.app;
self.env = self.instance.env;
});


after(function after () {
self.instance.server.close();
});


it('GET /api/versions', async () => {
let res = await request(self.app)
.get('/api/versions')
.expect(200);

res.body.length.should.be.aboveOrEqual(3);
res.body.forEach(obj => {
const fields = Object.getOwnPropertyNames(obj);
fields.sort().should.be.eql(['url', 'version']);

semver.valid(obj.version).should.be.ok();
obj.url.should.startWith('/api');
});
});

});

98 changes: 98 additions & 0 deletions tests/fixtures/api/instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
'use strict';

const fs = require('fs')
, path = require('path')
, language = require('../../../lib/language')()
, apiRoot = require('../../../lib/api/root')
, http = require('http')
, https = require('https')
;

function configure () {
const self = { };

self.prepareEnv = function prepareEnv({ apiSecret, useHttps, authDefaultRoles, enable }) {

if (useHttps) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
else {
process.env.INSECURE_USE_HTTP = true;
}
process.env.API_SECRET = apiSecret;

process.env.HOSTNAME = 'localhost';
const env = require('../../../env')();

if (useHttps) {
env.ssl = {
key: fs.readFileSync(path.join(__dirname, '../api3/localhost.key')),
cert: fs.readFileSync(path.join(__dirname, '../api3/localhost.crt'))
};
}

env.settings.authDefaultRoles = authDefaultRoles;
env.settings.enable = enable;

return env;
};


/*
* Create new web server instance for testing purposes
*/
self.create = function createHttpServer ({
apiSecret = 'this is my long pass phrase',
useHttps = true,
authDefaultRoles = '',
enable = ['careportal', 'api']
}) {

return new Promise(function (resolve, reject) {

try {
let instance = { },
hasBooted = false
;

instance.env = self.prepareEnv({ apiSecret, useHttps, authDefaultRoles, enable });

self.wares = require('../../../lib/middleware/')(instance.env);
instance.app = require('express')();
instance.app.enable('api');

require('../../../lib/server/bootevent')(instance.env, language).boot(function booted (ctx) {
instance.ctx = ctx;
instance.ctx.ddata = require('../../../lib/data/ddata')();
instance.ctx.apiRootApp = apiRoot(instance.env, ctx);

instance.app.use('/api', instance.ctx.apiRootApp);

const transport = useHttps ? https : http;

instance.server = transport.createServer(instance.env.ssl || { }, instance.app).listen(0);
instance.env.PORT = instance.server.address().port;

instance.baseUrl = `${useHttps ? 'https' : 'http'}://${instance.env.HOSTNAME}:${instance.env.PORT}`;

console.log(`Started ${useHttps ? 'SSL' : 'HTTP'} instance on ${instance.baseUrl}`);
hasBooted = true;
resolve(instance);
});

setTimeout(function watchDog() {
if (!hasBooted)
reject('timeout');
}, 30000);

} catch (err) {
reject(err);
}
});
};


return self;
}

module.exports = configure();