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'
11 flushAndRunMultipleServers,
13 getVideoChannelVideos,
14 setDefaultVideoChannel,
21 } from '../../../../shared/extra-utils'
22 import { getMyUserInformation, ServerInfo, setAccessTokensToServers, viewVideo } from '../../../../shared/extra-utils/index'
23 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
24 import { User, Video, VideoChannel, VideoDetails } from '../../../../shared/index'
26 const expect = chai.expect
28 async function findChannel (server: ServerInfo, channelId: number) {
29 const body = await server.channelsCommand.list({ sort: '-name' })
31 return body.data.find(c => c.id === channelId)
34 describe('Test video channels', function () {
35 let servers: ServerInfo[]
37 let secondVideoChannelId: number
38 let totoChannel: number
40 let accountName: string
42 const avatarPaths: { [ port: number ]: string } = {}
43 const bannerPaths: { [ port: number ]: string } = {}
45 before(async function () {
48 servers = await flushAndRunMultipleServers(2)
50 await setAccessTokensToServers(servers)
51 await setDefaultVideoChannel(servers)
53 await doubleFollow(servers[0], servers[1])
56 it('Should have one video channel (created with root)', async () => {
57 const body = await servers[0].channelsCommand.list({ start: 0, count: 2 })
59 expect(body.total).to.equal(1)
60 expect(body.data).to.be.an('array')
61 expect(body.data).to.have.lengthOf(1)
64 it('Should create another video channel', async function () {
68 const videoChannel = {
69 name: 'second_video_channel',
70 displayName: 'second video channel',
71 description: 'super video channel description',
72 support: 'super video channel support text'
74 const created = await servers[0].channelsCommand.create({ attributes: videoChannel })
75 secondVideoChannelId = created.id
78 // The channel is 1 is propagated to servers 2
80 const videoAttributesArg = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
81 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributesArg)
82 videoUUID = res.body.video.uuid
85 await waitJobs(servers)
88 it('Should have two video channels when getting my information', async () => {
89 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
92 expect(userInfo.videoChannels).to.be.an('array')
93 expect(userInfo.videoChannels).to.have.lengthOf(2)
95 const videoChannels = userInfo.videoChannels
96 expect(videoChannels[0].name).to.equal('root_channel')
97 expect(videoChannels[0].displayName).to.equal('Main root channel')
99 expect(videoChannels[1].name).to.equal('second_video_channel')
100 expect(videoChannels[1].displayName).to.equal('second video channel')
101 expect(videoChannels[1].description).to.equal('super video channel description')
102 expect(videoChannels[1].support).to.equal('super video channel support text')
104 accountName = userInfo.account.name + '@' + userInfo.account.host
107 it('Should have two video channels when getting account channels on server 1', async function () {
108 const body = await servers[0].channelsCommand.listByAccount({ accountName })
109 expect(body.total).to.equal(2)
111 const videoChannels = body.data
113 expect(videoChannels).to.be.an('array')
114 expect(videoChannels).to.have.lengthOf(2)
116 expect(videoChannels[0].name).to.equal('root_channel')
117 expect(videoChannels[0].displayName).to.equal('Main root channel')
119 expect(videoChannels[1].name).to.equal('second_video_channel')
120 expect(videoChannels[1].displayName).to.equal('second video channel')
121 expect(videoChannels[1].description).to.equal('super video channel description')
122 expect(videoChannels[1].support).to.equal('super video channel support text')
125 it('Should paginate and sort account channels', async function () {
127 const body = await servers[0].channelsCommand.listByAccount({
134 expect(body.total).to.equal(2)
135 expect(body.data).to.have.lengthOf(1)
137 const videoChannel: VideoChannel = body.data[0]
138 expect(videoChannel.name).to.equal('root_channel')
142 const body = await servers[0].channelsCommand.listByAccount({
149 expect(body.total).to.equal(2)
150 expect(body.data).to.have.lengthOf(1)
151 expect(body.data[0].name).to.equal('second_video_channel')
155 const body = await servers[0].channelsCommand.listByAccount({
162 expect(body.total).to.equal(2)
163 expect(body.data).to.have.lengthOf(1)
164 expect(body.data[0].name).to.equal('root_channel')
168 it('Should have one video channel when getting account channels on server 2', async function () {
169 const body = await servers[1].channelsCommand.listByAccount({ accountName })
171 expect(body.total).to.equal(1)
172 expect(body.data).to.be.an('array')
173 expect(body.data).to.have.lengthOf(1)
175 const videoChannel = body.data[0]
176 expect(videoChannel.name).to.equal('second_video_channel')
177 expect(videoChannel.displayName).to.equal('second video channel')
178 expect(videoChannel.description).to.equal('super video channel description')
179 expect(videoChannel.support).to.equal('super video channel support text')
182 it('Should list video channels', async function () {
183 const body = await servers[0].channelsCommand.list({ start: 1, count: 1, sort: '-name' })
185 expect(body.total).to.equal(2)
186 expect(body.data).to.be.an('array')
187 expect(body.data).to.have.lengthOf(1)
188 expect(body.data[0].name).to.equal('root_channel')
189 expect(body.data[0].displayName).to.equal('Main root channel')
192 it('Should update video channel', async function () {
195 const videoChannelAttributes = {
196 displayName: 'video channel updated',
197 description: 'video channel description updated',
198 support: 'support updated'
201 await servers[0].channelsCommand.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
203 await waitJobs(servers)
206 it('Should have video channel updated', async function () {
207 for (const server of servers) {
208 const body = await server.channelsCommand.list({ start: 0, count: 1, sort: '-name' })
210 expect(body.total).to.equal(2)
211 expect(body.data).to.be.an('array')
212 expect(body.data).to.have.lengthOf(1)
214 expect(body.data[0].name).to.equal('second_video_channel')
215 expect(body.data[0].displayName).to.equal('video channel updated')
216 expect(body.data[0].description).to.equal('video channel description updated')
217 expect(body.data[0].support).to.equal('support updated')
221 it('Should not have updated the video support field', async function () {
222 for (const server of servers) {
223 const res = await getVideo(server.url, videoUUID)
224 const video: VideoDetails = res.body
226 expect(video.support).to.equal('video support field')
230 it('Should update the channel support field and update videos too', async function () {
233 const videoChannelAttributes = {
234 support: 'video channel support text updated',
235 bulkVideosSupportUpdate: true
238 await servers[0].channelsCommand.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
240 await waitJobs(servers)
242 for (const server of servers) {
243 const res = await getVideo(server.url, videoUUID)
244 const video: VideoDetails = res.body
246 expect(video.support).to.equal(videoChannelAttributes.support)
250 it('Should update video channel avatar', async function () {
253 const fixture = 'avatar.png'
255 await servers[0].channelsCommand.updateImage({
256 channelName: 'second_video_channel',
261 await waitJobs(servers)
263 for (const server of servers) {
264 const videoChannel = await findChannel(server, secondVideoChannelId)
266 avatarPaths[server.port] = videoChannel.avatar.path
267 await testImage(server.url, 'avatar-resized', avatarPaths[server.port], '.png')
268 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
270 const row = await server.sqlCommand.getActorImage(basename(avatarPaths[server.port]))
271 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height)
272 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width)
276 it('Should update video channel banner', async function () {
279 const fixture = 'banner.jpg'
281 await servers[0].channelsCommand.updateImage({
282 channelName: 'second_video_channel',
287 await waitJobs(servers)
289 for (const server of servers) {
290 const videoChannel = await server.channelsCommand.get({ channelName: 'second_video_channel@' + servers[0].host })
292 bannerPaths[server.port] = videoChannel.banner.path
293 await testImage(server.url, 'banner-resized', bannerPaths[server.port])
294 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
296 const row = await server.sqlCommand.getActorImage(basename(bannerPaths[server.port]))
297 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height)
298 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width)
302 it('Should delete the video channel avatar', async function () {
305 await servers[0].channelsCommand.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
307 await waitJobs(servers)
309 for (const server of servers) {
310 const videoChannel = await findChannel(server, secondVideoChannelId)
311 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
313 expect(videoChannel.avatar).to.be.null
317 it('Should delete the video channel banner', async function () {
320 await servers[0].channelsCommand.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
322 await waitJobs(servers)
324 for (const server of servers) {
325 const videoChannel = await findChannel(server, secondVideoChannelId)
326 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
328 expect(videoChannel.banner).to.be.null
332 it('Should list the second video channel videos', async function () {
335 for (const server of servers) {
336 const channelURI = 'second_video_channel@localhost:' + servers[0].port
337 const res1 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
338 expect(res1.body.total).to.equal(1)
339 expect(res1.body.data).to.be.an('array')
340 expect(res1.body.data).to.have.lengthOf(1)
341 expect(res1.body.data[0].name).to.equal('my video name')
345 it('Should change the video channel of a video', async function () {
348 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { channelId: servers[0].videoChannel.id })
350 await waitJobs(servers)
353 it('Should list the first video channel videos', async function () {
356 for (const server of servers) {
357 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
358 const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondChannelURI, 0, 5)
359 expect(res1.body.total).to.equal(0)
361 const channelURI = 'root_channel@localhost:' + servers[0].port
362 const res2 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
363 expect(res2.body.total).to.equal(1)
365 const videos: Video[] = res2.body.data
366 expect(videos).to.be.an('array')
367 expect(videos).to.have.lengthOf(1)
368 expect(videos[0].name).to.equal('my video name')
372 it('Should delete video channel', async function () {
373 await servers[0].channelsCommand.delete({ channelName: 'second_video_channel' })
376 it('Should have video channel deleted', async function () {
377 const body = await servers[0].channelsCommand.list({ start: 0, count: 10 })
379 expect(body.total).to.equal(1)
380 expect(body.data).to.be.an('array')
381 expect(body.data).to.have.lengthOf(1)
382 expect(body.data[0].displayName).to.equal('Main root channel')
385 it('Should create the main channel with an uuid if there is a conflict', async function () {
387 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
388 const created = await servers[0].channelsCommand.create({ attributes: videoChannel })
389 totoChannel = created.id
393 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'toto', password: 'password' })
394 const accessToken = await userLogin(servers[0], { username: 'toto', password: 'password' })
396 const res = await getMyUserInformation(servers[0].url, accessToken)
397 const videoChannel = res.body.videoChannels[0]
398 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}/)
402 it('Should report correct channel views per days', async function () {
406 const { data } = await servers[0].channelsCommand.listByAccount({ accountName, withStats: true })
408 for (const channel of data) {
409 expect(channel).to.haveOwnProperty('viewsPerDay')
410 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
412 for (const v of channel.viewsPerDay) {
413 expect(v.date).to.be.an('string')
414 expect(v.views).to.equal(0)
420 // video has been posted on channel servers[0].videoChannel.id since last update
421 await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.1,127.0.0.1')
422 await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.2,127.0.0.1')
424 // Wait the repeatable job
427 const { data } = await servers[0].channelsCommand.listByAccount({ accountName, withStats: true })
428 const channelWithView = data.find(channel => channel.id === servers[0].videoChannel.id)
429 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
433 it('Should report correct videos count', async function () {
434 const { data } = await servers[0].channelsCommand.listByAccount({ accountName, withStats: true })
436 const totoChannel = data.find(c => c.name === 'toto_channel')
437 const rootChannel = data.find(c => c.name === 'root_channel')
439 expect(rootChannel.videosCount).to.equal(1)
440 expect(totoChannel.videosCount).to.equal(0)
443 it('Should search among account video channels', async function () {
445 const body = await servers[0].channelsCommand.listByAccount({ accountName, search: 'root' })
446 expect(body.total).to.equal(1)
448 const channels = body.data
449 expect(channels).to.have.lengthOf(1)
453 const body = await servers[0].channelsCommand.listByAccount({ accountName, search: 'does not exist' })
454 expect(body.total).to.equal(0)
456 const channels = body.data
457 expect(channels).to.have.lengthOf(0)
461 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
464 await uploadVideo(servers[0].url, servers[0].accessToken, { channelId: totoChannel })
465 await waitJobs(servers)
467 for (const server of servers) {
468 const { data } = await server.channelsCommand.listByAccount({ accountName, sort: '-updatedAt' })
470 expect(data[0].name).to.equal('toto_channel')
471 expect(data[1].name).to.equal('root_channel')
474 await uploadVideo(servers[0].url, servers[0].accessToken, { channelId: servers[0].videoChannel.id })
475 await waitJobs(servers)
477 for (const server of servers) {
478 const { data } = await server.channelsCommand.listByAccount({ accountName, sort: '-updatedAt' })
480 expect(data[0].name).to.equal('root_channel')
481 expect(data[1].name).to.equal('toto_channel')
485 after(async function () {
486 await cleanupTests(servers)