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({ url: servers[0].url, token: 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({ url: servers[0].url, token: users[0].accessToken, sort: '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 search among subscriptions', async function () {
161 const res = await listUserSubscriptions({
163 token: users[0].accessToken,
165 search: 'user3_channel'
167 expect(res.body.total).to.equal(1)
169 const subscriptions = res.body.data
170 expect(subscriptions).to.have.lengthOf(1)
174 const res = await listUserSubscriptions({
176 token: users[0].accessToken,
180 expect(res.body.total).to.equal(0)
182 const subscriptions = res.body.data
183 expect(subscriptions).to.have.lengthOf(0)
187 it('Should list subscription videos', async function () {
189 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
190 expect(res.body.total).to.equal(0)
191 expect(res.body.data).to.be.an('array')
192 expect(res.body.data).to.have.lengthOf(0)
196 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
197 expect(res.body.total).to.equal(3)
199 const videos: Video[] = res.body.data
200 expect(videos).to.be.an('array')
201 expect(videos).to.have.lengthOf(3)
203 expect(videos[0].name).to.equal('video 1-3')
204 expect(videos[1].name).to.equal('video 2-3')
205 expect(videos[2].name).to.equal('video server 3 added after follow')
209 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
212 const videoName = 'video server 1 added after follow'
213 await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
215 await waitJobs(servers)
218 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
219 expect(res.body.total).to.equal(0)
220 expect(res.body.data).to.be.an('array')
221 expect(res.body.data).to.have.lengthOf(0)
225 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
226 expect(res.body.total).to.equal(4)
228 const videos: Video[] = res.body.data
229 expect(videos).to.be.an('array')
230 expect(videos).to.have.lengthOf(4)
232 expect(videos[0].name).to.equal('video 1-3')
233 expect(videos[1].name).to.equal('video 2-3')
234 expect(videos[2].name).to.equal('video server 3 added after follow')
235 expect(videos[3].name).to.equal('video server 1 added after follow')
239 const res = await getVideosList(servers[0].url)
241 expect(res.body.total).to.equal(5)
242 for (const video of res.body.data) {
243 expect(video.name).to.not.contain('1-3')
244 expect(video.name).to.not.contain('2-3')
245 expect(video.name).to.not.contain('video server 3 added after follow')
250 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
253 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
255 await waitJobs(servers)
257 const res = await getVideosList(servers[0].url)
259 expect(res.body.total).to.equal(8)
261 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
262 for (const name of names) {
263 const video = res.body.data.find(v => v.name.indexOf(name) === -1)
264 expect(video).to.not.be.undefined
268 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
271 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
273 await waitJobs(servers)
275 const res = await getVideosList(servers[0].url)
277 expect(res.body.total).to.equal(5)
278 for (const video of res.body.data) {
279 expect(video.name).to.not.contain('1-3')
280 expect(video.name).to.not.contain('2-3')
281 expect(video.name).to.not.contain('video server 3 added after follow')
285 it('Should still list subscription videos', async function () {
287 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
288 expect(res.body.total).to.equal(0)
289 expect(res.body.data).to.be.an('array')
290 expect(res.body.data).to.have.lengthOf(0)
294 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
295 expect(res.body.total).to.equal(4)
297 const videos: Video[] = res.body.data
298 expect(videos).to.be.an('array')
299 expect(videos).to.have.lengthOf(4)
301 expect(videos[0].name).to.equal('video 1-3')
302 expect(videos[1].name).to.equal('video 2-3')
303 expect(videos[2].name).to.equal('video server 3 added after follow')
304 expect(videos[3].name).to.equal('video server 1 added after follow')
308 it('Should update a video of server 3 and see the updated video on server 1', async function () {
311 await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
313 await waitJobs(servers)
315 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
316 const videos: Video[] = res.body.data
317 expect(videos[2].name).to.equal('video server 3 added after follow updated')
320 it('Should remove user of server 3 subscription', async function () {
323 await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
325 await waitJobs(servers)
328 it('Should not display its videos anymore', async function () {
330 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
331 expect(res.body.total).to.equal(1)
333 const videos: Video[] = res.body.data
334 expect(videos).to.be.an('array')
335 expect(videos).to.have.lengthOf(1)
337 expect(videos[0].name).to.equal('video server 1 added after follow')
341 it('Should remove the root subscription and not display the videos anymore', async function () {
344 await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
346 await waitJobs(servers)
349 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
350 expect(res.body.total).to.equal(0)
352 const videos: Video[] = res.body.data
353 expect(videos).to.be.an('array')
354 expect(videos).to.have.lengthOf(0)
358 it('Should correctly display public videos on server 1', async function () {
359 const res = await getVideosList(servers[0].url)
361 expect(res.body.total).to.equal(5)
362 for (const video of res.body.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')
369 it('Should follow user of server 3 again', async function () {
372 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
374 await waitJobs(servers)
377 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
378 expect(res.body.total).to.equal(3)
380 const videos: Video[] = res.body.data
381 expect(videos).to.be.an('array')
382 expect(videos).to.have.lengthOf(3)
384 expect(videos[0].name).to.equal('video 1-3')
385 expect(videos[1].name).to.equal('video 2-3')
386 expect(videos[2].name).to.equal('video server 3 added after follow updated')
390 const res = await getVideosList(servers[0].url)
392 expect(res.body.total).to.equal(5)
393 for (const video of res.body.data) {
394 expect(video.name).to.not.contain('1-3')
395 expect(video.name).to.not.contain('2-3')
396 expect(video.name).to.not.contain('video server 3 added after follow updated')
401 after(async function () {
402 await cleanupTests(servers)