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