1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { VideoPrivacy } from '@shared/models'
10 setAccessTokensToServers,
11 setDefaultAccountAvatar,
12 setDefaultChannelAvatar,
15 } from '@shared/server-commands'
17 const expect = chai.expect
19 describe('Test users subscriptions', function () {
20 let servers: PeerTubeServer[] = []
21 const users: { accessToken: string }[] = []
22 let video3UUID: string
24 let command: SubscriptionsCommand
26 before(async function () {
29 servers = await createMultipleServers(3)
31 // Get the access tokens
32 await setAccessTokensToServers(servers)
33 await setDefaultChannelAvatar(servers)
34 await setDefaultAccountAvatar(servers)
36 // Server 1 and server 2 follow each other
37 await doubleFollow(servers[0], servers[1])
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 })
43 const accessToken = await server.login.getAccessToken(user)
44 users.push({ accessToken })
46 const videoName1 = 'video 1-' + server.serverNumber
47 await server.videos.upload({ token: accessToken, attributes: { name: videoName1 } })
49 const videoName2 = 'video 2-' + server.serverNumber
50 await server.videos.upload({ token: accessToken, attributes: { name: videoName2 } })
53 await waitJobs(servers)
55 command = servers[0].subscriptions
58 it('Should display videos of server 2 on server 1', async function () {
59 const { total } = await servers[0].videos.list()
61 expect(total).to.equal(4)
64 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
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 })
70 await waitJobs(servers)
72 const attributes = { name: 'video server 3 added after follow' }
73 const { uuid } = await servers[2].videos.upload({ token: users[2].accessToken, attributes })
76 await waitJobs(servers)
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)
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')
90 it('Should list subscriptions', async function () {
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)
99 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
100 expect(body.total).to.equal(2)
102 const subscriptions = body.data
103 expect(subscriptions).to.be.an('array')
104 expect(subscriptions).to.have.lengthOf(2)
106 expect(subscriptions[0].name).to.equal('user3_channel')
107 expect(subscriptions[1].name).to.equal('root_channel')
111 it('Should get subscription', async function () {
113 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
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)
123 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
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)
133 it('Should return the existing subscriptions', async function () {
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
141 const body = await command.exist({ token: users[0].accessToken, uris })
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
149 it('Should search among subscriptions', async function () {
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)
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)
163 it('Should list subscription videos', async function () {
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)
172 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
173 expect(body.total).to.equal(3)
175 const videos = body.data
176 expect(videos).to.be.an('array')
177 expect(videos).to.have.lengthOf(3)
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')
185 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
188 const videoName = 'video server 1 added after follow'
189 await servers[0].videos.upload({ attributes: { name: videoName } })
191 await waitJobs(servers)
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)
201 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
202 expect(body.total).to.equal(4)
204 const videos = body.data
205 expect(videos).to.be.an('array')
206 expect(videos).to.have.lengthOf(4)
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')
215 const { data, total } = await servers[0].videos.list()
216 expect(total).to.equal(5)
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')
226 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
229 await servers[0].follows.follow({ hosts: [ servers[2].url ] })
231 await waitJobs(servers)
233 const { data, total } = await servers[0].videos.list()
234 expect(total).to.equal(8)
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
243 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
246 await servers[0].follows.unfollow({ target: servers[2] })
248 await waitJobs(servers)
250 const { total, data } = await servers[0].videos.list()
251 expect(total).to.equal(5)
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')
260 it('Should still list subscription videos', async function () {
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)
269 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
270 expect(body.total).to.equal(4)
272 const videos = body.data
273 expect(videos).to.be.an('array')
274 expect(videos).to.have.lengthOf(4)
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')
283 it('Should update a video of server 3 and see the updated video on server 1', async function () {
286 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'video server 3 added after follow updated' } })
288 await waitJobs(servers)
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')
294 it('Should remove user of server 3 subscription', async function () {
297 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
299 await waitJobs(servers)
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)
306 const videos = body.data
307 expect(videos).to.be.an('array')
308 expect(videos).to.have.lengthOf(1)
310 expect(videos[0].name).to.equal('video server 1 added after follow')
313 it('Should remove the root subscription and not display the videos anymore', async function () {
316 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
318 await waitJobs(servers)
321 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
322 expect(body.total).to.equal(0)
324 const videos = body.data
325 expect(videos).to.be.an('array')
326 expect(videos).to.have.lengthOf(0)
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)
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')
341 it('Should follow user of server 3 again', async function () {
344 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
346 await waitJobs(servers)
349 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
350 expect(body.total).to.equal(3)
352 const videos = body.data
353 expect(videos).to.be.an('array')
354 expect(videos).to.have.lengthOf(3)
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')
362 const { total, data } = await servers[0].videos.list()
363 expect(total).to.equal(5)
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')
373 it('Should follow user channels of server 3 by root of server 3', async function () {
376 await servers[2].channels.create({ token: users[2].accessToken, attributes: { name: 'user3_channel2' } })
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 })
381 await waitJobs(servers)
384 it('Should list user 3 followers', async function () {
386 const { total, data } = await servers[2].accounts.listFollowers({
387 token: users[2].accessToken,
388 accountName: 'user3',
394 expect(total).to.equal(3)
395 expect(data).to.have.lengthOf(3)
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')
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')
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')
414 const { total, data } = await servers[2].accounts.listFollowers({
415 token: users[2].accessToken,
416 accountName: 'user3',
422 expect(total).to.equal(3)
423 expect(data).to.have.lengthOf(1)
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')
432 const { total, data } = await servers[2].accounts.listFollowers({
433 token: users[2].accessToken,
434 accountName: 'user3',
440 expect(total).to.equal(3)
441 expect(data).to.have.lengthOf(1)
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')
450 const { total, data } = await servers[2].accounts.listFollowers({
451 token: users[2].accessToken,
452 accountName: 'user3',
457 expect(total).to.equal(1)
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[0].host)
463 expect(data[0].follower.name).to.equal('user1')
467 it('Should list user3_channel followers', async function () {
469 const { total, data } = await servers[2].channels.listFollowers({
470 token: users[2].accessToken,
471 channelName: 'user3_channel',
477 expect(total).to.equal(2)
478 expect(data).to.have.lengthOf(2)
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')
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')
492 const { total, data } = await servers[2].channels.listFollowers({
493 token: users[2].accessToken,
494 channelName: 'user3_channel',
500 expect(total).to.equal(2)
501 expect(data).to.have.lengthOf(1)
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')
510 const { total, data } = await servers[2].channels.listFollowers({
511 token: users[2].accessToken,
512 channelName: 'user3_channel',
518 expect(total).to.equal(2)
519 expect(data).to.have.lengthOf(1)
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')
528 const { total, data } = await servers[2].channels.listFollowers({
529 token: users[2].accessToken,
530 channelName: 'user3_channel',
535 expect(total).to.equal(1)
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 it('Should update video as internal and not see from remote server', async function () {
548 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'internal', privacy: VideoPrivacy.INTERNAL } })
549 await waitJobs(servers)
552 const { data } = await command.listVideos({ token: users[0].accessToken })
553 expect(data.find(v => v.name === 'internal')).to.not.exist
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
562 it('Should update video as private and not see from anyone server', async function () {
565 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'private', privacy: VideoPrivacy.PRIVATE } })
566 await waitJobs(servers)
569 const { data } = await command.listVideos({ token: users[0].accessToken })
570 expect(data.find(v => v.name === 'private')).to.not.exist
574 const { data } = await servers[2].subscriptions.listVideos({ token: servers[2].accessToken })
575 expect(data.find(v => v.name === 'private')).to.not.exist
579 after(async function () {
580 await cleanupTests(servers)