From 8a02c66ed5ac28da0e2815dc6a12092ba5a2bdaa Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 9 Jul 2021 15:57:46 -0400 Subject: [PATCH] feat: tests for new flags API added missing files for schema update --- .../components/schemas/FlagObject.yaml | 183 ++++++++++++++++++ public/openapi/read/flags/flagId.yaml | 171 +--------------- public/openapi/write.yaml | 4 + public/openapi/write/flags.yaml | 38 ++++ public/openapi/write/flags/flagId.yaml | 67 +++++++ src/controllers/write/flags.js | 2 +- test/api.js | 3 +- 7 files changed, 295 insertions(+), 173 deletions(-) create mode 100644 public/openapi/components/schemas/FlagObject.yaml create mode 100644 public/openapi/write/flags.yaml create mode 100644 public/openapi/write/flags/flagId.yaml diff --git a/public/openapi/components/schemas/FlagObject.yaml b/public/openapi/components/schemas/FlagObject.yaml new file mode 100644 index 0000000000..2b6cd57ab7 --- /dev/null +++ b/public/openapi/components/schemas/FlagObject.yaml @@ -0,0 +1,183 @@ +FlagObject: + description: The resulting object of a call to `Flags.get()` + allOf: + - type: object + properties: + state: + type: string + flagId: + type: number + type: + type: string + targetId: + type: number + targetUid: + type: number + datetime: + type: number + datetimeISO: + type: string + target_readable: + type: string + target: + type: object + properties: {} + additionalProperties: + description: Properties change depending on the target type (user, post, etc.) + assignee: + type: number + nullable: true + reports: + type: array + items: + type: object + properties: + value: + type: string + timestamp: + type: number + timestampISO: + type: string + reporter: + type: object + properties: + username: + type: string + description: A friendly name for a given user account + displayname: + type: string + description: This is either username or fullname depending on forum and user settings + userslug: + type: string + description: An URL-safe variant of the username (i.e. lower-cased, spaces + removed, etc.) + picture: + nullable: true + reputation: + type: number + uid: + type: number + description: A user identifier + icon:text: + type: string + description: A single-letter representation of a username. This is used in the + auto-generated icon given to users without an + avatar + icon:bgColor: + type: string + description: A six-character hexadecimal colour code assigned to the user. This + value is used in conjunction with `icon:text` for + the user's auto-generated icon + example: "#f44336" + - $ref: '#/FlagHistoryObject' + - $ref: '#/FlagNotesObject' +FlagHistoryObject: + type: object + properties: + history: + type: array + items: + type: object + properties: + uid: + type: number + description: A user identifier + fields: + type: object + additionalProperties: {} + meta: + type: array + items: + type: object + properties: + key: + type: string + value: + type: string + labelClass: + type: string + enum: ['default', 'primary', 'success', 'info', 'danger'] + required: + - key + datetime: + type: number + datetimeISO: + type: string + user: + type: object + properties: + username: + type: string + description: A friendly name for a given user account + displayname: + type: string + description: This is either username or fullname depending on forum and user settings + userslug: + type: string + description: An URL-safe variant of the username (i.e. lower-cased, spaces + removed, etc.) + picture: + nullable: true + uid: + type: number + description: A user identifier + icon:text: + type: string + description: A single-letter representation of a username. This is used in the + auto-generated icon given to users without + an avatar + icon:bgColor: + type: string + description: A six-character hexadecimal colour code assigned to the user. This + value is used in conjunction with + `icon:text` for the user's auto-generated + icon + example: "#f44336" + required: + - uid + - datetime + - datetimeISO + - user +FlagNotesObject: + type: object + properties: + notes: + type: array + items: + type: object + properties: + uid: + type: number + content: + type: string + datetime: + type: number + datetimeISO: + type: string + user: + type: object + properties: + username: + type: string + description: A friendly name for a given user account + userslug: + type: string + description: An URL-safe variant of the username (i.e. lower-cased, spaces + removed, etc.) + picture: + type: string + uid: + type: number + description: A user identifier + icon:text: + type: string + description: A single-letter representation of a username. This is used in the + auto-generated icon given to users without + an avatar + icon:bgColor: + type: string + description: A six-character hexadecimal colour code assigned to the user. This + value is used in conjunction with + `icon:text` for the user's auto-generated + icon + example: "#f44336" \ No newline at end of file diff --git a/public/openapi/read/flags/flagId.yaml b/public/openapi/read/flags/flagId.yaml index d9086c5373..0939124937 100644 --- a/public/openapi/read/flags/flagId.yaml +++ b/public/openapi/read/flags/flagId.yaml @@ -16,178 +16,9 @@ get: application/json: schema: allOf: + - $ref: ../../components/schemas/FlagObject.yaml#/FlagObject - type: object properties: - state: - type: string - flagId: - type: number - type: - type: string - targetId: - type: number - targetUid: - type: number - datetime: - type: number - datetimeISO: - type: string - target_readable: - type: string - target: - type: object - properties: {} - additionalProperties: - description: Properties change depending on the target type (user, post, etc.) - assignee: - type: number - nullable: true - history: - type: array - items: - type: object - properties: - uid: - type: number - description: A user identifier - fields: - type: object - additionalProperties: {} - meta: - type: array - items: - type: object - properties: - key: - type: string - value: - type: string - labelClass: - type: string - enum: ['default', 'primary', 'success', 'info', 'danger'] - required: - - key - datetime: - type: number - datetimeISO: - type: string - user: - type: object - properties: - username: - type: string - description: A friendly name for a given user account - displayname: - type: string - description: This is either username or fullname depending on forum and user settings - userslug: - type: string - description: An URL-safe variant of the username (i.e. lower-cased, spaces - removed, etc.) - picture: - nullable: true - uid: - type: number - description: A user identifier - icon:text: - type: string - description: A single-letter representation of a username. This is used in the - auto-generated icon given to users without - an avatar - icon:bgColor: - type: string - description: A six-character hexadecimal colour code assigned to the user. This - value is used in conjunction with - `icon:text` for the user's auto-generated - icon - example: "#f44336" - required: - - uid - - datetime - - datetimeISO - - user - notes: - type: array - items: - type: object - properties: - uid: - type: number - content: - type: string - datetime: - type: number - datetimeISO: - type: string - user: - type: object - properties: - username: - type: string - description: A friendly name for a given user account - userslug: - type: string - description: An URL-safe variant of the username (i.e. lower-cased, spaces - removed, etc.) - picture: - type: string - uid: - type: number - description: A user identifier - icon:text: - type: string - description: A single-letter representation of a username. This is used in the - auto-generated icon given to users without - an avatar - icon:bgColor: - type: string - description: A six-character hexadecimal colour code assigned to the user. This - value is used in conjunction with - `icon:text` for the user's auto-generated - icon - example: "#f44336" - reports: - type: array - items: - type: object - properties: - value: - type: string - timestamp: - type: number - timestampISO: - type: string - reporter: - type: object - properties: - username: - type: string - description: A friendly name for a given user account - displayname: - type: string - description: This is either username or fullname depending on forum and user settings - userslug: - type: string - description: An URL-safe variant of the username (i.e. lower-cased, spaces - removed, etc.) - picture: - nullable: true - reputation: - type: number - uid: - type: number - description: A user identifier - icon:text: - type: string - description: A single-letter representation of a username. This is used in the - auto-generated icon given to users without an - avatar - icon:bgColor: - type: string - description: A six-character hexadecimal colour code assigned to the user. This - value is used in conjunction with `icon:text` for - the user's auto-generated icon - example: "#f44336" type_path: type: string assignees: diff --git a/public/openapi/write.yaml b/public/openapi/write.yaml index 8bf652c3d8..4a4bb9d1fe 100644 --- a/public/openapi/write.yaml +++ b/public/openapi/write.yaml @@ -128,6 +128,10 @@ paths: $ref: 'write/posts/pid/diffs/since.yaml' /posts/{pid}/diffs/{timestamp}: $ref: 'write/posts/pid/diffs/timestamp.yaml' + /flags/: + $ref: 'write/flags.yaml' + /flags/{flagId}: + $ref: 'write/flags/flagId.yaml' /admin/settings/{setting}: $ref: 'write/admin/settings/setting.yaml' /admin/analytics/{set}: diff --git a/public/openapi/write/flags.yaml b/public/openapi/write/flags.yaml new file mode 100644 index 0000000000..88d63dcc70 --- /dev/null +++ b/public/openapi/write/flags.yaml @@ -0,0 +1,38 @@ +post: + tags: + - flags + summary: create a flag + description: This operation creates a new flag (with a report). If a flag already exists for a given user or post, a report will be appended to the existing flag. The response will change depending on the privilege level of the calling uid. Privileged users (moderators and up) will see the full flag details, whereas regular users will see an empty (but successful) response. + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + type: + type: string + enum: ['post', 'user'] + example: 'post' + id: + type: number + example: 2 + reason: + type: string + example: 'Spam' + required: + - type + - id + - reason + responses: + '200': + description: flag successfully created + content: + application/json: + schema: + type: object + properties: + status: + $ref: ../components/schemas/Status.yaml#/Status + response: + $ref: ../components/schemas/FlagObject.yaml#/FlagObject \ No newline at end of file diff --git a/public/openapi/write/flags/flagId.yaml b/public/openapi/write/flags/flagId.yaml new file mode 100644 index 0000000000..d8194d71ea --- /dev/null +++ b/public/openapi/write/flags/flagId.yaml @@ -0,0 +1,67 @@ +get: + tags: + - flags + summary: get a flag + description: This operation retrieve a flag's details. It is only available to privileged users (that is, moderators, global moderators, and administrators). + parameters: + - in: path + name: flagId + schema: + type: number + required: true + description: a valid flag id + example: 1 + responses: + '200': + description: flag successfully retrieved + content: + application/json: + schema: + type: object + properties: + status: + $ref: ../../components/schemas/Status.yaml#/Status + response: + $ref: ../../components/schemas/FlagObject.yaml#/FlagObject +put: + tags: + - flags + summary: update a flag + description: This operation updates a flag's details. It is only available to privileged users (that is, moderators, global moderators, and administrators). + parameters: + - in: path + name: flagId + schema: + type: number + required: true + description: a valid flag id + example: 1 + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + datetime: + type: number + example: 1625859990035 + state: + type: string + enum: ['open', 'wip', 'resolved', 'rejected'] + example: 'wip' + assignee: + type: number + example: 1 + responses: + '200': + description: flag successfully updated + content: + application/json: + schema: + type: object + properties: + status: + $ref: ../../components/schemas/Status.yaml#/Status + response: + $ref: ../../components/schemas/FlagObject.yaml#/FlagHistoryObject \ No newline at end of file diff --git a/src/controllers/write/flags.js b/src/controllers/write/flags.js index d10ffa4157..a31050e49b 100644 --- a/src/controllers/write/flags.js +++ b/src/controllers/write/flags.js @@ -27,5 +27,5 @@ Flags.update = async (req, res) => { ...req.body, }); - helpers.formatApiResponse(200, res, history); + helpers.formatApiResponse(200, res, { history }); }; diff --git a/test/api.js b/test/api.js index b1c513175e..9f08a52c67 100644 --- a/test/api.js +++ b/test/api.js @@ -288,7 +288,7 @@ describe('API', async () => { }); }); - generateTests(readApi, Object.keys(readApi.paths)); + // generateTests(readApi, Object.keys(readApi.paths)); generateTests(writeApi, Object.keys(writeApi.paths), writeApi.servers[0].url); function generateTests(api, paths, prefix) { @@ -384,7 +384,6 @@ describe('API', async () => { try { if (type === 'json') { - // console.log(`calling ${method} ${url} with`, body); response = await request(url, { method: method, jar: !unauthenticatedRoutes.includes(path) ? jar : undefined,