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