1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
8 flushAndRunMultipleServers,
11 setAccessTokensToServers,
16 } from '@shared/extra-utils'
18 const expect = chai.expect
20 describe('Test users subscriptions', function () {
21 let servers: ServerInfo[] = []
22 const users: { accessToken: string }[] = []
23 let video3UUID: string
25 let command: SubscriptionsCommand
27 before(async function () {
30 servers = await flushAndRunMultipleServers(3)
32 // Get the access tokens
33 await setAccessTokensToServers(servers)
35 // Server 1 and server 2 follow each other
36 await doubleFollow(servers[0], servers[1])
39 for (const server of servers) {
40 const user = { username: 'user' + server.serverNumber, password: 'password' }
41 await server.usersCommand.create({ username: user.username, password: user.password })
43 const accessToken = await server.loginCommand.getAccessToken(user)
44 users.push({ accessToken })
46 const videoName1 = 'video 1-' + server.serverNumber
47 await uploadVideo(server.url, accessToken, { name: videoName1 })
49 const videoName2 = 'video 2-' + server.serverNumber
50 await uploadVideo(server.url, accessToken, { name: videoName2 })
54 await waitJobs(servers)
56 command = servers[0].subscriptionsCommand
59 it('Should display videos of server 2 on server 1', async function () {
60 const res = await getVideosList(servers[0].url)
62 expect(res.body.total).to.equal(4)
65 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
68 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
69 await command.add({ token: users[0].accessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
71 await waitJobs(servers)
73 const res = await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
74 video3UUID = res.body.video.uuid
76 await waitJobs(servers)
79 it('Should not display videos of server 3 on server 1', async function () {
80 const res = await getVideosList(servers[0].url)
82 expect(res.body.total).to.equal(4)
83 for (const video of res.body.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 uploadVideo(servers[0].url, servers[0].accessToken, { 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 res = await getVideosList(servers[0].url)
217 expect(res.body.total).to.equal(5)
218 for (const video of res.body.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].followsCommand.follow({ targets: [ servers[2].url ] })
231 await waitJobs(servers)
233 const res = await getVideosList(servers[0].url)
235 expect(res.body.total).to.equal(8)
237 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
238 for (const name of names) {
239 const video = res.body.data.find(v => v.name.indexOf(name) === -1)
240 expect(video).to.not.be.undefined
244 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
247 await servers[0].followsCommand.unfollow({ target: servers[2] })
249 await waitJobs(servers)
251 const res = await getVideosList(servers[0].url)
253 expect(res.body.total).to.equal(5)
254 for (const video of res.body.data) {
255 expect(video.name).to.not.contain('1-3')
256 expect(video.name).to.not.contain('2-3')
257 expect(video.name).to.not.contain('video server 3 added after follow')
261 it('Should still list subscription videos', async function () {
263 const body = await command.listVideos()
264 expect(body.total).to.equal(0)
265 expect(body.data).to.be.an('array')
266 expect(body.data).to.have.lengthOf(0)
270 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
271 expect(body.total).to.equal(4)
273 const videos = body.data
274 expect(videos).to.be.an('array')
275 expect(videos).to.have.lengthOf(4)
277 expect(videos[0].name).to.equal('video 1-3')
278 expect(videos[1].name).to.equal('video 2-3')
279 expect(videos[2].name).to.equal('video server 3 added after follow')
280 expect(videos[3].name).to.equal('video server 1 added after follow')
284 it('Should update a video of server 3 and see the updated video on server 1', async function () {
287 await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
289 await waitJobs(servers)
291 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
292 expect(body.data[2].name).to.equal('video server 3 added after follow updated')
295 it('Should remove user of server 3 subscription', async function () {
298 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
300 await waitJobs(servers)
303 it('Should not display its videos anymore', async function () {
304 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
305 expect(body.total).to.equal(1)
307 const videos = body.data
308 expect(videos).to.be.an('array')
309 expect(videos).to.have.lengthOf(1)
311 expect(videos[0].name).to.equal('video server 1 added after follow')
314 it('Should remove the root subscription and not display the videos anymore', async function () {
317 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
319 await waitJobs(servers)
322 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
323 expect(body.total).to.equal(0)
325 const videos = body.data
326 expect(videos).to.be.an('array')
327 expect(videos).to.have.lengthOf(0)
331 it('Should correctly display public videos on server 1', async function () {
332 const res = await getVideosList(servers[0].url)
334 expect(res.body.total).to.equal(5)
335 for (const video of res.body.data) {
336 expect(video.name).to.not.contain('1-3')
337 expect(video.name).to.not.contain('2-3')
338 expect(video.name).to.not.contain('video server 3 added after follow updated')
342 it('Should follow user of server 3 again', async function () {
345 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
347 await waitJobs(servers)
350 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
351 expect(body.total).to.equal(3)
353 const videos = body.data
354 expect(videos).to.be.an('array')
355 expect(videos).to.have.lengthOf(3)
357 expect(videos[0].name).to.equal('video 1-3')
358 expect(videos[1].name).to.equal('video 2-3')
359 expect(videos[2].name).to.equal('video server 3 added after follow updated')
363 const res = await getVideosList(servers[0].url)
365 expect(res.body.total).to.equal(5)
366 for (const video of res.body.data) {
367 expect(video.name).to.not.contain('1-3')
368 expect(video.name).to.not.contain('2-3')
369 expect(video.name).to.not.contain('video server 3 added after follow updated')
374 after(async function () {
375 await cleanupTests(servers)