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