1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
9 flushAndRunMultipleServers,
12 setAccessTokensToServers,
18 } from '@shared/extra-utils'
20 const expect = chai.expect
22 describe('Test users subscriptions', function () {
23 let servers: ServerInfo[] = []
24 const users: { accessToken: string }[] = []
25 let video3UUID: string
27 let command: SubscriptionsCommand
29 before(async function () {
32 servers = await flushAndRunMultipleServers(3)
34 // Get the access tokens
35 await setAccessTokensToServers(servers)
37 // Server 1 and server 2 follow each other
38 await doubleFollow(servers[0], servers[1])
41 for (const server of servers) {
42 const user = { username: 'user' + server.serverNumber, password: 'password' }
43 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
45 const accessToken = await userLogin(server, user)
46 users.push({ accessToken })
48 const videoName1 = 'video 1-' + server.serverNumber
49 await uploadVideo(server.url, accessToken, { name: videoName1 })
51 const videoName2 = 'video 2-' + server.serverNumber
52 await uploadVideo(server.url, accessToken, { name: videoName2 })
56 await waitJobs(servers)
58 command = servers[0].subscriptionsCommand
61 it('Should display videos of server 2 on server 1', async function () {
62 const res = await getVideosList(servers[0].url)
64 expect(res.body.total).to.equal(4)
67 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
70 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
71 await command.add({ token: users[0].accessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
73 await waitJobs(servers)
75 const res = await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
76 video3UUID = res.body.video.uuid
78 await waitJobs(servers)
81 it('Should not display videos of server 3 on server 1', async function () {
82 const res = await getVideosList(servers[0].url)
84 expect(res.body.total).to.equal(4)
85 for (const video of res.body.data) {
86 expect(video.name).to.not.contain('1-3')
87 expect(video.name).to.not.contain('2-3')
88 expect(video.name).to.not.contain('video server 3 added after follow')
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)
165 it('Should list subscription videos', async function () {
167 const body = await command.listVideos()
168 expect(body.total).to.equal(0)
169 expect(body.data).to.be.an('array')
170 expect(body.data).to.have.lengthOf(0)
174 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
175 expect(body.total).to.equal(3)
177 const videos = body.data
178 expect(videos).to.be.an('array')
179 expect(videos).to.have.lengthOf(3)
181 expect(videos[0].name).to.equal('video 1-3')
182 expect(videos[1].name).to.equal('video 2-3')
183 expect(videos[2].name).to.equal('video server 3 added after follow')
187 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
190 const videoName = 'video server 1 added after follow'
191 await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
193 await waitJobs(servers)
196 const body = await command.listVideos()
197 expect(body.total).to.equal(0)
198 expect(body.data).to.be.an('array')
199 expect(body.data).to.have.lengthOf(0)
203 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
204 expect(body.total).to.equal(4)
206 const videos = body.data
207 expect(videos).to.be.an('array')
208 expect(videos).to.have.lengthOf(4)
210 expect(videos[0].name).to.equal('video 1-3')
211 expect(videos[1].name).to.equal('video 2-3')
212 expect(videos[2].name).to.equal('video server 3 added after follow')
213 expect(videos[3].name).to.equal('video server 1 added after follow')
217 const res = await getVideosList(servers[0].url)
219 expect(res.body.total).to.equal(5)
220 for (const video of res.body.data) {
221 expect(video.name).to.not.contain('1-3')
222 expect(video.name).to.not.contain('2-3')
223 expect(video.name).to.not.contain('video server 3 added after follow')
228 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
231 await servers[0].followsCommand.follow({ targets: [ servers[2].url ] })
233 await waitJobs(servers)
235 const res = await getVideosList(servers[0].url)
237 expect(res.body.total).to.equal(8)
239 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
240 for (const name of names) {
241 const video = res.body.data.find(v => v.name.indexOf(name) === -1)
242 expect(video).to.not.be.undefined
246 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
249 await servers[0].followsCommand.unfollow({ target: servers[2] })
251 await waitJobs(servers)
253 const res = await getVideosList(servers[0].url)
255 expect(res.body.total).to.equal(5)
256 for (const video of res.body.data) {
257 expect(video.name).to.not.contain('1-3')
258 expect(video.name).to.not.contain('2-3')
259 expect(video.name).to.not.contain('video server 3 added after follow')
263 it('Should still list subscription videos', async function () {
265 const body = await command.listVideos()
266 expect(body.total).to.equal(0)
267 expect(body.data).to.be.an('array')
268 expect(body.data).to.have.lengthOf(0)
272 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
273 expect(body.total).to.equal(4)
275 const videos = body.data
276 expect(videos).to.be.an('array')
277 expect(videos).to.have.lengthOf(4)
279 expect(videos[0].name).to.equal('video 1-3')
280 expect(videos[1].name).to.equal('video 2-3')
281 expect(videos[2].name).to.equal('video server 3 added after follow')
282 expect(videos[3].name).to.equal('video server 1 added after follow')
286 it('Should update a video of server 3 and see the updated video on server 1', async function () {
289 await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
291 await waitJobs(servers)
293 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
294 expect(body.data[2].name).to.equal('video server 3 added after follow updated')
297 it('Should remove user of server 3 subscription', async function () {
300 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
302 await waitJobs(servers)
305 it('Should not display its videos anymore', async function () {
306 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
307 expect(body.total).to.equal(1)
309 const videos = body.data
310 expect(videos).to.be.an('array')
311 expect(videos).to.have.lengthOf(1)
313 expect(videos[0].name).to.equal('video server 1 added after follow')
316 it('Should remove the root subscription and not display the videos anymore', async function () {
319 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
321 await waitJobs(servers)
324 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
325 expect(body.total).to.equal(0)
327 const videos = body.data
328 expect(videos).to.be.an('array')
329 expect(videos).to.have.lengthOf(0)
333 it('Should correctly display public videos on server 1', async function () {
334 const res = await getVideosList(servers[0].url)
336 expect(res.body.total).to.equal(5)
337 for (const video of res.body.data) {
338 expect(video.name).to.not.contain('1-3')
339 expect(video.name).to.not.contain('2-3')
340 expect(video.name).to.not.contain('video server 3 added after follow updated')
344 it('Should follow user of server 3 again', async function () {
347 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
349 await waitJobs(servers)
352 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
353 expect(body.total).to.equal(3)
355 const videos = body.data
356 expect(videos).to.be.an('array')
357 expect(videos).to.have.lengthOf(3)
359 expect(videos[0].name).to.equal('video 1-3')
360 expect(videos[1].name).to.equal('video 2-3')
361 expect(videos[2].name).to.equal('video server 3 added after follow updated')
365 const res = await getVideosList(servers[0].url)
367 expect(res.body.total).to.equal(5)
368 for (const video of res.body.data) {
369 expect(video.name).to.not.contain('1-3')
370 expect(video.name).to.not.contain('2-3')
371 expect(video.name).to.not.contain('video server 3 added after follow updated')
376 after(async function () {
377 await cleanupTests(servers)