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,
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'
25 const expect = chai.expect
27 async function findChannel (server: ServerInfo, channelId: number) {
28 const body = await server.channelsCommand.list({ sort: '-name' })
30 return body.data.find(c => c.id === channelId)
33 describe('Test video channels', function () {
34 let servers: ServerInfo[]
36 let secondVideoChannelId: number
37 let totoChannel: number
39 let accountName: string
41 const avatarPaths: { [ port: number ]: string } = {}
42 const bannerPaths: { [ port: number ]: string } = {}
44 before(async function () {
47 servers = await flushAndRunMultipleServers(2)
49 await setAccessTokensToServers(servers)
50 await setDefaultVideoChannel(servers)
52 await doubleFollow(servers[0], servers[1])
55 it('Should have one video channel (created with root)', async () => {
56 const body = await servers[0].channelsCommand.list({ start: 0, count: 2 })
58 expect(body.total).to.equal(1)
59 expect(body.data).to.be.an('array')
60 expect(body.data).to.have.lengthOf(1)
63 it('Should create another video channel', async function () {
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'
73 const created = await servers[0].channelsCommand.create({ attributes: videoChannel })
74 secondVideoChannelId = created.id
77 // The channel is 1 is propagated to servers 2
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
84 await waitJobs(servers)
87 it('Should have two video channels when getting my information', async () => {
88 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
91 expect(userInfo.videoChannels).to.be.an('array')
92 expect(userInfo.videoChannels).to.have.lengthOf(2)
94 const videoChannels = userInfo.videoChannels
95 expect(videoChannels[0].name).to.equal('root_channel')
96 expect(videoChannels[0].displayName).to.equal('Main root channel')
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')
103 accountName = userInfo.account.name + '@' + userInfo.account.host
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)
110 const videoChannels = body.data
112 expect(videoChannels).to.be.an('array')
113 expect(videoChannels).to.have.lengthOf(2)
115 expect(videoChannels[0].name).to.equal('root_channel')
116 expect(videoChannels[0].displayName).to.equal('Main root channel')
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')
124 it('Should paginate and sort account channels', async function () {
126 const body = await servers[0].channelsCommand.listByAccount({
133 expect(body.total).to.equal(2)
134 expect(body.data).to.have.lengthOf(1)
136 const videoChannel: VideoChannel = body.data[0]
137 expect(videoChannel.name).to.equal('root_channel')
141 const body = await servers[0].channelsCommand.listByAccount({
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')
154 const body = await servers[0].channelsCommand.listByAccount({
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')
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 })
170 expect(body.total).to.equal(1)
171 expect(body.data).to.be.an('array')
172 expect(body.data).to.have.lengthOf(1)
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')
181 it('Should list video channels', async function () {
182 const body = await servers[0].channelsCommand.list({ start: 1, count: 1, sort: '-name' })
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')
191 it('Should update video channel', async function () {
194 const videoChannelAttributes = {
195 displayName: 'video channel updated',
196 description: 'video channel description updated',
197 support: 'support updated'
200 await servers[0].channelsCommand.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
202 await waitJobs(servers)
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' })
209 expect(body.total).to.equal(2)
210 expect(body.data).to.be.an('array')
211 expect(body.data).to.have.lengthOf(1)
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')
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
225 expect(video.support).to.equal('video support field')
229 it('Should update the channel support field and update videos too', async function () {
232 const videoChannelAttributes = {
233 support: 'video channel support text updated',
234 bulkVideosSupportUpdate: true
237 await servers[0].channelsCommand.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
239 await waitJobs(servers)
241 for (const server of servers) {
242 const res = await getVideo(server.url, videoUUID)
243 const video: VideoDetails = res.body
245 expect(video.support).to.equal(videoChannelAttributes.support)
249 it('Should update video channel avatar', async function () {
252 const fixture = 'avatar.png'
254 await servers[0].channelsCommand.updateImage({
255 channelName: 'second_video_channel',
260 await waitJobs(servers)
262 for (const server of servers) {
263 const videoChannel = await findChannel(server, secondVideoChannelId)
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)
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)
275 it('Should update video channel banner', async function () {
278 const fixture = 'banner.jpg'
280 await servers[0].channelsCommand.updateImage({
281 channelName: 'second_video_channel',
286 await waitJobs(servers)
288 for (const server of servers) {
289 const videoChannel = await server.channelsCommand.get({ channelName: 'second_video_channel@' + servers[0].host })
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)
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)
301 it('Should delete the video channel avatar', async function () {
304 await servers[0].channelsCommand.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
306 await waitJobs(servers)
308 for (const server of servers) {
309 const videoChannel = await findChannel(server, secondVideoChannelId)
310 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
312 expect(videoChannel.avatar).to.be.null
316 it('Should delete the video channel banner', async function () {
319 await servers[0].channelsCommand.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
321 await waitJobs(servers)
323 for (const server of servers) {
324 const videoChannel = await findChannel(server, secondVideoChannelId)
325 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
327 expect(videoChannel.banner).to.be.null
331 it('Should list the second video channel videos', async function () {
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')
344 it('Should change the video channel of a video', async function () {
347 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { channelId: servers[0].videoChannel.id })
349 await waitJobs(servers)
352 it('Should list the first video channel videos', async function () {
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)
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)
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')
371 it('Should delete video channel', async function () {
372 await servers[0].channelsCommand.delete({ channelName: 'second_video_channel' })
375 it('Should have video channel deleted', async function () {
376 const body = await servers[0].channelsCommand.list({ start: 0, count: 10 })
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')
384 it('Should create the main channel with an uuid if there is a conflict', async function () {
386 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
387 const created = await servers[0].channelsCommand.create({ attributes: videoChannel })
388 totoChannel = created.id
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' })
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}/)
401 it('Should report correct channel views per days', async function () {
405 const { data } = await servers[0].channelsCommand.listByAccount({ accountName, withStats: true })
407 for (const channel of data) {
408 expect(channel).to.haveOwnProperty('viewsPerDay')
409 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
411 for (const v of channel.viewsPerDay) {
412 expect(v.date).to.be.an('string')
413 expect(v.views).to.equal(0)
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')
423 // Wait the repeatable job
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)
432 it('Should report correct videos count', async function () {
433 const { data } = await servers[0].channelsCommand.listByAccount({ accountName, withStats: true })
435 const totoChannel = data.find(c => c.name === 'toto_channel')
436 const rootChannel = data.find(c => c.name === 'root_channel')
438 expect(rootChannel.videosCount).to.equal(1)
439 expect(totoChannel.videosCount).to.equal(0)
442 it('Should search among account video channels', async function () {
444 const body = await servers[0].channelsCommand.listByAccount({ accountName, search: 'root' })
445 expect(body.total).to.equal(1)
447 const channels = body.data
448 expect(channels).to.have.lengthOf(1)
452 const body = await servers[0].channelsCommand.listByAccount({ accountName, search: 'does not exist' })
453 expect(body.total).to.equal(0)
455 const channels = body.data
456 expect(channels).to.have.lengthOf(0)
460 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
463 await uploadVideo(servers[0].url, servers[0].accessToken, { channelId: totoChannel })
464 await waitJobs(servers)
466 for (const server of servers) {
467 const { data } = await server.channelsCommand.listByAccount({ accountName, sort: '-updatedAt' })
469 expect(data[0].name).to.equal('toto_channel')
470 expect(data[1].name).to.equal('root_channel')
473 await uploadVideo(servers[0].url, servers[0].accessToken, { channelId: servers[0].videoChannel.id })
474 await waitJobs(servers)
476 for (const server of servers) {
477 const { data } = await server.channelsCommand.listByAccount({ accountName, sort: '-updatedAt' })
479 expect(data[0].name).to.equal('root_channel')
480 expect(data[1].name).to.equal('toto_channel')
484 after(async function () {
485 await cleanupTests(servers)