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