1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
8 flushAndRunMultipleServers,
10 setAccessTokensToServers,
13 } from '@shared/extra-utils'
15 const expect = chai.expect
17 describe('Test users subscriptions', function () {
18 let servers: ServerInfo[] = []
19 const users: { accessToken: string }[] = []
20 let video3UUID: string
22 let command: SubscriptionsCommand
24 before(async function () {
27 servers = await flushAndRunMultipleServers(3)
29 // Get the access tokens
30 await setAccessTokensToServers(servers)
32 // Server 1 and server 2 follow each other
33 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 } })
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 { uuid } = await servers[2].videos.upload({ attributes: { name: 'video server 3 added after follow' } })
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({ targets: [ 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 after(async function () {
371 await cleanupTests(servers)