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