From 72b050b4a8d28d652b900b3f728d2acfdadb5e33 Mon Sep 17 00:00:00 2001 From: gasoved Date: Fri, 29 Jan 2021 08:26:50 +0300 Subject: [PATCH] test: post diff deletion tests --- public/openapi/write.yaml | 2 + public/openapi/write/posts/pid/diffs.yaml | 4 +- .../write/posts/pid/diffs/timestamp.yaml | 37 +++++++++++++++++++ src/posts/diffs.js | 2 + test/posts.js | 23 ++++++++++++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 public/openapi/write/posts/pid/diffs/timestamp.yaml diff --git a/public/openapi/write.yaml b/public/openapi/write.yaml index 9510876c44..8b2592412d 100644 --- a/public/openapi/write.yaml +++ b/public/openapi/write.yaml @@ -116,6 +116,8 @@ paths: $ref: 'write/posts/pid/diffs.yaml' /posts/{pid}/diffs/{since}: $ref: 'write/posts/pid/diffs/since.yaml' + /posts/{pid}/diffs/{timestamp}: + $ref: 'write/posts/pid/diffs/timestamp.yaml' /admin/settings/{setting}: $ref: 'write/admin/settings/setting.yaml' /files/: diff --git a/public/openapi/write/posts/pid/diffs.yaml b/public/openapi/write/posts/pid/diffs.yaml index 40d4201185..5f0046781d 100644 --- a/public/openapi/write/posts/pid/diffs.yaml +++ b/public/openapi/write/posts/pid/diffs.yaml @@ -38,4 +38,6 @@ get: username: type: string editable: - type: boolean \ No newline at end of file + type: boolean + deletable: + type: boolean diff --git a/public/openapi/write/posts/pid/diffs/timestamp.yaml b/public/openapi/write/posts/pid/diffs/timestamp.yaml new file mode 100644 index 0000000000..205c7e1e37 --- /dev/null +++ b/public/openapi/write/posts/pid/diffs/timestamp.yaml @@ -0,0 +1,37 @@ +delete: + tags: + - posts + summary: delete a post diff + description: This operation deletes a post diff from its history. + parameters: + - in: path + name: pid + schema: + type: string + required: true + description: a valid post id + example: 2 + - in: path + name: timestamp + schema: + type: number + required: true + description: a valid UNIX timestamp + example: 1611850000000 + responses: + '200': + description: Post diff successfully deleted + content: + application/json: + schema: + type: object + properties: + status: + $ref: ../../../../components/schemas/Status.yaml#/Status + response: + type: object + properties: {} + '400': + $ref: ../../../../components/responses/400.yaml#/400 + '403': + $ref: ../../../../components/responses/403.yaml#/403 \ No newline at end of file diff --git a/src/posts/diffs.js b/src/posts/diffs.js index 2ca11b016b..5dc1de9ba5 100644 --- a/src/posts/diffs.js +++ b/src/posts/diffs.js @@ -84,6 +84,7 @@ module.exports = function (Posts) { const lastTimestampIndex = timestamps.length - 1; if (timestamp === String(post[0].timestamp)) { + // Deleting oldest diff, so history rewrite is not needed return Promise.all([ db.delete(`diff:${pid}.${timestamps[lastTimestampIndex]}`), db.listRemoveAll(`post:${pid}:diffs`, timestamps[lastTimestampIndex]), @@ -102,6 +103,7 @@ module.exports = function (Posts) { /* eslint-disable no-await-in-loop */ for (let i = lastTimestampIndex; i >= timestampIndex; --i) { + // Recreate older diffs with skipping the deleted diff const newContentIndex = i === timestampIndex ? i - 2 : i - 1; const timestampToUpdate = newContentIndex + 1; const newContent = newContentIndex < 0 ? postContent : versionContents[timestamps[newContentIndex]]; diff --git a/test/posts.js b/test/posts.js index b777230f93..9c32d90c89 100644 --- a/test/posts.js +++ b/test/posts.js @@ -647,6 +647,29 @@ describe('Post\'s', function () { done(); }); }); + + it('should not delete first diff of a post', async function () { + const timestamps = await posts.diffs.list(replyPid); + await assert.rejects(async () => { + await posts.diffs.delete(replyPid, timestamps[0], voterUid); + }, { + message: '[[error:invalid-data]]', + }); + }); + + it('should delete a post diff', async function () { + await socketPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'another edit has been made' }); + await socketPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'most recent edit' }); + const timestamp = (await posts.diffs.list(replyPid)).pop(); + await posts.diffs.delete(replyPid, timestamp, voterUid); + const differentTimestamp = (await posts.diffs.list(replyPid)).pop(); + assert.notStrictEqual(timestamp, differentTimestamp); + }); + + it('should load (oldest) diff and reconstruct post correctly after a diff deletion', async function () { + const data = await posts.diffs.load(replyPid, 0, voterUid); + assert.strictEqual(data.content, 'A reply to edit'); + }); }); describe('move', function () {