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