]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/follows.ts
Introduce captions command
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / follows.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 addVideoCommentReply,
7 addVideoCommentThread,
8 cleanupTests,
9 completeVideoCheck,
10 createUser,
11 dateIsValid,
12 deleteVideoComment,
13 expectAccountFollows,
14 flushAndRunMultipleServers,
15 FollowsCommand,
16 getVideoCommentThreads,
17 getVideosList,
18 getVideoThreadComments,
19 rateVideo,
20 ServerInfo,
21 setAccessTokensToServers,
22 testCaptionFile,
23 uploadVideo,
24 userLogin,
25 waitJobs
26 } from '@shared/extra-utils'
27 import { Video, VideoComment, VideoCommentThreadTree, VideoPrivacy } from '@shared/models'
28
29 const expect = chai.expect
30
31 describe('Test follows', function () {
32 let servers: ServerInfo[] = []
33 let followsCommands: FollowsCommand[]
34
35 before(async function () {
36 this.timeout(30000)
37
38 servers = await flushAndRunMultipleServers(3)
39 followsCommands = servers.map(s => s.followsCommand)
40
41 // Get the access tokens
42 await setAccessTokensToServers(servers)
43 })
44
45 it('Should not have followers', async function () {
46 for (const server of servers) {
47 const body = await server.followsCommand.getFollowers({ start: 0, count: 5, sort: 'createdAt' })
48 expect(body.total).to.equal(0)
49
50 const follows = body.data
51 expect(follows).to.be.an('array')
52 expect(follows.length).to.equal(0)
53 }
54 })
55
56 it('Should not have following', async function () {
57 for (const server of servers) {
58 const body = await server.followsCommand.getFollowings({ start: 0, count: 5, sort: 'createdAt' })
59 expect(body.total).to.equal(0)
60
61 const follows = body.data
62 expect(follows).to.be.an('array')
63 expect(follows.length).to.equal(0)
64 }
65 })
66
67 it('Should have server 1 following server 2 and 3', async function () {
68 this.timeout(30000)
69
70 await followsCommands[0].follow({ targets: [ servers[1].url, servers[2].url ] })
71
72 await waitJobs(servers)
73 })
74
75 it('Should have 2 followings on server 1', async function () {
76 const body = await followsCommands[0].getFollowings({ start: 0, count: 1, sort: 'createdAt' })
77 expect(body.total).to.equal(2)
78
79 let follows = body.data
80 expect(follows).to.be.an('array')
81 expect(follows.length).to.equal(1)
82
83 const body2 = await followsCommands[0].getFollowings({ start: 1, count: 1, sort: 'createdAt' })
84 follows = follows.concat(body2.data)
85
86 const server2Follow = follows.find(f => f.following.host === 'localhost:' + servers[1].port)
87 const server3Follow = follows.find(f => f.following.host === 'localhost:' + servers[2].port)
88
89 expect(server2Follow).to.not.be.undefined
90 expect(server3Follow).to.not.be.undefined
91 expect(server2Follow.state).to.equal('accepted')
92 expect(server3Follow.state).to.equal('accepted')
93 })
94
95 it('Should search/filter followings on server 1', async function () {
96 const sort = 'createdAt'
97 const start = 0
98 const count = 1
99
100 {
101 const search = ':' + servers[1].port
102
103 {
104 const body = await followsCommands[0].getFollowings({ start, count, sort, search })
105 expect(body.total).to.equal(1)
106
107 const follows = body.data
108 expect(follows.length).to.equal(1)
109 expect(follows[0].following.host).to.equal('localhost:' + servers[1].port)
110 }
111
112 {
113 const body = await followsCommands[0].getFollowings({ start, count, sort, search, state: 'accepted' })
114 expect(body.total).to.equal(1)
115 expect(body.data).to.have.lengthOf(1)
116 }
117
118 {
119 const body = await followsCommands[0].getFollowings({ start, count, sort, search, state: 'accepted', actorType: 'Person' })
120 expect(body.total).to.equal(0)
121 expect(body.data).to.have.lengthOf(0)
122 }
123
124 {
125 const body = await followsCommands[0].getFollowings({
126 start,
127 count,
128 sort,
129 search,
130 state: 'accepted',
131 actorType: 'Application'
132 })
133 expect(body.total).to.equal(1)
134 expect(body.data).to.have.lengthOf(1)
135 }
136
137 {
138 const body = await followsCommands[0].getFollowings({ start, count, sort, search, state: 'pending' })
139 expect(body.total).to.equal(0)
140 expect(body.data).to.have.lengthOf(0)
141 }
142 }
143
144 {
145 const body = await followsCommands[0].getFollowings({ start, count, sort, search: 'bla' })
146 expect(body.total).to.equal(0)
147
148 expect(body.data.length).to.equal(0)
149 }
150 })
151
152 it('Should have 0 followings on server 2 and 3', async function () {
153 for (const server of [ servers[1], servers[2] ]) {
154 const body = await server.followsCommand.getFollowings({ start: 0, count: 5, sort: 'createdAt' })
155 expect(body.total).to.equal(0)
156
157 const follows = body.data
158 expect(follows).to.be.an('array')
159 expect(follows.length).to.equal(0)
160 }
161 })
162
163 it('Should have 1 followers on server 2 and 3', async function () {
164 for (const server of [ servers[1], servers[2] ]) {
165 const body = await server.followsCommand.getFollowers({ start: 0, count: 1, sort: 'createdAt' })
166 expect(body.total).to.equal(1)
167
168 const follows = body.data
169 expect(follows).to.be.an('array')
170 expect(follows.length).to.equal(1)
171 expect(follows[0].follower.host).to.equal('localhost:' + servers[0].port)
172 }
173 })
174
175 it('Should search/filter followers on server 2', async function () {
176 const start = 0
177 const count = 5
178 const sort = 'createdAt'
179
180 {
181 const search = servers[0].port + ''
182
183 {
184 const body = await followsCommands[2].getFollowers({ start, count, sort, search })
185 expect(body.total).to.equal(1)
186
187 const follows = body.data
188 expect(follows.length).to.equal(1)
189 expect(follows[0].following.host).to.equal('localhost:' + servers[2].port)
190 }
191
192 {
193 const body = await followsCommands[2].getFollowers({ start, count, sort, search, state: 'accepted' })
194 expect(body.total).to.equal(1)
195 expect(body.data).to.have.lengthOf(1)
196 }
197
198 {
199 const body = await followsCommands[2].getFollowers({ start, count, sort, search, state: 'accepted', actorType: 'Person' })
200 expect(body.total).to.equal(0)
201 expect(body.data).to.have.lengthOf(0)
202 }
203
204 {
205 const body = await followsCommands[2].getFollowers({
206 start,
207 count,
208 sort,
209 search,
210 state: 'accepted',
211 actorType: 'Application'
212 })
213 expect(body.total).to.equal(1)
214 expect(body.data).to.have.lengthOf(1)
215 }
216
217 {
218 const body = await followsCommands[2].getFollowers({ start, count, sort, search, state: 'pending' })
219 expect(body.total).to.equal(0)
220 expect(body.data).to.have.lengthOf(0)
221 }
222 }
223
224 {
225 const body = await followsCommands[2].getFollowers({ start, count, sort, search: 'bla' })
226 expect(body.total).to.equal(0)
227
228 const follows = body.data
229 expect(follows.length).to.equal(0)
230 }
231 })
232
233 it('Should have 0 followers on server 1', async function () {
234 const body = await followsCommands[0].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
235 expect(body.total).to.equal(0)
236
237 const follows = body.data
238 expect(follows).to.be.an('array')
239 expect(follows.length).to.equal(0)
240 })
241
242 it('Should have the correct follows counts', async function () {
243 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 2 })
244 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
245 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[2].port, followers: 1, following: 0 })
246
247 // Server 2 and 3 does not know server 1 follow another server (there was not a refresh)
248 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
249 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
250
251 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
252 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[2].port, followers: 1, following: 0 })
253 })
254
255 it('Should unfollow server 3 on server 1', async function () {
256 this.timeout(5000)
257
258 await followsCommands[0].unfollow({ target: servers[2] })
259
260 await waitJobs(servers)
261 })
262
263 it('Should not follow server 3 on server 1 anymore', async function () {
264 const body = await followsCommands[0].getFollowings({ start: 0, count: 2, sort: 'createdAt' })
265 expect(body.total).to.equal(1)
266
267 const follows = body.data
268 expect(follows).to.be.an('array')
269 expect(follows.length).to.equal(1)
270
271 expect(follows[0].following.host).to.equal('localhost:' + servers[1].port)
272 })
273
274 it('Should not have server 1 as follower on server 3 anymore', async function () {
275 const body = await followsCommands[2].getFollowers({ start: 0, count: 1, sort: 'createdAt' })
276 expect(body.total).to.equal(0)
277
278 const follows = body.data
279 expect(follows).to.be.an('array')
280 expect(follows.length).to.equal(0)
281 })
282
283 it('Should have the correct follows counts 2', async function () {
284 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
285 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
286
287 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
288 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
289
290 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 0 })
291 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[2].port, followers: 0, following: 0 })
292 })
293
294 it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {
295 this.timeout(60000)
296
297 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
298 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
299
300 await waitJobs(servers)
301
302 let res = await getVideosList(servers[0].url)
303 expect(res.body.total).to.equal(1)
304 expect(res.body.data[0].name).to.equal('server2')
305
306 res = await getVideosList(servers[1].url)
307 expect(res.body.total).to.equal(1)
308 expect(res.body.data[0].name).to.equal('server2')
309
310 res = await getVideosList(servers[2].url)
311 expect(res.body.total).to.equal(1)
312 expect(res.body.data[0].name).to.equal('server3')
313 })
314
315 describe('Should propagate data on a new following', function () {
316 let video4: Video
317
318 before(async function () {
319 this.timeout(50000)
320
321 const video4Attributes = {
322 name: 'server3-4',
323 category: 2,
324 nsfw: true,
325 licence: 6,
326 tags: [ 'tag1', 'tag2', 'tag3' ]
327 }
328
329 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-2' })
330 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-3' })
331 await uploadVideo(servers[2].url, servers[2].accessToken, video4Attributes)
332 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-5' })
333 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-6' })
334
335 {
336 const user = { username: 'captain', password: 'password' }
337 await createUser({ url: servers[2].url, accessToken: servers[2].accessToken, username: user.username, password: user.password })
338 const userAccessToken = await userLogin(servers[2], user)
339
340 const resVideos = await getVideosList(servers[2].url)
341 video4 = resVideos.body.data.find(v => v.name === 'server3-4')
342
343 {
344 await rateVideo(servers[2].url, servers[2].accessToken, video4.id, 'like')
345 await rateVideo(servers[2].url, userAccessToken, video4.id, 'dislike')
346 }
347
348 {
349 {
350 const text = 'my super first comment'
351 const res = await addVideoCommentThread(servers[2].url, servers[2].accessToken, video4.id, text)
352 const threadId = res.body.comment.id
353
354 const text1 = 'my super answer to thread 1'
355 const childCommentRes = await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text1)
356 const childCommentId = childCommentRes.body.comment.id
357
358 const text2 = 'my super answer to answer of thread 1'
359 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, childCommentId, text2)
360
361 const text3 = 'my second answer to thread 1'
362 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text3)
363 }
364
365 {
366 const text = 'will be deleted'
367 const res = await addVideoCommentThread(servers[2].url, servers[2].accessToken, video4.id, text)
368 const threadId = res.body.comment.id
369
370 const text1 = 'answer to deleted'
371 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text1)
372
373 const text2 = 'will also be deleted'
374 const childCommentRes = await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text2)
375 const childCommentId = childCommentRes.body.comment.id
376
377 const text3 = 'my second answer to deleted'
378 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, childCommentId, text3)
379
380 await deleteVideoComment(servers[2].url, servers[2].accessToken, video4.id, threadId)
381 await deleteVideoComment(servers[2].url, servers[2].accessToken, video4.id, childCommentId)
382 }
383 }
384
385 {
386 await servers[2].captionsCommand.createVideoCaption({
387 language: 'ar',
388 videoId: video4.id,
389 fixture: 'subtitle-good2.vtt'
390 })
391 }
392 }
393
394 await waitJobs(servers)
395
396 // Server 1 follows server 3
397 await followsCommands[0].follow({ targets: [ servers[2].url ] })
398
399 await waitJobs(servers)
400 })
401
402 it('Should have the correct follows counts 3', async function () {
403 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 2 })
404 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
405 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[2].port, followers: 1, following: 0 })
406
407 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
408 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
409
410 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
411 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[2].port, followers: 1, following: 0 })
412 })
413
414 it('Should have propagated videos', async function () {
415 const res = await getVideosList(servers[0].url)
416 expect(res.body.total).to.equal(7)
417
418 const video2 = res.body.data.find(v => v.name === 'server3-2')
419 video4 = res.body.data.find(v => v.name === 'server3-4')
420 const video6 = res.body.data.find(v => v.name === 'server3-6')
421
422 expect(video2).to.not.be.undefined
423 expect(video4).to.not.be.undefined
424 expect(video6).to.not.be.undefined
425
426 const isLocal = false
427 const checkAttributes = {
428 name: 'server3-4',
429 category: 2,
430 licence: 6,
431 language: 'zh',
432 nsfw: true,
433 description: 'my super description',
434 support: 'my super support text',
435 account: {
436 name: 'root',
437 host: 'localhost:' + servers[2].port
438 },
439 isLocal,
440 commentsEnabled: true,
441 downloadEnabled: true,
442 duration: 5,
443 tags: [ 'tag1', 'tag2', 'tag3' ],
444 privacy: VideoPrivacy.PUBLIC,
445 likes: 1,
446 dislikes: 1,
447 channel: {
448 displayName: 'Main root channel',
449 name: 'root_channel',
450 description: '',
451 isLocal
452 },
453 fixture: 'video_short.webm',
454 files: [
455 {
456 resolution: 720,
457 size: 218910
458 }
459 ]
460 }
461 await completeVideoCheck(servers[0].url, video4, checkAttributes)
462 })
463
464 it('Should have propagated comments', async function () {
465 const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5, 'createdAt')
466
467 expect(res1.body.total).to.equal(2)
468 expect(res1.body.data).to.be.an('array')
469 expect(res1.body.data).to.have.lengthOf(2)
470
471 {
472 const comment: VideoComment = res1.body.data[0]
473 expect(comment.inReplyToCommentId).to.be.null
474 expect(comment.text).equal('my super first comment')
475 expect(comment.videoId).to.equal(video4.id)
476 expect(comment.id).to.equal(comment.threadId)
477 expect(comment.account.name).to.equal('root')
478 expect(comment.account.host).to.equal('localhost:' + servers[2].port)
479 expect(comment.totalReplies).to.equal(3)
480 expect(dateIsValid(comment.createdAt as string)).to.be.true
481 expect(dateIsValid(comment.updatedAt as string)).to.be.true
482
483 const threadId = comment.threadId
484
485 const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId)
486
487 const tree: VideoCommentThreadTree = res2.body
488 expect(tree.comment.text).equal('my super first comment')
489 expect(tree.children).to.have.lengthOf(2)
490
491 const firstChild = tree.children[0]
492 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
493 expect(firstChild.children).to.have.lengthOf(1)
494
495 const childOfFirstChild = firstChild.children[0]
496 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
497 expect(childOfFirstChild.children).to.have.lengthOf(0)
498
499 const secondChild = tree.children[1]
500 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
501 expect(secondChild.children).to.have.lengthOf(0)
502 }
503
504 {
505 const deletedComment: VideoComment = res1.body.data[1]
506 expect(deletedComment).to.not.be.undefined
507 expect(deletedComment.isDeleted).to.be.true
508 expect(deletedComment.deletedAt).to.not.be.null
509 expect(deletedComment.text).to.equal('')
510 expect(deletedComment.inReplyToCommentId).to.be.null
511 expect(deletedComment.account).to.be.null
512 expect(deletedComment.totalReplies).to.equal(2)
513 expect(dateIsValid(deletedComment.deletedAt as string)).to.be.true
514
515 const res2 = await getVideoThreadComments(servers[0].url, video4.id, deletedComment.threadId)
516
517 const tree: VideoCommentThreadTree = res2.body
518 const [ commentRoot, deletedChildRoot ] = tree.children
519
520 expect(deletedChildRoot).to.not.be.undefined
521 expect(deletedChildRoot.comment.isDeleted).to.be.true
522 expect(deletedChildRoot.comment.deletedAt).to.not.be.null
523 expect(deletedChildRoot.comment.text).to.equal('')
524 expect(deletedChildRoot.comment.inReplyToCommentId).to.equal(deletedComment.id)
525 expect(deletedChildRoot.comment.account).to.be.null
526 expect(deletedChildRoot.children).to.have.lengthOf(1)
527
528 const answerToDeletedChild = deletedChildRoot.children[0]
529 expect(answerToDeletedChild.comment).to.not.be.undefined
530 expect(answerToDeletedChild.comment.inReplyToCommentId).to.equal(deletedChildRoot.comment.id)
531 expect(answerToDeletedChild.comment.text).to.equal('my second answer to deleted')
532 expect(answerToDeletedChild.comment.account.name).to.equal('root')
533
534 expect(commentRoot.comment).to.not.be.undefined
535 expect(commentRoot.comment.inReplyToCommentId).to.equal(deletedComment.id)
536 expect(commentRoot.comment.text).to.equal('answer to deleted')
537 expect(commentRoot.comment.account.name).to.equal('root')
538 }
539 })
540
541 it('Should have propagated captions', async function () {
542 const body = await servers[0].captionsCommand.listVideoCaptions({ videoId: video4.id })
543 expect(body.total).to.equal(1)
544 expect(body.data).to.have.lengthOf(1)
545
546 const caption1 = body.data[0]
547 expect(caption1.language.id).to.equal('ar')
548 expect(caption1.language.label).to.equal('Arabic')
549 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/.+-ar.vtt$'))
550 await testCaptionFile(servers[0].url, caption1.captionPath, 'Subtitle good 2.')
551 })
552
553 it('Should unfollow server 3 on server 1 and does not list server 3 videos', async function () {
554 this.timeout(5000)
555
556 await followsCommands[0].unfollow({ target: servers[2] })
557
558 await waitJobs(servers)
559
560 const res = await getVideosList(servers[0].url)
561 expect(res.body.total).to.equal(1)
562 })
563
564 })
565
566 after(async function () {
567 await cleanupTests(servers)
568 })
569 })