mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-30 18:46:01 +01:00 
			
		
		
		
	test: post diff deletion tests
This commit is contained in:
		| @@ -116,6 +116,8 @@ paths: | |||||||
|     $ref: 'write/posts/pid/diffs.yaml' |     $ref: 'write/posts/pid/diffs.yaml' | ||||||
|   /posts/{pid}/diffs/{since}: |   /posts/{pid}/diffs/{since}: | ||||||
|     $ref: 'write/posts/pid/diffs/since.yaml' |     $ref: 'write/posts/pid/diffs/since.yaml' | ||||||
|  |   /posts/{pid}/diffs/{timestamp}: | ||||||
|  |     $ref: 'write/posts/pid/diffs/timestamp.yaml' | ||||||
|   /admin/settings/{setting}: |   /admin/settings/{setting}: | ||||||
|     $ref: 'write/admin/settings/setting.yaml' |     $ref: 'write/admin/settings/setting.yaml' | ||||||
|   /files/: |   /files/: | ||||||
|   | |||||||
| @@ -39,3 +39,5 @@ get: | |||||||
|                           type: string |                           type: string | ||||||
|                   editable: |                   editable: | ||||||
|                     type: boolean |                     type: boolean | ||||||
|  |                   deletable: | ||||||
|  |                     type: boolean | ||||||
|   | |||||||
							
								
								
									
										37
									
								
								public/openapi/write/posts/pid/diffs/timestamp.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								public/openapi/write/posts/pid/diffs/timestamp.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | 		const lastTimestampIndex = timestamps.length - 1; | ||||||
|  |  | ||||||
| 		if (timestamp === String(post[0].timestamp)) { | 		if (timestamp === String(post[0].timestamp)) { | ||||||
|  | 			// Deleting oldest diff, so history rewrite is not needed | ||||||
| 			return Promise.all([ | 			return Promise.all([ | ||||||
| 				db.delete(`diff:${pid}.${timestamps[lastTimestampIndex]}`), | 				db.delete(`diff:${pid}.${timestamps[lastTimestampIndex]}`), | ||||||
| 				db.listRemoveAll(`post:${pid}:diffs`, timestamps[lastTimestampIndex]), | 				db.listRemoveAll(`post:${pid}:diffs`, timestamps[lastTimestampIndex]), | ||||||
| @@ -102,6 +103,7 @@ module.exports = function (Posts) { | |||||||
|  |  | ||||||
| 		/* eslint-disable no-await-in-loop */ | 		/* eslint-disable no-await-in-loop */ | ||||||
| 		for (let i = lastTimestampIndex; i >= timestampIndex; --i) { | 		for (let i = lastTimestampIndex; i >= timestampIndex; --i) { | ||||||
|  | 			// Recreate older diffs with skipping the deleted diff | ||||||
| 			const newContentIndex = i === timestampIndex ? i - 2 : i - 1; | 			const newContentIndex = i === timestampIndex ? i - 2 : i - 1; | ||||||
| 			const timestampToUpdate = newContentIndex + 1; | 			const timestampToUpdate = newContentIndex + 1; | ||||||
| 			const newContent = newContentIndex < 0 ? postContent : versionContents[timestamps[newContentIndex]]; | 			const newContent = newContentIndex < 0 ? postContent : versionContents[timestamps[newContentIndex]]; | ||||||
|   | |||||||
| @@ -647,6 +647,29 @@ describe('Post\'s', function () { | |||||||
| 				done(); | 				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 () { | 	describe('move', function () { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user