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