1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
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'
8 const expect = chai.expect
10 describe('Test follows', function () {
11 let servers: PeerTubeServer[] = []
13 before(async function () {
16 servers = await createMultipleServers(3)
18 // Get the access tokens
19 await setAccessTokensToServers(servers)
22 describe('Data propagation after follow', function () {
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' })
31 for (const body of bodies) {
32 expect(body.total).to.equal(0)
34 const follows = body.data
35 expect(follows).to.be.an('array')
36 expect(follows).to.have.lengthOf(0)
41 it('Should have server 1 following root account of server 2 and server 3', async function () {
44 await servers[0].follows.follow({
45 hosts: [ servers[2].url ],
46 handles: [ 'root@' + servers[1].host ]
49 await waitJobs(servers)
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)
56 let follows = body.data
57 expect(follows).to.be.an('array')
58 expect(follows).to.have.lengthOf(1)
60 const body2 = await servers[0].follows.getFollowings({ start: 1, count: 1, sort: 'createdAt' })
61 follows = follows.concat(body2.data)
63 const server2Follow = follows.find(f => f.following.host === servers[1].host)
64 const server3Follow = follows.find(f => f.following.host === servers[2].host)
66 expect(server2Follow).to.not.be.undefined
67 expect(server2Follow.following.name).to.equal('root')
68 expect(server2Follow.state).to.equal('accepted')
70 expect(server3Follow).to.not.be.undefined
71 expect(server3Follow.following.name).to.equal('peertube')
72 expect(server3Follow.state).to.equal('accepted')
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)
80 const follows = body.data
81 expect(follows).to.be.an('array')
82 expect(follows).to.have.lengthOf(0)
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)
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)
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)
101 const follows = body.data
102 expect(follows).to.be.an('array')
103 expect(follows).to.have.lengthOf(0)
107 it('Should search/filter followings on server 1', async function () {
108 const sort = 'createdAt'
113 const search = ':' + servers[1].port
116 const body = await servers[0].follows.getFollowings({ start, count, sort, search })
117 expect(body.total).to.equal(1)
119 const follows = body.data
120 expect(follows).to.have.lengthOf(1)
121 expect(follows[0].following.host).to.equal(servers[1].host)
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)
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)
137 const body = await servers[0].follows.getFollowings({
143 actorType: 'Application'
145 expect(body.total).to.equal(0)
146 expect(body.data).to.have.lengthOf(0)
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)
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)
163 const body = await servers[0].follows.getFollowings({ start, count, sort, search: 'bla' })
164 expect(body.total).to.equal(0)
166 expect(body.data).to.have.lengthOf(0)
170 it('Should search/filter followers on server 2', async function () {
173 const sort = 'createdAt'
176 const search = servers[0].port + ''
179 const body = await servers[2].follows.getFollowers({ start, count, sort, search })
180 expect(body.total).to.equal(1)
182 const follows = body.data
183 expect(follows).to.have.lengthOf(1)
184 expect(follows[0].following.host).to.equal(servers[2].host)
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)
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)
200 const body = await servers[2].follows.getFollowers({
206 actorType: 'Application'
208 expect(body.total).to.equal(1)
209 expect(body.data).to.have.lengthOf(1)
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)
220 const body = await servers[2].follows.getFollowers({ start, count, sort, search: 'bla' })
221 expect(body.total).to.equal(0)
223 const follows = body.data
224 expect(follows).to.have.lengthOf(0)
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 })
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 })
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 })
242 it('Should unfollow server 3 on server 1', async function () {
245 await servers[0].follows.unfollow({ target: servers[2] })
247 await waitJobs(servers)
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)
254 const follows = body.data
255 expect(follows).to.be.an('array')
256 expect(follows).to.have.lengthOf(1)
258 expect(follows[0].following.host).to.equal(servers[1].host)
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)
265 const follows = body.data
266 expect(follows).to.be.an('array')
267 expect(follows).to.have.lengthOf(0)
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 })
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 })
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 })
283 it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {
286 await servers[1].videos.upload({ attributes: { name: 'server2' } })
287 await servers[2].videos.upload({ attributes: { name: 'server3' } })
289 await waitJobs(servers)
292 const { total, data } = await servers[0].videos.list()
293 expect(total).to.equal(1)
294 expect(data[0].name).to.equal('server2')
298 const { total, data } = await servers[1].videos.list()
299 expect(total).to.equal(1)
300 expect(data[0].name).to.equal('server2')
304 const { total, data } = await servers[2].videos.list()
305 expect(total).to.equal(1)
306 expect(data[0].name).to.equal('server3')
310 it('Should remove account follow', async function () {
313 await servers[0].follows.unfollow({ target: 'root@' + servers[1].host })
315 await waitJobs(servers)
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 })
323 const { total, data } = await servers[0].follows.getFollowings()
324 expect(total).to.equal(0)
325 expect(data).to.have.lengthOf(0)
329 const { total, data } = await servers[0].videos.list()
330 expect(total).to.equal(0)
331 expect(data).to.have.lengthOf(0)
335 it('Should follow a channel', async function () {
338 await servers[0].follows.follow({
339 handles: [ 'root_channel@' + servers[1].host ]
342 await waitJobs(servers)
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 })
348 const { total, data } = await servers[0].follows.getFollowings()
349 expect(total).to.equal(1)
350 expect(data).to.have.lengthOf(1)
354 const { total, data } = await servers[0].videos.list()
355 expect(total).to.equal(1)
356 expect(data).to.have.lengthOf(1)
361 describe('Should propagate data on a new server follow', function () {
362 let video4: VideoCreateResult
364 before(async function () {
367 const video4Attributes = {
372 tags: [ 'tag1', 'tag2', 'tag3' ]
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' } })
382 const userAccessToken = await servers[2].users.generateUserAndToken('captain')
384 await servers[2].videos.rate({ id: video4.id, rating: 'like' })
385 await servers[2].videos.rate({ token: userAccessToken, id: video4.id, rating: 'dislike' })
389 await servers[2].comments.createThread({ videoId: video4.id, text: 'my super first comment' })
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' })
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' })
400 const { id: replyId } = await servers[2].comments.addReplyToLastThread({ text: 'will also be deleted' })
402 await servers[2].comments.addReplyToLastReply({ text: 'my second answer to deleted' })
404 await servers[2].comments.delete({ videoId: video4.id, commentId: threadId })
405 await servers[2].comments.delete({ videoId: video4.id, commentId: replyId })
408 await servers[2].captions.add({
411 fixture: 'subtitle-good2.vtt'
414 await waitJobs(servers)
416 // Server 1 follows server 3
417 await servers[0].follows.follow({ hosts: [ servers[2].url ] })
419 await waitJobs(servers)
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 })
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 })
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 })
437 it('Should have propagated videos', async function () {
438 const { total, data } = await servers[0].videos.list()
439 expect(total).to.equal(7)
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')
445 expect(video2).to.not.be.undefined
446 expect(video4).to.not.be.undefined
447 expect(video6).to.not.be.undefined
449 const isLocal = false
450 const checkAttributes = {
456 description: 'my super description',
457 support: 'my super support text',
460 host: servers[2].host
463 commentsEnabled: true,
464 downloadEnabled: true,
466 tags: [ 'tag1', 'tag2', 'tag3' ],
467 privacy: VideoPrivacy.PUBLIC,
471 displayName: 'Main root channel',
472 name: 'root_channel',
476 fixture: 'video_short.webm',
484 await completeVideoCheck(servers[0], video4, checkAttributes)
487 it('Should have propagated comments', async function () {
488 const { total, data } = await servers[0].comments.listThreads({ videoId: video4.id, sort: 'createdAt' })
490 expect(total).to.equal(2)
491 expect(data).to.be.an('array')
492 expect(data).to.have.lengthOf(2)
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
506 const threadId = comment.threadId
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)
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)
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)
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)
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
536 const tree = await servers[0].comments.getThread({ videoId: video4.id, threadId: deletedComment.threadId })
537 const [ commentRoot, deletedChildRoot ] = tree.children
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)
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')
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')
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)
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.')
572 it('Should unfollow server 3 on server 1 and does not list server 3 videos', async function () {
575 await servers[0].follows.unfollow({ target: servers[2] })
577 await waitJobs(servers)
579 const { total } = await servers[0].videos.list()
580 expect(total).to.equal(1)
584 describe('Should propagate data on a new channel follow', function () {
586 before(async function () {
589 await servers[2].videos.upload({ attributes: { name: 'server3-7' } })
591 await waitJobs(servers)
593 const video = await servers[0].videos.find({ name: 'server3-7' })
594 expect(video).to.not.exist
597 it('Should have propagated channel video', async function () {
600 await servers[0].follows.follow({ handles: [ 'root_channel@' + servers[2].host ] })
602 await waitJobs(servers)
604 const video = await servers[0].videos.find({ name: 'server3-7' })
606 expect(video).to.exist
610 after(async function () {
611 await cleanupTests(servers)