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