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 { ActorImageType, User, VideoChannel } from '@shared/models'
12 createMultipleServers,
15 setAccessTokensToServers,
16 setDefaultAccountAvatar,
17 setDefaultVideoChannel,
19 } from '@shared/server-commands'
21 const expect = chai.expect
23 async function findChannel (server: PeerTubeServer, channelId: number) {
24 const body = await server.channels.list({ sort: '-name' })
26 return body.data.find(c => c.id === channelId)
29 describe('Test video channels', function () {
30 let servers: PeerTubeServer[]
32 let secondVideoChannelId: number
33 let totoChannel: number
35 let accountName: string
36 let secondUserChannelName: string
38 const avatarPaths: { [ port: number ]: string } = {}
39 const bannerPaths: { [ port: number ]: string } = {}
41 before(async function () {
44 servers = await createMultipleServers(2)
46 await setAccessTokensToServers(servers)
47 await setDefaultVideoChannel(servers)
48 await setDefaultAccountAvatar(servers)
50 await doubleFollow(servers[0], servers[1])
53 it('Should have one video channel (created with root)', async () => {
54 const body = await servers[0].channels.list({ start: 0, count: 2 })
56 expect(body.total).to.equal(1)
57 expect(body.data).to.be.an('array')
58 expect(body.data).to.have.lengthOf(1)
61 it('Should create another video channel', async function () {
65 const videoChannel = {
66 name: 'second_video_channel',
67 displayName: 'second video channel',
68 description: 'super video channel description',
69 support: 'super video channel support text'
71 const created = await servers[0].channels.create({ attributes: videoChannel })
72 secondVideoChannelId = created.id
75 // The channel is 1 is propagated to servers 2
77 const attributes = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
78 const { uuid } = await servers[0].videos.upload({ attributes })
82 await waitJobs(servers)
85 it('Should have two video channels when getting my information', async () => {
86 userInfo = await servers[0].users.getMyInfo()
88 expect(userInfo.videoChannels).to.be.an('array')
89 expect(userInfo.videoChannels).to.have.lengthOf(2)
91 const videoChannels = userInfo.videoChannels
92 expect(videoChannels[0].name).to.equal('root_channel')
93 expect(videoChannels[0].displayName).to.equal('Main root channel')
95 expect(videoChannels[1].name).to.equal('second_video_channel')
96 expect(videoChannels[1].displayName).to.equal('second video channel')
97 expect(videoChannels[1].description).to.equal('super video channel description')
98 expect(videoChannels[1].support).to.equal('super video channel support text')
100 accountName = userInfo.account.name + '@' + userInfo.account.host
103 it('Should have two video channels when getting account channels on server 1', async function () {
104 const body = await servers[0].channels.listByAccount({ accountName })
105 expect(body.total).to.equal(2)
107 const videoChannels = body.data
109 expect(videoChannels).to.be.an('array')
110 expect(videoChannels).to.have.lengthOf(2)
112 expect(videoChannels[0].name).to.equal('root_channel')
113 expect(videoChannels[0].displayName).to.equal('Main root channel')
115 expect(videoChannels[1].name).to.equal('second_video_channel')
116 expect(videoChannels[1].displayName).to.equal('second video channel')
117 expect(videoChannels[1].description).to.equal('super video channel description')
118 expect(videoChannels[1].support).to.equal('super video channel support text')
121 it('Should paginate and sort account channels', async function () {
123 const body = await servers[0].channels.listByAccount({
130 expect(body.total).to.equal(2)
131 expect(body.data).to.have.lengthOf(1)
133 const videoChannel: VideoChannel = body.data[0]
134 expect(videoChannel.name).to.equal('root_channel')
138 const body = await servers[0].channels.listByAccount({
145 expect(body.total).to.equal(2)
146 expect(body.data).to.have.lengthOf(1)
147 expect(body.data[0].name).to.equal('second_video_channel')
151 const body = await servers[0].channels.listByAccount({
158 expect(body.total).to.equal(2)
159 expect(body.data).to.have.lengthOf(1)
160 expect(body.data[0].name).to.equal('root_channel')
164 it('Should have one video channel when getting account channels on server 2', async function () {
165 const body = await servers[1].channels.listByAccount({ accountName })
167 expect(body.total).to.equal(1)
168 expect(body.data).to.be.an('array')
169 expect(body.data).to.have.lengthOf(1)
171 const videoChannel = body.data[0]
172 expect(videoChannel.name).to.equal('second_video_channel')
173 expect(videoChannel.displayName).to.equal('second video channel')
174 expect(videoChannel.description).to.equal('super video channel description')
175 expect(videoChannel.support).to.equal('super video channel support text')
178 it('Should list video channels', async function () {
179 const body = await servers[0].channels.list({ start: 1, count: 1, sort: '-name' })
181 expect(body.total).to.equal(2)
182 expect(body.data).to.be.an('array')
183 expect(body.data).to.have.lengthOf(1)
184 expect(body.data[0].name).to.equal('root_channel')
185 expect(body.data[0].displayName).to.equal('Main root channel')
188 it('Should update video channel', async function () {
191 const videoChannelAttributes = {
192 displayName: 'video channel updated',
193 description: 'video channel description updated',
194 support: 'support updated'
197 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
199 await waitJobs(servers)
202 it('Should have video channel updated', async function () {
203 for (const server of servers) {
204 const body = await server.channels.list({ start: 0, count: 1, sort: '-name' })
206 expect(body.total).to.equal(2)
207 expect(body.data).to.be.an('array')
208 expect(body.data).to.have.lengthOf(1)
210 expect(body.data[0].name).to.equal('second_video_channel')
211 expect(body.data[0].displayName).to.equal('video channel updated')
212 expect(body.data[0].description).to.equal('video channel description updated')
213 expect(body.data[0].support).to.equal('support updated')
217 it('Should not have updated the video support field', async function () {
218 for (const server of servers) {
219 const video = await server.videos.get({ id: videoUUID })
220 expect(video.support).to.equal('video support field')
224 it('Should update another accounts video channel', async function () {
227 const result = await servers[0].users.generate('second_user')
228 secondUserChannelName = result.userChannelName
230 await servers[0].videos.quickUpload({ name: 'video', token: result.token })
232 const videoChannelAttributes = {
233 displayName: 'video channel updated',
234 description: 'video channel description updated',
235 support: 'support updated'
238 await servers[0].channels.update({ channelName: secondUserChannelName, attributes: videoChannelAttributes })
240 await waitJobs(servers)
243 it('Should have another accounts video channel updated', async function () {
244 for (const server of servers) {
245 const body = await server.channels.get({ channelName: `${secondUserChannelName}@${servers[0].host}` })
247 expect(body.displayName).to.equal('video channel updated')
248 expect(body.description).to.equal('video channel description updated')
249 expect(body.support).to.equal('support updated')
253 it('Should update the channel support field and update videos too', async function () {
256 const videoChannelAttributes = {
257 support: 'video channel support text updated',
258 bulkVideosSupportUpdate: true
261 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
263 await waitJobs(servers)
265 for (const server of servers) {
266 const video = await server.videos.get({ id: videoUUID })
267 expect(video.support).to.equal(videoChannelAttributes.support)
271 it('Should update video channel avatar', async function () {
274 const fixture = 'avatar.png'
276 await servers[0].channels.updateImage({
277 channelName: 'second_video_channel',
282 await waitJobs(servers)
284 for (const server of servers) {
285 const videoChannel = await findChannel(server, secondVideoChannelId)
286 const expectedSizes = ACTOR_IMAGES_SIZE[ActorImageType.AVATAR]
288 expect(videoChannel.avatars.length).to.equal(expectedSizes.length, 'Expected avatars to be generated in all sizes')
290 for (const avatar of videoChannel.avatars) {
291 avatarPaths[server.port] = avatar.path
292 await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatarPaths[server.port], '.png')
293 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
295 const row = await server.sql.getActorImage(basename(avatarPaths[server.port]))
297 expect(expectedSizes.some(({ height, width }) => row.height === height && row.width === width)).to.equal(true)
302 it('Should update video channel banner', async function () {
305 const fixture = 'banner.jpg'
307 await servers[0].channels.updateImage({
308 channelName: 'second_video_channel',
313 await waitJobs(servers)
315 for (const server of servers) {
316 const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
318 bannerPaths[server.port] = videoChannel.banners[0].path
319 await testImage(server.url, 'banner-resized', bannerPaths[server.port])
320 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
322 const row = await server.sql.getActorImage(basename(bannerPaths[server.port]))
323 expect(row.height).to.equal(ACTOR_IMAGES_SIZE[ActorImageType.BANNER][0].height)
324 expect(row.width).to.equal(ACTOR_IMAGES_SIZE[ActorImageType.BANNER][0].width)
328 it('Should delete the video channel avatar', async function () {
330 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
332 await waitJobs(servers)
334 for (const server of servers) {
335 const videoChannel = await findChannel(server, secondVideoChannelId)
336 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
338 expect(videoChannel.avatars).to.be.empty
342 it('Should delete the video channel banner', async function () {
345 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
347 await waitJobs(servers)
349 for (const server of servers) {
350 const videoChannel = await findChannel(server, secondVideoChannelId)
351 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
353 expect(videoChannel.banners).to.be.empty
357 it('Should list the second video channel videos', async function () {
360 for (const server of servers) {
361 const channelURI = 'second_video_channel@localhost:' + servers[0].port
362 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
364 expect(total).to.equal(1)
365 expect(data).to.be.an('array')
366 expect(data).to.have.lengthOf(1)
367 expect(data[0].name).to.equal('my video name')
371 it('Should change the video channel of a video', async function () {
374 await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
376 await waitJobs(servers)
379 it('Should list the first video channel videos', async function () {
382 for (const server of servers) {
384 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
385 const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
386 expect(total).to.equal(0)
390 const channelURI = 'root_channel@localhost:' + servers[0].port
391 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
392 expect(total).to.equal(1)
394 expect(data).to.be.an('array')
395 expect(data).to.have.lengthOf(1)
396 expect(data[0].name).to.equal('my video name')
401 it('Should delete video channel', async function () {
402 await servers[0].channels.delete({ channelName: 'second_video_channel' })
405 it('Should have video channel deleted', async function () {
406 const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
408 expect(body.total).to.equal(2)
409 expect(body.data).to.be.an('array')
410 expect(body.data).to.have.lengthOf(2)
411 expect(body.data[0].displayName).to.equal('Main root channel')
412 expect(body.data[1].displayName).to.equal('video channel updated')
415 it('Should create the main channel with an uuid if there is a conflict', async function () {
417 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
418 const created = await servers[0].channels.create({ attributes: videoChannel })
419 totoChannel = created.id
423 await servers[0].users.create({ username: 'toto', password: 'password' })
424 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
426 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
427 const videoChannel = videoChannels[0]
428 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}/)
432 it('Should report correct channel views per days', async function () {
436 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
438 for (const channel of data) {
439 expect(channel).to.haveOwnProperty('viewsPerDay')
440 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
442 for (const v of channel.viewsPerDay) {
443 expect(v.date).to.be.an('string')
444 expect(v.views).to.equal(0)
450 // video has been posted on channel servers[0].store.videoChannel.id since last update
451 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
452 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
454 // Wait the repeatable job
457 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
458 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
459 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
463 it('Should report correct videos count', async function () {
464 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
466 const totoChannel = data.find(c => c.name === 'toto_channel')
467 const rootChannel = data.find(c => c.name === 'root_channel')
469 expect(rootChannel.videosCount).to.equal(1)
470 expect(totoChannel.videosCount).to.equal(0)
473 it('Should search among account video channels', async function () {
475 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
476 expect(body.total).to.equal(1)
478 const channels = body.data
479 expect(channels).to.have.lengthOf(1)
483 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
484 expect(body.total).to.equal(0)
486 const channels = body.data
487 expect(channels).to.have.lengthOf(0)
491 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
494 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
495 await waitJobs(servers)
497 for (const server of servers) {
498 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
500 expect(data[0].name).to.equal('toto_channel')
501 expect(data[1].name).to.equal('root_channel')
504 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
505 await waitJobs(servers)
507 for (const server of servers) {
508 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
510 expect(data[0].name).to.equal('root_channel')
511 expect(data[1].name).to.equal('toto_channel')
515 after(async function () {
516 await cleanupTests(servers)