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