]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-channels.ts
e50582218c305880a189f831a94bc0c56756a330
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-channels.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 deleteVideoChannelImage,
9 doubleFollow,
10 flushAndRunMultipleServers,
11 getVideo,
12 getVideoChannel,
13 getVideoChannelVideos,
14 testImage,
15 updateVideo,
16 updateVideoChannelImage,
17 uploadVideo,
18 userLogin,
19 wait
20 } from '../../../../shared/extra-utils'
21 import {
22 addVideoChannel,
23 deleteVideoChannel,
24 getAccountVideoChannelsList,
25 getMyUserInformation,
26 getVideoChannelsList,
27 ServerInfo,
28 setAccessTokensToServers,
29 updateVideoChannel,
30 viewVideo
31 } from '../../../../shared/extra-utils/index'
32 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
33 import { User, Video, VideoChannel, VideoDetails } from '../../../../shared/index'
34
35 const expect = chai.expect
36
37 async function findChannel (server: ServerInfo, channelId: number) {
38 const res = await getVideoChannelsList(server.url, 0, 5, '-name')
39 const videoChannel = res.body.data.find(c => c.id === channelId)
40
41 return videoChannel as VideoChannel
42 }
43
44 describe('Test video channels', function () {
45 let servers: ServerInfo[]
46 let userInfo: User
47 let firstVideoChannelId: number
48 let secondVideoChannelId: number
49 let videoUUID: string
50
51 before(async function () {
52 this.timeout(60000)
53
54 servers = await flushAndRunMultipleServers(2)
55
56 await setAccessTokensToServers(servers)
57 await doubleFollow(servers[0], servers[1])
58
59 {
60 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
61 const user: User = res.body
62
63 firstVideoChannelId = user.videoChannels[0].id
64 }
65
66 await waitJobs(servers)
67 })
68
69 it('Should have one video channel (created with root)', async () => {
70 const res = await getVideoChannelsList(servers[0].url, 0, 2)
71
72 expect(res.body.total).to.equal(1)
73 expect(res.body.data).to.be.an('array')
74 expect(res.body.data).to.have.lengthOf(1)
75 })
76
77 it('Should create another video channel', async function () {
78 this.timeout(10000)
79
80 {
81 const videoChannel = {
82 name: 'second_video_channel',
83 displayName: 'second video channel',
84 description: 'super video channel description',
85 support: 'super video channel support text'
86 }
87 const res = await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
88 secondVideoChannelId = res.body.videoChannel.id
89 }
90
91 // The channel is 1 is propagated to servers 2
92 {
93 const videoAttributesArg = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
94 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributesArg)
95 videoUUID = res.body.video.uuid
96 }
97
98 await waitJobs(servers)
99 })
100
101 it('Should have two video channels when getting my information', async () => {
102 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
103 userInfo = res.body
104
105 expect(userInfo.videoChannels).to.be.an('array')
106 expect(userInfo.videoChannels).to.have.lengthOf(2)
107
108 const videoChannels = userInfo.videoChannels
109 expect(videoChannels[0].name).to.equal('root_channel')
110 expect(videoChannels[0].displayName).to.equal('Main root channel')
111
112 expect(videoChannels[1].name).to.equal('second_video_channel')
113 expect(videoChannels[1].displayName).to.equal('second video channel')
114 expect(videoChannels[1].description).to.equal('super video channel description')
115 expect(videoChannels[1].support).to.equal('super video channel support text')
116 })
117
118 it('Should have two video channels when getting account channels on server 1', async function () {
119 const res = await getAccountVideoChannelsList({
120 url: servers[0].url,
121 accountName: userInfo.account.name + '@' + userInfo.account.host
122 })
123
124 expect(res.body.total).to.equal(2)
125 expect(res.body.data).to.be.an('array')
126 expect(res.body.data).to.have.lengthOf(2)
127
128 const videoChannels = res.body.data
129 expect(videoChannels[0].name).to.equal('root_channel')
130 expect(videoChannels[0].displayName).to.equal('Main root channel')
131
132 expect(videoChannels[1].name).to.equal('second_video_channel')
133 expect(videoChannels[1].displayName).to.equal('second video channel')
134 expect(videoChannels[1].description).to.equal('super video channel description')
135 expect(videoChannels[1].support).to.equal('super video channel support text')
136 })
137
138 it('Should paginate and sort account channels', async function () {
139 {
140 const res = await getAccountVideoChannelsList({
141 url: servers[0].url,
142 accountName: userInfo.account.name + '@' + userInfo.account.host,
143 start: 0,
144 count: 1,
145 sort: 'createdAt'
146 })
147
148 expect(res.body.total).to.equal(2)
149 expect(res.body.data).to.have.lengthOf(1)
150
151 const videoChannel: VideoChannel = res.body.data[0]
152 expect(videoChannel.name).to.equal('root_channel')
153 }
154
155 {
156 const res = await getAccountVideoChannelsList({
157 url: servers[0].url,
158 accountName: userInfo.account.name + '@' + userInfo.account.host,
159 start: 0,
160 count: 1,
161 sort: '-createdAt'
162 })
163
164 expect(res.body.total).to.equal(2)
165 expect(res.body.data).to.have.lengthOf(1)
166
167 const videoChannel: VideoChannel = res.body.data[0]
168 expect(videoChannel.name).to.equal('second_video_channel')
169 }
170
171 {
172 const res = await getAccountVideoChannelsList({
173 url: servers[0].url,
174 accountName: userInfo.account.name + '@' + userInfo.account.host,
175 start: 1,
176 count: 1,
177 sort: '-createdAt'
178 })
179
180 expect(res.body.total).to.equal(2)
181 expect(res.body.data).to.have.lengthOf(1)
182
183 const videoChannel: VideoChannel = res.body.data[0]
184 expect(videoChannel.name).to.equal('root_channel')
185 }
186 })
187
188 it('Should have one video channel when getting account channels on server 2', async function () {
189 const res = await getAccountVideoChannelsList({
190 url: servers[1].url,
191 accountName: userInfo.account.name + '@' + userInfo.account.host
192 })
193
194 expect(res.body.total).to.equal(1)
195 expect(res.body.data).to.be.an('array')
196 expect(res.body.data).to.have.lengthOf(1)
197
198 const videoChannels = res.body.data
199 expect(videoChannels[0].name).to.equal('second_video_channel')
200 expect(videoChannels[0].displayName).to.equal('second video channel')
201 expect(videoChannels[0].description).to.equal('super video channel description')
202 expect(videoChannels[0].support).to.equal('super video channel support text')
203 })
204
205 it('Should list video channels', async function () {
206 const res = await getVideoChannelsList(servers[0].url, 1, 1, '-name')
207
208 expect(res.body.total).to.equal(2)
209 expect(res.body.data).to.be.an('array')
210 expect(res.body.data).to.have.lengthOf(1)
211 expect(res.body.data[0].name).to.equal('root_channel')
212 expect(res.body.data[0].displayName).to.equal('Main root channel')
213 })
214
215 it('Should update video channel', async function () {
216 this.timeout(15000)
217
218 const videoChannelAttributes = {
219 displayName: 'video channel updated',
220 description: 'video channel description updated',
221 support: 'support updated'
222 }
223
224 await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes)
225
226 await waitJobs(servers)
227 })
228
229 it('Should have video channel updated', async function () {
230 for (const server of servers) {
231 const res = await getVideoChannelsList(server.url, 0, 1, '-name')
232
233 expect(res.body.total).to.equal(2)
234 expect(res.body.data).to.be.an('array')
235 expect(res.body.data).to.have.lengthOf(1)
236 expect(res.body.data[0].name).to.equal('second_video_channel')
237 expect(res.body.data[0].displayName).to.equal('video channel updated')
238 expect(res.body.data[0].description).to.equal('video channel description updated')
239 expect(res.body.data[0].support).to.equal('support updated')
240 }
241 })
242
243 it('Should not have updated the video support field', async function () {
244 for (const server of servers) {
245 const res = await getVideo(server.url, videoUUID)
246 const video: VideoDetails = res.body
247
248 expect(video.support).to.equal('video support field')
249 }
250 })
251
252 it('Should update the channel support field and update videos too', async function () {
253 this.timeout(35000)
254
255 const videoChannelAttributes = {
256 support: 'video channel support text updated',
257 bulkVideosSupportUpdate: true
258 }
259
260 await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes)
261
262 await waitJobs(servers)
263
264 for (const server of servers) {
265 const res = await getVideo(server.url, videoUUID)
266 const video: VideoDetails = res.body
267
268 expect(video.support).to.equal(videoChannelAttributes.support)
269 }
270 })
271
272 it('Should update video channel avatar', async function () {
273 this.timeout(15000)
274
275 const fixture = 'avatar.png'
276
277 await updateVideoChannelImage({
278 url: servers[0].url,
279 accessToken: servers[0].accessToken,
280 videoChannelName: 'second_video_channel',
281 fixture,
282 type: 'avatar'
283 })
284
285 await waitJobs(servers)
286
287 for (const server of servers) {
288 const videoChannel = await findChannel(server, secondVideoChannelId)
289
290 await testImage(server.url, 'avatar-resized', videoChannel.avatar.path, '.png')
291 }
292 })
293
294 it('Should update video channel banner', async function () {
295 this.timeout(15000)
296
297 const fixture = 'banner.jpg'
298
299 await updateVideoChannelImage({
300 url: servers[0].url,
301 accessToken: servers[0].accessToken,
302 videoChannelName: 'second_video_channel',
303 fixture,
304 type: 'banner'
305 })
306
307 await waitJobs(servers)
308
309 for (const server of servers) {
310 const res = await getVideoChannel(server.url, 'second_video_channel@' + servers[0].host)
311 const videoChannel = res.body
312
313 await testImage(server.url, 'banner-resized', videoChannel.banner.path)
314 }
315 })
316
317 it('Should delete the video channel avatar', async function () {
318 this.timeout(15000)
319
320 await deleteVideoChannelImage({
321 url: servers[0].url,
322 accessToken: servers[0].accessToken,
323 videoChannelName: 'second_video_channel',
324 type: 'avatar'
325 })
326
327 await waitJobs(servers)
328
329 for (const server of servers) {
330 const videoChannel = await findChannel(server, secondVideoChannelId)
331
332 expect(videoChannel.avatar).to.be.null
333 }
334 })
335
336 it('Should delete the video channel banner', async function () {
337 this.timeout(15000)
338
339 await deleteVideoChannelImage({
340 url: servers[0].url,
341 accessToken: servers[0].accessToken,
342 videoChannelName: 'second_video_channel',
343 type: 'banner'
344 })
345
346 await waitJobs(servers)
347
348 for (const server of servers) {
349 const videoChannel = await findChannel(server, secondVideoChannelId)
350
351 expect(videoChannel.banner).to.be.null
352 }
353 })
354
355 it('Should list the second video channel videos', async function () {
356 this.timeout(10000)
357
358 for (const server of servers) {
359 const channelURI = 'second_video_channel@localhost:' + servers[0].port
360 const res1 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
361 expect(res1.body.total).to.equal(1)
362 expect(res1.body.data).to.be.an('array')
363 expect(res1.body.data).to.have.lengthOf(1)
364 expect(res1.body.data[0].name).to.equal('my video name')
365 }
366 })
367
368 it('Should change the video channel of a video', async function () {
369 this.timeout(10000)
370
371 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { channelId: firstVideoChannelId })
372
373 await waitJobs(servers)
374 })
375
376 it('Should list the first video channel videos', async function () {
377 this.timeout(10000)
378
379 for (const server of servers) {
380 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
381 const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondChannelURI, 0, 5)
382 expect(res1.body.total).to.equal(0)
383
384 const channelURI = 'root_channel@localhost:' + servers[0].port
385 const res2 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
386 expect(res2.body.total).to.equal(1)
387
388 const videos: Video[] = res2.body.data
389 expect(videos).to.be.an('array')
390 expect(videos).to.have.lengthOf(1)
391 expect(videos[0].name).to.equal('my video name')
392 }
393 })
394
395 it('Should delete video channel', async function () {
396 await deleteVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel')
397 })
398
399 it('Should have video channel deleted', async function () {
400 const res = await getVideoChannelsList(servers[0].url, 0, 10)
401
402 expect(res.body.total).to.equal(1)
403 expect(res.body.data).to.be.an('array')
404 expect(res.body.data).to.have.lengthOf(1)
405 expect(res.body.data[0].displayName).to.equal('Main root channel')
406 })
407
408 it('Should create the main channel with an uuid if there is a conflict', async function () {
409 {
410 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
411 await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
412 }
413
414 {
415 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'toto', password: 'password' })
416 const accessToken = await userLogin(servers[0], { username: 'toto', password: 'password' })
417
418 const res = await getMyUserInformation(servers[0].url, accessToken)
419 const videoChannel = res.body.videoChannels[0]
420 expect(videoChannel.name).to.match(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/)
421 }
422 })
423
424 it('Should report correct channel views per days', async function () {
425 this.timeout(10000)
426
427 {
428 const res = await getAccountVideoChannelsList({
429 url: servers[0].url,
430 accountName: userInfo.account.name + '@' + userInfo.account.host,
431 withStats: true
432 })
433
434 const channels: VideoChannel[] = res.body.data
435
436 for (const channel of channels) {
437 expect(channel).to.haveOwnProperty('viewsPerDay')
438 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
439
440 for (const v of channel.viewsPerDay) {
441 expect(v.date).to.be.an('string')
442 expect(v.views).to.equal(0)
443 }
444 }
445 }
446
447 {
448 // video has been posted on channel firstVideoChannelId since last update
449 await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.1,127.0.0.1')
450 await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.2,127.0.0.1')
451
452 // Wait the repeatable job
453 await wait(8000)
454
455 const res = await getAccountVideoChannelsList({
456 url: servers[0].url,
457 accountName: userInfo.account.name + '@' + userInfo.account.host,
458 withStats: true
459 })
460 const channelWithView = res.body.data.find((channel: VideoChannel) => channel.id === firstVideoChannelId)
461 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
462 }
463 })
464
465 it('Should report correct videos count', async function () {
466 const res = await getAccountVideoChannelsList({
467 url: servers[0].url,
468 accountName: userInfo.account.name + '@' + userInfo.account.host,
469 withStats: true
470 })
471 const channels: VideoChannel[] = res.body.data
472
473 const totoChannel = channels.find(c => c.name === 'toto_channel')
474 const rootChannel = channels.find(c => c.name === 'root_channel')
475
476 expect(rootChannel.videosCount).to.equal(1)
477 expect(totoChannel.videosCount).to.equal(0)
478 })
479
480 it('Should search among account video channels', async function () {
481 {
482 const res = await getAccountVideoChannelsList({
483 url: servers[0].url,
484 accountName: userInfo.account.name + '@' + userInfo.account.host,
485 search: 'root'
486 })
487 expect(res.body.total).to.equal(1)
488
489 const channels = res.body.data
490 expect(channels).to.have.lengthOf(1)
491 }
492
493 {
494 const res = await getAccountVideoChannelsList({
495 url: servers[0].url,
496 accountName: userInfo.account.name + '@' + userInfo.account.host,
497 search: 'does not exist'
498 })
499 expect(res.body.total).to.equal(0)
500
501 const channels = res.body.data
502 expect(channels).to.have.lengthOf(0)
503 }
504 })
505
506 after(async function () {
507 await cleanupTests(servers)
508 })
509 })