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