]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-channels.ts
Introduce sql command
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-channels.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { basename } from 'path'
6 import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
7 import {
8 cleanupTests,
9 createUser,
10 doubleFollow,
11 flushAndRunMultipleServers,
12 getVideo,
13 getVideoChannelVideos,
14 setDefaultVideoChannel,
15 testFileExistsOrNot,
16 testImage,
17 updateVideo,
18 uploadVideo,
19 userLogin,
20 wait
21 } from '../../../../shared/extra-utils'
22 import { getMyUserInformation, ServerInfo, setAccessTokensToServers, viewVideo } from '../../../../shared/extra-utils/index'
23 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
24 import { User, Video, VideoChannel, VideoDetails } from '../../../../shared/index'
25
26 const expect = chai.expect
27
28 async function findChannel (server: ServerInfo, channelId: number) {
29 const body = await server.channelsCommand.list({ sort: '-name' })
30
31 return body.data.find(c => c.id === channelId)
32 }
33
34 describe('Test video channels', function () {
35 let servers: ServerInfo[]
36 let userInfo: User
37 let secondVideoChannelId: number
38 let totoChannel: number
39 let videoUUID: string
40 let accountName: string
41
42 const avatarPaths: { [ port: number ]: string } = {}
43 const bannerPaths: { [ port: number ]: string } = {}
44
45 before(async function () {
46 this.timeout(60000)
47
48 servers = await flushAndRunMultipleServers(2)
49
50 await setAccessTokensToServers(servers)
51 await setDefaultVideoChannel(servers)
52
53 await doubleFollow(servers[0], servers[1])
54 })
55
56 it('Should have one video channel (created with root)', async () => {
57 const body = await servers[0].channelsCommand.list({ start: 0, count: 2 })
58
59 expect(body.total).to.equal(1)
60 expect(body.data).to.be.an('array')
61 expect(body.data).to.have.lengthOf(1)
62 })
63
64 it('Should create another video channel', async function () {
65 this.timeout(10000)
66
67 {
68 const videoChannel = {
69 name: 'second_video_channel',
70 displayName: 'second video channel',
71 description: 'super video channel description',
72 support: 'super video channel support text'
73 }
74 const created = await servers[0].channelsCommand.create({ attributes: videoChannel })
75 secondVideoChannelId = created.id
76 }
77
78 // The channel is 1 is propagated to servers 2
79 {
80 const videoAttributesArg = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
81 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributesArg)
82 videoUUID = res.body.video.uuid
83 }
84
85 await waitJobs(servers)
86 })
87
88 it('Should have two video channels when getting my information', async () => {
89 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
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
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')
100 expect(videoChannels[1].displayName).to.equal('second video channel')
101 expect(videoChannels[1].description).to.equal('super video channel description')
102 expect(videoChannels[1].support).to.equal('super video channel support text')
103
104 accountName = userInfo.account.name + '@' + userInfo.account.host
105 })
106
107 it('Should have two video channels when getting account channels on server 1', async function () {
108 const body = await servers[0].channelsCommand.listByAccount({ accountName })
109 expect(body.total).to.equal(2)
110
111 const videoChannels = body.data
112
113 expect(videoChannels).to.be.an('array')
114 expect(videoChannels).to.have.lengthOf(2)
115
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')
120 expect(videoChannels[1].displayName).to.equal('second video channel')
121 expect(videoChannels[1].description).to.equal('super video channel description')
122 expect(videoChannels[1].support).to.equal('super video channel support text')
123 })
124
125 it('Should paginate and sort account channels', async function () {
126 {
127 const body = await servers[0].channelsCommand.listByAccount({
128 accountName,
129 start: 0,
130 count: 1,
131 sort: 'createdAt'
132 })
133
134 expect(body.total).to.equal(2)
135 expect(body.data).to.have.lengthOf(1)
136
137 const videoChannel: VideoChannel = body.data[0]
138 expect(videoChannel.name).to.equal('root_channel')
139 }
140
141 {
142 const body = await servers[0].channelsCommand.listByAccount({
143 accountName,
144 start: 0,
145 count: 1,
146 sort: '-createdAt'
147 })
148
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')
152 }
153
154 {
155 const body = await servers[0].channelsCommand.listByAccount({
156 accountName,
157 start: 1,
158 count: 1,
159 sort: '-createdAt'
160 })
161
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')
165 }
166 })
167
168 it('Should have one video channel when getting account channels on server 2', async function () {
169 const body = await servers[1].channelsCommand.listByAccount({ accountName })
170
171 expect(body.total).to.equal(1)
172 expect(body.data).to.be.an('array')
173 expect(body.data).to.have.lengthOf(1)
174
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')
180 })
181
182 it('Should list video channels', async function () {
183 const body = await servers[0].channelsCommand.list({ start: 1, count: 1, sort: '-name' })
184
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')
190 })
191
192 it('Should update video channel', async function () {
193 this.timeout(15000)
194
195 const videoChannelAttributes = {
196 displayName: 'video channel updated',
197 description: 'video channel description updated',
198 support: 'support updated'
199 }
200
201 await servers[0].channelsCommand.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
202
203 await waitJobs(servers)
204 })
205
206 it('Should have video channel updated', async function () {
207 for (const server of servers) {
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')
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
238 await servers[0].channelsCommand.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
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)
247 }
248 })
249
250 it('Should update video channel avatar', async function () {
251 this.timeout(15000)
252
253 const fixture = 'avatar.png'
254
255 await servers[0].channelsCommand.updateImage({
256 channelName: 'second_video_channel',
257 fixture,
258 type: 'avatar'
259 })
260
261 await waitJobs(servers)
262
263 for (const server of servers) {
264 const videoChannel = await findChannel(server, secondVideoChannelId)
265
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)
269
270 const row = await server.sqlCommand.getActorImage(basename(avatarPaths[server.port]))
271 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height)
272 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width)
273 }
274 })
275
276 it('Should update video channel banner', async function () {
277 this.timeout(15000)
278
279 const fixture = 'banner.jpg'
280
281 await servers[0].channelsCommand.updateImage({
282 channelName: 'second_video_channel',
283 fixture,
284 type: 'banner'
285 })
286
287 await waitJobs(servers)
288
289 for (const server of servers) {
290 const videoChannel = await server.channelsCommand.get({ channelName: 'second_video_channel@' + servers[0].host })
291
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)
295
296 const row = await server.sqlCommand.getActorImage(basename(bannerPaths[server.port]))
297 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height)
298 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width)
299 }
300 })
301
302 it('Should delete the video channel avatar', async function () {
303 this.timeout(15000)
304
305 await servers[0].channelsCommand.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
306
307 await waitJobs(servers)
308
309 for (const server of servers) {
310 const videoChannel = await findChannel(server, secondVideoChannelId)
311 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
312
313 expect(videoChannel.avatar).to.be.null
314 }
315 })
316
317 it('Should delete the video channel banner', async function () {
318 this.timeout(15000)
319
320 await servers[0].channelsCommand.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
321
322 await waitJobs(servers)
323
324 for (const server of servers) {
325 const videoChannel = await findChannel(server, secondVideoChannelId)
326 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
327
328 expect(videoChannel.banner).to.be.null
329 }
330 })
331
332 it('Should list the second video channel videos', async function () {
333 this.timeout(10000)
334
335 for (const server of servers) {
336 const channelURI = 'second_video_channel@localhost:' + servers[0].port
337 const res1 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
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
348 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { channelId: servers[0].videoChannel.id })
349
350 await waitJobs(servers)
351 })
352
353 it('Should list the first video channel videos', async function () {
354 this.timeout(10000)
355
356 for (const server of servers) {
357 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
358 const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondChannelURI, 0, 5)
359 expect(res1.body.total).to.equal(0)
360
361 const channelURI = 'root_channel@localhost:' + servers[0].port
362 const res2 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
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')
369 }
370 })
371
372 it('Should delete video channel', async function () {
373 await servers[0].channelsCommand.delete({ channelName: 'second_video_channel' })
374 })
375
376 it('Should have video channel deleted', async function () {
377 const body = await servers[0].channelsCommand.list({ start: 0, count: 10 })
378
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')
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' }
388 const created = await servers[0].channelsCommand.create({ attributes: videoChannel })
389 totoChannel = created.id
390 }
391
392 {
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' })
395
396 const res = await getMyUserInformation(servers[0].url, accessToken)
397 const videoChannel = res.body.videoChannels[0]
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 }
400 })
401
402 it('Should report correct channel views per days', async function () {
403 this.timeout(10000)
404
405 {
406 const { data } = await servers[0].channelsCommand.listByAccount({ accountName, withStats: true })
407
408 for (const channel of data) {
409 expect(channel).to.haveOwnProperty('viewsPerDay')
410 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
411
412 for (const v of channel.viewsPerDay) {
413 expect(v.date).to.be.an('string')
414 expect(v.views).to.equal(0)
415 }
416 }
417 }
418
419 {
420 // video has been posted on channel servers[0].videoChannel.id since last update
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
427 const { data } = await servers[0].channelsCommand.listByAccount({ accountName, withStats: true })
428 const channelWithView = data.find(channel => channel.id === servers[0].videoChannel.id)
429 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
430 }
431 })
432
433 it('Should report correct videos count', async function () {
434 const { data } = await servers[0].channelsCommand.listByAccount({ accountName, withStats: true })
435
436 const totoChannel = data.find(c => c.name === 'toto_channel')
437 const rootChannel = data.find(c => c.name === 'root_channel')
438
439 expect(rootChannel.videosCount).to.equal(1)
440 expect(totoChannel.videosCount).to.equal(0)
441 })
442
443 it('Should search among account video channels', async function () {
444 {
445 const body = await servers[0].channelsCommand.listByAccount({ accountName, search: 'root' })
446 expect(body.total).to.equal(1)
447
448 const channels = body.data
449 expect(channels).to.have.lengthOf(1)
450 }
451
452 {
453 const body = await servers[0].channelsCommand.listByAccount({ accountName, search: 'does not exist' })
454 expect(body.total).to.equal(0)
455
456 const channels = body.data
457 expect(channels).to.have.lengthOf(0)
458 }
459 })
460
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) {
468 const { data } = await server.channelsCommand.listByAccount({ accountName, sort: '-updatedAt' })
469
470 expect(data[0].name).to.equal('toto_channel')
471 expect(data[1].name).to.equal('root_channel')
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) {
478 const { data } = await server.channelsCommand.listByAccount({ accountName, sort: '-updatedAt' })
479
480 expect(data[0].name).to.equal('root_channel')
481 expect(data[1].name).to.equal('toto_channel')
482 }
483 })
484
485 after(async function () {
486 await cleanupTests(servers)
487 })
488 })