]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-channels.ts
Add ability to update the banner
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-channels.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5f04dd2f 2
2422c46b 3import 'mocha'
8eb07b01 4import * as chai from 'chai'
4bbfc6c6 5import {
7c3b7976 6 cleanupTests,
8a19bee1 7 createUser,
213e30ef 8 deleteVideoChannelImage,
4bbfc6c6 9 doubleFollow,
a1587156
C
10 flushAndRunMultipleServers,
11 getVideo,
cdeddff1 12 getVideoChannel,
d175a6f7
C
13 getVideoChannelVideos,
14 testImage,
4bbfc6c6 15 updateVideo,
213e30ef 16 updateVideoChannelImage,
d175a6f7 17 uploadVideo,
714bfcc5
RK
18 userLogin,
19 wait
94565d52 20} from '../../../../shared/extra-utils'
5f04dd2f 21import {
2422c46b
C
22 addVideoChannel,
23 deleteVideoChannel,
2422c46b 24 getAccountVideoChannelsList,
5f04dd2f
C
25 getMyUserInformation,
26 getVideoChannelsList,
2422c46b
C
27 ServerInfo,
28 setAccessTokensToServers,
714bfcc5
RK
29 updateVideoChannel,
30 viewVideo
94565d52
C
31} from '../../../../shared/extra-utils/index'
32import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
8eb07b01 33import { User, Video, VideoChannel, VideoDetails } from '../../../../shared/index'
5f04dd2f 34
2422c46b
C
35const expect = chai.expect
36
213e30ef
C
37async 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
2422c46b
C
44describe('Test video channels', function () {
45 let servers: ServerInfo[]
5f04dd2f 46 let userInfo: User
0f320037 47 let firstVideoChannelId: number
0f320037 48 let secondVideoChannelId: number
0f320037 49 let videoUUID: string
5f04dd2f
C
50
51 before(async function () {
48f07b4a 52 this.timeout(60000)
5f04dd2f 53
2422c46b
C
54 servers = await flushAndRunMultipleServers(2)
55
56 await setAccessTokensToServers(servers)
57 await doubleFollow(servers[0], servers[1])
5f04dd2f 58
48dce1c9 59 {
6b738c7a
C
60 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
61 const user: User = res.body
0f320037
C
62
63 firstVideoChannelId = user.videoChannels[0].id
48dce1c9
C
64 }
65
3cd0734f 66 await waitJobs(servers)
5f04dd2f
C
67 })
68
69 it('Should have one video channel (created with root)', async () => {
2422c46b 70 const res = await getVideoChannelsList(servers[0].url, 0, 2)
5f04dd2f
C
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
2422c46b
C
77 it('Should create another video channel', async function () {
78 this.timeout(10000)
79
0f320037
C
80 {
81 const videoChannel = {
8a19bee1 82 name: 'second_video_channel',
0f320037
C
83 displayName: 'second video channel',
84 description: 'super video channel description',
85 support: 'super video channel support text'
86 }
a1587156 87 const res = await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
0f320037 88 secondVideoChannelId = res.body.videoChannel.id
5f04dd2f 89 }
2422c46b
C
90
91 // The channel is 1 is propagated to servers 2
0f320037 92 {
7d14d4d2 93 const videoAttributesArg = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
a1587156 94 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributesArg)
0f320037
C
95 videoUUID = res.body.video.uuid
96 }
2422c46b 97
3cd0734f 98 await waitJobs(servers)
5f04dd2f
C
99 })
100
101 it('Should have two video channels when getting my information', async () => {
2422c46b 102 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
5f04dd2f
C
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
8a19bee1
C
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')
7bc29171 113 expect(videoChannels[1].displayName).to.equal('second video channel')
5f04dd2f 114 expect(videoChannels[1].description).to.equal('super video channel description')
2422c46b 115 expect(videoChannels[1].support).to.equal('super video channel support text')
5f04dd2f
C
116 })
117
2422c46b 118 it('Should have two video channels when getting account channels on server 1', async function () {
91b66319 119 const res = await getAccountVideoChannelsList({
a1587156 120 url: servers[0].url,
91b66319
C
121 accountName: userInfo.account.name + '@' + userInfo.account.host
122 })
123
5f04dd2f
C
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
8a19bee1
C
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')
7bc29171 133 expect(videoChannels[1].displayName).to.equal('second video channel')
5f04dd2f 134 expect(videoChannels[1].description).to.equal('super video channel description')
2422c46b
C
135 expect(videoChannels[1].support).to.equal('super video channel support text')
136 })
137
91b66319
C
138 it('Should paginate and sort account channels', async function () {
139 {
140 const res = await getAccountVideoChannelsList({
a1587156 141 url: servers[0].url,
91b66319
C
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
a1587156 151 const videoChannel: VideoChannel = res.body.data[0]
91b66319
C
152 expect(videoChannel.name).to.equal('root_channel')
153 }
154
155 {
156 const res = await getAccountVideoChannelsList({
a1587156 157 url: servers[0].url,
91b66319
C
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
a1587156 167 const videoChannel: VideoChannel = res.body.data[0]
91b66319
C
168 expect(videoChannel.name).to.equal('second_video_channel')
169 }
170
171 {
172 const res = await getAccountVideoChannelsList({
a1587156 173 url: servers[0].url,
91b66319
C
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
a1587156 183 const videoChannel: VideoChannel = res.body.data[0]
91b66319
C
184 expect(videoChannel.name).to.equal('root_channel')
185 }
186 })
187
2422c46b 188 it('Should have one video channel when getting account channels on server 2', async function () {
91b66319 189 const res = await getAccountVideoChannelsList({
a1587156 190 url: servers[1].url,
91b66319
C
191 accountName: userInfo.account.name + '@' + userInfo.account.host
192 })
193
2422c46b
C
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)
5f04dd2f 197
2422c46b 198 const videoChannels = res.body.data
8a19bee1 199 expect(videoChannels[0].name).to.equal('second_video_channel')
2422c46b
C
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')
5f04dd2f
C
203 })
204
2422c46b
C
205 it('Should list video channels', async function () {
206 const res = await getVideoChannelsList(servers[0].url, 1, 1, '-name')
5f04dd2f
C
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)
8a19bee1
C
211 expect(res.body.data[0].name).to.equal('root_channel')
212 expect(res.body.data[0].displayName).to.equal('Main root channel')
5f04dd2f
C
213 })
214
2422c46b 215 it('Should update video channel', async function () {
7d14d4d2 216 this.timeout(15000)
2422c46b 217
5f04dd2f 218 const videoChannelAttributes = {
08c1efbe 219 displayName: 'video channel updated',
2422c46b 220 description: 'video channel description updated',
7d14d4d2 221 support: 'support updated'
5f04dd2f
C
222 }
223
8a19bee1 224 await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes)
2422c46b 225
3cd0734f 226 await waitJobs(servers)
5f04dd2f
C
227 })
228
2422c46b
C
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')
5f04dd2f 232
2422c46b
C
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)
8a19bee1 236 expect(res.body.data[0].name).to.equal('second_video_channel')
2422c46b
C
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')
7d14d4d2
C
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)
2422c46b 269 }
5f04dd2f
C
270 })
271
4bbfc6c6 272 it('Should update video channel avatar', async function () {
213e30ef 273 this.timeout(15000)
4bbfc6c6
C
274
275 const fixture = 'avatar.png'
276
213e30ef 277 await updateVideoChannelImage({
4bbfc6c6
C
278 url: servers[0].url,
279 accessToken: servers[0].accessToken,
8a19bee1 280 videoChannelName: 'second_video_channel',
213e30ef
C
281 fixture,
282 type: 'avatar'
4bbfc6c6
C
283 })
284
285 await waitJobs(servers)
213e30ef
C
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 }
4bbfc6c6
C
292 })
293
213e30ef
C
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
4bbfc6c6 309 for (const server of servers) {
cdeddff1
C
310 const res = await getVideoChannel(server.url, 'second_video_channel@' + servers[0].host)
311 const videoChannel = res.body
4bbfc6c6 312
213e30ef
C
313 await testImage(server.url, 'banner-resized', videoChannel.banner.path)
314 }
315 })
4bbfc6c6 316
213e30ef
C
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
4bbfc6c6
C
333 }
334 })
335
213e30ef
C
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 })
5f04dd2f 345
213e30ef
C
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 }
5f04dd2f
C
353 })
354
0f320037 355 it('Should list the second video channel videos', async function () {
6b738c7a
C
356 this.timeout(10000)
357
358 for (const server of servers) {
48f07b4a 359 const channelURI = 'second_video_channel@localhost:' + servers[0].port
8a19bee1 360 const res1 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
0f320037
C
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
3cd0734f 373 await waitJobs(servers)
0f320037
C
374 })
375
376 it('Should list the first video channel videos', async function () {
377 this.timeout(10000)
378
379 for (const server of servers) {
48f07b4a 380 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
8a19bee1 381 const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondChannelURI, 0, 5)
0f320037
C
382 expect(res1.body.total).to.equal(0)
383
48f07b4a 384 const channelURI = 'root_channel@localhost:' + servers[0].port
8a19bee1 385 const res2 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
0f320037
C
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')
6b738c7a
C
392 }
393 })
394
2422c46b 395 it('Should delete video channel', async function () {
8a19bee1 396 await deleteVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel')
5f04dd2f
C
397 })
398
2422c46b
C
399 it('Should have video channel deleted', async function () {
400 const res = await getVideoChannelsList(servers[0].url, 0, 10)
5f04dd2f
C
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)
8a19bee1
C
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' }
a1587156 411 await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
8a19bee1
C
412 }
413
414 {
a1587156
C
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' })
8a19bee1 417
a1587156
C
418 const res = await getMyUserInformation(servers[0].url, accessToken)
419 const videoChannel = res.body.videoChannels[0]
8a19bee1
C
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 }
5f04dd2f
C
422 })
423
1ba471c5 424 it('Should report correct channel views per days', async function () {
5a61ffbb 425 this.timeout(10000)
714bfcc5
RK
426
427 {
428 const res = await getAccountVideoChannelsList({
429 url: servers[0].url,
430 accountName: userInfo.account.name + '@' + userInfo.account.host,
431 withStats: true
432 })
1ba471c5
C
433
434 const channels: VideoChannel[] = res.body.data
435
436 for (const channel of channels) {
714bfcc5
RK
437 expect(channel).to.haveOwnProperty('viewsPerDay')
438 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
1ba471c5
C
439
440 for (const v of channel.viewsPerDay) {
714bfcc5
RK
441 expect(v.date).to.be.an('string')
442 expect(v.views).to.equal(0)
1ba471c5
C
443 }
444 }
714bfcc5
RK
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
1ba471c5
C
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
7b390964
RK
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
7c3b7976
C
506 after(async function () {
507 await cleanupTests(servers)
5f04dd2f
C
508 })
509})