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