]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-channels.ts
Implement avatar miniatures (#4639)
[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
213e30ef
C
328 it('Should delete the video channel avatar', async function () {
329 this.timeout(15000)
89d241a7 330 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
213e30ef
C
331
332 await waitJobs(servers)
333
334 for (const server of servers) {
335 const videoChannel = await findChannel(server, secondVideoChannelId)
06c27593 336 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
213e30ef 337
d0800f76 338 expect(videoChannel.avatars).to.be.empty
4bbfc6c6
C
339 }
340 })
341
213e30ef
C
342 it('Should delete the video channel banner', async function () {
343 this.timeout(15000)
344
89d241a7 345 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
5f04dd2f 346
213e30ef
C
347 await waitJobs(servers)
348
349 for (const server of servers) {
350 const videoChannel = await findChannel(server, secondVideoChannelId)
06c27593 351 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
213e30ef 352
d0800f76 353 expect(videoChannel.banners).to.be.empty
213e30ef 354 }
5f04dd2f
C
355 })
356
0f320037 357 it('Should list the second video channel videos', async function () {
6b738c7a
C
358 this.timeout(10000)
359
360 for (const server of servers) {
48f07b4a 361 const channelURI = 'second_video_channel@localhost:' + servers[0].port
c0e8b12e 362 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
d23dd9fb
C
363
364 expect(total).to.equal(1)
365 expect(data).to.be.an('array')
366 expect(data).to.have.lengthOf(1)
367 expect(data[0].name).to.equal('my video name')
0f320037
C
368 }
369 })
370
371 it('Should change the video channel of a video', async function () {
372 this.timeout(10000)
373
89d241a7 374 await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
0f320037 375
3cd0734f 376 await waitJobs(servers)
0f320037
C
377 })
378
379 it('Should list the first video channel videos', async function () {
380 this.timeout(10000)
381
382 for (const server of servers) {
d23dd9fb
C
383 {
384 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
c0e8b12e 385 const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
d23dd9fb
C
386 expect(total).to.equal(0)
387 }
388
389 {
390 const channelURI = 'root_channel@localhost:' + servers[0].port
c0e8b12e 391 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
d23dd9fb
C
392 expect(total).to.equal(1)
393
394 expect(data).to.be.an('array')
395 expect(data).to.have.lengthOf(1)
396 expect(data[0].name).to.equal('my video name')
397 }
6b738c7a
C
398 }
399 })
400
2422c46b 401 it('Should delete video channel', async function () {
89d241a7 402 await servers[0].channels.delete({ channelName: 'second_video_channel' })
5f04dd2f
C
403 })
404
2422c46b 405 it('Should have video channel deleted', async function () {
a37e9e74 406 const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
5f04dd2f 407
a37e9e74 408 expect(body.total).to.equal(2)
a5461888 409 expect(body.data).to.be.an('array')
a37e9e74 410 expect(body.data).to.have.lengthOf(2)
a5461888 411 expect(body.data[0].displayName).to.equal('Main root channel')
a37e9e74 412 expect(body.data[1].displayName).to.equal('video channel updated')
8a19bee1
C
413 })
414
415 it('Should create the main channel with an uuid if there is a conflict', async function () {
416 {
417 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
89d241a7 418 const created = await servers[0].channels.create({ attributes: videoChannel })
a5461888 419 totoChannel = created.id
8a19bee1
C
420 }
421
422 {
89d241a7
C
423 await servers[0].users.create({ username: 'toto', password: 'password' })
424 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
8a19bee1 425
89d241a7 426 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
7926c5f9 427 const videoChannel = videoChannels[0]
8a19bee1
C
428 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}/)
429 }
5f04dd2f
C
430 })
431
1ba471c5 432 it('Should report correct channel views per days', async function () {
5a61ffbb 433 this.timeout(10000)
714bfcc5
RK
434
435 {
89d241a7 436 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
1ba471c5 437
a5461888 438 for (const channel of data) {
714bfcc5
RK
439 expect(channel).to.haveOwnProperty('viewsPerDay')
440 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
1ba471c5
C
441
442 for (const v of channel.viewsPerDay) {
714bfcc5
RK
443 expect(v.date).to.be.an('string')
444 expect(v.views).to.equal(0)
1ba471c5
C
445 }
446 }
714bfcc5
RK
447 }
448
449 {
89d241a7
C
450 // video has been posted on channel servers[0].store.videoChannel.id since last update
451 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
452 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
714bfcc5
RK
453
454 // Wait the repeatable job
455 await wait(8000)
456
89d241a7
C
457 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
458 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
714bfcc5
RK
459 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
460 }
461 })
462
1ba471c5 463 it('Should report correct videos count', async function () {
89d241a7 464 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
1ba471c5 465
a5461888
C
466 const totoChannel = data.find(c => c.name === 'toto_channel')
467 const rootChannel = data.find(c => c.name === 'root_channel')
1ba471c5
C
468
469 expect(rootChannel.videosCount).to.equal(1)
470 expect(totoChannel.videosCount).to.equal(0)
471 })
472
7b390964
RK
473 it('Should search among account video channels', async function () {
474 {
89d241a7 475 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
a5461888 476 expect(body.total).to.equal(1)
7b390964 477
a5461888 478 const channels = body.data
7b390964
RK
479 expect(channels).to.have.lengthOf(1)
480 }
481
482 {
89d241a7 483 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
a5461888 484 expect(body.total).to.equal(0)
7b390964 485
a5461888 486 const channels = body.data
7b390964
RK
487 expect(channels).to.have.lengthOf(0)
488 }
489 })
490
e024fd6a
C
491 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
492 this.timeout(30000)
493
89d241a7 494 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
e024fd6a
C
495 await waitJobs(servers)
496
497 for (const server of servers) {
89d241a7 498 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
e024fd6a 499
a5461888
C
500 expect(data[0].name).to.equal('toto_channel')
501 expect(data[1].name).to.equal('root_channel')
e024fd6a
C
502 }
503
89d241a7 504 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
e024fd6a
C
505 await waitJobs(servers)
506
507 for (const server of servers) {
89d241a7 508 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
e024fd6a 509
a5461888
C
510 expect(data[0].name).to.equal('root_channel')
511 expect(data[1].name).to.equal('toto_channel')
e024fd6a
C
512 }
513 })
514
7c3b7976
C
515 after(async function () {
516 await cleanupTests(servers)
5f04dd2f
C
517 })
518})