1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { basename } from 'path'
6 import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
10 deleteVideoChannelImage,
12 flushAndRunMultipleServers,
16 getVideoChannelVideos,
17 setDefaultVideoChannel,
20 updateVideoChannelImage,
24 } from '../../../../shared/extra-utils'
28 getAccountVideoChannelsList,
32 setAccessTokensToServers,
35 } from '../../../../shared/extra-utils/index'
36 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
37 import { User, Video, VideoChannel, VideoDetails } from '../../../../shared/index'
39 const expect = chai.expect
41 async function findChannel (server: ServerInfo, channelId: number) {
42 const res = await getVideoChannelsList(server.url, 0, 5, '-name')
43 const videoChannel = res.body.data.find(c => c.id === channelId)
45 return videoChannel as VideoChannel
48 describe('Test video channels', function () {
49 let servers: ServerInfo[]
51 let secondVideoChannelId: number
52 let totoChannel: number
54 let accountName: string
56 before(async function () {
59 servers = await flushAndRunMultipleServers(2)
61 await setAccessTokensToServers(servers)
62 await setDefaultVideoChannel(servers)
64 await doubleFollow(servers[0], servers[1])
67 it('Should have one video channel (created with root)', async () => {
68 const res = await getVideoChannelsList(servers[0].url, 0, 2)
70 expect(res.body.total).to.equal(1)
71 expect(res.body.data).to.be.an('array')
72 expect(res.body.data).to.have.lengthOf(1)
75 it('Should create another video channel', async function () {
79 const videoChannel = {
80 name: 'second_video_channel',
81 displayName: 'second video channel',
82 description: 'super video channel description',
83 support: 'super video channel support text'
85 const res = await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
86 secondVideoChannelId = res.body.videoChannel.id
89 // The channel is 1 is propagated to servers 2
91 const videoAttributesArg = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
92 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributesArg)
93 videoUUID = res.body.video.uuid
96 await waitJobs(servers)
99 it('Should have two video channels when getting my information', async () => {
100 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
103 expect(userInfo.videoChannels).to.be.an('array')
104 expect(userInfo.videoChannels).to.have.lengthOf(2)
106 const videoChannels = userInfo.videoChannels
107 expect(videoChannels[0].name).to.equal('root_channel')
108 expect(videoChannels[0].displayName).to.equal('Main root channel')
110 expect(videoChannels[1].name).to.equal('second_video_channel')
111 expect(videoChannels[1].displayName).to.equal('second video channel')
112 expect(videoChannels[1].description).to.equal('super video channel description')
113 expect(videoChannels[1].support).to.equal('super video channel support text')
115 accountName = userInfo.account.name + '@' + userInfo.account.host
118 it('Should have two video channels when getting account channels on server 1', async function () {
119 const res = await getAccountVideoChannelsList({
124 expect(res.body.total).to.equal(2)
125 expect(res.body.data).to.be.an('array')
126 expect(res.body.data).to.have.lengthOf(2)
128 const videoChannels = res.body.data
129 expect(videoChannels[0].name).to.equal('root_channel')
130 expect(videoChannels[0].displayName).to.equal('Main root channel')
132 expect(videoChannels[1].name).to.equal('second_video_channel')
133 expect(videoChannels[1].displayName).to.equal('second video channel')
134 expect(videoChannels[1].description).to.equal('super video channel description')
135 expect(videoChannels[1].support).to.equal('super video channel support text')
138 it('Should paginate and sort account channels', async function () {
140 const res = await getAccountVideoChannelsList({
148 expect(res.body.total).to.equal(2)
149 expect(res.body.data).to.have.lengthOf(1)
151 const videoChannel: VideoChannel = res.body.data[0]
152 expect(videoChannel.name).to.equal('root_channel')
156 const res = await getAccountVideoChannelsList({
164 expect(res.body.total).to.equal(2)
165 expect(res.body.data).to.have.lengthOf(1)
167 const videoChannel: VideoChannel = res.body.data[0]
168 expect(videoChannel.name).to.equal('second_video_channel')
172 const res = await getAccountVideoChannelsList({
180 expect(res.body.total).to.equal(2)
181 expect(res.body.data).to.have.lengthOf(1)
183 const videoChannel: VideoChannel = res.body.data[0]
184 expect(videoChannel.name).to.equal('root_channel')
188 it('Should have one video channel when getting account channels on server 2', async function () {
189 const res = await getAccountVideoChannelsList({
194 expect(res.body.total).to.equal(1)
195 expect(res.body.data).to.be.an('array')
196 expect(res.body.data).to.have.lengthOf(1)
198 const videoChannels = res.body.data
199 expect(videoChannels[0].name).to.equal('second_video_channel')
200 expect(videoChannels[0].displayName).to.equal('second video channel')
201 expect(videoChannels[0].description).to.equal('super video channel description')
202 expect(videoChannels[0].support).to.equal('super video channel support text')
205 it('Should list video channels', async function () {
206 const res = await getVideoChannelsList(servers[0].url, 1, 1, '-name')
208 expect(res.body.total).to.equal(2)
209 expect(res.body.data).to.be.an('array')
210 expect(res.body.data).to.have.lengthOf(1)
211 expect(res.body.data[0].name).to.equal('root_channel')
212 expect(res.body.data[0].displayName).to.equal('Main root channel')
215 it('Should update video channel', async function () {
218 const videoChannelAttributes = {
219 displayName: 'video channel updated',
220 description: 'video channel description updated',
221 support: 'support updated'
224 await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes)
226 await waitJobs(servers)
229 it('Should have video channel updated', async function () {
230 for (const server of servers) {
231 const res = await getVideoChannelsList(server.url, 0, 1, '-name')
233 expect(res.body.total).to.equal(2)
234 expect(res.body.data).to.be.an('array')
235 expect(res.body.data).to.have.lengthOf(1)
236 expect(res.body.data[0].name).to.equal('second_video_channel')
237 expect(res.body.data[0].displayName).to.equal('video channel updated')
238 expect(res.body.data[0].description).to.equal('video channel description updated')
239 expect(res.body.data[0].support).to.equal('support updated')
243 it('Should not have updated the video support field', async function () {
244 for (const server of servers) {
245 const res = await getVideo(server.url, videoUUID)
246 const video: VideoDetails = res.body
248 expect(video.support).to.equal('video support field')
252 it('Should update the channel support field and update videos too', async function () {
255 const videoChannelAttributes = {
256 support: 'video channel support text updated',
257 bulkVideosSupportUpdate: true
260 await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes)
262 await waitJobs(servers)
264 for (const server of servers) {
265 const res = await getVideo(server.url, videoUUID)
266 const video: VideoDetails = res.body
268 expect(video.support).to.equal(videoChannelAttributes.support)
272 it('Should update video channel avatar', async function () {
275 const fixture = 'avatar.png'
277 await updateVideoChannelImage({
279 accessToken: servers[0].accessToken,
280 videoChannelName: 'second_video_channel',
285 await waitJobs(servers)
287 for (const server of servers) {
288 const videoChannel = await findChannel(server, secondVideoChannelId)
290 await testImage(server.url, 'avatar-resized', videoChannel.avatar.path, '.png')
292 const row = await getActorImage(server.internalServerNumber, basename(videoChannel.avatar.path))
293 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height)
294 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width)
298 it('Should update video channel banner', async function () {
301 const fixture = 'banner.jpg'
303 await updateVideoChannelImage({
305 accessToken: servers[0].accessToken,
306 videoChannelName: 'second_video_channel',
311 await waitJobs(servers)
313 for (const server of servers) {
314 const res = await getVideoChannel(server.url, 'second_video_channel@' + servers[0].host)
315 const videoChannel = res.body
317 await testImage(server.url, 'banner-resized', videoChannel.banner.path)
319 const row = await getActorImage(server.internalServerNumber, basename(videoChannel.banner.path))
320 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height)
321 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width)
325 it('Should delete the video channel avatar', async function () {
328 await deleteVideoChannelImage({
330 accessToken: servers[0].accessToken,
331 videoChannelName: 'second_video_channel',
335 await waitJobs(servers)
337 for (const server of servers) {
338 const videoChannel = await findChannel(server, secondVideoChannelId)
340 expect(videoChannel.avatar).to.be.null
344 it('Should delete the video channel banner', async function () {
347 await deleteVideoChannelImage({
349 accessToken: servers[0].accessToken,
350 videoChannelName: 'second_video_channel',
354 await waitJobs(servers)
356 for (const server of servers) {
357 const videoChannel = await findChannel(server, secondVideoChannelId)
359 expect(videoChannel.banner).to.be.null
363 it('Should list the second video channel videos', async function () {
366 for (const server of servers) {
367 const channelURI = 'second_video_channel@localhost:' + servers[0].port
368 const res1 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
369 expect(res1.body.total).to.equal(1)
370 expect(res1.body.data).to.be.an('array')
371 expect(res1.body.data).to.have.lengthOf(1)
372 expect(res1.body.data[0].name).to.equal('my video name')
376 it('Should change the video channel of a video', async function () {
379 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { channelId: servers[0].videoChannel.id })
381 await waitJobs(servers)
384 it('Should list the first video channel videos', async function () {
387 for (const server of servers) {
388 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
389 const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondChannelURI, 0, 5)
390 expect(res1.body.total).to.equal(0)
392 const channelURI = 'root_channel@localhost:' + servers[0].port
393 const res2 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
394 expect(res2.body.total).to.equal(1)
396 const videos: Video[] = res2.body.data
397 expect(videos).to.be.an('array')
398 expect(videos).to.have.lengthOf(1)
399 expect(videos[0].name).to.equal('my video name')
403 it('Should delete video channel', async function () {
404 await deleteVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel')
407 it('Should have video channel deleted', async function () {
408 const res = await getVideoChannelsList(servers[0].url, 0, 10)
410 expect(res.body.total).to.equal(1)
411 expect(res.body.data).to.be.an('array')
412 expect(res.body.data).to.have.lengthOf(1)
413 expect(res.body.data[0].displayName).to.equal('Main root channel')
416 it('Should create the main channel with an uuid if there is a conflict', async function () {
418 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
419 const res = await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
420 totoChannel = res.body.videoChannel.id
424 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'toto', password: 'password' })
425 const accessToken = await userLogin(servers[0], { username: 'toto', password: 'password' })
427 const res = await getMyUserInformation(servers[0].url, accessToken)
428 const videoChannel = res.body.videoChannels[0]
429 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}/)
433 it('Should report correct channel views per days', async function () {
437 const res = await getAccountVideoChannelsList({
443 const channels: VideoChannel[] = res.body.data
445 for (const channel of channels) {
446 expect(channel).to.haveOwnProperty('viewsPerDay')
447 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
449 for (const v of channel.viewsPerDay) {
450 expect(v.date).to.be.an('string')
451 expect(v.views).to.equal(0)
457 // video has been posted on channel servers[0].videoChannel.id since last update
458 await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.1,127.0.0.1')
459 await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.2,127.0.0.1')
461 // Wait the repeatable job
464 const res = await getAccountVideoChannelsList({
469 const channelWithView = res.body.data.find((channel: VideoChannel) => channel.id === servers[0].videoChannel.id)
470 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
474 it('Should report correct videos count', async function () {
475 const res = await getAccountVideoChannelsList({
480 const channels: VideoChannel[] = res.body.data
482 const totoChannel = channels.find(c => c.name === 'toto_channel')
483 const rootChannel = channels.find(c => c.name === 'root_channel')
485 expect(rootChannel.videosCount).to.equal(1)
486 expect(totoChannel.videosCount).to.equal(0)
489 it('Should search among account video channels', async function () {
491 const res = await getAccountVideoChannelsList({
496 expect(res.body.total).to.equal(1)
498 const channels = res.body.data
499 expect(channels).to.have.lengthOf(1)
503 const res = await getAccountVideoChannelsList({
506 search: 'does not exist'
508 expect(res.body.total).to.equal(0)
510 const channels = res.body.data
511 expect(channels).to.have.lengthOf(0)
515 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
518 await uploadVideo(servers[0].url, servers[0].accessToken, { channelId: totoChannel })
519 await waitJobs(servers)
521 for (const server of servers) {
522 const res = await getAccountVideoChannelsList({
528 const channels: VideoChannel[] = res.body.data
529 expect(channels[0].name).to.equal('toto_channel')
530 expect(channels[1].name).to.equal('root_channel')
533 await uploadVideo(servers[0].url, servers[0].accessToken, { channelId: servers[0].videoChannel.id })
534 await waitJobs(servers)
536 for (const server of servers) {
537 const res = await getAccountVideoChannelsList({
543 const channels: VideoChannel[] = res.body.data
544 expect(channels[0].name).to.equal('root_channel')
545 expect(channels[1].name).to.equal('toto_channel')
549 after(async function () {
550 await cleanupTests(servers)