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