2021-02-13 06:42:20 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-03-29 20:56:49 +02:00
|
|
|
/**
|
|
|
|
* API specs
|
|
|
|
*
|
|
|
|
* Tests for generic overarching HTTP API related features not related to any
|
|
|
|
* specific part of the data model or domain. For example: tests for versioning
|
|
|
|
* and openapi definitions.
|
|
|
|
*/
|
|
|
|
|
2020-10-08 07:37:17 +02:00
|
|
|
const common = require('../../common');
|
2021-02-03 13:08:43 +01:00
|
|
|
const validateOpenAPI = require('openapi-schema-validation').validate;
|
2020-03-29 20:56:49 +02:00
|
|
|
|
2021-02-13 00:57:07 +01:00
|
|
|
let agent;
|
2020-10-08 07:37:17 +02:00
|
|
|
const apiKey = common.apiKey;
|
2020-11-23 19:21:51 +01:00
|
|
|
let apiVersion = 1;
|
2020-03-29 20:56:49 +02:00
|
|
|
|
2021-02-13 06:42:20 +01:00
|
|
|
const makeid = () => {
|
|
|
|
let text = '';
|
|
|
|
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
const testPadId = makeid();
|
2020-03-29 20:56:49 +02:00
|
|
|
|
2021-02-13 06:42:20 +01:00
|
|
|
const endPoint = (point) => `/api/${apiVersion}/${point}?apikey=${apiKey}`;
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
describe(__filename, function () {
|
2021-02-13 00:57:07 +01:00
|
|
|
before(async function () { agent = await common.init(); });
|
|
|
|
|
2021-02-13 01:01:46 +01:00
|
|
|
it('can obtain API version', async function () {
|
|
|
|
await agent.get('/api/')
|
|
|
|
.expect(200)
|
2021-02-13 00:51:42 +01:00
|
|
|
.expect((res) => {
|
|
|
|
apiVersion = res.body.currentVersion;
|
|
|
|
if (!res.body.currentVersion) throw new Error('No version set in API');
|
|
|
|
return;
|
2021-02-13 01:01:46 +01:00
|
|
|
});
|
2020-03-29 20:56:49 +02:00
|
|
|
});
|
|
|
|
|
2021-02-13 01:01:46 +01:00
|
|
|
it('can obtain valid openapi definition document', async function () {
|
2021-02-13 20:00:06 +01:00
|
|
|
this.timeout(15000);
|
2021-02-13 01:01:46 +01:00
|
|
|
await agent.get('/api/openapi.json')
|
|
|
|
.expect(200)
|
2021-02-13 00:51:42 +01:00
|
|
|
.expect((res) => {
|
|
|
|
const {valid, errors} = validateOpenAPI(res.body, 3);
|
|
|
|
if (!valid) {
|
|
|
|
const prettyErrors = JSON.stringify(errors, null, 2);
|
|
|
|
throw new Error(`Document is not valid OpenAPI. ${errors.length} ` +
|
|
|
|
`validation errors:\n${prettyErrors}`);
|
|
|
|
}
|
2021-02-13 01:01:46 +01:00
|
|
|
});
|
2020-03-29 20:56:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|