]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-channels.ts
user-right: moderator can't manage admins channel
[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'
2c627c15 19import { HttpStatusCode, User, UserRole, 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
3a8fe5a2 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
3a8fe5a2 223 it('Should update another accounts video channel', async function () {
224 this.timeout(15000)
952881aa 225
3a8fe5a2 226 const result = await servers[0].users.generate('second_user')
227 secondUserChannelName = result.userChannelName
228
952881aa
C
229 await servers[0].videos.quickUpload({ name: 'video', token: result.token })
230
3a8fe5a2 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
952881aa 242 it('Should have another accounts video channel updated', async function () {
3a8fe5a2 243 for (const server of servers) {
952881aa 244 const body = await server.channels.get({ channelName: `${secondUserChannelName}@${servers[0].host}` })
3a8fe5a2 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 () {
952881aa 401 const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
5f04dd2f 402
952881aa 403 expect(body.total).to.equal(2)
a5461888 404 expect(body.data).to.be.an('array')
952881aa 405 expect(body.data).to.have.lengthOf(2)
a5461888 406 expect(body.data[0].displayName).to.equal('Main root channel')
952881aa 407 expect(body.data[1].displayName).to.equal('video channel updated')
8a19bee1
C
408 })
409
2c627c15 410 it('Should not allow moderator to update an admins video channel', async function () {
411 this.timeout(15000)
412
413 const { token: moderatorToken } = await servers[0].users.generate('moderator1', UserRole.MODERATOR)
414 const result = await servers[0].users.generate('admin_user', UserRole.ADMINISTRATOR)
415
416 await servers[0].videos.quickUpload({ name: 'video', token: result.token })
417
418 const videoChannelAttributes = {
419 displayName: 'video channel updated',
420 description: 'video channel description updated',
421 support: 'support updated'
422 }
423
424 await servers[0].channels.update({
425 channelName: result.userChannelName,
426 attributes: videoChannelAttributes,
427 expectedStatus: HttpStatusCode.FORBIDDEN_403,
428 token: moderatorToken
429 })
430 })
431
8a19bee1
C
432 it('Should create the main channel with an uuid if there is a conflict', async function () {
433 {
434 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
89d241a7 435 const created = await servers[0].channels.create({ attributes: videoChannel })
a5461888 436 totoChannel = created.id
8a19bee1
C
437 }
438
439 {
89d241a7
C
440 await servers[0].users.create({ username: 'toto', password: 'password' })
441 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
8a19bee1 442
89d241a7 443 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
7926c5f9 444 const videoChannel = videoChannels[0]
8a19bee1
C
445 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}/)
446 }
5f04dd2f
C
447 })
448
1ba471c5 449 it('Should report correct channel views per days', async function () {
5a61ffbb 450 this.timeout(10000)
714bfcc5
RK
451
452 {
89d241a7 453 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
1ba471c5 454
a5461888 455 for (const channel of data) {
714bfcc5
RK
456 expect(channel).to.haveOwnProperty('viewsPerDay')
457 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
1ba471c5
C
458
459 for (const v of channel.viewsPerDay) {
714bfcc5
RK
460 expect(v.date).to.be.an('string')
461 expect(v.views).to.equal(0)
1ba471c5
C
462 }
463 }
714bfcc5
RK
464 }
465
466 {
89d241a7
C
467 // video has been posted on channel servers[0].store.videoChannel.id since last update
468 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
469 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
714bfcc5
RK
470
471 // Wait the repeatable job
472 await wait(8000)
473
89d241a7
C
474 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
475 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
714bfcc5
RK
476 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
477 }
478 })
479
1ba471c5 480 it('Should report correct videos count', async function () {
89d241a7 481 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
1ba471c5 482
a5461888
C
483 const totoChannel = data.find(c => c.name === 'toto_channel')
484 const rootChannel = data.find(c => c.name === 'root_channel')
1ba471c5
C
485
486 expect(rootChannel.videosCount).to.equal(1)
487 expect(totoChannel.videosCount).to.equal(0)
488 })
489
7b390964
RK
490 it('Should search among account video channels', async function () {
491 {
89d241a7 492 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
a5461888 493 expect(body.total).to.equal(1)
7b390964 494
a5461888 495 const channels = body.data
7b390964
RK
496 expect(channels).to.have.lengthOf(1)
497 }
498
499 {
89d241a7 500 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
a5461888 501 expect(body.total).to.equal(0)
7b390964 502
a5461888 503 const channels = body.data
7b390964
RK
504 expect(channels).to.have.lengthOf(0)
505 }
506 })
507
e024fd6a
C
508 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
509 this.timeout(30000)
510
89d241a7 511 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
e024fd6a
C
512 await waitJobs(servers)
513
514 for (const server of servers) {
89d241a7 515 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
e024fd6a 516
a5461888
C
517 expect(data[0].name).to.equal('toto_channel')
518 expect(data[1].name).to.equal('root_channel')
e024fd6a
C
519 }
520
89d241a7 521 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
e024fd6a
C
522 await waitJobs(servers)
523
524 for (const server of servers) {
89d241a7 525 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
e024fd6a 526
a5461888
C
527 expect(data[0].name).to.equal('root_channel')
528 expect(data[1].name).to.equal('toto_channel')
e024fd6a
C
529 }
530 })
531
7c3b7976
C
532 after(async function () {
533 await cleanupTests(servers)
5f04dd2f
C
534 })
535})