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