Skip to content

Node.js based forum software built for the modern web

test: new api test to ensure each path's parameters are defined in context

Julian Lam97842c43d38e
diff --git a/public/openapi/write/admin/settings/setting.yaml b/public/openapi/write/admin/settings/setting.yaml
index dcd85aed56..009f478b81 100644
--- a/public/openapi/write/admin/settings/setting.yaml
+++ b/public/openapi/write/admin/settings/setting.yaml
@@ -11,13 +11,6 @@ put:
required: true
description: backend id of the setting to update
example: maximumRelatedTopics
- - in: path
- name: value
- schema:
- type: string
- required: true
- description: the value of the new setting
- example: 2
requestBody:
required: true
content:
diff --git a/test/api.js b/test/api.js
index 8f98c503bc..5e145abc83 100644
--- a/test/api.js
+++ b/test/api.js
@@ -83,7 +83,7 @@ describe('API', async () => {
tokens: [{
token: mocks.delete['/users/{uid}/tokens/{token}'][1].example,
uid: 1,
- description: 'for testing of token deletion rotue',
+ description: 'for testing of token deletion route',
timestamp: Date.now(),
}],
});
@@ -172,8 +172,17 @@ describe('API', async () => {
return;
}
- it('should have examples when parameters are present', () => {
+ it('should have each path parameter defined in its context', () => {
method = _method;
+ if (!context[method].parameters) {
+ return;
+ }
+
+ const names = (path.match(/{[\w\-_]+}?/g) || []).map(match => match.slice(1, -1));
+ assert(context[method].parameters.map(param => (param.in === 'path' ? param.name : null)).filter(Boolean).every(name => names.includes(name)), `${method.toUpperCase()} ${path} has parameter(s) in path that are not defined in schema`);
+ });
+
+ it('should have examples when parameters are present', () => {
let parameters = context[method].parameters;
let testPath = path;
@@ -255,9 +264,11 @@ describe('API', async () => {
});
it('should successfully re-login if needed', async () => {
- const reloginPaths = ['/users/{uid}/password'];
- if (method === 'put' && reloginPaths.includes(path)) {
+ const reloginPaths = ['PUT /users/{uid}/password', 'DELETE /users/{uid}/sessions/{uuid}'];
+ if (reloginPaths.includes(`${method.toUpperCase()} ${path}`)) {
jar = await helpers.loginUser('admin', '123456');
+ const sessionUUIDs = await db.getObject('uid:1:sessionUUID:sessionId');
+ mocks.delete['/users/{uid}/sessions/{uuid}'][1].example = Object.keys(sessionUUIDs).pop();
// Retrieve CSRF token using cookie, to test Write API
const config = await request({