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