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