Tests for totalRepliesFromVideoAuthor
[oweals/peertube.git] / server / tests / api / users / user-subscriptions.ts
1 /* tslint:disable:no-unused-expression */
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 { killallServers, 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   listUserSubscriptions,
23   listUserSubscriptionVideos,
24   removeUserSubscription,
25   getUserSubscription, areSubscriptionsExist
26 } from '../../../../shared/extra-utils/users/user-subscriptions'
27
28 const expect = chai.expect
29
30 describe('Test users subscriptions', function () {
31   let servers: ServerInfo[] = []
32   const users: { accessToken: string }[] = []
33   let video3UUID: string
34
35   before(async function () {
36     this.timeout(120000)
37
38     servers = await flushAndRunMultipleServers(3)
39
40     // Get the access tokens
41     await setAccessTokensToServers(servers)
42
43     // Server 1 and server 2 follow each other
44     await doubleFollow(servers[0], servers[1])
45
46     {
47       for (const server of servers) {
48         const user = { username: 'user' + server.serverNumber, password: 'password' }
49         await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
50
51         const accessToken = await userLogin(server, user)
52         users.push({ accessToken })
53
54         const videoName1 = 'video 1-' + server.serverNumber
55         await uploadVideo(server.url, accessToken, { name: videoName1 })
56
57         const videoName2 = 'video 2-' + server.serverNumber
58         await uploadVideo(server.url, accessToken, { name: videoName2 })
59       }
60     }
61
62     await waitJobs(servers)
63   })
64
65   it('Should display videos of server 2 on server 1', async function () {
66     const res = await getVideosList(servers[0].url)
67
68     expect(res.body.total).to.equal(4)
69   })
70
71   it('User of server 1 should follow user of server 3 and root of server 1', async function () {
72     this.timeout(60000)
73
74     await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
75     await addUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
76
77     await waitJobs(servers)
78
79     const res = await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
80     video3UUID = res.body.video.uuid
81
82     await waitJobs(servers)
83   })
84
85   it('Should not display videos of server 3 on server 1', async function () {
86     const res = await getVideosList(servers[0].url)
87
88     expect(res.body.total).to.equal(4)
89     for (const video of res.body.data) {
90       expect(video.name).to.not.contain('1-3')
91       expect(video.name).to.not.contain('2-3')
92       expect(video.name).to.not.contain('video server 3 added after follow')
93     }
94   })
95
96   it('Should list subscriptions', async function () {
97     {
98       const res = await listUserSubscriptions(servers[0].url, servers[0].accessToken)
99       expect(res.body.total).to.equal(0)
100       expect(res.body.data).to.be.an('array')
101       expect(res.body.data).to.have.lengthOf(0)
102     }
103
104     {
105       const res = await listUserSubscriptions(servers[0].url, users[0].accessToken)
106       expect(res.body.total).to.equal(2)
107
108       const subscriptions: VideoChannel[] = res.body.data
109       expect(subscriptions).to.be.an('array')
110       expect(subscriptions).to.have.lengthOf(2)
111
112       expect(subscriptions[0].name).to.equal('user3_channel')
113       expect(subscriptions[1].name).to.equal('root_channel')
114     }
115   })
116
117   it('Should get subscription', async function () {
118     {
119       const res = await getUserSubscription(servers[ 0 ].url, users[ 0 ].accessToken, 'user3_channel@localhost:' + servers[2].port)
120       const videoChannel: VideoChannel = res.body
121
122       expect(videoChannel.name).to.equal('user3_channel')
123       expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
124       expect(videoChannel.displayName).to.equal('Main user3 channel')
125       expect(videoChannel.followingCount).to.equal(0)
126       expect(videoChannel.followersCount).to.equal(1)
127     }
128
129     {
130       const res = await getUserSubscription(servers[ 0 ].url, users[ 0 ].accessToken, 'root_channel@localhost:' + servers[0].port)
131       const videoChannel: VideoChannel = res.body
132
133       expect(videoChannel.name).to.equal('root_channel')
134       expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
135       expect(videoChannel.displayName).to.equal('Main root channel')
136       expect(videoChannel.followingCount).to.equal(0)
137       expect(videoChannel.followersCount).to.equal(1)
138     }
139   })
140
141   it('Should return the existing subscriptions', async function () {
142     const uris = [
143       'user3_channel@localhost:' + servers[2].port,
144       'root2_channel@localhost:' + servers[0].port,
145       'root_channel@localhost:' + servers[0].port,
146       'user3_channel@localhost:' + servers[0].port
147     ]
148
149     const res = await areSubscriptionsExist(servers[ 0 ].url, users[ 0 ].accessToken, uris)
150     const body = res.body
151
152     expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true
153     expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false
154     expect(body['root_channel@localhost:' + servers[0].port]).to.be.true
155     expect(body['user3_channel@localhost:' + servers[0].port]).to.be.false
156   })
157
158   it('Should list subscription videos', async function () {
159     {
160       const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
161       expect(res.body.total).to.equal(0)
162       expect(res.body.data).to.be.an('array')
163       expect(res.body.data).to.have.lengthOf(0)
164     }
165
166     {
167       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
168       expect(res.body.total).to.equal(3)
169
170       const videos: Video[] = res.body.data
171       expect(videos).to.be.an('array')
172       expect(videos).to.have.lengthOf(3)
173
174       expect(videos[0].name).to.equal('video 1-3')
175       expect(videos[1].name).to.equal('video 2-3')
176       expect(videos[2].name).to.equal('video server 3 added after follow')
177     }
178   })
179
180   it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
181     this.timeout(60000)
182
183     const videoName = 'video server 1 added after follow'
184     await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
185
186     await waitJobs(servers)
187
188     {
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)
193     }
194
195     {
196       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
197       expect(res.body.total).to.equal(4)
198
199       const videos: Video[] = res.body.data
200       expect(videos).to.be.an('array')
201       expect(videos).to.have.lengthOf(4)
202
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')
206       expect(videos[3].name).to.equal('video server 1 added after follow')
207     }
208
209     {
210       const res = await getVideosList(servers[0].url)
211
212       expect(res.body.total).to.equal(5)
213       for (const video of res.body.data) {
214         expect(video.name).to.not.contain('1-3')
215         expect(video.name).to.not.contain('2-3')
216         expect(video.name).to.not.contain('video server 3 added after follow')
217       }
218     }
219   })
220
221   it('Should have server 1 follow server 3 and display server 3 videos', async function () {
222     this.timeout(60000)
223
224     await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
225
226     await waitJobs(servers)
227
228     const res = await getVideosList(servers[0].url)
229
230     expect(res.body.total).to.equal(8)
231
232     const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
233     for (const name of names) {
234       const video = res.body.data.find(v => v.name.indexOf(name) === -1)
235       expect(video).to.not.be.undefined
236     }
237   })
238
239   it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
240     this.timeout(60000)
241
242     await unfollow(servers[0].url, servers[0].accessToken, servers[2])
243
244     await waitJobs(servers)
245
246     const res = await getVideosList(servers[0].url)
247
248     expect(res.body.total).to.equal(5)
249     for (const video of res.body.data) {
250       expect(video.name).to.not.contain('1-3')
251       expect(video.name).to.not.contain('2-3')
252       expect(video.name).to.not.contain('video server 3 added after follow')
253     }
254   })
255
256   it('Should still list subscription videos', async function () {
257     {
258       const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
259       expect(res.body.total).to.equal(0)
260       expect(res.body.data).to.be.an('array')
261       expect(res.body.data).to.have.lengthOf(0)
262     }
263
264     {
265       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
266       expect(res.body.total).to.equal(4)
267
268       const videos: Video[] = res.body.data
269       expect(videos).to.be.an('array')
270       expect(videos).to.have.lengthOf(4)
271
272       expect(videos[0].name).to.equal('video 1-3')
273       expect(videos[1].name).to.equal('video 2-3')
274       expect(videos[2].name).to.equal('video server 3 added after follow')
275       expect(videos[3].name).to.equal('video server 1 added after follow')
276     }
277   })
278
279   it('Should update a video of server 3 and see the updated video on server 1', async function () {
280     this.timeout(30000)
281
282     await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
283
284     await waitJobs(servers)
285
286     const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
287     const videos: Video[] = res.body.data
288     expect(videos[2].name).to.equal('video server 3 added after follow updated')
289   })
290
291   it('Should remove user of server 3 subscription', async function () {
292     this.timeout(30000)
293
294     await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
295
296     await waitJobs(servers)
297   })
298
299   it('Should not display its videos anymore', async function () {
300     {
301       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
302       expect(res.body.total).to.equal(1)
303
304       const videos: Video[] = res.body.data
305       expect(videos).to.be.an('array')
306       expect(videos).to.have.lengthOf(1)
307
308       expect(videos[0].name).to.equal('video server 1 added after follow')
309     }
310   })
311
312   it('Should remove the root subscription and not display the videos anymore', async function () {
313     this.timeout(30000)
314
315     await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
316
317     await waitJobs(servers)
318
319     {
320       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
321       expect(res.body.total).to.equal(0)
322
323       const videos: Video[] = res.body.data
324       expect(videos).to.be.an('array')
325       expect(videos).to.have.lengthOf(0)
326     }
327   })
328
329   it('Should correctly display public videos on server 1', async function () {
330     const res = await getVideosList(servers[0].url)
331
332     expect(res.body.total).to.equal(5)
333     for (const video of res.body.data) {
334       expect(video.name).to.not.contain('1-3')
335       expect(video.name).to.not.contain('2-3')
336       expect(video.name).to.not.contain('video server 3 added after follow updated')
337     }
338   })
339
340   it('Should follow user of server 3 again', async function () {
341     this.timeout(60000)
342
343     await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
344
345     await waitJobs(servers)
346
347     {
348       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
349       expect(res.body.total).to.equal(3)
350
351       const videos: Video[] = res.body.data
352       expect(videos).to.be.an('array')
353       expect(videos).to.have.lengthOf(3)
354
355       expect(videos[0].name).to.equal('video 1-3')
356       expect(videos[1].name).to.equal('video 2-3')
357       expect(videos[2].name).to.equal('video server 3 added after follow updated')
358     }
359
360     {
361       const res = await getVideosList(servers[0].url)
362
363       expect(res.body.total).to.equal(5)
364       for (const video of res.body.data) {
365         expect(video.name).to.not.contain('1-3')
366         expect(video.name).to.not.contain('2-3')
367         expect(video.name).to.not.contain('video server 3 added after follow updated')
368       }
369     }
370   })
371
372   after(async function () {
373     await cleanupTests(servers)
374   })
375 })