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