allow limiting video-comments rss feeds to an account or video channel
[oweals/peertube.git] / server / tests / api / users / user-subscriptions.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   cleanupTests,
7   createUser,
8   doubleFollow,
9   flushAndRunMultipleServers,
10   follow,
11   getVideosList,
12   unfollow,
13   updateVideo,
14   userLogin
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'
20 import {
21   addUserSubscription,
22   areSubscriptionsExist,
23   getUserSubscription,
24   listUserSubscriptions,
25   listUserSubscriptionVideos,
26   removeUserSubscription
27 } from '../../../../shared/extra-utils/users/user-subscriptions'
28
29 const expect = chai.expect
30
31 describe('Test users subscriptions', function () {
32   let servers: ServerInfo[] = []
33   const users: { accessToken: string }[] = []
34   let video3UUID: string
35
36   before(async function () {
37     this.timeout(120000)
38
39     servers = await flushAndRunMultipleServers(3)
40
41     // Get the access tokens
42     await setAccessTokensToServers(servers)
43
44     // Server 1 and server 2 follow each other
45     await doubleFollow(servers[0], servers[1])
46
47     {
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 })
51
52         const accessToken = await userLogin(server, user)
53         users.push({ accessToken })
54
55         const videoName1 = 'video 1-' + server.serverNumber
56         await uploadVideo(server.url, accessToken, { name: videoName1 })
57
58         const videoName2 = 'video 2-' + server.serverNumber
59         await uploadVideo(server.url, accessToken, { name: videoName2 })
60       }
61     }
62
63     await waitJobs(servers)
64   })
65
66   it('Should display videos of server 2 on server 1', async function () {
67     const res = await getVideosList(servers[0].url)
68
69     expect(res.body.total).to.equal(4)
70   })
71
72   it('User of server 1 should follow user of server 3 and root of server 1', async function () {
73     this.timeout(60000)
74
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)
77
78     await waitJobs(servers)
79
80     const res = await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
81     video3UUID = res.body.video.uuid
82
83     await waitJobs(servers)
84   })
85
86   it('Should not display videos of server 3 on server 1', async function () {
87     const res = await getVideosList(servers[0].url)
88
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')
94     }
95   })
96
97   it('Should list subscriptions', async function () {
98     {
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)
103     }
104
105     {
106       const res = await listUserSubscriptions(servers[0].url, users[0].accessToken, 'createdAt')
107       expect(res.body.total).to.equal(2)
108
109       const subscriptions: VideoChannel[] = res.body.data
110       expect(subscriptions).to.be.an('array')
111       expect(subscriptions).to.have.lengthOf(2)
112
113       expect(subscriptions[0].name).to.equal('user3_channel')
114       expect(subscriptions[1].name).to.equal('root_channel')
115     }
116   })
117
118   it('Should get subscription', async function () {
119     {
120       const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
121       const videoChannel: VideoChannel = res.body
122
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)
128     }
129
130     {
131       const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
132       const videoChannel: VideoChannel = res.body
133
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)
139     }
140   })
141
142   it('Should return the existing subscriptions', async function () {
143     const uris = [
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
148     ]
149
150     const res = await areSubscriptionsExist(servers[0].url, users[0].accessToken, uris)
151     const body = res.body
152
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
157   })
158
159   it('Should list subscription videos', async function () {
160     {
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)
165     }
166
167     {
168       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
169       expect(res.body.total).to.equal(3)
170
171       const videos: Video[] = res.body.data
172       expect(videos).to.be.an('array')
173       expect(videos).to.have.lengthOf(3)
174
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')
178     }
179   })
180
181   it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
182     this.timeout(60000)
183
184     const videoName = 'video server 1 added after follow'
185     await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
186
187     await waitJobs(servers)
188
189     {
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)
194     }
195
196     {
197       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
198       expect(res.body.total).to.equal(4)
199
200       const videos: Video[] = res.body.data
201       expect(videos).to.be.an('array')
202       expect(videos).to.have.lengthOf(4)
203
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')
208     }
209
210     {
211       const res = await getVideosList(servers[0].url)
212
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')
218       }
219     }
220   })
221
222   it('Should have server 1 follow server 3 and display server 3 videos', async function () {
223     this.timeout(60000)
224
225     await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
226
227     await waitJobs(servers)
228
229     const res = await getVideosList(servers[0].url)
230
231     expect(res.body.total).to.equal(8)
232
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
237     }
238   })
239
240   it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
241     this.timeout(60000)
242
243     await unfollow(servers[0].url, servers[0].accessToken, servers[2])
244
245     await waitJobs(servers)
246
247     const res = await getVideosList(servers[0].url)
248
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')
254     }
255   })
256
257   it('Should still list subscription videos', async function () {
258     {
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)
263     }
264
265     {
266       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
267       expect(res.body.total).to.equal(4)
268
269       const videos: Video[] = res.body.data
270       expect(videos).to.be.an('array')
271       expect(videos).to.have.lengthOf(4)
272
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')
277     }
278   })
279
280   it('Should update a video of server 3 and see the updated video on server 1', async function () {
281     this.timeout(30000)
282
283     await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
284
285     await waitJobs(servers)
286
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')
290   })
291
292   it('Should remove user of server 3 subscription', async function () {
293     this.timeout(30000)
294
295     await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
296
297     await waitJobs(servers)
298   })
299
300   it('Should not display its videos anymore', async function () {
301     {
302       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
303       expect(res.body.total).to.equal(1)
304
305       const videos: Video[] = res.body.data
306       expect(videos).to.be.an('array')
307       expect(videos).to.have.lengthOf(1)
308
309       expect(videos[0].name).to.equal('video server 1 added after follow')
310     }
311   })
312
313   it('Should remove the root subscription and not display the videos anymore', async function () {
314     this.timeout(30000)
315
316     await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
317
318     await waitJobs(servers)
319
320     {
321       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
322       expect(res.body.total).to.equal(0)
323
324       const videos: Video[] = res.body.data
325       expect(videos).to.be.an('array')
326       expect(videos).to.have.lengthOf(0)
327     }
328   })
329
330   it('Should correctly display public videos on server 1', async function () {
331     const res = await getVideosList(servers[0].url)
332
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')
338     }
339   })
340
341   it('Should follow user of server 3 again', async function () {
342     this.timeout(60000)
343
344     await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
345
346     await waitJobs(servers)
347
348     {
349       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
350       expect(res.body.total).to.equal(3)
351
352       const videos: Video[] = res.body.data
353       expect(videos).to.be.an('array')
354       expect(videos).to.have.lengthOf(3)
355
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')
359     }
360
361     {
362       const res = await getVideosList(servers[0].url)
363
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')
369       }
370     }
371   })
372
373   after(async function () {
374     await cleanupTests(servers)
375   })
376 })