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