1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { VideoPrivacy } from '@shared/models'
11 setAccessTokensToServers,
14 } from '@shared/server-commands'
16 const expect = chai.expect
18 describe('Test users subscriptions', function () {
19 let servers: PeerTubeServer[] = []
20 const users: { accessToken: string }[] = []
21 let video3UUID: string
23 let command: SubscriptionsCommand
25 before(async function () {
28 servers = await createMultipleServers(3)
30 // Get the access tokens
31 await setAccessTokensToServers(servers)
33 // Server 1 and server 2 follow each other
34 await doubleFollow(servers[0], servers[1])
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 })
40 const accessToken = await server.login.getAccessToken(user)
41 users.push({ accessToken })
43 const videoName1 = 'video 1-' + server.serverNumber
44 await server.videos.upload({ token: accessToken, attributes: { name: videoName1 } })
46 const videoName2 = 'video 2-' + server.serverNumber
47 await server.videos.upload({ token: accessToken, attributes: { name: videoName2 } })
50 await waitJobs(servers)
52 command = servers[0].subscriptions
55 it('Should display videos of server 2 on server 1', async function () {
56 const { total } = await servers[0].videos.list()
58 expect(total).to.equal(4)
61 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
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 })
67 await waitJobs(servers)
69 const attributes = { name: 'video server 3 added after follow' }
70 const { uuid } = await servers[2].videos.upload({ token: users[2].accessToken, attributes })
73 await waitJobs(servers)
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)
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')
87 it('Should list subscriptions', async function () {
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)
96 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
97 expect(body.total).to.equal(2)
99 const subscriptions = body.data
100 expect(subscriptions).to.be.an('array')
101 expect(subscriptions).to.have.lengthOf(2)
103 expect(subscriptions[0].name).to.equal('user3_channel')
104 expect(subscriptions[1].name).to.equal('root_channel')
108 it('Should get subscription', async function () {
110 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
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)
120 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
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)
130 it('Should return the existing subscriptions', async function () {
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
138 const body = await command.exist({ token: users[0].accessToken, uris })
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
146 it('Should search among subscriptions', async function () {
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)
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)
160 it('Should list subscription videos', async function () {
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)
169 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
170 expect(body.total).to.equal(3)
172 const videos = body.data
173 expect(videos).to.be.an('array')
174 expect(videos).to.have.lengthOf(3)
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')
182 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
185 const videoName = 'video server 1 added after follow'
186 await servers[0].videos.upload({ attributes: { name: videoName } })
188 await waitJobs(servers)
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)
198 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
199 expect(body.total).to.equal(4)
201 const videos = body.data
202 expect(videos).to.be.an('array')
203 expect(videos).to.have.lengthOf(4)
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')
212 const { data, total } = await servers[0].videos.list()
213 expect(total).to.equal(5)
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')
223 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
226 await servers[0].follows.follow({ hosts: [ servers[2].url ] })
228 await waitJobs(servers)
230 const { data, total } = await servers[0].videos.list()
231 expect(total).to.equal(8)
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
240 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
243 await servers[0].follows.unfollow({ target: servers[2] })
245 await waitJobs(servers)
247 const { total, data } = await servers[0].videos.list()
248 expect(total).to.equal(5)
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')
257 it('Should still list subscription videos', async function () {
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)
266 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
267 expect(body.total).to.equal(4)
269 const videos = body.data
270 expect(videos).to.be.an('array')
271 expect(videos).to.have.lengthOf(4)
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')
280 it('Should update a video of server 3 and see the updated video on server 1', async function () {
283 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'video server 3 added after follow updated' } })
285 await waitJobs(servers)
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')
291 it('Should remove user of server 3 subscription', async function () {
294 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
296 await waitJobs(servers)
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)
303 const videos = body.data
304 expect(videos).to.be.an('array')
305 expect(videos).to.have.lengthOf(1)
307 expect(videos[0].name).to.equal('video server 1 added after follow')
310 it('Should remove the root subscription and not display the videos anymore', async function () {
313 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
315 await waitJobs(servers)
318 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
319 expect(body.total).to.equal(0)
321 const videos = body.data
322 expect(videos).to.be.an('array')
323 expect(videos).to.have.lengthOf(0)
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)
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')
338 it('Should follow user of server 3 again', async function () {
341 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
343 await waitJobs(servers)
346 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
347 expect(body.total).to.equal(3)
349 const videos = body.data
350 expect(videos).to.be.an('array')
351 expect(videos).to.have.lengthOf(3)
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')
359 const { total, data } = await servers[0].videos.list()
360 expect(total).to.equal(5)
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')
370 it('Should follow user channels of server 3 by root of server 3', async function () {
373 await servers[2].channels.create({ token: users[2].accessToken, attributes: { name: 'user3_channel2' } })
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 })
378 await waitJobs(servers)
381 it('Should list user 3 followers', async function () {
383 const { total, data } = await servers[2].accounts.listFollowers({
384 token: users[2].accessToken,
385 accountName: 'user3',
391 expect(total).to.equal(3)
392 expect(data).to.have.lengthOf(3)
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')
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')
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')
411 const { total, data } = await servers[2].accounts.listFollowers({
412 token: users[2].accessToken,
413 accountName: 'user3',
419 expect(total).to.equal(3)
420 expect(data).to.have.lengthOf(1)
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')
429 const { total, data } = await servers[2].accounts.listFollowers({
430 token: users[2].accessToken,
431 accountName: 'user3',
437 expect(total).to.equal(3)
438 expect(data).to.have.lengthOf(1)
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')
447 const { total, data } = await servers[2].accounts.listFollowers({
448 token: users[2].accessToken,
449 accountName: 'user3',
454 expect(total).to.equal(1)
455 expect(data).to.have.lengthOf(1)
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')
464 it('Should list user3_channel followers', async function () {
466 const { total, data } = await servers[2].channels.listFollowers({
467 token: users[2].accessToken,
468 channelName: 'user3_channel',
474 expect(total).to.equal(2)
475 expect(data).to.have.lengthOf(2)
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')
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')
489 const { total, data } = await servers[2].channels.listFollowers({
490 token: users[2].accessToken,
491 channelName: 'user3_channel',
497 expect(total).to.equal(2)
498 expect(data).to.have.lengthOf(1)
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')
507 const { total, data } = await servers[2].channels.listFollowers({
508 token: users[2].accessToken,
509 channelName: 'user3_channel',
515 expect(total).to.equal(2)
516 expect(data).to.have.lengthOf(1)
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')
525 const { total, data } = await servers[2].channels.listFollowers({
526 token: users[2].accessToken,
527 channelName: 'user3_channel',
532 expect(total).to.equal(1)
533 expect(data).to.have.lengthOf(1)
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')
542 it('Should update video as internal and not see from remote server', async function () {
545 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'internal', privacy: VideoPrivacy.INTERNAL } })
546 await waitJobs(servers)
549 const { data } = await command.listVideos({ token: users[0].accessToken })
550 expect(data.find(v => v.name === 'internal')).to.not.exist
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
559 it('Should update video as private and not see from anyone server', async function () {
562 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'private', privacy: VideoPrivacy.PRIVATE } })
563 await waitJobs(servers)
566 const { data } = await command.listVideos({ token: users[0].accessToken })
567 expect(data.find(v => v.name === 'private')).to.not.exist
571 const { data } = await servers[2].subscriptions.listVideos({ token: servers[2].accessToken })
572 expect(data.find(v => v.name === 'private')).to.not.exist
576 after(async function () {
577 await cleanupTests(servers)