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