]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/user-subscriptions.ts
Use an object to represent a server
[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 createMultipleServers,
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 { uuid } = await servers[2].videos.upload({ attributes: { name: 'video server 3 added after follow' } })
71 video3UUID = uuid
72
73 await waitJobs(servers)
74 })
75
76 it('Should not display videos of server 3 on server 1', async function () {
77 const { total, data } = await servers[0].videos.list()
78 expect(total).to.equal(4)
79
80 for (const video of data) {
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 {
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)
93 }
94
95 {
96 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
97 expect(body.total).to.equal(2)
98
99 const subscriptions = body.data
100 expect(subscriptions).to.be.an('array')
101 expect(subscriptions).to.have.lengthOf(2)
102
103 expect(subscriptions[0].name).to.equal('user3_channel')
104 expect(subscriptions[1].name).to.equal('root_channel')
105 }
106 })
107
108 it('Should get subscription', async function () {
109 {
110 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
111
112 expect(videoChannel.name).to.equal('user3_channel')
113 expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
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 {
120 const videoChannel = await command.get({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
121
122 expect(videoChannel.name).to.equal('root_channel')
123 expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
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
130 it('Should return the existing subscriptions', async function () {
131 const uris = [
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
136 ]
137
138 const body = await command.exist({ token: users[0].accessToken, uris })
139
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
144 })
145
146 it('Should search among subscriptions', async function () {
147 {
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)
151 }
152
153 {
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)
157 }
158 })
159
160 it('Should list subscription videos', async function () {
161 {
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)
166 }
167
168 {
169 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
170 expect(body.total).to.equal(3)
171
172 const videos = body.data
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 () {
183 this.timeout(60000)
184
185 const videoName = 'video server 1 added after follow'
186 await servers[0].videos.upload({ attributes: { name: videoName } })
187
188 await waitJobs(servers)
189
190 {
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)
195 }
196
197 {
198 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
199 expect(body.total).to.equal(4)
200
201 const videos = body.data
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 {
212 const { data, total } = await servers[0].videos.list()
213 expect(total).to.equal(5)
214
215 for (const video of data) {
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 () {
224 this.timeout(60000)
225
226 await servers[0].follows.follow({ targets: [ servers[2].url ] })
227
228 await waitJobs(servers)
229
230 const { data, total } = await servers[0].videos.list()
231 expect(total).to.equal(8)
232
233 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
234 for (const name of names) {
235 const video = data.find(v => v.name.includes(name))
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 () {
241 this.timeout(60000)
242
243 await servers[0].follows.unfollow({ target: servers[2] })
244
245 await waitJobs(servers)
246
247 const { total, data } = await servers[0].videos.list()
248 expect(total).to.equal(5)
249
250 for (const video of data) {
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 {
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)
263 }
264
265 {
266 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
267 expect(body.total).to.equal(4)
268
269 const videos = body.data
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
280 it('Should update a video of server 3 and see the updated video on server 1', async function () {
281 this.timeout(30000)
282
283 await servers[2].videos.update({ id: video3UUID, attributes: { name: 'video server 3 added after follow updated' } })
284
285 await waitJobs(servers)
286
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')
289 })
290
291 it('Should remove user of server 3 subscription', async function () {
292 this.timeout(30000)
293
294 await command.remove({ token: users[0].accessToken, uri: 'user3_channel@localhost:' + servers[2].port })
295
296 await waitJobs(servers)
297 })
298
299 it('Should not display its videos anymore', async function () {
300 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
301 expect(body.total).to.equal(1)
302
303 const videos = body.data
304 expect(videos).to.be.an('array')
305 expect(videos).to.have.lengthOf(1)
306
307 expect(videos[0].name).to.equal('video server 1 added after follow')
308 })
309
310 it('Should remove the root subscription and not display the videos anymore', async function () {
311 this.timeout(30000)
312
313 await command.remove({ token: users[0].accessToken, uri: 'root_channel@localhost:' + servers[0].port })
314
315 await waitJobs(servers)
316
317 {
318 const body = await command.list({ token: users[0].accessToken, sort: 'createdAt' })
319 expect(body.total).to.equal(0)
320
321 const videos = body.data
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 () {
328 const { total, data } = await servers[0].videos.list()
329 expect(total).to.equal(5)
330
331 for (const video of data) {
332 expect(video.name).to.not.contain('1-3')
333 expect(video.name).to.not.contain('2-3')
334 expect(video.name).to.not.contain('video server 3 added after follow updated')
335 }
336 })
337
338 it('Should follow user of server 3 again', async function () {
339 this.timeout(60000)
340
341 await command.add({ token: users[0].accessToken, targetUri: 'user3_channel@localhost:' + servers[2].port })
342
343 await waitJobs(servers)
344
345 {
346 const body = await command.listVideos({ token: users[0].accessToken, sort: 'createdAt' })
347 expect(body.total).to.equal(3)
348
349 const videos = body.data
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')
355 expect(videos[2].name).to.equal('video server 3 added after follow updated')
356 }
357
358 {
359 const { total, data } = await servers[0].videos.list()
360 expect(total).to.equal(5)
361
362 for (const video of data) {
363 expect(video.name).to.not.contain('1-3')
364 expect(video.name).to.not.contain('2-3')
365 expect(video.name).to.not.contain('video server 3 added after follow updated')
366 }
367 }
368 })
369
370 after(async function () {
371 await cleanupTests(servers)
372 })
373 })