]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-comments.ts
dc47f8a4a41c83b9b3eef8835d8873b5b6aa6f38
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-comments.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { expect } from 'chai'
4 import { dateIsValid, testImage } from '@server/tests/shared'
5 import {
6 cleanupTests,
7 CommentsCommand,
8 createSingleServer,
9 PeerTubeServer,
10 setAccessTokensToServers,
11 setDefaultAccountAvatar,
12 setDefaultChannelAvatar
13 } from '@shared/server-commands'
14
15 describe('Test video comments', function () {
16 let server: PeerTubeServer
17 let videoId: number
18 let videoUUID: string
19 let threadId: number
20 let replyToDeleteId: number
21
22 let userAccessTokenServer1: string
23
24 let command: CommentsCommand
25
26 before(async function () {
27 this.timeout(30000)
28
29 server = await createSingleServer(1)
30
31 await setAccessTokensToServers([ server ])
32
33 const { id, uuid } = await server.videos.upload()
34 videoUUID = uuid
35 videoId = id
36
37 await setDefaultChannelAvatar(server)
38 await setDefaultAccountAvatar(server)
39
40 userAccessTokenServer1 = await server.users.generateUserAndToken('user1')
41
42 command = server.comments
43 })
44
45 describe('User comments', function () {
46
47 it('Should not have threads on this video', async function () {
48 const body = await command.listThreads({ videoId: videoUUID })
49
50 expect(body.total).to.equal(0)
51 expect(body.totalNotDeletedComments).to.equal(0)
52 expect(body.data).to.be.an('array')
53 expect(body.data).to.have.lengthOf(0)
54 })
55
56 it('Should create a thread in this video', async function () {
57 const text = 'my super first comment'
58
59 const comment = await command.createThread({ videoId: videoUUID, text })
60
61 expect(comment.inReplyToCommentId).to.be.null
62 expect(comment.text).equal('my super first comment')
63 expect(comment.videoId).to.equal(videoId)
64 expect(comment.id).to.equal(comment.threadId)
65 expect(comment.account.name).to.equal('root')
66 expect(comment.account.host).to.equal(server.host)
67 expect(comment.account.url).to.equal(server.url + '/accounts/root')
68 expect(comment.totalReplies).to.equal(0)
69 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
70 expect(dateIsValid(comment.createdAt as string)).to.be.true
71 expect(dateIsValid(comment.updatedAt as string)).to.be.true
72 })
73
74 it('Should list threads of this video', async function () {
75 const body = await command.listThreads({ videoId: videoUUID })
76
77 expect(body.total).to.equal(1)
78 expect(body.totalNotDeletedComments).to.equal(1)
79 expect(body.data).to.be.an('array')
80 expect(body.data).to.have.lengthOf(1)
81
82 const comment = body.data[0]
83 expect(comment.inReplyToCommentId).to.be.null
84 expect(comment.text).equal('my super first comment')
85 expect(comment.videoId).to.equal(videoId)
86 expect(comment.id).to.equal(comment.threadId)
87 expect(comment.account.name).to.equal('root')
88 expect(comment.account.host).to.equal(server.host)
89
90 for (const avatar of comment.account.avatars) {
91 await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatar.path, '.png')
92 }
93
94 expect(comment.totalReplies).to.equal(0)
95 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
96 expect(dateIsValid(comment.createdAt as string)).to.be.true
97 expect(dateIsValid(comment.updatedAt as string)).to.be.true
98
99 threadId = comment.threadId
100 })
101
102 it('Should get all the thread created', async function () {
103 const body = await command.getThread({ videoId: videoUUID, threadId })
104
105 const rootComment = body.comment
106 expect(rootComment.inReplyToCommentId).to.be.null
107 expect(rootComment.text).equal('my super first comment')
108 expect(rootComment.videoId).to.equal(videoId)
109 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
110 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
111 })
112
113 it('Should create multiple replies in this thread', async function () {
114 const text1 = 'my super answer to thread 1'
115 const created = await command.addReply({ videoId, toCommentId: threadId, text: text1 })
116 const childCommentId = created.id
117
118 const text2 = 'my super answer to answer of thread 1'
119 await command.addReply({ videoId, toCommentId: childCommentId, text: text2 })
120
121 const text3 = 'my second answer to thread 1'
122 await command.addReply({ videoId, toCommentId: threadId, text: text3 })
123 })
124
125 it('Should get correctly the replies', async function () {
126 const tree = await command.getThread({ videoId: videoUUID, threadId })
127
128 expect(tree.comment.text).equal('my super first comment')
129 expect(tree.children).to.have.lengthOf(2)
130
131 const firstChild = tree.children[0]
132 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
133 expect(firstChild.children).to.have.lengthOf(1)
134
135 const childOfFirstChild = firstChild.children[0]
136 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
137 expect(childOfFirstChild.children).to.have.lengthOf(0)
138
139 const secondChild = tree.children[1]
140 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
141 expect(secondChild.children).to.have.lengthOf(0)
142
143 replyToDeleteId = secondChild.comment.id
144 })
145
146 it('Should create other threads', async function () {
147 const text1 = 'super thread 2'
148 await command.createThread({ videoId: videoUUID, text: text1 })
149
150 const text2 = 'super thread 3'
151 await command.createThread({ videoId: videoUUID, text: text2 })
152 })
153
154 it('Should list the threads', async function () {
155 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
156
157 expect(body.total).to.equal(3)
158 expect(body.totalNotDeletedComments).to.equal(6)
159 expect(body.data).to.be.an('array')
160 expect(body.data).to.have.lengthOf(3)
161
162 expect(body.data[0].text).to.equal('my super first comment')
163 expect(body.data[0].totalReplies).to.equal(3)
164 expect(body.data[1].text).to.equal('super thread 2')
165 expect(body.data[1].totalReplies).to.equal(0)
166 expect(body.data[2].text).to.equal('super thread 3')
167 expect(body.data[2].totalReplies).to.equal(0)
168 })
169
170 it('Should delete a reply', async function () {
171 await command.delete({ videoId, commentId: replyToDeleteId })
172
173 {
174 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
175
176 expect(body.total).to.equal(3)
177 expect(body.totalNotDeletedComments).to.equal(5)
178 }
179
180 {
181 const tree = await command.getThread({ videoId: videoUUID, threadId })
182
183 expect(tree.comment.text).equal('my super first comment')
184 expect(tree.children).to.have.lengthOf(2)
185
186 const firstChild = tree.children[0]
187 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
188 expect(firstChild.children).to.have.lengthOf(1)
189
190 const childOfFirstChild = firstChild.children[0]
191 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
192 expect(childOfFirstChild.children).to.have.lengthOf(0)
193
194 const deletedChildOfFirstChild = tree.children[1]
195 expect(deletedChildOfFirstChild.comment.text).to.equal('')
196 expect(deletedChildOfFirstChild.comment.isDeleted).to.be.true
197 expect(deletedChildOfFirstChild.comment.deletedAt).to.not.be.null
198 expect(deletedChildOfFirstChild.comment.account).to.be.null
199 expect(deletedChildOfFirstChild.children).to.have.lengthOf(0)
200 }
201 })
202
203 it('Should delete a complete thread', async function () {
204 await command.delete({ videoId, commentId: threadId })
205
206 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
207 expect(body.total).to.equal(3)
208 expect(body.data).to.be.an('array')
209 expect(body.data).to.have.lengthOf(3)
210
211 expect(body.data[0].text).to.equal('')
212 expect(body.data[0].isDeleted).to.be.true
213 expect(body.data[0].deletedAt).to.not.be.null
214 expect(body.data[0].account).to.be.null
215 expect(body.data[0].totalReplies).to.equal(2)
216 expect(body.data[1].text).to.equal('super thread 2')
217 expect(body.data[1].totalReplies).to.equal(0)
218 expect(body.data[2].text).to.equal('super thread 3')
219 expect(body.data[2].totalReplies).to.equal(0)
220 })
221
222 it('Should count replies from the video author correctly', async function () {
223 await command.createThread({ videoId: videoUUID, text: 'my super first comment' })
224
225 const { data } = await command.listThreads({ videoId: videoUUID })
226 const threadId2 = data[0].threadId
227
228 const text2 = 'a first answer to thread 4 by a third party'
229 await command.addReply({ token: userAccessTokenServer1, videoId, toCommentId: threadId2, text: text2 })
230
231 const text3 = 'my second answer to thread 4'
232 await command.addReply({ videoId, toCommentId: threadId2, text: text3 })
233
234 const tree = await command.getThread({ videoId: videoUUID, threadId: threadId2 })
235 expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1)
236 })
237 })
238
239 describe('All instance comments', function () {
240
241 it('Should list instance comments as admin', async function () {
242 const { data } = await command.listForAdmin({ start: 0, count: 1 })
243
244 expect(data[0].text).to.equal('my second answer to thread 4')
245 })
246
247 it('Should filter instance comments by isLocal', async function () {
248 const { total, data } = await command.listForAdmin({ isLocal: false })
249
250 expect(data).to.have.lengthOf(0)
251 expect(total).to.equal(0)
252 })
253
254 it('Should filter instance comments by onLocalVideo', async function () {
255 {
256 const { total, data } = await command.listForAdmin({ onLocalVideo: false })
257
258 expect(data).to.have.lengthOf(0)
259 expect(total).to.equal(0)
260 }
261
262 {
263 const { total, data } = await command.listForAdmin({ onLocalVideo: true })
264
265 expect(data).to.not.have.lengthOf(0)
266 expect(total).to.not.equal(0)
267 }
268 })
269
270 it('Should search instance comments by account', async function () {
271 const { total, data } = await command.listForAdmin({ searchAccount: 'user' })
272
273 expect(data).to.have.lengthOf(1)
274 expect(total).to.equal(1)
275
276 expect(data[0].text).to.equal('a first answer to thread 4 by a third party')
277 })
278
279 it('Should search instance comments by video', async function () {
280 {
281 const { total, data } = await command.listForAdmin({ searchVideo: 'video' })
282
283 expect(data).to.have.lengthOf(7)
284 expect(total).to.equal(7)
285 }
286
287 {
288 const { total, data } = await command.listForAdmin({ searchVideo: 'hello' })
289
290 expect(data).to.have.lengthOf(0)
291 expect(total).to.equal(0)
292 }
293 })
294
295 it('Should search instance comments', async function () {
296 const { total, data } = await command.listForAdmin({ search: 'super thread 3' })
297
298 expect(total).to.equal(1)
299
300 expect(data).to.have.lengthOf(1)
301 expect(data[0].text).to.equal('super thread 3')
302 })
303 })
304
305 after(async function () {
306 await cleanupTests([ server ])
307 })
308 })