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