1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
9 flushAndRunMultipleServers,
15 } from '../../../../shared/extra-utils'
16 import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
17 import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
18 import { Video, VideoChannel } from '../../../../shared/models/videos'
19 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
22 areSubscriptionsExist,
24 listUserSubscriptions,
25 listUserSubscriptionVideos,
26 removeUserSubscription
27 } from '../../../../shared/extra-utils/users/user-subscriptions'
29 const expect = chai.expect
31 describe('Test users subscriptions', function () {
32 let servers: ServerInfo[] = []
33 const users: { accessToken: string }[] = []
34 let video3UUID: string
36 before(async function () {
39 servers = await flushAndRunMultipleServers(3)
41 // Get the access tokens
42 await setAccessTokensToServers(servers)
44 // Server 1 and server 2 follow each other
45 await doubleFollow(servers[0], servers[1])
48 for (const server of servers) {
49 const user = { username: 'user' + server.serverNumber, password: 'password' }
50 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
52 const accessToken = await userLogin(server, user)
53 users.push({ accessToken })
55 const videoName1 = 'video 1-' + server.serverNumber
56 await uploadVideo(server.url, accessToken, { name: videoName1 })
58 const videoName2 = 'video 2-' + server.serverNumber
59 await uploadVideo(server.url, accessToken, { name: videoName2 })
63 await waitJobs(servers)
66 it('Should display videos of server 2 on server 1', async function () {
67 const res = await getVideosList(servers[0].url)
69 expect(res.body.total).to.equal(4)
72 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
75 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
76 await addUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
78 await waitJobs(servers)
80 const res = await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
81 video3UUID = res.body.video.uuid
83 await waitJobs(servers)
86 it('Should not display videos of server 3 on server 1', async function () {
87 const res = await getVideosList(servers[0].url)
89 expect(res.body.total).to.equal(4)
90 for (const video of res.body.data) {
91 expect(video.name).to.not.contain('1-3')
92 expect(video.name).to.not.contain('2-3')
93 expect(video.name).to.not.contain('video server 3 added after follow')
97 it('Should list subscriptions', async function () {
99 const res = await listUserSubscriptions(servers[0].url, servers[0].accessToken)
100 expect(res.body.total).to.equal(0)
101 expect(res.body.data).to.be.an('array')
102 expect(res.body.data).to.have.lengthOf(0)
106 const res = await listUserSubscriptions(servers[0].url, users[0].accessToken, 'createdAt')
107 expect(res.body.total).to.equal(2)
109 const subscriptions: VideoChannel[] = res.body.data
110 expect(subscriptions).to.be.an('array')
111 expect(subscriptions).to.have.lengthOf(2)
113 expect(subscriptions[0].name).to.equal('user3_channel')
114 expect(subscriptions[1].name).to.equal('root_channel')
118 it('Should get subscription', async function () {
120 const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
121 const videoChannel: VideoChannel = res.body
123 expect(videoChannel.name).to.equal('user3_channel')
124 expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
125 expect(videoChannel.displayName).to.equal('Main user3 channel')
126 expect(videoChannel.followingCount).to.equal(0)
127 expect(videoChannel.followersCount).to.equal(1)
131 const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
132 const videoChannel: VideoChannel = res.body
134 expect(videoChannel.name).to.equal('root_channel')
135 expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
136 expect(videoChannel.displayName).to.equal('Main root channel')
137 expect(videoChannel.followingCount).to.equal(0)
138 expect(videoChannel.followersCount).to.equal(1)
142 it('Should return the existing subscriptions', async function () {
144 'user3_channel@localhost:' + servers[2].port,
145 'root2_channel@localhost:' + servers[0].port,
146 'root_channel@localhost:' + servers[0].port,
147 'user3_channel@localhost:' + servers[0].port
150 const res = await areSubscriptionsExist(servers[0].url, users[0].accessToken, uris)
151 const body = res.body
153 expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true
154 expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false
155 expect(body['root_channel@localhost:' + servers[0].port]).to.be.true
156 expect(body['user3_channel@localhost:' + servers[0].port]).to.be.false
159 it('Should list subscription videos', async function () {
161 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
162 expect(res.body.total).to.equal(0)
163 expect(res.body.data).to.be.an('array')
164 expect(res.body.data).to.have.lengthOf(0)
168 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
169 expect(res.body.total).to.equal(3)
171 const videos: Video[] = res.body.data
172 expect(videos).to.be.an('array')
173 expect(videos).to.have.lengthOf(3)
175 expect(videos[0].name).to.equal('video 1-3')
176 expect(videos[1].name).to.equal('video 2-3')
177 expect(videos[2].name).to.equal('video server 3 added after follow')
181 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
184 const videoName = 'video server 1 added after follow'
185 await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
187 await waitJobs(servers)
190 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
191 expect(res.body.total).to.equal(0)
192 expect(res.body.data).to.be.an('array')
193 expect(res.body.data).to.have.lengthOf(0)
197 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
198 expect(res.body.total).to.equal(4)
200 const videos: Video[] = res.body.data
201 expect(videos).to.be.an('array')
202 expect(videos).to.have.lengthOf(4)
204 expect(videos[0].name).to.equal('video 1-3')
205 expect(videos[1].name).to.equal('video 2-3')
206 expect(videos[2].name).to.equal('video server 3 added after follow')
207 expect(videos[3].name).to.equal('video server 1 added after follow')
211 const res = await getVideosList(servers[0].url)
213 expect(res.body.total).to.equal(5)
214 for (const video of res.body.data) {
215 expect(video.name).to.not.contain('1-3')
216 expect(video.name).to.not.contain('2-3')
217 expect(video.name).to.not.contain('video server 3 added after follow')
222 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
225 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
227 await waitJobs(servers)
229 const res = await getVideosList(servers[0].url)
231 expect(res.body.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 = res.body.data.find(v => v.name.indexOf(name) === -1)
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 unfollow(servers[0].url, servers[0].accessToken, servers[2])
245 await waitJobs(servers)
247 const res = await getVideosList(servers[0].url)
249 expect(res.body.total).to.equal(5)
250 for (const video of res.body.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 res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
260 expect(res.body.total).to.equal(0)
261 expect(res.body.data).to.be.an('array')
262 expect(res.body.data).to.have.lengthOf(0)
266 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
267 expect(res.body.total).to.equal(4)
269 const videos: Video[] = res.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 updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
285 await waitJobs(servers)
287 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
288 const videos: Video[] = res.body.data
289 expect(videos[2].name).to.equal('video server 3 added after follow updated')
292 it('Should remove user of server 3 subscription', async function () {
295 await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
297 await waitJobs(servers)
300 it('Should not display its videos anymore', async function () {
302 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
303 expect(res.body.total).to.equal(1)
305 const videos: Video[] = res.body.data
306 expect(videos).to.be.an('array')
307 expect(videos).to.have.lengthOf(1)
309 expect(videos[0].name).to.equal('video server 1 added after follow')
313 it('Should remove the root subscription and not display the videos anymore', async function () {
316 await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
318 await waitJobs(servers)
321 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
322 expect(res.body.total).to.equal(0)
324 const videos: Video[] = res.body.data
325 expect(videos).to.be.an('array')
326 expect(videos).to.have.lengthOf(0)
330 it('Should correctly display public videos on server 1', async function () {
331 const res = await getVideosList(servers[0].url)
333 expect(res.body.total).to.equal(5)
334 for (const video of res.body.data) {
335 expect(video.name).to.not.contain('1-3')
336 expect(video.name).to.not.contain('2-3')
337 expect(video.name).to.not.contain('video server 3 added after follow updated')
341 it('Should follow user of server 3 again', async function () {
344 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
346 await waitJobs(servers)
349 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
350 expect(res.body.total).to.equal(3)
352 const videos: Video[] = res.body.data
353 expect(videos).to.be.an('array')
354 expect(videos).to.have.lengthOf(3)
356 expect(videos[0].name).to.equal('video 1-3')
357 expect(videos[1].name).to.equal('video 2-3')
358 expect(videos[2].name).to.equal('video server 3 added after follow updated')
362 const res = await getVideosList(servers[0].url)
364 expect(res.body.total).to.equal(5)
365 for (const video of res.body.data) {
366 expect(video.name).to.not.contain('1-3')
367 expect(video.name).to.not.contain('2-3')
368 expect(video.name).to.not.contain('video server 3 added after follow updated')
373 after(async function () {
374 await cleanupTests(servers)