test: post diff deletion tests

v1.18.x
gasoved 4 years ago committed by Julian Lam
parent eb642f40b9
commit 72b050b4a8

@ -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/:

@ -38,4 +38,6 @@ get:
username:
type: string
editable:
type: boolean
type: boolean
deletable:
type: boolean

@ -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

@ -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]];

@ -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 () {

Loading…
Cancel
Save