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