]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/user-subscriptions.ts
We don't need to import mocha
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / user-subscriptions.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import { VideoPrivacy } from '@shared/models'
5 import {
6 cleanupTests,
7 createMultipleServers,
8 doubleFollow,
9 PeerTubeServer,
10 setAccessTokensToServers,
11 setDefaultAccountAvatar,
12 setDefaultChannelAvatar,
13 SubscriptionsCommand,
14 waitJobs
15 } from '@shared/server-commands'
16
17 const expect = chai.expect
18
19 describe('Test users subscriptions', function () {
20 let servers: PeerTubeServer[] = []
21 const users: { accessToken: string }[] = []
22 let video3UUID: string
23
24 let command: SubscriptionsCommand
25
26 before(async function () {
27 this.timeout(240000)
28
29 servers = await createMultipleServers(3)
30
31 // Get the access tokens
32 await setAccessTokensToServers(servers)
33 await setDefaultChannelAvatar(servers)
34 await setDefaultAccountAvatar(servers)
35
36 // Server 1 and server 2 follow each other
37 await doubleFollow(servers[0], servers[1])
38
39 for (const server of servers) {
40 const user = { username: 'user' + server.serverNumber, password: 'password' }
41 await server.users.create({ username: user.username, password: user.password })
42
43 const accessToken = await server.login.getAccessToken(user)
44 users.push({ accessToken })
45
46 const videoName1 = 'video 1-' + server.serverNumber
47 await server.videos.upload({ token: accessToken, attributes: { name: videoName1 } })
48
49 const videoName2 = 'video 2-' + server.serverNumber
50 await server.videos.upload({ token: accessToken, attributes: { name: videoName2 } })
51 }
52
53 await waitJobs(servers)
54
55 command = servers[0].subscriptions
56 })
57
58 it('Should display videos of server 2 on server 1', async function () {
59 const { total } = await servers[0].videos.list()
60
61 expect(total).to.equal(4)
62 })
63
64 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
65 this.timeout(60000)
66
67 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
68 await command.add({ token: users[0].accessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
69
70 await waitJobs(servers)
71
72 const attributes = { name: 'video server 3 added after follow' }
73 const { uuid } = await servers[2].videos.upload({ token: users[2].accessToken, attributes })
74 video3UUID = uuid
75
76 await waitJobs(servers)
77 })
78
79 it('Should not display videos of server 3 on server 1', async function () {
80 const { total, data } = await servers[0].videos.list()
81 expect(total).to.equal(4)
82
83 for (const video of data) {
84 expect(video.name).to.not.contain('1-3')
85 expect(video.name).to.not.contain('2-3')
86 expect(video.name).to.not.contain('video server 3 added after follow')
87 }
88 })
89
90 it('Should list subscriptions', async function () {
91 {
92 const body = await command.list()
93 expect(body.total).to.equal(0)
94 expect(body.data).to.be.an('array')
95 expect(body.data).to.have.lengthOf(0)
96 }
97
98 {
99 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
100 expect(body.total).to.equal(2)
101
102 const subscriptions = body.data
103 expect(subscriptions).to.be.an('array')
104 expect(subscriptions).to.have.lengthOf(2)
105
106 expect(subscriptions[0].name).to.equal('user3_channel')
107 expect(subscriptions[1].name).to.equal('root_channel')
108 }
109 })
110
111 it('Should get subscription', async function () {
112 {
113 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
114
115 expect(videoChannel.name).to.equal('user3_channel')
116 expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
117 expect(videoChannel.displayName).to.equal('Main user3 channel')
118 expect(videoChannel.followingCount).to.equal(0)
119 expect(videoChannel.followersCount).to.equal(1)
120 }
121
122 {
123 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
124
125 expect(videoChannel.name).to.equal('root_channel')
126 expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
127 expect(videoChannel.displayName).to.equal('Main root channel')
128 expect(videoChannel.followingCount).to.equal(0)
129 expect(videoChannel.followersCount).to.equal(1)
130 }
131 })
132
133 it('Should return the existing subscriptions', async function () {
134 const uris = [
135 'user3_channel@localhost:' + servers[2].port,
136 'root2_channel@localhost:' + servers[0].port,
137 'root_channel@localhost:' + servers[0].port,
138 'user3_channel@localhost:' + servers[0].port
139 ]
140
141 const body = await command.exist({ token: users[0].accessToken, uris })
142
143 expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true
144 expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false
145 expect(body['root_channel@localhost:' + servers[0].port]).to.be.true
146 expect(body['user3_channel@localhost:' + servers[0].port]).to.be.false
147 })
148
149 it('Should search among subscriptions', async function () {
150 {
151 const body = await command.list({ token: users[0].accessToken, sort: '-createdAt', search: 'user3_channel' })
152 expect(body.total).to.equal(1)
153 expect(body.data).to.have.lengthOf(1)
154 }
155
156 {
157 const body = await command.list({ token: users[0].accessToken, sort: '-createdAt', search: 'toto' })
158 expect(body.total).to.equal(0)
159 expect(body.data).to.have.lengthOf(0)
160 }
161 })
162
163 it('Should list subscription videos', async function () {
164 {
165 const body = await command.listVideos()
166 expect(body.total).to.equal(0)
167 expect(body.data).to.be.an('array')
168 expect(body.data).to.have.lengthOf(0)
169 }
170
171 {
172 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
173 expect(body.total).to.equal(3)
174
175 const videos = body.data
176 expect(videos).to.be.an('array')
177 expect(videos).to.have.lengthOf(3)
178
179 expect(videos[0].name).to.equal('video 1-3')
180 expect(videos[1].name).to.equal('video 2-3')
181 expect(videos[2].name).to.equal('video server 3 added after follow')
182 }
183 })
184
185 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
186 this.timeout(60000)
187
188 const videoName = 'video server 1 added after follow'
189 await servers[0].videos.upload({ attributes: { name: videoName } })
190
191 await waitJobs(servers)
192
193 {
194 const body = await command.listVideos()
195 expect(body.total).to.equal(0)
196 expect(body.data).to.be.an('array')
197 expect(body.data).to.have.lengthOf(0)
198 }
199
200 {
201 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
202 expect(body.total).to.equal(4)
203
204 const videos = body.data
205 expect(videos).to.be.an('array')
206 expect(videos).to.have.lengthOf(4)
207
208 expect(videos[0].name).to.equal('video 1-3')
209 expect(videos[1].name).to.equal('video 2-3')
210 expect(videos[2].name).to.equal('video server 3 added after follow')
211 expect(videos[3].name).to.equal('video server 1 added after follow')
212 }
213
214 {
215 const { data, total } = await servers[0].videos.list()
216 expect(total).to.equal(5)
217
218 for (const video of data) {
219 expect(video.name).to.not.contain('1-3')
220 expect(video.name).to.not.contain('2-3')
221 expect(video.name).to.not.contain('video server 3 added after follow')
222 }
223 }
224 })
225
226 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
227 this.timeout(60000)
228
229 await servers[0].follows.follow({ hosts: [ servers[2].url ] })
230
231 await waitJobs(servers)
232
233 const { data, total } = await servers[0].videos.list()
234 expect(total).to.equal(8)
235
236 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
237 for (const name of names) {
238 const video = data.find(v => v.name.includes(name))
239 expect(video).to.not.be.undefined
240 }
241 })
242
243 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
244 this.timeout(60000)
245
246 await servers[0].follows.unfollow({ target: servers[2] })
247
248 await waitJobs(servers)
249
250 const { total, data } = await servers[0].videos.list()
251 expect(total).to.equal(5)
252
253 for (const video of data) {
254 expect(video.name).to.not.contain('1-3')
255 expect(video.name).to.not.contain('2-3')
256 expect(video.name).to.not.contain('video server 3 added after follow')
257 }
258 })
259
260 it('Should still list subscription videos', async function () {
261 {
262 const body = await command.listVideos()
263 expect(body.total).to.equal(0)
264 expect(body.data).to.be.an('array')
265 expect(body.data).to.have.lengthOf(0)
266 }
267
268 {
269 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
270 expect(body.total).to.equal(4)
271
272 const videos = body.data
273 expect(videos).to.be.an('array')
274 expect(videos).to.have.lengthOf(4)
275
276 expect(videos[0].name).to.equal('video 1-3')
277 expect(videos[1].name).to.equal('video 2-3')
278 expect(videos[2].name).to.equal('video server 3 added after follow')
279 expect(videos[3].name).to.equal('video server 1 added after follow')
280 }
281 })
282
283 it('Should update a video of server 3 and see the updated video on server 1', async function () {
284 this.timeout(30000)
285
286 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'video server 3 added after follow updated' } })
287
288 await waitJobs(servers)
289
290 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
291 expect(body.data[2].name).to.equal('video server 3 added after follow updated')
292 })
293
294 it('Should remove user of server 3 subscription', async function () {
295 this.timeout(30000)
296
297 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
298
299 await waitJobs(servers)
300 })
301
302 it('Should not display its videos anymore', async function () {
303 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
304 expect(body.total).to.equal(1)
305
306 const videos = body.data
307 expect(videos).to.be.an('array')
308 expect(videos).to.have.lengthOf(1)
309
310 expect(videos[0].name).to.equal('video server 1 added after follow')
311 })
312
313 it('Should remove the root subscription and not display the videos anymore', async function () {
314 this.timeout(30000)
315
316 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
317
318 await waitJobs(servers)
319
320 {
321 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
322 expect(body.total).to.equal(0)
323
324 const videos = body.data
325 expect(videos).to.be.an('array')
326 expect(videos).to.have.lengthOf(0)
327 }
328 })
329
330 it('Should correctly display public videos on server 1', async function () {
331 const { total, data } = await servers[0].videos.list()
332 expect(total).to.equal(5)
333
334 for (const video of data) {
335 expect(video.name).to.not.contain('1-3')
336 expect(video.name).to.not.contain('2-3')
337 expect(video.name).to.not.contain('video server 3 added after follow updated')
338 }
339 })
340
341 it('Should follow user of server 3 again', async function () {
342 this.timeout(60000)
343
344 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
345
346 await waitJobs(servers)
347
348 {
349 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
350 expect(body.total).to.equal(3)
351
352 const videos = body.data
353 expect(videos).to.be.an('array')
354 expect(videos).to.have.lengthOf(3)
355
356 expect(videos[0].name).to.equal('video 1-3')
357 expect(videos[1].name).to.equal('video 2-3')
358 expect(videos[2].name).to.equal('video server 3 added after follow updated')
359 }
360
361 {
362 const { total, data } = await servers[0].videos.list()
363 expect(total).to.equal(5)
364
365 for (const video of data) {
366 expect(video.name).to.not.contain('1-3')
367 expect(video.name).to.not.contain('2-3')
368 expect(video.name).to.not.contain('video server 3 added after follow updated')
369 }
370 }
371 })
372
373 it('Should follow user channels of server 3 by root of server 3', async function () {
374 this.timeout(60000)
375
376 await servers[2].channels.create({ token: users[2].accessToken, attributes: { name: 'user3_channel2' } })
377
378 await servers[2].subscriptions.add({ token: servers[2].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
379 await servers[2].subscriptions.add({ token: servers[2].accessToken, targetUri: 'user3_channel2@localhost:' + servers[2].port })
380
381 await waitJobs(servers)
382 })
383
384 it('Should list user 3 followers', async function () {
385 {
386 const { total, data } = await servers[2].accounts.listFollowers({
387 token: users[2].accessToken,
388 accountName: 'user3',
389 start: 0,
390 count: 5,
391 sort: 'createdAt'
392 })
393
394 expect(total).to.equal(3)
395 expect(data).to.have.lengthOf(3)
396
397 expect(data[0].following.host).to.equal(servers[2].host)
398 expect(data[0].following.name).to.equal('user3_channel')
399 expect(data[0].follower.host).to.equal(servers[0].host)
400 expect(data[0].follower.name).to.equal('user1')
401
402 expect(data[1].following.host).to.equal(servers[2].host)
403 expect(data[1].following.name).to.equal('user3_channel')
404 expect(data[1].follower.host).to.equal(servers[2].host)
405 expect(data[1].follower.name).to.equal('root')
406
407 expect(data[2].following.host).to.equal(servers[2].host)
408 expect(data[2].following.name).to.equal('user3_channel2')
409 expect(data[2].follower.host).to.equal(servers[2].host)
410 expect(data[2].follower.name).to.equal('root')
411 }
412
413 {
414 const { total, data } = await servers[2].accounts.listFollowers({
415 token: users[2].accessToken,
416 accountName: 'user3',
417 start: 0,
418 count: 1,
419 sort: '-createdAt'
420 })
421
422 expect(total).to.equal(3)
423 expect(data).to.have.lengthOf(1)
424
425 expect(data[0].following.host).to.equal(servers[2].host)
426 expect(data[0].following.name).to.equal('user3_channel2')
427 expect(data[0].follower.host).to.equal(servers[2].host)
428 expect(data[0].follower.name).to.equal('root')
429 }
430
431 {
432 const { total, data } = await servers[2].accounts.listFollowers({
433 token: users[2].accessToken,
434 accountName: 'user3',
435 start: 1,
436 count: 1,
437 sort: '-createdAt'
438 })
439
440 expect(total).to.equal(3)
441 expect(data).to.have.lengthOf(1)
442
443 expect(data[0].following.host).to.equal(servers[2].host)
444 expect(data[0].following.name).to.equal('user3_channel')
445 expect(data[0].follower.host).to.equal(servers[2].host)
446 expect(data[0].follower.name).to.equal('root')
447 }
448
449 {
450 const { total, data } = await servers[2].accounts.listFollowers({
451 token: users[2].accessToken,
452 accountName: 'user3',
453 search: 'user1',
454 sort: '-createdAt'
455 })
456
457 expect(total).to.equal(1)
458 expect(data).to.have.lengthOf(1)
459
460 expect(data[0].following.host).to.equal(servers[2].host)
461 expect(data[0].following.name).to.equal('user3_channel')
462 expect(data[0].follower.host).to.equal(servers[0].host)
463 expect(data[0].follower.name).to.equal('user1')
464 }
465 })
466
467 it('Should list user3_channel followers', async function () {
468 {
469 const { total, data } = await servers[2].channels.listFollowers({
470 token: users[2].accessToken,
471 channelName: 'user3_channel',
472 start: 0,
473 count: 5,
474 sort: 'createdAt'
475 })
476
477 expect(total).to.equal(2)
478 expect(data).to.have.lengthOf(2)
479
480 expect(data[0].following.host).to.equal(servers[2].host)
481 expect(data[0].following.name).to.equal('user3_channel')
482 expect(data[0].follower.host).to.equal(servers[0].host)
483 expect(data[0].follower.name).to.equal('user1')
484
485 expect(data[1].following.host).to.equal(servers[2].host)
486 expect(data[1].following.name).to.equal('user3_channel')
487 expect(data[1].follower.host).to.equal(servers[2].host)
488 expect(data[1].follower.name).to.equal('root')
489 }
490
491 {
492 const { total, data } = await servers[2].channels.listFollowers({
493 token: users[2].accessToken,
494 channelName: 'user3_channel',
495 start: 0,
496 count: 1,
497 sort: '-createdAt'
498 })
499
500 expect(total).to.equal(2)
501 expect(data).to.have.lengthOf(1)
502
503 expect(data[0].following.host).to.equal(servers[2].host)
504 expect(data[0].following.name).to.equal('user3_channel')
505 expect(data[0].follower.host).to.equal(servers[2].host)
506 expect(data[0].follower.name).to.equal('root')
507 }
508
509 {
510 const { total, data } = await servers[2].channels.listFollowers({
511 token: users[2].accessToken,
512 channelName: 'user3_channel',
513 start: 1,
514 count: 1,
515 sort: '-createdAt'
516 })
517
518 expect(total).to.equal(2)
519 expect(data).to.have.lengthOf(1)
520
521 expect(data[0].following.host).to.equal(servers[2].host)
522 expect(data[0].following.name).to.equal('user3_channel')
523 expect(data[0].follower.host).to.equal(servers[0].host)
524 expect(data[0].follower.name).to.equal('user1')
525 }
526
527 {
528 const { total, data } = await servers[2].channels.listFollowers({
529 token: users[2].accessToken,
530 channelName: 'user3_channel',
531 search: 'user1',
532 sort: '-createdAt'
533 })
534
535 expect(total).to.equal(1)
536 expect(data).to.have.lengthOf(1)
537
538 expect(data[0].following.host).to.equal(servers[2].host)
539 expect(data[0].following.name).to.equal('user3_channel')
540 expect(data[0].follower.host).to.equal(servers[0].host)
541 expect(data[0].follower.name).to.equal('user1')
542 }
543 })
544
545 it('Should update video as internal and not see from remote server', async function () {
546 this.timeout(30000)
547
548 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'internal', privacy: VideoPrivacy.INTERNAL } })
549 await waitJobs(servers)
550
551 {
552 const { data } = await command.listVideos({ token: users[0].accessToken })
553 expect(data.find(v => v.name === 'internal')).to.not.exist
554 }
555 })
556
557 it('Should see internal from local user', async function () {
558 const { data } = await servers[2].subscriptions.listVideos({ token: servers[2].accessToken })
559 expect(data.find(v => v.name === 'internal')).to.exist
560 })
561
562 it('Should update video as private and not see from anyone server', async function () {
563 this.timeout(30000)
564
565 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'private', privacy: VideoPrivacy.PRIVATE } })
566 await waitJobs(servers)
567
568 {
569 const { data } = await command.listVideos({ token: users[0].accessToken })
570 expect(data.find(v => v.name === 'private')).to.not.exist
571 }
572
573 {
574 const { data } = await servers[2].subscriptions.listVideos({ token: servers[2].accessToken })
575 expect(data.find(v => v.name === 'private')).to.not.exist
576 }
577 })
578
579 after(async function () {
580 await cleanupTests(servers)
581 })
582 })