mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-26 08:31:22 +01:00
refactor: api test suite to accept methods other than GET
This commit is contained in:
81
test/api.js
81
test/api.js
@@ -94,26 +94,32 @@ describe('Read API', async () => {
|
|||||||
writeApi = await SwaggerParser.dereference(writeApiPath);
|
writeApi = await SwaggerParser.dereference(writeApiPath);
|
||||||
|
|
||||||
// Iterate through all documented paths, make a call to it, and compare the result body with what is defined in the spec
|
// Iterate through all documented paths, make a call to it, and compare the result body with what is defined in the spec
|
||||||
const paths = Object.keys(writeApi.paths);
|
// const paths = Object.keys(writeApi.paths);
|
||||||
// const paths = Object.keys(readApi.paths);
|
const paths = Object.keys(readApi.paths);
|
||||||
|
|
||||||
paths.forEach((path) => {
|
paths.forEach((path) => {
|
||||||
const context = writeApi.paths[path];
|
// const context = writeApi.paths[path];
|
||||||
// const context = readApi.paths[path];
|
const context = readApi.paths[path];
|
||||||
let schema;
|
let schema;
|
||||||
let response;
|
let response;
|
||||||
const urls = [];
|
let url;
|
||||||
const methods = [];
|
let method;
|
||||||
const headers = {};
|
const headers = {};
|
||||||
const qs = {};
|
const qs = {};
|
||||||
|
|
||||||
it('should have examples when parameters are present', () => {
|
Object.keys(context).forEach((_method) => {
|
||||||
Object.keys(context).forEach((method) => {
|
if (_method !== 'get') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
it('should have examples when parameters are present', () => {
|
||||||
|
method = _method;
|
||||||
const parameters = context[method].parameters;
|
const parameters = context[method].parameters;
|
||||||
let testPath = path;
|
let testPath = path;
|
||||||
|
|
||||||
if (parameters) {
|
if (parameters) {
|
||||||
parameters.forEach((param) => {
|
parameters.forEach((param) => {
|
||||||
assert(param.example !== null && param.example !== undefined, path + ' has parameters without examples');
|
assert(param.example !== null && param.example !== undefined, `${method.toUpperCase()} ${path} has parameters without examples`);
|
||||||
|
|
||||||
switch (param.in) {
|
switch (param.in) {
|
||||||
case 'path':
|
case 'path':
|
||||||
@@ -129,40 +135,41 @@ describe('Read API', async () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
urls.push = nconf.get('url') + testPath;
|
url = nconf.get('url') + testPath;
|
||||||
methods.push(method);
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it('should resolve with a 200 when called', async () => {
|
it('should resolve with a 200 when called', async () => {
|
||||||
await setupData();
|
await setupData();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response = await request(url, {
|
response = await request(url, {
|
||||||
jar: !unauthenticatedRoutes.includes(path) ? jar : undefined,
|
method: method,
|
||||||
json: true,
|
jar: !unauthenticatedRoutes.includes(path) ? jar : undefined,
|
||||||
headers: headers,
|
json: true,
|
||||||
qs: qs,
|
headers: headers,
|
||||||
});
|
qs: qs,
|
||||||
} catch (e) {
|
});
|
||||||
assert(!e, path + ' resolved with ' + e.message);
|
} catch (e) {
|
||||||
}
|
assert(!e, `${method.toUpperCase()} ${path} resolved with ${e.message}`);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
console.log(response);
|
||||||
|
|
||||||
// Recursively iterate through schema properties, comparing type
|
// Recursively iterate through schema properties, comparing type
|
||||||
it('response should match schema definition', () => {
|
it('response should match schema definition', () => {
|
||||||
const has200 = context.get.responses['200'];
|
const has200 = context[method].responses['200'];
|
||||||
if (!has200) {
|
if (!has200) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const hasJSON = has200.content && has200.content['application/json'];
|
const hasJSON = has200.content && has200.content['application/json'];
|
||||||
if (hasJSON) {
|
if (hasJSON) {
|
||||||
schema = context.get.responses['200'].content['application/json'].schema;
|
schema = context[method].responses['200'].content['application/json'].schema;
|
||||||
compare(schema, response, 'root');
|
compare(schema, response, 'root');
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO someday: text/csv, binary file type checking?
|
// TODO someday: text/csv, binary file type checking?
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user