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