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,
21 updateVideoChannelImage,
25 } from '../../../../shared/extra-utils'
29 getAccountVideoChannelsList,
33 setAccessTokensToServers,
36 } from '../../../../shared/extra-utils/index'
37 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
38 import { User, Video, VideoChannel, VideoDetails } from '../../../../shared/index'
40 const expect = chai.expect
42 async function findChannel (server: ServerInfo, channelId: number) {
43 const res = await getVideoChannelsList(server.url, 0, 5, '-name')
44 const videoChannel = res.body.data.find(c => c.id === channelId)
46 return videoChannel as VideoChannel
49 describe('Test video channels', function () {
50 let servers: ServerInfo[]
52 let secondVideoChannelId: number
53 let totoChannel: number
55 let accountName: string
57 const avatarPaths: { [ port: number ]: string } = {}
58 const bannerPaths: { [ port: number ]: string } = {}
60 before(async function () {
63 servers = await flushAndRunMultipleServers(2)
65 await setAccessTokensToServers(servers)
66 await setDefaultVideoChannel(servers)
68 await doubleFollow(servers[0], servers[1])
71 it('Should have one video channel (created with root)', async () => {
72 const res = await getVideoChannelsList(servers[0].url, 0, 2)
74 expect(res.body.total).to.equal(1)
75 expect(res.body.data).to.be.an('array')
76 expect(res.body.data).to.have.lengthOf(1)
79 it('Should create another video channel', async function () {
83 const videoChannel = {
84 name: 'second_video_channel',
85 displayName: 'second video channel',
86 description: 'super video channel description',
87 support: 'super video channel support text'
89 const res = await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
90 secondVideoChannelId = res.body.videoChannel.id
93 // The channel is 1 is propagated to servers 2
95 const videoAttributesArg = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
96 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributesArg)
97 videoUUID = res.body.video.uuid
100 await waitJobs(servers)
103 it('Should have two video channels when getting my information', async () => {
104 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
107 expect(userInfo.videoChannels).to.be.an('array')
108 expect(userInfo.videoChannels).to.have.lengthOf(2)
110 const videoChannels = userInfo.videoChannels
111 expect(videoChannels[0].name).to.equal('root_channel')
112 expect(videoChannels[0].displayName).to.equal('Main root channel')
114 expect(videoChannels[1].name).to.equal('second_video_channel')
115 expect(videoChannels[1].displayName).to.equal('second video channel')
116 expect(videoChannels[1].description).to.equal('super video channel description')
117 expect(videoChannels[1].support).to.equal('super video channel support text')
119 accountName = userInfo.account.name + '@' + userInfo.account.host
122 it('Should have two video channels when getting account channels on server 1', async function () {
123 const res = await getAccountVideoChannelsList({
128 expect(res.body.total).to.equal(2)
129 expect(res.body.data).to.be.an('array')
130 expect(res.body.data).to.have.lengthOf(2)
132 const videoChannels = res.body.data
133 expect(videoChannels[0].name).to.equal('root_channel')
134 expect(videoChannels[0].displayName).to.equal('Main root channel')
136 expect(videoChannels[1].name).to.equal('second_video_channel')
137 expect(videoChannels[1].displayName).to.equal('second video channel')
138 expect(videoChannels[1].description).to.equal('super video channel description')
139 expect(videoChannels[1].support).to.equal('super video channel support text')
142 it('Should paginate and sort account channels', async function () {
144 const res = await getAccountVideoChannelsList({
152 expect(res.body.total).to.equal(2)
153 expect(res.body.data).to.have.lengthOf(1)
155 const videoChannel: VideoChannel = res.body.data[0]
156 expect(videoChannel.name).to.equal('root_channel')
160 const res = await getAccountVideoChannelsList({
168 expect(res.body.total).to.equal(2)
169 expect(res.body.data).to.have.lengthOf(1)
171 const videoChannel: VideoChannel = res.body.data[0]
172 expect(videoChannel.name).to.equal('second_video_channel')
176 const res = await getAccountVideoChannelsList({
184 expect(res.body.total).to.equal(2)
185 expect(res.body.data).to.have.lengthOf(1)
187 const videoChannel: VideoChannel = res.body.data[0]
188 expect(videoChannel.name).to.equal('root_channel')
192 it('Should have one video channel when getting account channels on server 2', async function () {
193 const res = await getAccountVideoChannelsList({
198 expect(res.body.total).to.equal(1)
199 expect(res.body.data).to.be.an('array')
200 expect(res.body.data).to.have.lengthOf(1)
202 const videoChannels = res.body.data
203 expect(videoChannels[0].name).to.equal('second_video_channel')
204 expect(videoChannels[0].displayName).to.equal('second video channel')
205 expect(videoChannels[0].description).to.equal('super video channel description')
206 expect(videoChannels[0].support).to.equal('super video channel support text')
209 it('Should list video channels', async function () {
210 const res = await getVideoChannelsList(servers[0].url, 1, 1, '-name')
212 expect(res.body.total).to.equal(2)
213 expect(res.body.data).to.be.an('array')
214 expect(res.body.data).to.have.lengthOf(1)
215 expect(res.body.data[0].name).to.equal('root_channel')
216 expect(res.body.data[0].displayName).to.equal('Main root channel')
219 it('Should update video channel', async function () {
222 const videoChannelAttributes = {
223 displayName: 'video channel updated',
224 description: 'video channel description updated',
225 support: 'support updated'
228 await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes)
230 await waitJobs(servers)
233 it('Should have video channel updated', async function () {
234 for (const server of servers) {
235 const res = await getVideoChannelsList(server.url, 0, 1, '-name')
237 expect(res.body.total).to.equal(2)
238 expect(res.body.data).to.be.an('array')
239 expect(res.body.data).to.have.lengthOf(1)
240 expect(res.body.data[0].name).to.equal('second_video_channel')
241 expect(res.body.data[0].displayName).to.equal('video channel updated')
242 expect(res.body.data[0].description).to.equal('video channel description updated')
243 expect(res.body.data[0].support).to.equal('support updated')
247 it('Should not have updated the video support field', async function () {
248 for (const server of servers) {
249 const res = await getVideo(server.url, videoUUID)
250 const video: VideoDetails = res.body
252 expect(video.support).to.equal('video support field')
256 it('Should update the channel support field and update videos too', async function () {
259 const videoChannelAttributes = {
260 support: 'video channel support text updated',
261 bulkVideosSupportUpdate: true
264 await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes)
266 await waitJobs(servers)
268 for (const server of servers) {
269 const res = await getVideo(server.url, videoUUID)
270 const video: VideoDetails = res.body
272 expect(video.support).to.equal(videoChannelAttributes.support)
276 it('Should update video channel avatar', async function () {
279 const fixture = 'avatar.png'
281 await updateVideoChannelImage({
283 accessToken: servers[0].accessToken,
284 videoChannelName: 'second_video_channel',
289 await waitJobs(servers)
291 for (const server of servers) {
292 const videoChannel = await findChannel(server, secondVideoChannelId)
294 avatarPaths[server.port] = videoChannel.avatar.path
295 await testImage(server.url, 'avatar-resized', avatarPaths[server.port], '.png')
296 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
298 const row = await getActorImage(server.internalServerNumber, basename(avatarPaths[server.port]))
299 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height)
300 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width)
304 it('Should update video channel banner', async function () {
307 const fixture = 'banner.jpg'
309 await updateVideoChannelImage({
311 accessToken: servers[0].accessToken,
312 videoChannelName: 'second_video_channel',
317 await waitJobs(servers)
319 for (const server of servers) {
320 const res = await getVideoChannel(server.url, 'second_video_channel@' + servers[0].host)
321 const videoChannel = res.body
323 bannerPaths[server.port] = videoChannel.banner.path
324 await testImage(server.url, 'banner-resized', bannerPaths[server.port])
325 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
327 const row = await getActorImage(server.internalServerNumber, basename(bannerPaths[server.port]))
328 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height)
329 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width)
333 it('Should delete the video channel avatar', async function () {
336 await deleteVideoChannelImage({
338 accessToken: servers[0].accessToken,
339 videoChannelName: 'second_video_channel',
343 await waitJobs(servers)
345 for (const server of servers) {
346 const videoChannel = await findChannel(server, secondVideoChannelId)
347 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
349 expect(videoChannel.avatar).to.be.null
353 it('Should delete the video channel banner', async function () {
356 await deleteVideoChannelImage({
358 accessToken: servers[0].accessToken,
359 videoChannelName: 'second_video_channel',
363 await waitJobs(servers)
365 for (const server of servers) {
366 const videoChannel = await findChannel(server, secondVideoChannelId)
367 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
369 expect(videoChannel.banner).to.be.null
373 it('Should list the second video channel videos', async function () {
376 for (const server of servers) {
377 const channelURI = 'second_video_channel@localhost:' + servers[0].port
378 const res1 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
379 expect(res1.body.total).to.equal(1)
380 expect(res1.body.data).to.be.an('array')
381 expect(res1.body.data).to.have.lengthOf(1)
382 expect(res1.body.data[0].name).to.equal('my video name')
386 it('Should change the video channel of a video', async function () {
389 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { channelId: servers[0].videoChannel.id })
391 await waitJobs(servers)
394 it('Should list the first video channel videos', async function () {
397 for (const server of servers) {
398 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
399 const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondChannelURI, 0, 5)
400 expect(res1.body.total).to.equal(0)
402 const channelURI = 'root_channel@localhost:' + servers[0].port
403 const res2 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
404 expect(res2.body.total).to.equal(1)
406 const videos: Video[] = res2.body.data
407 expect(videos).to.be.an('array')
408 expect(videos).to.have.lengthOf(1)
409 expect(videos[0].name).to.equal('my video name')
413 it('Should delete video channel', async function () {
414 await deleteVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel')
417 it('Should have video channel deleted', async function () {
418 const res = await getVideoChannelsList(servers[0].url, 0, 10)
420 expect(res.body.total).to.equal(1)
421 expect(res.body.data).to.be.an('array')
422 expect(res.body.data).to.have.lengthOf(1)
423 expect(res.body.data[0].displayName).to.equal('Main root channel')
426 it('Should create the main channel with an uuid if there is a conflict', async function () {
428 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
429 const res = await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
430 totoChannel = res.body.videoChannel.id
434 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'toto', password: 'password' })
435 const accessToken = await userLogin(servers[0], { username: 'toto', password: 'password' })
437 const res = await getMyUserInformation(servers[0].url, accessToken)
438 const videoChannel = res.body.videoChannels[0]
439 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}/)
443 it('Should report correct channel views per days', async function () {
447 const res = await getAccountVideoChannelsList({
453 const channels: VideoChannel[] = res.body.data
455 for (const channel of channels) {
456 expect(channel).to.haveOwnProperty('viewsPerDay')
457 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
459 for (const v of channel.viewsPerDay) {
460 expect(v.date).to.be.an('string')
461 expect(v.views).to.equal(0)
467 // video has been posted on channel servers[0].videoChannel.id since last update
468 await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.1,127.0.0.1')
469 await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.2,127.0.0.1')
471 // Wait the repeatable job
474 const res = await getAccountVideoChannelsList({
479 const channelWithView = res.body.data.find((channel: VideoChannel) => channel.id === servers[0].videoChannel.id)
480 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
484 it('Should report correct videos count', async function () {
485 const res = await getAccountVideoChannelsList({
490 const channels: VideoChannel[] = res.body.data
492 const totoChannel = channels.find(c => c.name === 'toto_channel')
493 const rootChannel = channels.find(c => c.name === 'root_channel')
495 expect(rootChannel.videosCount).to.equal(1)
496 expect(totoChannel.videosCount).to.equal(0)
499 it('Should search among account video channels', async function () {
501 const res = await getAccountVideoChannelsList({
506 expect(res.body.total).to.equal(1)
508 const channels = res.body.data
509 expect(channels).to.have.lengthOf(1)
513 const res = await getAccountVideoChannelsList({
516 search: 'does not exist'
518 expect(res.body.total).to.equal(0)
520 const channels = res.body.data
521 expect(channels).to.have.lengthOf(0)
525 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
528 await uploadVideo(servers[0].url, servers[0].accessToken, { channelId: totoChannel })
529 await waitJobs(servers)
531 for (const server of servers) {
532 const res = await getAccountVideoChannelsList({
538 const channels: VideoChannel[] = res.body.data
539 expect(channels[0].name).to.equal('toto_channel')
540 expect(channels[1].name).to.equal('root_channel')
543 await uploadVideo(servers[0].url, servers[0].accessToken, { channelId: servers[0].videoChannel.id })
544 await waitJobs(servers)
546 for (const server of servers) {
547 const res = await getAccountVideoChannelsList({
553 const channels: VideoChannel[] = res.body.data
554 expect(channels[0].name).to.equal('root_channel')
555 expect(channels[1].name).to.equal('toto_channel')
559 after(async function () {
560 await cleanupTests(servers)