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:
|
/api/admin/advanced/cache:
|
||||||
$ref: 'read/admin/advanced/cache.yaml'
|
$ref: 'read/admin/advanced/cache.yaml'
|
||||||
/api/admin/advanced/cache/dump:
|
/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:
|
/api/admin/development/logger:
|
||||||
$ref: 'read/admin/development/logger.yaml'
|
$ref: 'read/admin/development/logger.yaml'
|
||||||
/api/admin/development/info:
|
/api/admin/development/info:
|
||||||
|
|||||||
@@ -2,6 +2,15 @@ get:
|
|||||||
tags:
|
tags:
|
||||||
- admin
|
- admin
|
||||||
summary: Get system cache info
|
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:
|
responses:
|
||||||
"200":
|
"200":
|
||||||
description: ""
|
description: ""
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ get:
|
|||||||
- shorthand
|
- shorthand
|
||||||
summary: Get category data
|
summary: Get category data
|
||||||
parameters:
|
parameters:
|
||||||
|
- name: type
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
example: category
|
||||||
- name: id
|
- name: id
|
||||||
in: path
|
in: path
|
||||||
required: true
|
required: true
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ get:
|
|||||||
- shorthand
|
- shorthand
|
||||||
summary: Get post data
|
summary: Get post data
|
||||||
parameters:
|
parameters:
|
||||||
|
- name: type
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
example: post
|
||||||
- name: id
|
- name: id
|
||||||
in: path
|
in: path
|
||||||
required: true
|
required: true
|
||||||
|
|||||||
@@ -3,8 +3,15 @@ get:
|
|||||||
- authentication
|
- authentication
|
||||||
summary: /api/register/complete
|
summary: /api/register/complete
|
||||||
responses:
|
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":
|
"200":
|
||||||
description: ""
|
description: ''
|
||||||
content:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ get:
|
|||||||
- shorthand
|
- shorthand
|
||||||
summary: Get topic data
|
summary: Get topic data
|
||||||
parameters:
|
parameters:
|
||||||
|
- name: type
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
example: topic
|
||||||
- name: id
|
- name: id
|
||||||
in: path
|
in: path
|
||||||
required: true
|
required: true
|
||||||
|
|||||||
35
test/api.js
35
test/api.js
@@ -324,6 +324,9 @@ describe('API', async () => {
|
|||||||
method: method,
|
method: method,
|
||||||
jar: !unauthenticatedRoutes.includes(path) ? jar : undefined,
|
jar: !unauthenticatedRoutes.includes(path) ? jar : undefined,
|
||||||
json: true,
|
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,
|
headers: headers,
|
||||||
qs: qs,
|
qs: qs,
|
||||||
body: body,
|
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
|
// Recursively iterate through schema properties, comparing type
|
||||||
it('response should match schema definition', () => {
|
it('response body should match schema definition', () => {
|
||||||
const has200 = context[method].responses['200'];
|
const http302 = context[method].responses['302'];
|
||||||
if (!has200) {
|
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;
|
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) {
|
if (hasJSON) {
|
||||||
schema = context[method].responses['200'].content['application/json'].schema;
|
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?
|
// TODO someday: text/csv, binary file type checking?
|
||||||
|
|||||||
Reference in New Issue
Block a user