]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users/user-subscriptions.ts
Cleanup tests imports
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / user-subscriptions.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
06a05d5f 2
86347717 3import { expect } from 'chai'
6def7d34 4import { VideoPrivacy } from '@shared/models'
9639bd17 5import {
7c3b7976 6 cleanupTests,
254d3579 7 createMultipleServers,
4c7e60bc 8 doubleFollow,
254d3579 9 PeerTubeServer,
c3d29f69 10 setAccessTokensToServers,
d0800f76 11 setDefaultAccountAvatar,
12 setDefaultChannelAvatar,
2c27e704 13 SubscriptionsCommand,
c3d29f69 14 waitJobs
bf54587a 15} from '@shared/server-commands'
06a05d5f 16
06a05d5f 17describe('Test users subscriptions', function () {
254d3579 18 let servers: PeerTubeServer[] = []
8a19bee1 19 const users: { accessToken: string }[] = []
f6eebcb3 20 let video3UUID: string
06a05d5f 21
2c27e704
C
22 let command: SubscriptionsCommand
23
06a05d5f 24 before(async function () {
87c0f718 25 this.timeout(240000)
06a05d5f 26
254d3579 27 servers = await createMultipleServers(3)
06a05d5f
C
28
29 // Get the access tokens
30 await setAccessTokensToServers(servers)
d0800f76 31 await setDefaultChannelAvatar(servers)
32 await setDefaultAccountAvatar(servers)
06a05d5f
C
33
34 // Server 1 and server 2 follow each other
35 await doubleFollow(servers[0], servers[1])
36
6def7d34
C
37 for (const server of servers) {
38 const user = { username: 'user' + server.serverNumber, password: 'password' }
39 await server.users.create({ username: user.username, password: user.password })
06a05d5f 40
6def7d34
C
41 const accessToken = await server.login.getAccessToken(user)
42 users.push({ accessToken })
06a05d5f 43
6def7d34
C
44 const videoName1 = 'video 1-' + server.serverNumber
45 await server.videos.upload({ token: accessToken, attributes: { name: videoName1 } })
06a05d5f 46
6def7d34
C
47 const videoName2 = 'video 2-' + server.serverNumber
48 await server.videos.upload({ token: accessToken, attributes: { name: videoName2 } })
06a05d5f
C
49 }
50
51 await waitJobs(servers)
2c27e704 52
89d241a7 53 command = servers[0].subscriptions
06a05d5f
C
54 })
55
56 it('Should display videos of server 2 on server 1', async function () {
89d241a7 57 const { total } = await servers[0].videos.list()
06a05d5f 58
d23dd9fb 59 expect(total).to.equal(4)
06a05d5f
C
60 })
61
62 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
8a19bee1 63 this.timeout(60000)
06a05d5f 64
2c27e704
C
65 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
66 await command.add({ token: users[0].accessToken, targetUri: 'root_channel@localhost:' + servers[0].port })
06a05d5f
C
67
68 await waitJobs(servers)
69
4c7e60bc
C
70 const attributes = { name: 'video server 3 added after follow' }
71 const { uuid } = await servers[2].videos.upload({ token: users[2].accessToken, attributes })
d23dd9fb 72 video3UUID = uuid
06a05d5f
C
73
74 await waitJobs(servers)
75 })
76
77 it('Should not display videos of server 3 on server 1', async function () {
89d241a7 78 const { total, data } = await servers[0].videos.list()
d23dd9fb 79 expect(total).to.equal(4)
06a05d5f 80
d23dd9fb 81 for (const video of data) {
06a05d5f
C
82 expect(video.name).to.not.contain('1-3')
83 expect(video.name).to.not.contain('2-3')
84 expect(video.name).to.not.contain('video server 3 added after follow')
85 }
86 })
87
88 it('Should list subscriptions', async function () {
89 {
2c27e704
C
90 const body = await command.list()
91 expect(body.total).to.equal(0)
92 expect(body.data).to.be.an('array')
93 expect(body.data).to.have.lengthOf(0)
06a05d5f
C
94 }
95
96 {
2c27e704
C
97 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
98 expect(body.total).to.equal(2)
06a05d5f 99
2c27e704 100 const subscriptions = body.data
06a05d5f
C
101 expect(subscriptions).to.be.an('array')
102 expect(subscriptions).to.have.lengthOf(2)
103
8a19bee1
C
104 expect(subscriptions[0].name).to.equal('user3_channel')
105 expect(subscriptions[1].name).to.equal('root_channel')
06a05d5f
C
106 }
107 })
108
99492dbc
C
109 it('Should get subscription', async function () {
110 {
2c27e704 111 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
99492dbc
C
112
113 expect(videoChannel.name).to.equal('user3_channel')
48f07b4a 114 expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
99492dbc
C
115 expect(videoChannel.displayName).to.equal('Main user3 channel')
116 expect(videoChannel.followingCount).to.equal(0)
117 expect(videoChannel.followersCount).to.equal(1)
118 }
119
120 {
2c27e704 121 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
99492dbc
C
122
123 expect(videoChannel.name).to.equal('root_channel')
48f07b4a 124 expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
99492dbc
C
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
f37dc0dd
C
131 it('Should return the existing subscriptions', async function () {
132 const uris = [
48f07b4a
C
133 'user3_channel@localhost:' + servers[2].port,
134 'root2_channel@localhost:' + servers[0].port,
135 'root_channel@localhost:' + servers[0].port,
136 'user3_channel@localhost:' + servers[0].port
f37dc0dd
C
137 ]
138
2c27e704 139 const body = await command.exist({ token: users[0].accessToken, uris })
f37dc0dd 140
48f07b4a
C
141 expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true
142 expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false
143 expect(body['root_channel@localhost:' + servers[0].port]).to.be.true
144 expect(body['user3_channel@localhost:' + servers[0].port]).to.be.false
f37dc0dd
C
145 })
146
7b390964
RK
147 it('Should search among subscriptions', async function () {
148 {
2c27e704
C
149 const body = await command.list({ token: users[0].accessToken, sort: '-createdAt', search: 'user3_channel' })
150 expect(body.total).to.equal(1)
151 expect(body.data).to.have.lengthOf(1)
7b390964
RK
152 }
153
154 {
2c27e704
C
155 const body = await command.list({ token: users[0].accessToken, sort: '-createdAt', search: 'toto' })
156 expect(body.total).to.equal(0)
157 expect(body.data).to.have.lengthOf(0)
7b390964
RK
158 }
159 })
160
06a05d5f
C
161 it('Should list subscription videos', async function () {
162 {
2c27e704
C
163 const body = await command.listVideos()
164 expect(body.total).to.equal(0)
165 expect(body.data).to.be.an('array')
166 expect(body.data).to.have.lengthOf(0)
06a05d5f
C
167 }
168
169 {
2c27e704
C
170 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
171 expect(body.total).to.equal(3)
06a05d5f 172
2c27e704 173 const videos = body.data
06a05d5f
C
174 expect(videos).to.be.an('array')
175 expect(videos).to.have.lengthOf(3)
176
177 expect(videos[0].name).to.equal('video 1-3')
178 expect(videos[1].name).to.equal('video 2-3')
179 expect(videos[2].name).to.equal('video server 3 added after follow')
180 }
181 })
182
183 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
8a19bee1 184 this.timeout(60000)
06a05d5f
C
185
186 const videoName = 'video server 1 added after follow'
89d241a7 187 await servers[0].videos.upload({ attributes: { name: videoName } })
06a05d5f
C
188
189 await waitJobs(servers)
190
191 {
2c27e704
C
192 const body = await command.listVideos()
193 expect(body.total).to.equal(0)
194 expect(body.data).to.be.an('array')
195 expect(body.data).to.have.lengthOf(0)
06a05d5f
C
196 }
197
198 {
2c27e704
C
199 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
200 expect(body.total).to.equal(4)
06a05d5f 201
2c27e704 202 const videos = body.data
06a05d5f
C
203 expect(videos).to.be.an('array')
204 expect(videos).to.have.lengthOf(4)
205
206 expect(videos[0].name).to.equal('video 1-3')
207 expect(videos[1].name).to.equal('video 2-3')
208 expect(videos[2].name).to.equal('video server 3 added after follow')
209 expect(videos[3].name).to.equal('video server 1 added after follow')
210 }
211
212 {
89d241a7 213 const { data, total } = await servers[0].videos.list()
d23dd9fb 214 expect(total).to.equal(5)
06a05d5f 215
d23dd9fb 216 for (const video of data) {
06a05d5f
C
217 expect(video.name).to.not.contain('1-3')
218 expect(video.name).to.not.contain('2-3')
219 expect(video.name).to.not.contain('video server 3 added after follow')
220 }
221 }
222 })
223
224 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
8a19bee1 225 this.timeout(60000)
06a05d5f 226
4d029ef8 227 await servers[0].follows.follow({ hosts: [ servers[2].url ] })
06a05d5f
C
228
229 await waitJobs(servers)
230
89d241a7 231 const { data, total } = await servers[0].videos.list()
d23dd9fb 232 expect(total).to.equal(8)
06a05d5f
C
233
234 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
235 for (const name of names) {
d23dd9fb 236 const video = data.find(v => v.name.includes(name))
06a05d5f
C
237 expect(video).to.not.be.undefined
238 }
239 })
240
241 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
8a19bee1 242 this.timeout(60000)
06a05d5f 243
89d241a7 244 await servers[0].follows.unfollow({ target: servers[2] })
06a05d5f
C
245
246 await waitJobs(servers)
247
89d241a7 248 const { total, data } = await servers[0].videos.list()
d23dd9fb 249 expect(total).to.equal(5)
06a05d5f 250
d23dd9fb 251 for (const video of data) {
06a05d5f
C
252 expect(video.name).to.not.contain('1-3')
253 expect(video.name).to.not.contain('2-3')
254 expect(video.name).to.not.contain('video server 3 added after follow')
255 }
256 })
257
258 it('Should still list subscription videos', async function () {
259 {
2c27e704
C
260 const body = await command.listVideos()
261 expect(body.total).to.equal(0)
262 expect(body.data).to.be.an('array')
263 expect(body.data).to.have.lengthOf(0)
06a05d5f
C
264 }
265
266 {
2c27e704
C
267 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
268 expect(body.total).to.equal(4)
06a05d5f 269
2c27e704 270 const videos = body.data
06a05d5f
C
271 expect(videos).to.be.an('array')
272 expect(videos).to.have.lengthOf(4)
273
274 expect(videos[0].name).to.equal('video 1-3')
275 expect(videos[1].name).to.equal('video 2-3')
276 expect(videos[2].name).to.equal('video server 3 added after follow')
277 expect(videos[3].name).to.equal('video server 1 added after follow')
278 }
279 })
280
f6eebcb3
C
281 it('Should update a video of server 3 and see the updated video on server 1', async function () {
282 this.timeout(30000)
283
89d241a7 284 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'video server 3 added after follow updated' } })
f6eebcb3
C
285
286 await waitJobs(servers)
287
2c27e704
C
288 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
289 expect(body.data[2].name).to.equal('video server 3 added after follow updated')
f6eebcb3
C
290 })
291
06a05d5f 292 it('Should remove user of server 3 subscription', async function () {
f6eebcb3
C
293 this.timeout(30000)
294
2c27e704 295 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
06a05d5f
C
296
297 await waitJobs(servers)
298 })
299
300 it('Should not display its videos anymore', async function () {
2c27e704
C
301 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
302 expect(body.total).to.equal(1)
06a05d5f 303
2c27e704
C
304 const videos = body.data
305 expect(videos).to.be.an('array')
306 expect(videos).to.have.lengthOf(1)
06a05d5f 307
2c27e704 308 expect(videos[0].name).to.equal('video server 1 added after follow')
06a05d5f
C
309 })
310
311 it('Should remove the root subscription and not display the videos anymore', async function () {
f6eebcb3
C
312 this.timeout(30000)
313
2c27e704 314 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
06a05d5f
C
315
316 await waitJobs(servers)
317
318 {
2c27e704
C
319 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
320 expect(body.total).to.equal(0)
06a05d5f 321
2c27e704 322 const videos = body.data
06a05d5f
C
323 expect(videos).to.be.an('array')
324 expect(videos).to.have.lengthOf(0)
325 }
326 })
327
328 it('Should correctly display public videos on server 1', async function () {
89d241a7 329 const { total, data } = await servers[0].videos.list()
d23dd9fb 330 expect(total).to.equal(5)
06a05d5f 331
d23dd9fb 332 for (const video of data) {
06a05d5f
C
333 expect(video.name).to.not.contain('1-3')
334 expect(video.name).to.not.contain('2-3')
f6eebcb3 335 expect(video.name).to.not.contain('video server 3 added after follow updated')
06a05d5f
C
336 }
337 })
338
339 it('Should follow user of server 3 again', async function () {
8a19bee1 340 this.timeout(60000)
06a05d5f 341
2c27e704 342 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
06a05d5f
C
343
344 await waitJobs(servers)
345
346 {
2c27e704
C
347 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
348 expect(body.total).to.equal(3)
06a05d5f 349
2c27e704 350 const videos = body.data
06a05d5f
C
351 expect(videos).to.be.an('array')
352 expect(videos).to.have.lengthOf(3)
353
354 expect(videos[0].name).to.equal('video 1-3')
355 expect(videos[1].name).to.equal('video 2-3')
f6eebcb3 356 expect(videos[2].name).to.equal('video server 3 added after follow updated')
06a05d5f
C
357 }
358
359 {
89d241a7 360 const { total, data } = await servers[0].videos.list()
d23dd9fb 361 expect(total).to.equal(5)
06a05d5f 362
d23dd9fb 363 for (const video of data) {
06a05d5f
C
364 expect(video.name).to.not.contain('1-3')
365 expect(video.name).to.not.contain('2-3')
f6eebcb3 366 expect(video.name).to.not.contain('video server 3 added after follow updated')
06a05d5f
C
367 }
368 }
369 })
370
4beda9e1
C
371 it('Should follow user channels of server 3 by root of server 3', async function () {
372 this.timeout(60000)
373
374 await servers[2].channels.create({ token: users[2].accessToken, attributes: { name: 'user3_channel2' } })
375
376 await servers[2].subscriptions.add({ token: servers[2].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
377 await servers[2].subscriptions.add({ token: servers[2].accessToken, targetUri: 'user3_channel2@localhost:' + servers[2].port })
378
379 await waitJobs(servers)
380 })
381
382 it('Should list user 3 followers', async function () {
383 {
384 const { total, data } = await servers[2].accounts.listFollowers({
385 token: users[2].accessToken,
386 accountName: 'user3',
387 start: 0,
388 count: 5,
389 sort: 'createdAt'
390 })
391
392 expect(total).to.equal(3)
906f46d0
C
393 expect(data).to.have.lengthOf(3)
394
4beda9e1
C
395 expect(data[0].following.host).to.equal(servers[2].host)
396 expect(data[0].following.name).to.equal('user3_channel')
397 expect(data[0].follower.host).to.equal(servers[0].host)
398 expect(data[0].follower.name).to.equal('user1')
399
400 expect(data[1].following.host).to.equal(servers[2].host)
401 expect(data[1].following.name).to.equal('user3_channel')
402 expect(data[1].follower.host).to.equal(servers[2].host)
403 expect(data[1].follower.name).to.equal('root')
404
405 expect(data[2].following.host).to.equal(servers[2].host)
406 expect(data[2].following.name).to.equal('user3_channel2')
407 expect(data[2].follower.host).to.equal(servers[2].host)
408 expect(data[2].follower.name).to.equal('root')
409 }
410
411 {
412 const { total, data } = await servers[2].accounts.listFollowers({
413 token: users[2].accessToken,
414 accountName: 'user3',
415 start: 0,
416 count: 1,
417 sort: '-createdAt'
418 })
419
420 expect(total).to.equal(3)
906f46d0
C
421 expect(data).to.have.lengthOf(1)
422
4beda9e1
C
423 expect(data[0].following.host).to.equal(servers[2].host)
424 expect(data[0].following.name).to.equal('user3_channel2')
425 expect(data[0].follower.host).to.equal(servers[2].host)
426 expect(data[0].follower.name).to.equal('root')
427 }
428
429 {
430 const { total, data } = await servers[2].accounts.listFollowers({
431 token: users[2].accessToken,
432 accountName: 'user3',
433 start: 1,
434 count: 1,
435 sort: '-createdAt'
436 })
437
438 expect(total).to.equal(3)
906f46d0
C
439 expect(data).to.have.lengthOf(1)
440
4beda9e1
C
441 expect(data[0].following.host).to.equal(servers[2].host)
442 expect(data[0].following.name).to.equal('user3_channel')
443 expect(data[0].follower.host).to.equal(servers[2].host)
444 expect(data[0].follower.name).to.equal('root')
445 }
446
447 {
448 const { total, data } = await servers[2].accounts.listFollowers({
449 token: users[2].accessToken,
450 accountName: 'user3',
451 search: 'user1',
452 sort: '-createdAt'
453 })
454
455 expect(total).to.equal(1)
906f46d0
C
456 expect(data).to.have.lengthOf(1)
457
4beda9e1
C
458 expect(data[0].following.host).to.equal(servers[2].host)
459 expect(data[0].following.name).to.equal('user3_channel')
460 expect(data[0].follower.host).to.equal(servers[0].host)
461 expect(data[0].follower.name).to.equal('user1')
462 }
463 })
464
465 it('Should list user3_channel followers', async function () {
466 {
467 const { total, data } = await servers[2].channels.listFollowers({
468 token: users[2].accessToken,
469 channelName: 'user3_channel',
470 start: 0,
471 count: 5,
472 sort: 'createdAt'
473 })
474
475 expect(total).to.equal(2)
906f46d0
C
476 expect(data).to.have.lengthOf(2)
477
4beda9e1
C
478 expect(data[0].following.host).to.equal(servers[2].host)
479 expect(data[0].following.name).to.equal('user3_channel')
480 expect(data[0].follower.host).to.equal(servers[0].host)
481 expect(data[0].follower.name).to.equal('user1')
482
483 expect(data[1].following.host).to.equal(servers[2].host)
484 expect(data[1].following.name).to.equal('user3_channel')
485 expect(data[1].follower.host).to.equal(servers[2].host)
486 expect(data[1].follower.name).to.equal('root')
487 }
488
489 {
490 const { total, data } = await servers[2].channels.listFollowers({
491 token: users[2].accessToken,
492 channelName: 'user3_channel',
493 start: 0,
494 count: 1,
495 sort: '-createdAt'
496 })
497
498 expect(total).to.equal(2)
906f46d0
C
499 expect(data).to.have.lengthOf(1)
500
4beda9e1
C
501 expect(data[0].following.host).to.equal(servers[2].host)
502 expect(data[0].following.name).to.equal('user3_channel')
503 expect(data[0].follower.host).to.equal(servers[2].host)
504 expect(data[0].follower.name).to.equal('root')
505 }
506
507 {
508 const { total, data } = await servers[2].channels.listFollowers({
509 token: users[2].accessToken,
510 channelName: 'user3_channel',
511 start: 1,
512 count: 1,
513 sort: '-createdAt'
514 })
515
516 expect(total).to.equal(2)
906f46d0
C
517 expect(data).to.have.lengthOf(1)
518
4beda9e1
C
519 expect(data[0].following.host).to.equal(servers[2].host)
520 expect(data[0].following.name).to.equal('user3_channel')
521 expect(data[0].follower.host).to.equal(servers[0].host)
906f46d0 522 expect(data[0].follower.name).to.equal('user1')
4beda9e1
C
523 }
524
525 {
526 const { total, data } = await servers[2].channels.listFollowers({
527 token: users[2].accessToken,
528 channelName: 'user3_channel',
529 search: 'user1',
530 sort: '-createdAt'
531 })
532
533 expect(total).to.equal(1)
906f46d0
C
534 expect(data).to.have.lengthOf(1)
535
4beda9e1
C
536 expect(data[0].following.host).to.equal(servers[2].host)
537 expect(data[0].following.name).to.equal('user3_channel')
538 expect(data[0].follower.host).to.equal(servers[0].host)
539 expect(data[0].follower.name).to.equal('user1')
540 }
541 })
542
6def7d34
C
543 it('Should update video as internal and not see from remote server', async function () {
544 this.timeout(30000)
545
546 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'internal', privacy: VideoPrivacy.INTERNAL } })
547 await waitJobs(servers)
548
549 {
550 const { data } = await command.listVideos({ token: users[0].accessToken })
551 expect(data.find(v => v.name === 'internal')).to.not.exist
552 }
553 })
554
555 it('Should see internal from local user', async function () {
556 const { data } = await servers[2].subscriptions.listVideos({ token: servers[2].accessToken })
557 expect(data.find(v => v.name === 'internal')).to.exist
558 })
559
560 it('Should update video as private and not see from anyone server', async function () {
561 this.timeout(30000)
562
563 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'private', privacy: VideoPrivacy.PRIVATE } })
564 await waitJobs(servers)
565
566 {
567 const { data } = await command.listVideos({ token: users[0].accessToken })
568 expect(data.find(v => v.name === 'private')).to.not.exist
569 }
570
571 {
572 const { data } = await servers[2].subscriptions.listVideos({ token: servers[2].accessToken })
573 expect(data.find(v => v.name === 'private')).to.not.exist
574 }
575 })
576
7c3b7976
C
577 after(async function () {
578 await cleanupTests(servers)
06a05d5f
C
579 })
580})