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