Merge branch 'release/v1.0.0' into develop
[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, areSubscriptionsExist
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 return the existing subscriptions', async function () {
132     const uris = [
133       'user3_channel@localhost:9003',
134       'root2_channel@localhost:9001',
135       'root_channel@localhost:9001',
136       'user3_channel@localhost:9001'
137     ]
138
139     const res = await areSubscriptionsExist(servers[ 0 ].url, users[ 0 ].accessToken, uris)
140     const body = res.body
141
142     expect(body['user3_channel@localhost:9003']).to.be.true
143     expect(body['root2_channel@localhost:9001']).to.be.false
144     expect(body['root_channel@localhost:9001']).to.be.true
145     expect(body['user3_channel@localhost:9001']).to.be.false
146   })
147
148   it('Should list subscription videos', async function () {
149     {
150       const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
151       expect(res.body.total).to.equal(0)
152       expect(res.body.data).to.be.an('array')
153       expect(res.body.data).to.have.lengthOf(0)
154     }
155
156     {
157       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
158       expect(res.body.total).to.equal(3)
159
160       const videos: Video[] = res.body.data
161       expect(videos).to.be.an('array')
162       expect(videos).to.have.lengthOf(3)
163
164       expect(videos[0].name).to.equal('video 1-3')
165       expect(videos[1].name).to.equal('video 2-3')
166       expect(videos[2].name).to.equal('video server 3 added after follow')
167     }
168   })
169
170   it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
171     this.timeout(60000)
172
173     const videoName = 'video server 1 added after follow'
174     await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
175
176     await waitJobs(servers)
177
178     {
179       const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
180       expect(res.body.total).to.equal(0)
181       expect(res.body.data).to.be.an('array')
182       expect(res.body.data).to.have.lengthOf(0)
183     }
184
185     {
186       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
187       expect(res.body.total).to.equal(4)
188
189       const videos: Video[] = res.body.data
190       expect(videos).to.be.an('array')
191       expect(videos).to.have.lengthOf(4)
192
193       expect(videos[0].name).to.equal('video 1-3')
194       expect(videos[1].name).to.equal('video 2-3')
195       expect(videos[2].name).to.equal('video server 3 added after follow')
196       expect(videos[3].name).to.equal('video server 1 added after follow')
197     }
198
199     {
200       const res = await getVideosList(servers[0].url)
201
202       expect(res.body.total).to.equal(5)
203       for (const video of res.body.data) {
204         expect(video.name).to.not.contain('1-3')
205         expect(video.name).to.not.contain('2-3')
206         expect(video.name).to.not.contain('video server 3 added after follow')
207       }
208     }
209   })
210
211   it('Should have server 1 follow server 3 and display server 3 videos', async function () {
212     this.timeout(60000)
213
214     await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
215
216     await waitJobs(servers)
217
218     const res = await getVideosList(servers[0].url)
219
220     expect(res.body.total).to.equal(8)
221
222     const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
223     for (const name of names) {
224       const video = res.body.data.find(v => v.name.indexOf(name) === -1)
225       expect(video).to.not.be.undefined
226     }
227   })
228
229   it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
230     this.timeout(60000)
231
232     await unfollow(servers[0].url, servers[0].accessToken, servers[2])
233
234     await waitJobs(servers)
235
236     const res = await getVideosList(servers[0].url)
237
238     expect(res.body.total).to.equal(5)
239     for (const video of res.body.data) {
240       expect(video.name).to.not.contain('1-3')
241       expect(video.name).to.not.contain('2-3')
242       expect(video.name).to.not.contain('video server 3 added after follow')
243     }
244   })
245
246   it('Should still list subscription videos', async function () {
247     {
248       const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
249       expect(res.body.total).to.equal(0)
250       expect(res.body.data).to.be.an('array')
251       expect(res.body.data).to.have.lengthOf(0)
252     }
253
254     {
255       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
256       expect(res.body.total).to.equal(4)
257
258       const videos: Video[] = res.body.data
259       expect(videos).to.be.an('array')
260       expect(videos).to.have.lengthOf(4)
261
262       expect(videos[0].name).to.equal('video 1-3')
263       expect(videos[1].name).to.equal('video 2-3')
264       expect(videos[2].name).to.equal('video server 3 added after follow')
265       expect(videos[3].name).to.equal('video server 1 added after follow')
266     }
267   })
268
269   it('Should update a video of server 3 and see the updated video on server 1', async function () {
270     this.timeout(30000)
271
272     await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
273
274     await waitJobs(servers)
275
276     const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
277     const videos: Video[] = res.body.data
278     expect(videos[2].name).to.equal('video server 3 added after follow updated')
279   })
280
281   it('Should remove user of server 3 subscription', async function () {
282     this.timeout(30000)
283
284     await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:9003')
285
286     await waitJobs(servers)
287   })
288
289   it('Should not display its videos anymore', async function () {
290     {
291       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
292       expect(res.body.total).to.equal(1)
293
294       const videos: Video[] = res.body.data
295       expect(videos).to.be.an('array')
296       expect(videos).to.have.lengthOf(1)
297
298       expect(videos[0].name).to.equal('video server 1 added after follow')
299     }
300   })
301
302   it('Should remove the root subscription and not display the videos anymore', async function () {
303     this.timeout(30000)
304
305     await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:9001')
306
307     await waitJobs(servers)
308
309     {
310       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
311       expect(res.body.total).to.equal(0)
312
313       const videos: Video[] = res.body.data
314       expect(videos).to.be.an('array')
315       expect(videos).to.have.lengthOf(0)
316     }
317   })
318
319   it('Should correctly display public videos on server 1', async function () {
320     const res = await getVideosList(servers[0].url)
321
322     expect(res.body.total).to.equal(5)
323     for (const video of res.body.data) {
324       expect(video.name).to.not.contain('1-3')
325       expect(video.name).to.not.contain('2-3')
326       expect(video.name).to.not.contain('video server 3 added after follow updated')
327     }
328   })
329
330   it('Should follow user of server 3 again', async function () {
331     this.timeout(60000)
332
333     await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:9003')
334
335     await waitJobs(servers)
336
337     {
338       const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
339       expect(res.body.total).to.equal(3)
340
341       const videos: Video[] = res.body.data
342       expect(videos).to.be.an('array')
343       expect(videos).to.have.lengthOf(3)
344
345       expect(videos[0].name).to.equal('video 1-3')
346       expect(videos[1].name).to.equal('video 2-3')
347       expect(videos[2].name).to.equal('video server 3 added after follow updated')
348     }
349
350     {
351       const res = await getVideosList(servers[0].url)
352
353       expect(res.body.total).to.equal(5)
354       for (const video of res.body.data) {
355         expect(video.name).to.not.contain('1-3')
356         expect(video.name).to.not.contain('2-3')
357         expect(video.name).to.not.contain('video server 3 added after follow updated')
358       }
359     }
360   })
361
362   after(async function () {
363     killallServers(servers)
364   })
365 })