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