]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-channels.ts
shared/ typescript types dir server-commands
[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 createMultipleServers,
10 doubleFollow,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
14 testFileExistsOrNot,
15 testImage,
16 wait,
17 waitJobs
18 } from '@shared/server-commands'
19 import { User, VideoChannel } from '@shared/models'
20
21 const expect = chai.expect
22
23 async function findChannel (server: PeerTubeServer, channelId: number) {
24 const body = await server.channels.list({ sort: '-name' })
25
26 return body.data.find(c => c.id === channelId)
27 }
28
29 describe('Test video channels', function () {
30 let servers: PeerTubeServer[]
31 let userInfo: User
32 let secondVideoChannelId: number
33 let totoChannel: number
34 let videoUUID: string
35 let accountName: string
36 let secondUserChannelName: string
37
38 const avatarPaths: { [ port: number ]: string } = {}
39 const bannerPaths: { [ port: number ]: string } = {}
40
41 before(async function () {
42 this.timeout(60000)
43
44 servers = await createMultipleServers(2)
45
46 await setAccessTokensToServers(servers)
47 await setDefaultVideoChannel(servers)
48
49 await doubleFollow(servers[0], servers[1])
50 })
51
52 it('Should have one video channel (created with root)', async () => {
53 const body = await servers[0].channels.list({ start: 0, count: 2 })
54
55 expect(body.total).to.equal(1)
56 expect(body.data).to.be.an('array')
57 expect(body.data).to.have.lengthOf(1)
58 })
59
60 it('Should create another video channel', async function () {
61 this.timeout(10000)
62
63 {
64 const videoChannel = {
65 name: 'second_video_channel',
66 displayName: 'second video channel',
67 description: 'super video channel description',
68 support: 'super video channel support text'
69 }
70 const created = await servers[0].channels.create({ attributes: videoChannel })
71 secondVideoChannelId = created.id
72 }
73
74 // The channel is 1 is propagated to servers 2
75 {
76 const attributes = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
77 const { uuid } = await servers[0].videos.upload({ attributes })
78 videoUUID = uuid
79 }
80
81 await waitJobs(servers)
82 })
83
84 it('Should have two video channels when getting my information', async () => {
85 userInfo = await servers[0].users.getMyInfo()
86
87 expect(userInfo.videoChannels).to.be.an('array')
88 expect(userInfo.videoChannels).to.have.lengthOf(2)
89
90 const videoChannels = userInfo.videoChannels
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')
95 expect(videoChannels[1].displayName).to.equal('second video channel')
96 expect(videoChannels[1].description).to.equal('super video channel description')
97 expect(videoChannels[1].support).to.equal('super video channel support text')
98
99 accountName = userInfo.account.name + '@' + userInfo.account.host
100 })
101
102 it('Should have two video channels when getting account channels on server 1', async function () {
103 const body = await servers[0].channels.listByAccount({ accountName })
104 expect(body.total).to.equal(2)
105
106 const videoChannels = body.data
107
108 expect(videoChannels).to.be.an('array')
109 expect(videoChannels).to.have.lengthOf(2)
110
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')
115 expect(videoChannels[1].displayName).to.equal('second video channel')
116 expect(videoChannels[1].description).to.equal('super video channel description')
117 expect(videoChannels[1].support).to.equal('super video channel support text')
118 })
119
120 it('Should paginate and sort account channels', async function () {
121 {
122 const body = await servers[0].channels.listByAccount({
123 accountName,
124 start: 0,
125 count: 1,
126 sort: 'createdAt'
127 })
128
129 expect(body.total).to.equal(2)
130 expect(body.data).to.have.lengthOf(1)
131
132 const videoChannel: VideoChannel = body.data[0]
133 expect(videoChannel.name).to.equal('root_channel')
134 }
135
136 {
137 const body = await servers[0].channels.listByAccount({
138 accountName,
139 start: 0,
140 count: 1,
141 sort: '-createdAt'
142 })
143
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')
147 }
148
149 {
150 const body = await servers[0].channels.listByAccount({
151 accountName,
152 start: 1,
153 count: 1,
154 sort: '-createdAt'
155 })
156
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')
160 }
161 })
162
163 it('Should have one video channel when getting account channels on server 2', async function () {
164 const body = await servers[1].channels.listByAccount({ accountName })
165
166 expect(body.total).to.equal(1)
167 expect(body.data).to.be.an('array')
168 expect(body.data).to.have.lengthOf(1)
169
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')
175 })
176
177 it('Should list video channels', async function () {
178 const body = await servers[0].channels.list({ start: 1, count: 1, sort: '-name' })
179
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')
185 })
186
187 it('Should update video channel', async function () {
188 this.timeout(15000)
189
190 const videoChannelAttributes = {
191 displayName: 'video channel updated',
192 description: 'video channel description updated',
193 support: 'support updated'
194 }
195
196 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
197
198 await waitJobs(servers)
199 })
200
201 it('Should have video channel updated', async function () {
202 for (const server of servers) {
203 const body = await server.channels.list({ start: 0, count: 1, sort: '-name' })
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')
213 }
214 })
215
216 it('Should not have updated the video support field', async function () {
217 for (const server of servers) {
218 const video = await server.videos.get({ id: videoUUID })
219 expect(video.support).to.equal('video support field')
220 }
221 })
222
223 it('Should update another accounts video channel', async function () {
224 this.timeout(15000)
225
226 const result = await servers[0].users.generate('second_user')
227 secondUserChannelName = result.userChannelName
228
229 await servers[0].videos.quickUpload({ name: 'video', token: result.token })
230
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
242 it('Should have another accounts video channel updated', async function () {
243 for (const server of servers) {
244 const body = await server.channels.get({ channelName: `${secondUserChannelName}@${servers[0].host}` })
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
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
260 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
261
262 await waitJobs(servers)
263
264 for (const server of servers) {
265 const video = await server.videos.get({ id: videoUUID })
266 expect(video.support).to.equal(videoChannelAttributes.support)
267 }
268 })
269
270 it('Should update video channel avatar', async function () {
271 this.timeout(15000)
272
273 const fixture = 'avatar.png'
274
275 await servers[0].channels.updateImage({
276 channelName: 'second_video_channel',
277 fixture,
278 type: 'avatar'
279 })
280
281 await waitJobs(servers)
282
283 for (const server of servers) {
284 const videoChannel = await findChannel(server, secondVideoChannelId)
285
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)
289
290 const row = await server.sql.getActorImage(basename(avatarPaths[server.port]))
291 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height)
292 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width)
293 }
294 })
295
296 it('Should update video channel banner', async function () {
297 this.timeout(15000)
298
299 const fixture = 'banner.jpg'
300
301 await servers[0].channels.updateImage({
302 channelName: 'second_video_channel',
303 fixture,
304 type: 'banner'
305 })
306
307 await waitJobs(servers)
308
309 for (const server of servers) {
310 const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
311
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)
315
316 const row = await server.sql.getActorImage(basename(bannerPaths[server.port]))
317 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height)
318 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width)
319 }
320 })
321
322 it('Should delete the video channel avatar', async function () {
323 this.timeout(15000)
324
325 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
326
327 await waitJobs(servers)
328
329 for (const server of servers) {
330 const videoChannel = await findChannel(server, secondVideoChannelId)
331 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
332
333 expect(videoChannel.avatar).to.be.null
334 }
335 })
336
337 it('Should delete the video channel banner', async function () {
338 this.timeout(15000)
339
340 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
341
342 await waitJobs(servers)
343
344 for (const server of servers) {
345 const videoChannel = await findChannel(server, secondVideoChannelId)
346 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
347
348 expect(videoChannel.banner).to.be.null
349 }
350 })
351
352 it('Should list the second video channel videos', async function () {
353 this.timeout(10000)
354
355 for (const server of servers) {
356 const channelURI = 'second_video_channel@localhost:' + servers[0].port
357 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
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')
363 }
364 })
365
366 it('Should change the video channel of a video', async function () {
367 this.timeout(10000)
368
369 await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
370
371 await waitJobs(servers)
372 })
373
374 it('Should list the first video channel videos', async function () {
375 this.timeout(10000)
376
377 for (const server of servers) {
378 {
379 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
380 const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
381 expect(total).to.equal(0)
382 }
383
384 {
385 const channelURI = 'root_channel@localhost:' + servers[0].port
386 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
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 }
393 }
394 })
395
396 it('Should delete video channel', async function () {
397 await servers[0].channels.delete({ channelName: 'second_video_channel' })
398 })
399
400 it('Should have video channel deleted', async function () {
401 const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
402
403 expect(body.total).to.equal(2)
404 expect(body.data).to.be.an('array')
405 expect(body.data).to.have.lengthOf(2)
406 expect(body.data[0].displayName).to.equal('Main root channel')
407 expect(body.data[1].displayName).to.equal('video channel updated')
408 })
409
410 it('Should create the main channel with an uuid if there is a conflict', async function () {
411 {
412 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
413 const created = await servers[0].channels.create({ attributes: videoChannel })
414 totoChannel = created.id
415 }
416
417 {
418 await servers[0].users.create({ username: 'toto', password: 'password' })
419 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
420
421 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
422 const videoChannel = videoChannels[0]
423 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}/)
424 }
425 })
426
427 it('Should report correct channel views per days', async function () {
428 this.timeout(10000)
429
430 {
431 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
432
433 for (const channel of data) {
434 expect(channel).to.haveOwnProperty('viewsPerDay')
435 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
436
437 for (const v of channel.viewsPerDay) {
438 expect(v.date).to.be.an('string')
439 expect(v.views).to.equal(0)
440 }
441 }
442 }
443
444 {
445 // video has been posted on channel servers[0].store.videoChannel.id since last update
446 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
447 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
448
449 // Wait the repeatable job
450 await wait(8000)
451
452 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
453 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
454 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
455 }
456 })
457
458 it('Should report correct videos count', async function () {
459 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
460
461 const totoChannel = data.find(c => c.name === 'toto_channel')
462 const rootChannel = data.find(c => c.name === 'root_channel')
463
464 expect(rootChannel.videosCount).to.equal(1)
465 expect(totoChannel.videosCount).to.equal(0)
466 })
467
468 it('Should search among account video channels', async function () {
469 {
470 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
471 expect(body.total).to.equal(1)
472
473 const channels = body.data
474 expect(channels).to.have.lengthOf(1)
475 }
476
477 {
478 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
479 expect(body.total).to.equal(0)
480
481 const channels = body.data
482 expect(channels).to.have.lengthOf(0)
483 }
484 })
485
486 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
487 this.timeout(30000)
488
489 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
490 await waitJobs(servers)
491
492 for (const server of servers) {
493 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
494
495 expect(data[0].name).to.equal('toto_channel')
496 expect(data[1].name).to.equal('root_channel')
497 }
498
499 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
500 await waitJobs(servers)
501
502 for (const server of servers) {
503 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
504
505 expect(data[0].name).to.equal('root_channel')
506 expect(data[1].name).to.equal('toto_channel')
507 }
508 })
509
510 after(async function () {
511 await cleanupTests(servers)
512 })
513 })