From cb32e32ae307d0111597220a5551e5a6997cd2dd Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 17 Dec 2020 21:54:38 -0500 Subject: [PATCH] feat: fix more tests, add more routes, update api test suite --- public/openapi/read.yaml | 2 +- public/openapi/read/admin/advanced/cache.yaml | 9 +++++ public/openapi/read/category/cid/id.yaml | 6 ++++ public/openapi/read/post/pid/id.yaml | 6 ++++ public/openapi/read/register/complete.yaml | 9 ++++- public/openapi/read/topic/tid/id.yaml | 6 ++++ test/api.js | 35 ++++++++++++++++--- 7 files changed, 66 insertions(+), 7 deletions(-) diff --git a/public/openapi/read.yaml b/public/openapi/read.yaml index f4e684e1e2..f6e6312f37 100644 --- a/public/openapi/read.yaml +++ b/public/openapi/read.yaml @@ -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: diff --git a/public/openapi/read/admin/advanced/cache.yaml b/public/openapi/read/admin/advanced/cache.yaml index 06d7890971..c6daf4fbea 100644 --- a/public/openapi/read/admin/advanced/cache.yaml +++ b/public/openapi/read/admin/advanced/cache.yaml @@ -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: "" diff --git a/public/openapi/read/category/cid/id.yaml b/public/openapi/read/category/cid/id.yaml index e70f774b07..298147dea3 100644 --- a/public/openapi/read/category/cid/id.yaml +++ b/public/openapi/read/category/cid/id.yaml @@ -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 diff --git a/public/openapi/read/post/pid/id.yaml b/public/openapi/read/post/pid/id.yaml index 527ee4ec29..58dd822f30 100644 --- a/public/openapi/read/post/pid/id.yaml +++ b/public/openapi/read/post/pid/id.yaml @@ -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 diff --git a/public/openapi/read/register/complete.yaml b/public/openapi/read/register/complete.yaml index fe6471c850..b0308c7094 100644 --- a/public/openapi/read/register/complete.yaml +++ b/public/openapi/read/register/complete.yaml @@ -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: diff --git a/public/openapi/read/topic/tid/id.yaml b/public/openapi/read/topic/tid/id.yaml index 5305a99c7f..dd77ad2670 100644 --- a/public/openapi/read/topic/tid/id.yaml +++ b/public/openapi/read/topic/tid/id.yaml @@ -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 diff --git a/test/api.js b/test/api.js index cd9b7e0141..3a9c82f4ee 100644 --- a/test/api.js +++ b/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 http200 = context[method].responses['200']; + if (!http200) { return; } - const hasJSON = has200.content && has200.content['application/json']; + 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?