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 it('Should display videos of server 2 on server 1', async function () {
57 const { total } = await servers[0].videos.list()
59 expect(total).to.equal(4)
62 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
65 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
66 await command.add({ token: users[0].accessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
68 await waitJobs(servers)
70 const attributes = { name: 'video server 3 added after follow' }
71 const { uuid } = await servers[2].videos.upload({ token: users[2].accessToken, attributes })
74 await waitJobs(servers)
77 it('Should not display videos of server 3 on server 1', async function () {
78 const { total, data } = await servers[0].videos.list()
79 expect(total).to.equal(4)
81 for (const video of data) {
82 expect(video.name).to.not.contain('1-3')
83 expect(video.name).to.not.contain('2-3')
84 expect(video.name).to.not.contain('video server 3 added after follow')
88 it('Should list subscriptions', async function () {
90 const body = await command.list()
91 expect(body.total).to.equal(0)
92 expect(body.data).to.be.an('array')
93 expect(body.data).to.have.lengthOf(0)
97 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
98 expect(body.total).to.equal(2)
100 const subscriptions = body.data
101 expect(subscriptions).to.be.an('array')
102 expect(subscriptions).to.have.lengthOf(2)
104 expect(subscriptions[0].name).to.equal('user3_channel')
105 expect(subscriptions[1].name).to.equal('root_channel')
109 it('Should get subscription', async function () {
111 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
113 expect(videoChannel.name).to.equal('user3_channel')
114 expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
115 expect(videoChannel.displayName).to.equal('Main user3 channel')
116 expect(videoChannel.followingCount).to.equal(0)
117 expect(videoChannel.followersCount).to.equal(1)
121 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
123 expect(videoChannel.name).to.equal('root_channel')
124 expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
125 expect(videoChannel.displayName).to.equal('Main root channel')
126 expect(videoChannel.followingCount).to.equal(0)
127 expect(videoChannel.followersCount).to.equal(1)
131 it('Should return the existing subscriptions', async function () {
133 'user3_channel@localhost:' + servers[2].port,
134 'root2_channel@localhost:' + servers[0].port,
135 'root_channel@localhost:' + servers[0].port,
136 'user3_channel@localhost:' + servers[0].port
139 const body = await command.exist({ token: users[0].accessToken, uris })
141 expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true
142 expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false
143 expect(body['root_channel@localhost:' + servers[0].port]).to.be.true
144 expect(body['user3_channel@localhost:' + servers[0].port]).to.be.false
147 it('Should search among subscriptions', async function () {
149 const body = await command.list({ token: users[0].accessToken, sort: '-createdAt', search: 'user3_channel' })
150 expect(body.total).to.equal(1)
151 expect(body.data).to.have.lengthOf(1)
155 const body = await command.list({ token: users[0].accessToken, sort: '-createdAt', search: 'toto' })
156 expect(body.total).to.equal(0)
157 expect(body.data).to.have.lengthOf(0)
161 it('Should list subscription videos', async function () {
163 const body = await command.listVideos()
164 expect(body.total).to.equal(0)
165 expect(body.data).to.be.an('array')
166 expect(body.data).to.have.lengthOf(0)
170 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
171 expect(body.total).to.equal(3)
173 const videos = body.data
174 expect(videos).to.be.an('array')
175 expect(videos).to.have.lengthOf(3)
177 expect(videos[0].name).to.equal('video 1-3')
178 expect(videos[1].name).to.equal('video 2-3')
179 expect(videos[2].name).to.equal('video server 3 added after follow')
183 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
186 const videoName = 'video server 1 added after follow'
187 await servers[0].videos.upload({ attributes: { name: videoName } })
189 await waitJobs(servers)
192 const body = await command.listVideos()
193 expect(body.total).to.equal(0)
194 expect(body.data).to.be.an('array')
195 expect(body.data).to.have.lengthOf(0)
199 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
200 expect(body.total).to.equal(4)
202 const videos = body.data
203 expect(videos).to.be.an('array')
204 expect(videos).to.have.lengthOf(4)
206 expect(videos[0].name).to.equal('video 1-3')
207 expect(videos[1].name).to.equal('video 2-3')
208 expect(videos[2].name).to.equal('video server 3 added after follow')
209 expect(videos[3].name).to.equal('video server 1 added after follow')
213 const { data, total } = await servers[0].videos.list()
214 expect(total).to.equal(5)
216 for (const video of data) {
217 expect(video.name).to.not.contain('1-3')
218 expect(video.name).to.not.contain('2-3')
219 expect(video.name).to.not.contain('video server 3 added after follow')
224 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
227 await servers[0].follows.follow({ hosts: [ servers[2].url ] })
229 await waitJobs(servers)
231 const { data, total } = await servers[0].videos.list()
232 expect(total).to.equal(8)
234 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
235 for (const name of names) {
236 const video = data.find(v => v.name.includes(name))
237 expect(video).to.not.be.undefined
241 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
244 await servers[0].follows.unfollow({ target: servers[2] })
246 await waitJobs(servers)
248 const { total, data } = await servers[0].videos.list()
249 expect(total).to.equal(5)
251 for (const video of data) {
252 expect(video.name).to.not.contain('1-3')
253 expect(video.name).to.not.contain('2-3')
254 expect(video.name).to.not.contain('video server 3 added after follow')
258 it('Should still list subscription videos', async function () {
260 const body = await command.listVideos()
261 expect(body.total).to.equal(0)
262 expect(body.data).to.be.an('array')
263 expect(body.data).to.have.lengthOf(0)
267 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
268 expect(body.total).to.equal(4)
270 const videos = body.data
271 expect(videos).to.be.an('array')
272 expect(videos).to.have.lengthOf(4)
274 expect(videos[0].name).to.equal('video 1-3')
275 expect(videos[1].name).to.equal('video 2-3')
276 expect(videos[2].name).to.equal('video server 3 added after follow')
277 expect(videos[3].name).to.equal('video server 1 added after follow')
281 it('Should update a video of server 3 and see the updated video on server 1', async function () {
284 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'video server 3 added after follow updated' } })
286 await waitJobs(servers)
288 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
289 expect(body.data[2].name).to.equal('video server 3 added after follow updated')
292 it('Should remove user of server 3 subscription', async function () {
295 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
297 await waitJobs(servers)
300 it('Should not display its videos anymore', async function () {
301 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
302 expect(body.total).to.equal(1)
304 const videos = body.data
305 expect(videos).to.be.an('array')
306 expect(videos).to.have.lengthOf(1)
308 expect(videos[0].name).to.equal('video server 1 added after follow')
311 it('Should remove the root subscription and not display the videos anymore', async function () {
314 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
316 await waitJobs(servers)
319 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
320 expect(body.total).to.equal(0)
322 const videos = body.data
323 expect(videos).to.be.an('array')
324 expect(videos).to.have.lengthOf(0)
328 it('Should correctly display public videos on server 1', async function () {
329 const { total, data } = await servers[0].videos.list()
330 expect(total).to.equal(5)
332 for (const video of data) {
333 expect(video.name).to.not.contain('1-3')
334 expect(video.name).to.not.contain('2-3')
335 expect(video.name).to.not.contain('video server 3 added after follow updated')
339 it('Should follow user of server 3 again', async function () {
342 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
344 await waitJobs(servers)
347 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
348 expect(body.total).to.equal(3)
350 const videos = body.data
351 expect(videos).to.be.an('array')
352 expect(videos).to.have.lengthOf(3)
354 expect(videos[0].name).to.equal('video 1-3')
355 expect(videos[1].name).to.equal('video 2-3')
356 expect(videos[2].name).to.equal('video server 3 added after follow updated')
360 const { total, data } = await servers[0].videos.list()
361 expect(total).to.equal(5)
363 for (const video of data) {
364 expect(video.name).to.not.contain('1-3')
365 expect(video.name).to.not.contain('2-3')
366 expect(video.name).to.not.contain('video server 3 added after follow updated')
371 it('Should follow user channels of server 3 by root of server 3', async function () {
374 await servers[2].channels.create({ token: users[2].accessToken, attributes: { name: 'user3_channel2' } })
376 await servers[2].subscriptions.add({ token: servers[2].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
377 await servers[2].subscriptions.add({ token: servers[2].accessToken, targetUri: 'user3_channel2@localhost:' + servers[2].port })
379 await waitJobs(servers)
382 it('Should list user 3 followers', async function () {
384 const { total, data } = await servers[2].accounts.listFollowers({
385 token: users[2].accessToken,
386 accountName: 'user3',
392 expect(total).to.equal(3)
393 expect(data).to.have.lengthOf(3)
395 expect(data[0].following.host).to.equal(servers[2].host)
396 expect(data[0].following.name).to.equal('user3_channel')
397 expect(data[0].follower.host).to.equal(servers[0].host)
398 expect(data[0].follower.name).to.equal('user1')
400 expect(data[1].following.host).to.equal(servers[2].host)
401 expect(data[1].following.name).to.equal('user3_channel')
402 expect(data[1].follower.host).to.equal(servers[2].host)
403 expect(data[1].follower.name).to.equal('root')
405 expect(data[2].following.host).to.equal(servers[2].host)
406 expect(data[2].following.name).to.equal('user3_channel2')
407 expect(data[2].follower.host).to.equal(servers[2].host)
408 expect(data[2].follower.name).to.equal('root')
412 const { total, data } = await servers[2].accounts.listFollowers({
413 token: users[2].accessToken,
414 accountName: 'user3',
420 expect(total).to.equal(3)
421 expect(data).to.have.lengthOf(1)
423 expect(data[0].following.host).to.equal(servers[2].host)
424 expect(data[0].following.name).to.equal('user3_channel2')
425 expect(data[0].follower.host).to.equal(servers[2].host)
426 expect(data[0].follower.name).to.equal('root')
430 const { total, data } = await servers[2].accounts.listFollowers({
431 token: users[2].accessToken,
432 accountName: 'user3',
438 expect(total).to.equal(3)
439 expect(data).to.have.lengthOf(1)
441 expect(data[0].following.host).to.equal(servers[2].host)
442 expect(data[0].following.name).to.equal('user3_channel')
443 expect(data[0].follower.host).to.equal(servers[2].host)
444 expect(data[0].follower.name).to.equal('root')
448 const { total, data } = await servers[2].accounts.listFollowers({
449 token: users[2].accessToken,
450 accountName: 'user3',
455 expect(total).to.equal(1)
456 expect(data).to.have.lengthOf(1)
458 expect(data[0].following.host).to.equal(servers[2].host)
459 expect(data[0].following.name).to.equal('user3_channel')
460 expect(data[0].follower.host).to.equal(servers[0].host)
461 expect(data[0].follower.name).to.equal('user1')
465 it('Should list user3_channel followers', async function () {
467 const { total, data } = await servers[2].channels.listFollowers({
468 token: users[2].accessToken,
469 channelName: 'user3_channel',
475 expect(total).to.equal(2)
476 expect(data).to.have.lengthOf(2)
478 expect(data[0].following.host).to.equal(servers[2].host)
479 expect(data[0].following.name).to.equal('user3_channel')
480 expect(data[0].follower.host).to.equal(servers[0].host)
481 expect(data[0].follower.name).to.equal('user1')
483 expect(data[1].following.host).to.equal(servers[2].host)
484 expect(data[1].following.name).to.equal('user3_channel')
485 expect(data[1].follower.host).to.equal(servers[2].host)
486 expect(data[1].follower.name).to.equal('root')
490 const { total, data } = await servers[2].channels.listFollowers({
491 token: users[2].accessToken,
492 channelName: 'user3_channel',
498 expect(total).to.equal(2)
499 expect(data).to.have.lengthOf(1)
501 expect(data[0].following.host).to.equal(servers[2].host)
502 expect(data[0].following.name).to.equal('user3_channel')
503 expect(data[0].follower.host).to.equal(servers[2].host)
504 expect(data[0].follower.name).to.equal('root')
508 const { total, data } = await servers[2].channels.listFollowers({
509 token: users[2].accessToken,
510 channelName: 'user3_channel',
516 expect(total).to.equal(2)
517 expect(data).to.have.lengthOf(1)
519 expect(data[0].following.host).to.equal(servers[2].host)
520 expect(data[0].following.name).to.equal('user3_channel')
521 expect(data[0].follower.host).to.equal(servers[0].host)
522 expect(data[0].follower.name).to.equal('user1')
526 const { total, data } = await servers[2].channels.listFollowers({
527 token: users[2].accessToken,
528 channelName: 'user3_channel',
533 expect(total).to.equal(1)
534 expect(data).to.have.lengthOf(1)
536 expect(data[0].following.host).to.equal(servers[2].host)
537 expect(data[0].following.name).to.equal('user3_channel')
538 expect(data[0].follower.host).to.equal(servers[0].host)
539 expect(data[0].follower.name).to.equal('user1')
543 it('Should update video as internal and not see from remote server', async function () {
546 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'internal', privacy: VideoPrivacy.INTERNAL } })
547 await waitJobs(servers)
550 const { data } = await command.listVideos({ token: users[0].accessToken })
551 expect(data.find(v => v.name === 'internal')).to.not.exist
555 it('Should see internal from local user', async function () {
556 const { data } = await servers[2].subscriptions.listVideos({ token: servers[2].accessToken })
557 expect(data.find(v => v.name === 'internal')).to.exist
560 it('Should update video as private and not see from anyone server', async function () {
563 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'private', privacy: VideoPrivacy.PRIVATE } })
564 await waitJobs(servers)
567 const { data } = await command.listVideos({ token: users[0].accessToken })
568 expect(data.find(v => v.name === 'private')).to.not.exist
572 const { data } = await servers[2].subscriptions.listVideos({ token: servers[2].accessToken })
573 expect(data.find(v => v.name === 'private')).to.not.exist
577 after(async function () {
578 await cleanupTests(servers)