]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/user-subscriptions.ts
Add ability for instances to follow any actor
[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 createMultipleServers,
8 doubleFollow,
9 PeerTubeServer,
10 setAccessTokensToServers,
11 SubscriptionsCommand,
12 waitJobs
13 } from '@shared/extra-utils'
14
15 const expect = chai.expect
16
17 describe('Test users subscriptions', function () {
18 let servers: PeerTubeServer[] = []
19 const users: { accessToken: string }[] = []
20 let video3UUID: string
21
22 let command: SubscriptionsCommand
23
24 before(async function () {
25 this.timeout(120000)
26
27 servers = await createMultipleServers(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 server.users.create({ username: user.username, password: user.password })
39
40 const accessToken = await server.login.getAccessToken(user)
41 users.push({ accessToken })
42
43 const videoName1 = 'video 1-' + server.serverNumber
44 await server.videos.upload({ token: accessToken, attributes: { name: videoName1 } })
45
46 const videoName2 = 'video 2-' + server.serverNumber
47 await server.videos.upload({ token: accessToken, attributes: { name: videoName2 } })
48 }
49 }
50
51 await waitJobs(servers)
52
53 command = servers[0].subscriptions
54 })
55
56 it('Should display videos of server 2 on server 1', async function () {
57 const { total } = await servers[0].videos.list()
58
59 expect(total).to.equal(4)
60 })
61
62 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
63 this.timeout(60000)
64
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 })
67
68 await waitJobs(servers)
69
70 const attributes = { name: 'video server 3 added after follow' }
71 const { uuid } = await servers[2].videos.upload({ token: users[2].accessToken, attributes })
72 video3UUID = uuid
73
74 await waitJobs(servers)
75 })
76
77 it('Should not display videos of server 3 on server 1', async function () {
78 const { total, data } = await servers[0].videos.list()
79 expect(total).to.equal(4)
80
81 for (const video of data) {
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 {
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)
94 }
95
96 {
97 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
98 expect(body.total).to.equal(2)
99
100 const subscriptions = body.data
101 expect(subscriptions).to.be.an('array')
102 expect(subscriptions).to.have.lengthOf(2)
103
104 expect(subscriptions[0].name).to.equal('user3_channel')
105 expect(subscriptions[1].name).to.equal('root_channel')
106 }
107 })
108
109 it('Should get subscription', async function () {
110 {
111 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
112
113 expect(videoChannel.name).to.equal('user3_channel')
114 expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
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 {
121 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
122
123 expect(videoChannel.name).to.equal('root_channel')
124 expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
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:' + servers[2].port,
134 'root2_channel@localhost:' + servers[0].port,
135 'root_channel@localhost:' + servers[0].port,
136 'user3_channel@localhost:' + servers[0].port
137 ]
138
139 const body = await command.exist({ token: users[0].accessToken, uris })
140
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
145 })
146
147 it('Should search among subscriptions', async function () {
148 {
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)
152 }
153
154 {
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)
158 }
159 })
160
161 it('Should list subscription videos', async function () {
162 {
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)
167 }
168
169 {
170 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
171 expect(body.total).to.equal(3)
172
173 const videos = body.data
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 () {
184 this.timeout(60000)
185
186 const videoName = 'video server 1 added after follow'
187 await servers[0].videos.upload({ attributes: { name: videoName } })
188
189 await waitJobs(servers)
190
191 {
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)
196 }
197
198 {
199 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
200 expect(body.total).to.equal(4)
201
202 const videos = body.data
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 {
213 const { data, total } = await servers[0].videos.list()
214 expect(total).to.equal(5)
215
216 for (const video of data) {
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 () {
225 this.timeout(60000)
226
227 await servers[0].follows.follow({ hosts: [ servers[2].url ] })
228
229 await waitJobs(servers)
230
231 const { data, total } = await servers[0].videos.list()
232 expect(total).to.equal(8)
233
234 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
235 for (const name of names) {
236 const video = data.find(v => v.name.includes(name))
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 () {
242 this.timeout(60000)
243
244 await servers[0].follows.unfollow({ target: servers[2] })
245
246 await waitJobs(servers)
247
248 const { total, data } = await servers[0].videos.list()
249 expect(total).to.equal(5)
250
251 for (const video of data) {
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 {
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)
264 }
265
266 {
267 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
268 expect(body.total).to.equal(4)
269
270 const videos = body.data
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
281 it('Should update a video of server 3 and see the updated video on server 1', async function () {
282 this.timeout(30000)
283
284 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'video server 3 added after follow updated' } })
285
286 await waitJobs(servers)
287
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')
290 })
291
292 it('Should remove user of server 3 subscription', async function () {
293 this.timeout(30000)
294
295 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
296
297 await waitJobs(servers)
298 })
299
300 it('Should not display its videos anymore', async function () {
301 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
302 expect(body.total).to.equal(1)
303
304 const videos = body.data
305 expect(videos).to.be.an('array')
306 expect(videos).to.have.lengthOf(1)
307
308 expect(videos[0].name).to.equal('video server 1 added after follow')
309 })
310
311 it('Should remove the root subscription and not display the videos anymore', async function () {
312 this.timeout(30000)
313
314 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
315
316 await waitJobs(servers)
317
318 {
319 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
320 expect(body.total).to.equal(0)
321
322 const videos = body.data
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 () {
329 const { total, data } = await servers[0].videos.list()
330 expect(total).to.equal(5)
331
332 for (const video of data) {
333 expect(video.name).to.not.contain('1-3')
334 expect(video.name).to.not.contain('2-3')
335 expect(video.name).to.not.contain('video server 3 added after follow updated')
336 }
337 })
338
339 it('Should follow user of server 3 again', async function () {
340 this.timeout(60000)
341
342 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
343
344 await waitJobs(servers)
345
346 {
347 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
348 expect(body.total).to.equal(3)
349
350 const videos = body.data
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')
356 expect(videos[2].name).to.equal('video server 3 added after follow updated')
357 }
358
359 {
360 const { total, data } = await servers[0].videos.list()
361 expect(total).to.equal(5)
362
363 for (const video of data) {
364 expect(video.name).to.not.contain('1-3')
365 expect(video.name).to.not.contain('2-3')
366 expect(video.name).to.not.contain('video server 3 added after follow updated')
367 }
368 }
369 })
370
371 after(async function () {
372 await cleanupTests(servers)
373 })
374 })