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