1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
11 flushAndRunMultipleServers,
14 listUserSubscriptions,
15 listUserSubscriptionVideos,
16 removeUserSubscription,
18 setAccessTokensToServers,
23 } from '@shared/extra-utils'
24 import { Video, VideoChannel } from '@shared/models'
26 const expect = chai.expect
28 describe('Test users subscriptions', function () {
29 let servers: ServerInfo[] = []
30 const users: { accessToken: string }[] = []
31 let video3UUID: string
33 before(async function () {
36 servers = await flushAndRunMultipleServers(3)
38 // Get the access tokens
39 await setAccessTokensToServers(servers)
41 // Server 1 and server 2 follow each other
42 await doubleFollow(servers[0], servers[1])
45 for (const server of servers) {
46 const user = { username: 'user' + server.serverNumber, password: 'password' }
47 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
49 const accessToken = await userLogin(server, user)
50 users.push({ accessToken })
52 const videoName1 = 'video 1-' + server.serverNumber
53 await uploadVideo(server.url, accessToken, { name: videoName1 })
55 const videoName2 = 'video 2-' + server.serverNumber
56 await uploadVideo(server.url, accessToken, { name: videoName2 })
60 await waitJobs(servers)
63 it('Should display videos of server 2 on server 1', async function () {
64 const res = await getVideosList(servers[0].url)
66 expect(res.body.total).to.equal(4)
69 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
72 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
73 await addUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
75 await waitJobs(servers)
77 const res = await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
78 video3UUID = res.body.video.uuid
80 await waitJobs(servers)
83 it('Should not display videos of server 3 on server 1', async function () {
84 const res = await getVideosList(servers[0].url)
86 expect(res.body.total).to.equal(4)
87 for (const video of res.body.data) {
88 expect(video.name).to.not.contain('1-3')
89 expect(video.name).to.not.contain('2-3')
90 expect(video.name).to.not.contain('video server 3 added after follow')
94 it('Should list subscriptions', async function () {
96 const res = await listUserSubscriptions({ url: servers[0].url, token: servers[0].accessToken })
97 expect(res.body.total).to.equal(0)
98 expect(res.body.data).to.be.an('array')
99 expect(res.body.data).to.have.lengthOf(0)
103 const res = await listUserSubscriptions({ url: servers[0].url, token: users[0].accessToken, sort: 'createdAt' })
104 expect(res.body.total).to.equal(2)
106 const subscriptions: VideoChannel[] = res.body.data
107 expect(subscriptions).to.be.an('array')
108 expect(subscriptions).to.have.lengthOf(2)
110 expect(subscriptions[0].name).to.equal('user3_channel')
111 expect(subscriptions[1].name).to.equal('root_channel')
115 it('Should get subscription', async function () {
117 const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
118 const videoChannel: VideoChannel = res.body
120 expect(videoChannel.name).to.equal('user3_channel')
121 expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
122 expect(videoChannel.displayName).to.equal('Main user3 channel')
123 expect(videoChannel.followingCount).to.equal(0)
124 expect(videoChannel.followersCount).to.equal(1)
128 const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
129 const videoChannel: VideoChannel = res.body
131 expect(videoChannel.name).to.equal('root_channel')
132 expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
133 expect(videoChannel.displayName).to.equal('Main root channel')
134 expect(videoChannel.followingCount).to.equal(0)
135 expect(videoChannel.followersCount).to.equal(1)
139 it('Should return the existing subscriptions', async function () {
141 'user3_channel@localhost:' + servers[2].port,
142 'root2_channel@localhost:' + servers[0].port,
143 'root_channel@localhost:' + servers[0].port,
144 'user3_channel@localhost:' + servers[0].port
147 const res = await areSubscriptionsExist(servers[0].url, users[0].accessToken, uris)
148 const body = res.body
150 expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true
151 expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false
152 expect(body['root_channel@localhost:' + servers[0].port]).to.be.true
153 expect(body['user3_channel@localhost:' + servers[0].port]).to.be.false
156 it('Should search among subscriptions', async function () {
158 const res = await listUserSubscriptions({
160 token: users[0].accessToken,
162 search: 'user3_channel'
164 expect(res.body.total).to.equal(1)
166 const subscriptions = res.body.data
167 expect(subscriptions).to.have.lengthOf(1)
171 const res = await listUserSubscriptions({
173 token: users[0].accessToken,
177 expect(res.body.total).to.equal(0)
179 const subscriptions = res.body.data
180 expect(subscriptions).to.have.lengthOf(0)
184 it('Should list subscription videos', async function () {
186 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
187 expect(res.body.total).to.equal(0)
188 expect(res.body.data).to.be.an('array')
189 expect(res.body.data).to.have.lengthOf(0)
193 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
194 expect(res.body.total).to.equal(3)
196 const videos: Video[] = res.body.data
197 expect(videos).to.be.an('array')
198 expect(videos).to.have.lengthOf(3)
200 expect(videos[0].name).to.equal('video 1-3')
201 expect(videos[1].name).to.equal('video 2-3')
202 expect(videos[2].name).to.equal('video server 3 added after follow')
206 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
209 const videoName = 'video server 1 added after follow'
210 await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
212 await waitJobs(servers)
215 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
216 expect(res.body.total).to.equal(0)
217 expect(res.body.data).to.be.an('array')
218 expect(res.body.data).to.have.lengthOf(0)
222 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
223 expect(res.body.total).to.equal(4)
225 const videos: Video[] = res.body.data
226 expect(videos).to.be.an('array')
227 expect(videos).to.have.lengthOf(4)
229 expect(videos[0].name).to.equal('video 1-3')
230 expect(videos[1].name).to.equal('video 2-3')
231 expect(videos[2].name).to.equal('video server 3 added after follow')
232 expect(videos[3].name).to.equal('video server 1 added after follow')
236 const res = await getVideosList(servers[0].url)
238 expect(res.body.total).to.equal(5)
239 for (const video of res.body.data) {
240 expect(video.name).to.not.contain('1-3')
241 expect(video.name).to.not.contain('2-3')
242 expect(video.name).to.not.contain('video server 3 added after follow')
247 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
250 await servers[0].followsCommand.follow({ targets: [ servers[2].url ] })
252 await waitJobs(servers)
254 const res = await getVideosList(servers[0].url)
256 expect(res.body.total).to.equal(8)
258 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
259 for (const name of names) {
260 const video = res.body.data.find(v => v.name.indexOf(name) === -1)
261 expect(video).to.not.be.undefined
265 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
268 await servers[0].followsCommand.unfollow({ target: servers[2] })
270 await waitJobs(servers)
272 const res = await getVideosList(servers[0].url)
274 expect(res.body.total).to.equal(5)
275 for (const video of res.body.data) {
276 expect(video.name).to.not.contain('1-3')
277 expect(video.name).to.not.contain('2-3')
278 expect(video.name).to.not.contain('video server 3 added after follow')
282 it('Should still list subscription videos', async function () {
284 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
285 expect(res.body.total).to.equal(0)
286 expect(res.body.data).to.be.an('array')
287 expect(res.body.data).to.have.lengthOf(0)
291 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
292 expect(res.body.total).to.equal(4)
294 const videos: Video[] = res.body.data
295 expect(videos).to.be.an('array')
296 expect(videos).to.have.lengthOf(4)
298 expect(videos[0].name).to.equal('video 1-3')
299 expect(videos[1].name).to.equal('video 2-3')
300 expect(videos[2].name).to.equal('video server 3 added after follow')
301 expect(videos[3].name).to.equal('video server 1 added after follow')
305 it('Should update a video of server 3 and see the updated video on server 1', async function () {
308 await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
310 await waitJobs(servers)
312 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
313 const videos: Video[] = res.body.data
314 expect(videos[2].name).to.equal('video server 3 added after follow updated')
317 it('Should remove user of server 3 subscription', async function () {
320 await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
322 await waitJobs(servers)
325 it('Should not display its videos anymore', async function () {
327 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
328 expect(res.body.total).to.equal(1)
330 const videos: Video[] = res.body.data
331 expect(videos).to.be.an('array')
332 expect(videos).to.have.lengthOf(1)
334 expect(videos[0].name).to.equal('video server 1 added after follow')
338 it('Should remove the root subscription and not display the videos anymore', async function () {
341 await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
343 await waitJobs(servers)
346 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
347 expect(res.body.total).to.equal(0)
349 const videos: Video[] = res.body.data
350 expect(videos).to.be.an('array')
351 expect(videos).to.have.lengthOf(0)
355 it('Should correctly display public videos on server 1', async function () {
356 const res = await getVideosList(servers[0].url)
358 expect(res.body.total).to.equal(5)
359 for (const video of res.body.data) {
360 expect(video.name).to.not.contain('1-3')
361 expect(video.name).to.not.contain('2-3')
362 expect(video.name).to.not.contain('video server 3 added after follow updated')
366 it('Should follow user of server 3 again', async function () {
369 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
371 await waitJobs(servers)
374 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
375 expect(res.body.total).to.equal(3)
377 const videos: Video[] = res.body.data
378 expect(videos).to.be.an('array')
379 expect(videos).to.have.lengthOf(3)
381 expect(videos[0].name).to.equal('video 1-3')
382 expect(videos[1].name).to.equal('video 2-3')
383 expect(videos[2].name).to.equal('video server 3 added after follow updated')
387 const res = await getVideosList(servers[0].url)
389 expect(res.body.total).to.equal(5)
390 for (const video of res.body.data) {
391 expect(video.name).to.not.contain('1-3')
392 expect(video.name).to.not.contain('2-3')
393 expect(video.name).to.not.contain('video server 3 added after follow updated')
398 after(async function () {
399 await cleanupTests(servers)