1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { VideoPrivacy } from '@shared/models'
10 setAccessTokensToServers,
11 setDefaultAccountAvatar,
12 setDefaultChannelAvatar,
15 } from '@shared/server-commands'
17 describe('Test users subscriptions', function () {
18 let servers: PeerTubeServer[] = []
19 const users: { accessToken: string }[] = []
20 let video3UUID: string
22 let command: SubscriptionsCommand
24 before(async function () {
27 servers = await createMultipleServers(3)
29 // Get the access tokens
30 await setAccessTokensToServers(servers)
31 await setDefaultChannelAvatar(servers)
32 await setDefaultAccountAvatar(servers)
34 // Server 1 and server 2 follow each other
35 await doubleFollow(servers[0], servers[1])
37 for (const server of servers) {
38 const user = { username: 'user' + server.serverNumber, password: 'password' }
39 await server.users.create({ username: user.username, password: user.password })
41 const accessToken = await server.login.getAccessToken(user)
42 users.push({ accessToken })
44 const videoName1 = 'video 1-' + server.serverNumber
45 await server.videos.upload({ token: accessToken, attributes: { name: videoName1 } })
47 const videoName2 = 'video 2-' + server.serverNumber
48 await server.videos.upload({ token: accessToken, attributes: { name: videoName2 } })
51 await waitJobs(servers)
53 command = servers[0].subscriptions
56 describe('Destinction between server videos and user videos', function () {
57 it('Should display videos of server 2 on server 1', async function () {
58 const { total } = await servers[0].videos.list()
60 expect(total).to.equal(4)
63 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
66 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
67 await command.add({ token: users[0].accessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
69 await waitJobs(servers)
71 const attributes = { name: 'video server 3 added after follow' }
72 const { uuid } = await servers[2].videos.upload({ token: users[2].accessToken, attributes })
75 await waitJobs(servers)
78 it('Should not display videos of server 3 on server 1', async function () {
79 const { total, data } = await servers[0].videos.list()
80 expect(total).to.equal(4)
82 for (const video of data) {
83 expect(video.name).to.not.contain('1-3')
84 expect(video.name).to.not.contain('2-3')
85 expect(video.name).to.not.contain('video server 3 added after follow')
90 describe('Subscription endpoints', function () {
92 it('Should list subscriptions', async function () {
94 const body = await command.list()
95 expect(body.total).to.equal(0)
96 expect(body.data).to.be.an('array')
97 expect(body.data).to.have.lengthOf(0)
101 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
102 expect(body.total).to.equal(2)
104 const subscriptions = body.data
105 expect(subscriptions).to.be.an('array')
106 expect(subscriptions).to.have.lengthOf(2)
108 expect(subscriptions[0].name).to.equal('user3_channel')
109 expect(subscriptions[1].name).to.equal('root_channel')
113 it('Should get subscription', async function () {
115 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
117 expect(videoChannel.name).to.equal('user3_channel')
118 expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
119 expect(videoChannel.displayName).to.equal('Main user3 channel')
120 expect(videoChannel.followingCount).to.equal(0)
121 expect(videoChannel.followersCount).to.equal(1)
125 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
127 expect(videoChannel.name).to.equal('root_channel')
128 expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
129 expect(videoChannel.displayName).to.equal('Main root channel')
130 expect(videoChannel.followingCount).to.equal(0)
131 expect(videoChannel.followersCount).to.equal(1)
135 it('Should return the existing subscriptions', async function () {
137 'user3_channel@localhost:' + servers[2].port,
138 'root2_channel@localhost:' + servers[0].port,
139 'root_channel@localhost:' + servers[0].port,
140 'user3_channel@localhost:' + servers[0].port
143 const body = await command.exist({ token: users[0].accessToken, uris })
145 expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true
146 expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false
147 expect(body['root_channel@localhost:' + servers[0].port]).to.be.true
148 expect(body['user3_channel@localhost:' + servers[0].port]).to.be.false
151 it('Should search among subscriptions', async function () {
153 const body = await command.list({ token: users[0].accessToken, sort: '-createdAt', search: 'user3_channel' })
154 expect(body.total).to.equal(1)
155 expect(body.data).to.have.lengthOf(1)
159 const body = await command.list({ token: users[0].accessToken, sort: '-createdAt', search: 'toto' })
160 expect(body.total).to.equal(0)
161 expect(body.data).to.have.lengthOf(0)
166 describe('Subscription videos', function () {
168 it('Should list subscription videos', async function () {
170 const body = await command.listVideos()
171 expect(body.total).to.equal(0)
172 expect(body.data).to.be.an('array')
173 expect(body.data).to.have.lengthOf(0)
177 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
178 expect(body.total).to.equal(3)
180 const videos = body.data
181 expect(videos).to.be.an('array')
182 expect(videos).to.have.lengthOf(3)
184 expect(videos[0].name).to.equal('video 1-3')
185 expect(videos[1].name).to.equal('video 2-3')
186 expect(videos[2].name).to.equal('video server 3 added after follow')
190 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
193 const videoName = 'video server 1 added after follow'
194 await servers[0].videos.upload({ attributes: { name: videoName } })
196 await waitJobs(servers)
199 const body = await command.listVideos()
200 expect(body.total).to.equal(0)
201 expect(body.data).to.be.an('array')
202 expect(body.data).to.have.lengthOf(0)
206 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
207 expect(body.total).to.equal(4)
209 const videos = body.data
210 expect(videos).to.be.an('array')
211 expect(videos).to.have.lengthOf(4)
213 expect(videos[0].name).to.equal('video 1-3')
214 expect(videos[1].name).to.equal('video 2-3')
215 expect(videos[2].name).to.equal('video server 3 added after follow')
216 expect(videos[3].name).to.equal('video server 1 added after follow')
220 const { data, total } = await servers[0].videos.list()
221 expect(total).to.equal(5)
223 for (const video of data) {
224 expect(video.name).to.not.contain('1-3')
225 expect(video.name).to.not.contain('2-3')
226 expect(video.name).to.not.contain('video server 3 added after follow')
231 it('Should have server 1 following server 3 and display server 3 videos', async function () {
234 await servers[0].follows.follow({ hosts: [ servers[2].url ] })
236 await waitJobs(servers)
238 const { data, total } = await servers[0].videos.list()
239 expect(total).to.equal(8)
241 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
242 for (const name of names) {
243 const video = data.find(v => v.name.includes(name))
244 expect(video).to.not.be.undefined
248 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
251 await servers[0].follows.unfollow({ target: servers[2] })
253 await waitJobs(servers)
255 const { total, data } = await servers[0].videos.list()
256 expect(total).to.equal(5)
258 for (const video of data) {
259 expect(video.name).to.not.contain('1-3')
260 expect(video.name).to.not.contain('2-3')
261 expect(video.name).to.not.contain('video server 3 added after follow')
265 it('Should still list subscription videos', async function () {
267 const body = await command.listVideos()
268 expect(body.total).to.equal(0)
269 expect(body.data).to.be.an('array')
270 expect(body.data).to.have.lengthOf(0)
274 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
275 expect(body.total).to.equal(4)
277 const videos = body.data
278 expect(videos).to.be.an('array')
279 expect(videos).to.have.lengthOf(4)
281 expect(videos[0].name).to.equal('video 1-3')
282 expect(videos[1].name).to.equal('video 2-3')
283 expect(videos[2].name).to.equal('video server 3 added after follow')
284 expect(videos[3].name).to.equal('video server 1 added after follow')
289 describe('Existing subscription video update', function () {
291 it('Should update a video of server 3 and see the updated video on server 1', async function () {
294 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'video server 3 added after follow updated' } })
296 await waitJobs(servers)
298 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
299 expect(body.data[2].name).to.equal('video server 3 added after follow updated')
303 describe('Subscription removal', function () {
305 it('Should remove user of server 3 subscription', async function () {
308 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
310 await waitJobs(servers)
313 it('Should not display its videos anymore', async function () {
314 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
315 expect(body.total).to.equal(1)
317 const videos = body.data
318 expect(videos).to.be.an('array')
319 expect(videos).to.have.lengthOf(1)
321 expect(videos[0].name).to.equal('video server 1 added after follow')
324 it('Should remove the root subscription and not display the videos anymore', async function () {
327 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
329 await waitJobs(servers)
332 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
333 expect(body.total).to.equal(0)
335 const videos = body.data
336 expect(videos).to.be.an('array')
337 expect(videos).to.have.lengthOf(0)
341 it('Should correctly display public videos on server 1', async function () {
342 const { total, data } = await servers[0].videos.list()
343 expect(total).to.equal(5)
345 for (const video of data) {
346 expect(video.name).to.not.contain('1-3')
347 expect(video.name).to.not.contain('2-3')
348 expect(video.name).to.not.contain('video server 3 added after follow updated')
353 describe('Re-follow', function () {
355 it('Should follow user of server 3 again', async function () {
358 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
360 await waitJobs(servers)
363 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
364 expect(body.total).to.equal(3)
366 const videos = body.data
367 expect(videos).to.be.an('array')
368 expect(videos).to.have.lengthOf(3)
370 expect(videos[0].name).to.equal('video 1-3')
371 expect(videos[1].name).to.equal('video 2-3')
372 expect(videos[2].name).to.equal('video server 3 added after follow updated')
376 const { total, data } = await servers[0].videos.list()
377 expect(total).to.equal(5)
379 for (const video of data) {
380 expect(video.name).to.not.contain('1-3')
381 expect(video.name).to.not.contain('2-3')
382 expect(video.name).to.not.contain('video server 3 added after follow updated')
387 it('Should follow user channels of server 3 by root of server 3', async function () {
390 await servers[2].channels.create({ token: users[2].accessToken, attributes: { name: 'user3_channel2' } })
392 await servers[2].subscriptions.add({ token: servers[2].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
393 await servers[2].subscriptions.add({ token: servers[2].accessToken, targetUri: 'user3_channel2@localhost:' + servers[2].port })
395 await waitJobs(servers)
399 describe('Followers listing', function () {
401 it('Should list user 3 followers', async function () {
403 const { total, data } = await servers[2].accounts.listFollowers({
404 token: users[2].accessToken,
405 accountName: 'user3',
411 expect(total).to.equal(3)
412 expect(data).to.have.lengthOf(3)
414 expect(data[0].following.host).to.equal(servers[2].host)
415 expect(data[0].following.name).to.equal('user3_channel')
416 expect(data[0].follower.host).to.equal(servers[0].host)
417 expect(data[0].follower.name).to.equal('user1')
419 expect(data[1].following.host).to.equal(servers[2].host)
420 expect(data[1].following.name).to.equal('user3_channel')
421 expect(data[1].follower.host).to.equal(servers[2].host)
422 expect(data[1].follower.name).to.equal('root')
424 expect(data[2].following.host).to.equal(servers[2].host)
425 expect(data[2].following.name).to.equal('user3_channel2')
426 expect(data[2].follower.host).to.equal(servers[2].host)
427 expect(data[2].follower.name).to.equal('root')
431 const { total, data } = await servers[2].accounts.listFollowers({
432 token: users[2].accessToken,
433 accountName: 'user3',
439 expect(total).to.equal(3)
440 expect(data).to.have.lengthOf(1)
442 expect(data[0].following.host).to.equal(servers[2].host)
443 expect(data[0].following.name).to.equal('user3_channel2')
444 expect(data[0].follower.host).to.equal(servers[2].host)
445 expect(data[0].follower.name).to.equal('root')
449 const { total, data } = await servers[2].accounts.listFollowers({
450 token: users[2].accessToken,
451 accountName: 'user3',
457 expect(total).to.equal(3)
458 expect(data).to.have.lengthOf(1)
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[2].host)
463 expect(data[0].follower.name).to.equal('root')
467 const { total, data } = await servers[2].accounts.listFollowers({
468 token: users[2].accessToken,
469 accountName: 'user3',
474 expect(total).to.equal(1)
475 expect(data).to.have.lengthOf(1)
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')
484 it('Should list user3_channel followers', async function () {
486 const { total, data } = await servers[2].channels.listFollowers({
487 token: users[2].accessToken,
488 channelName: 'user3_channel',
494 expect(total).to.equal(2)
495 expect(data).to.have.lengthOf(2)
497 expect(data[0].following.host).to.equal(servers[2].host)
498 expect(data[0].following.name).to.equal('user3_channel')
499 expect(data[0].follower.host).to.equal(servers[0].host)
500 expect(data[0].follower.name).to.equal('user1')
502 expect(data[1].following.host).to.equal(servers[2].host)
503 expect(data[1].following.name).to.equal('user3_channel')
504 expect(data[1].follower.host).to.equal(servers[2].host)
505 expect(data[1].follower.name).to.equal('root')
509 const { total, data } = await servers[2].channels.listFollowers({
510 token: users[2].accessToken,
511 channelName: 'user3_channel',
517 expect(total).to.equal(2)
518 expect(data).to.have.lengthOf(1)
520 expect(data[0].following.host).to.equal(servers[2].host)
521 expect(data[0].following.name).to.equal('user3_channel')
522 expect(data[0].follower.host).to.equal(servers[2].host)
523 expect(data[0].follower.name).to.equal('root')
527 const { total, data } = await servers[2].channels.listFollowers({
528 token: users[2].accessToken,
529 channelName: 'user3_channel',
535 expect(total).to.equal(2)
536 expect(data).to.have.lengthOf(1)
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')
545 const { total, data } = await servers[2].channels.listFollowers({
546 token: users[2].accessToken,
547 channelName: 'user3_channel',
552 expect(total).to.equal(1)
553 expect(data).to.have.lengthOf(1)
555 expect(data[0].following.host).to.equal(servers[2].host)
556 expect(data[0].following.name).to.equal('user3_channel')
557 expect(data[0].follower.host).to.equal(servers[0].host)
558 expect(data[0].follower.name).to.equal('user1')
563 describe('Subscription videos privacy', function () {
565 it('Should update video as internal and not see from remote server', async function () {
568 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'internal', privacy: VideoPrivacy.INTERNAL } })
569 await waitJobs(servers)
572 const { data } = await command.listVideos({ token: users[0].accessToken })
573 expect(data.find(v => v.name === 'internal')).to.not.exist
577 it('Should see internal from local user', async function () {
578 const { data } = await servers[2].subscriptions.listVideos({ token: servers[2].accessToken })
579 expect(data.find(v => v.name === 'internal')).to.exist
582 it('Should update video as private and not see from anyone server', async function () {
585 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'private', privacy: VideoPrivacy.PRIVATE } })
586 await waitJobs(servers)
589 const { data } = await command.listVideos({ token: users[0].accessToken })
590 expect(data.find(v => v.name === 'private')).to.not.exist
594 const { data } = await servers[2].subscriptions.listVideos({ token: servers[2].accessToken })
595 expect(data.find(v => v.name === 'private')).to.not.exist
600 after(async function () {
601 await cleanupTests(servers)