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'
7 import { testFileExistsOrNot, testImage } from '@server/tests/shared'
8 import { wait } from '@shared/core-utils'
9 import { User, VideoChannel } from '@shared/models'
12 createMultipleServers,
15 setAccessTokensToServers,
16 setDefaultVideoChannel,
18 } from '@shared/server-commands'
20 const expect = chai.expect
22 async function findChannel (server: PeerTubeServer, channelId: number) {
23 const body = await server.channels.list({ sort: '-name' })
25 return body.data.find(c => c.id === channelId)
28 describe('Test video channels', function () {
29 let servers: PeerTubeServer[]
31 let secondVideoChannelId: number
32 let totoChannel: number
34 let accountName: string
35 let secondUserChannelName: string
37 const avatarPaths: { [ port: number ]: string } = {}
38 const bannerPaths: { [ port: number ]: string } = {}
40 before(async function () {
43 servers = await createMultipleServers(2)
45 await setAccessTokensToServers(servers)
46 await setDefaultVideoChannel(servers)
48 await doubleFollow(servers[0], servers[1])
51 it('Should have one video channel (created with root)', async () => {
52 const body = await servers[0].channels.list({ start: 0, count: 2 })
54 expect(body.total).to.equal(1)
55 expect(body.data).to.be.an('array')
56 expect(body.data).to.have.lengthOf(1)
59 it('Should create another video channel', async function () {
63 const videoChannel = {
64 name: 'second_video_channel',
65 displayName: 'second video channel',
66 description: 'super video channel description',
67 support: 'super video channel support text'
69 const created = await servers[0].channels.create({ attributes: videoChannel })
70 secondVideoChannelId = created.id
73 // The channel is 1 is propagated to servers 2
75 const attributes = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
76 const { uuid } = await servers[0].videos.upload({ attributes })
80 await waitJobs(servers)
83 it('Should have two video channels when getting my information', async () => {
84 userInfo = await servers[0].users.getMyInfo()
86 expect(userInfo.videoChannels).to.be.an('array')
87 expect(userInfo.videoChannels).to.have.lengthOf(2)
89 const videoChannels = userInfo.videoChannels
90 expect(videoChannels[0].name).to.equal('root_channel')
91 expect(videoChannels[0].displayName).to.equal('Main root channel')
93 expect(videoChannels[1].name).to.equal('second_video_channel')
94 expect(videoChannels[1].displayName).to.equal('second video channel')
95 expect(videoChannels[1].description).to.equal('super video channel description')
96 expect(videoChannels[1].support).to.equal('super video channel support text')
98 accountName = userInfo.account.name + '@' + userInfo.account.host
101 it('Should have two video channels when getting account channels on server 1', async function () {
102 const body = await servers[0].channels.listByAccount({ accountName })
103 expect(body.total).to.equal(2)
105 const videoChannels = body.data
107 expect(videoChannels).to.be.an('array')
108 expect(videoChannels).to.have.lengthOf(2)
110 expect(videoChannels[0].name).to.equal('root_channel')
111 expect(videoChannels[0].displayName).to.equal('Main root channel')
113 expect(videoChannels[1].name).to.equal('second_video_channel')
114 expect(videoChannels[1].displayName).to.equal('second video channel')
115 expect(videoChannels[1].description).to.equal('super video channel description')
116 expect(videoChannels[1].support).to.equal('super video channel support text')
119 it('Should paginate and sort account channels', async function () {
121 const body = await servers[0].channels.listByAccount({
128 expect(body.total).to.equal(2)
129 expect(body.data).to.have.lengthOf(1)
131 const videoChannel: VideoChannel = body.data[0]
132 expect(videoChannel.name).to.equal('root_channel')
136 const body = await servers[0].channels.listByAccount({
143 expect(body.total).to.equal(2)
144 expect(body.data).to.have.lengthOf(1)
145 expect(body.data[0].name).to.equal('second_video_channel')
149 const body = await servers[0].channels.listByAccount({
156 expect(body.total).to.equal(2)
157 expect(body.data).to.have.lengthOf(1)
158 expect(body.data[0].name).to.equal('root_channel')
162 it('Should have one video channel when getting account channels on server 2', async function () {
163 const body = await servers[1].channels.listByAccount({ accountName })
165 expect(body.total).to.equal(1)
166 expect(body.data).to.be.an('array')
167 expect(body.data).to.have.lengthOf(1)
169 const videoChannel = body.data[0]
170 expect(videoChannel.name).to.equal('second_video_channel')
171 expect(videoChannel.displayName).to.equal('second video channel')
172 expect(videoChannel.description).to.equal('super video channel description')
173 expect(videoChannel.support).to.equal('super video channel support text')
176 it('Should list video channels', async function () {
177 const body = await servers[0].channels.list({ start: 1, count: 1, sort: '-name' })
179 expect(body.total).to.equal(2)
180 expect(body.data).to.be.an('array')
181 expect(body.data).to.have.lengthOf(1)
182 expect(body.data[0].name).to.equal('root_channel')
183 expect(body.data[0].displayName).to.equal('Main root channel')
186 it('Should update video channel', async function () {
189 const videoChannelAttributes = {
190 displayName: 'video channel updated',
191 description: 'video channel description updated',
192 support: 'support updated'
195 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
197 await waitJobs(servers)
200 it('Should have video channel updated', async function () {
201 for (const server of servers) {
202 const body = await server.channels.list({ start: 0, count: 1, sort: '-name' })
204 expect(body.total).to.equal(2)
205 expect(body.data).to.be.an('array')
206 expect(body.data).to.have.lengthOf(1)
208 expect(body.data[0].name).to.equal('second_video_channel')
209 expect(body.data[0].displayName).to.equal('video channel updated')
210 expect(body.data[0].description).to.equal('video channel description updated')
211 expect(body.data[0].support).to.equal('support updated')
215 it('Should not have updated the video support field', async function () {
216 for (const server of servers) {
217 const video = await server.videos.get({ id: videoUUID })
218 expect(video.support).to.equal('video support field')
222 it('Should update another accounts video channel', async function () {
225 const result = await servers[0].users.generate('second_user')
226 secondUserChannelName = result.userChannelName
228 await servers[0].videos.quickUpload({ name: 'video', token: result.token })
230 const videoChannelAttributes = {
231 displayName: 'video channel updated',
232 description: 'video channel description updated',
233 support: 'support updated'
236 await servers[0].channels.update({ channelName: secondUserChannelName, attributes: videoChannelAttributes })
238 await waitJobs(servers)
241 it('Should have another accounts video channel updated', async function () {
242 for (const server of servers) {
243 const body = await server.channels.get({ channelName: `${secondUserChannelName}@${servers[0].host}` })
245 expect(body.displayName).to.equal('video channel updated')
246 expect(body.description).to.equal('video channel description updated')
247 expect(body.support).to.equal('support updated')
251 it('Should update the channel support field and update videos too', async function () {
254 const videoChannelAttributes = {
255 support: 'video channel support text updated',
256 bulkVideosSupportUpdate: true
259 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
261 await waitJobs(servers)
263 for (const server of servers) {
264 const video = await server.videos.get({ id: videoUUID })
265 expect(video.support).to.equal(videoChannelAttributes.support)
269 it('Should update video channel avatar', async function () {
272 const fixture = 'avatar.png'
274 await servers[0].channels.updateImage({
275 channelName: 'second_video_channel',
280 await waitJobs(servers)
282 for (const server of servers) {
283 const videoChannel = await findChannel(server, secondVideoChannelId)
285 avatarPaths[server.port] = videoChannel.avatar.path
286 await testImage(server.url, 'avatar-resized', avatarPaths[server.port], '.png')
287 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
289 const row = await server.sql.getActorImage(basename(avatarPaths[server.port]))
290 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height)
291 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width)
295 it('Should update video channel banner', async function () {
298 const fixture = 'banner.jpg'
300 await servers[0].channels.updateImage({
301 channelName: 'second_video_channel',
306 await waitJobs(servers)
308 for (const server of servers) {
309 const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
311 bannerPaths[server.port] = videoChannel.banner.path
312 await testImage(server.url, 'banner-resized', bannerPaths[server.port])
313 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
315 const row = await server.sql.getActorImage(basename(bannerPaths[server.port]))
316 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height)
317 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width)
321 it('Should delete the video channel avatar', async function () {
324 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
326 await waitJobs(servers)
328 for (const server of servers) {
329 const videoChannel = await findChannel(server, secondVideoChannelId)
330 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
332 expect(videoChannel.avatar).to.be.null
336 it('Should delete the video channel banner', async function () {
339 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
341 await waitJobs(servers)
343 for (const server of servers) {
344 const videoChannel = await findChannel(server, secondVideoChannelId)
345 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
347 expect(videoChannel.banner).to.be.null
351 it('Should list the second video channel videos', async function () {
354 for (const server of servers) {
355 const channelURI = 'second_video_channel@localhost:' + servers[0].port
356 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
358 expect(total).to.equal(1)
359 expect(data).to.be.an('array')
360 expect(data).to.have.lengthOf(1)
361 expect(data[0].name).to.equal('my video name')
365 it('Should change the video channel of a video', async function () {
368 await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
370 await waitJobs(servers)
373 it('Should list the first video channel videos', async function () {
376 for (const server of servers) {
378 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
379 const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
380 expect(total).to.equal(0)
384 const channelURI = 'root_channel@localhost:' + servers[0].port
385 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
386 expect(total).to.equal(1)
388 expect(data).to.be.an('array')
389 expect(data).to.have.lengthOf(1)
390 expect(data[0].name).to.equal('my video name')
395 it('Should delete video channel', async function () {
396 await servers[0].channels.delete({ channelName: 'second_video_channel' })
399 it('Should have video channel deleted', async function () {
400 const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
402 expect(body.total).to.equal(2)
403 expect(body.data).to.be.an('array')
404 expect(body.data).to.have.lengthOf(2)
405 expect(body.data[0].displayName).to.equal('Main root channel')
406 expect(body.data[1].displayName).to.equal('video channel updated')
409 it('Should create the main channel with an uuid if there is a conflict', async function () {
411 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
412 const created = await servers[0].channels.create({ attributes: videoChannel })
413 totoChannel = created.id
417 await servers[0].users.create({ username: 'toto', password: 'password' })
418 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
420 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
421 const videoChannel = videoChannels[0]
422 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}/)
426 it('Should report correct channel views per days', async function () {
430 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
432 for (const channel of data) {
433 expect(channel).to.haveOwnProperty('viewsPerDay')
434 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
436 for (const v of channel.viewsPerDay) {
437 expect(v.date).to.be.an('string')
438 expect(v.views).to.equal(0)
444 // video has been posted on channel servers[0].store.videoChannel.id since last update
445 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
446 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
448 // Wait the repeatable job
451 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
452 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
453 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
457 it('Should report correct videos count', async function () {
458 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
460 const totoChannel = data.find(c => c.name === 'toto_channel')
461 const rootChannel = data.find(c => c.name === 'root_channel')
463 expect(rootChannel.videosCount).to.equal(1)
464 expect(totoChannel.videosCount).to.equal(0)
467 it('Should search among account video channels', async function () {
469 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
470 expect(body.total).to.equal(1)
472 const channels = body.data
473 expect(channels).to.have.lengthOf(1)
477 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
478 expect(body.total).to.equal(0)
480 const channels = body.data
481 expect(channels).to.have.lengthOf(0)
485 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
488 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
489 await waitJobs(servers)
491 for (const server of servers) {
492 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
494 expect(data[0].name).to.equal('toto_channel')
495 expect(data[1].name).to.equal('root_channel')
498 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
499 await waitJobs(servers)
501 for (const server of servers) {
502 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
504 expect(data[0].name).to.equal('root_channel')
505 expect(data[1].name).to.equal('toto_channel')
509 after(async function () {
510 await cleanupTests(servers)