mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: fix more tests, add more routes, update api test suite
This commit is contained in:
@@ -124,7 +124,7 @@ paths:
|
||||
/api/admin/advanced/cache:
|
||||
$ref: 'read/admin/advanced/cache.yaml'
|
||||
/api/admin/advanced/cache/dump:
|
||||
$ref: 'read/admin/advanced/cache.yaml' # todo: @baris to fix, see gh#9122
|
||||
$ref: 'read/admin/advanced/cache/dump.yaml'
|
||||
/api/admin/development/logger:
|
||||
$ref: 'read/admin/development/logger.yaml'
|
||||
/api/admin/development/info:
|
||||
|
||||
@@ -2,6 +2,15 @@ get:
|
||||
tags:
|
||||
- admin
|
||||
summary: Get system cache info
|
||||
parameters:
|
||||
- in: query
|
||||
name: name
|
||||
schema:
|
||||
type: string
|
||||
enum: ['post', 'object', 'group', 'local']
|
||||
required: false
|
||||
description: Specify cache to dump if calling `/dump`
|
||||
example: 'post'
|
||||
responses:
|
||||
"200":
|
||||
description: ""
|
||||
|
||||
@@ -3,6 +3,12 @@ get:
|
||||
- shorthand
|
||||
summary: Get category data
|
||||
parameters:
|
||||
- name: type
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
example: category
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
|
||||
@@ -3,6 +3,12 @@ get:
|
||||
- shorthand
|
||||
summary: Get post data
|
||||
parameters:
|
||||
- name: type
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
example: post
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
|
||||
@@ -3,8 +3,15 @@ get:
|
||||
- authentication
|
||||
summary: /api/register/complete
|
||||
responses:
|
||||
"302":
|
||||
description: If there are no additional registration steps to complete, then the user is redirected back to the registration page (`/register`)
|
||||
headers:
|
||||
Location:
|
||||
schema:
|
||||
type: string
|
||||
example: /register
|
||||
"200":
|
||||
description: ""
|
||||
description: ''
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
|
||||
@@ -3,6 +3,12 @@ get:
|
||||
- shorthand
|
||||
summary: Get topic data
|
||||
parameters:
|
||||
- name: type
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
example: topic
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
|
||||
35
test/api.js
35
test/api.js
@@ -324,6 +324,9 @@ describe('API', async () => {
|
||||
method: method,
|
||||
jar: !unauthenticatedRoutes.includes(path) ? jar : undefined,
|
||||
json: true,
|
||||
followRedirect: false, // all responses are significant (e.g. 302)
|
||||
simple: false, // don't throw on non-200 (e.g. 302)
|
||||
resolveWithFullResponse: true, // send full request back (to check statusCode)
|
||||
headers: headers,
|
||||
qs: qs,
|
||||
body: body,
|
||||
@@ -343,17 +346,39 @@ describe('API', async () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('response status code should match one of the schema defined responses', () => {
|
||||
// HACK: allow HTTP 418 I am a teapot, for now 👇
|
||||
assert(context[method].responses.hasOwnProperty('418') || Object.keys(context[method].responses).includes(String(response.statusCode)), `${method.toUpperCase()} ${path} sent back unexpected HTTP status code: ${response.statusCode}`);
|
||||
});
|
||||
|
||||
// Recursively iterate through schema properties, comparing type
|
||||
it('response should match schema definition', () => {
|
||||
const has200 = context[method].responses['200'];
|
||||
if (!has200) {
|
||||
it('response body should match schema definition', () => {
|
||||
const http302 = context[method].responses['302'];
|
||||
if (http302 && response.statusCode === 302) {
|
||||
// Compare headers instead
|
||||
const expectedHeaders = Object.keys(http302.headers).reduce((memo, name) => {
|
||||
memo[name] = http302.headers[name].schema.example;
|
||||
return memo;
|
||||
}, {});
|
||||
|
||||
for (const header in expectedHeaders) {
|
||||
if (expectedHeaders.hasOwnProperty(header)) {
|
||||
assert(response.headers[header.toLowerCase()]);
|
||||
assert(response.headers[header.toLowerCase()] === expectedHeaders[header]);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const hasJSON = has200.content && has200.content['application/json'];
|
||||
const http200 = context[method].responses['200'];
|
||||
if (!http200) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hasJSON = http200.content && http200.content['application/json'];
|
||||
if (hasJSON) {
|
||||
schema = context[method].responses['200'].content['application/json'].schema;
|
||||
compare(schema, response, method.toUpperCase(), path, 'root');
|
||||
compare(schema, response.body, method.toUpperCase(), path, 'root');
|
||||
}
|
||||
|
||||
// TODO someday: text/csv, binary file type checking?
|
||||
|
||||
Reference in New Issue
Block a user