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