1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { basename } from 'path'
5 import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
6 import { testFileExistsOrNot, testImage } from '@server/tests/shared'
7 import { wait } from '@shared/core-utils'
8 import { ActorImageType, User, VideoChannel } from '@shared/models'
11 createMultipleServers,
14 setAccessTokensToServers,
15 setDefaultAccountAvatar,
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)
47 await setDefaultAccountAvatar(servers)
49 await doubleFollow(servers[0], servers[1])
52 it('Should have one video channel (created with root)', async () => {
53 const body = await servers[0].channels.list({ start: 0, count: 2 })
55 expect(body.total).to.equal(1)
56 expect(body.data).to.be.an('array')
57 expect(body.data).to.have.lengthOf(1)
60 it('Should create another video channel', async function () {
64 const videoChannel = {
65 name: 'second_video_channel',
66 displayName: 'second video channel',
67 description: 'super video channel description',
68 support: 'super video channel support text'
70 const created = await servers[0].channels.create({ attributes: videoChannel })
71 secondVideoChannelId = created.id
74 // The channel is 1 is propagated to servers 2
76 const attributes = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
77 const { uuid } = await servers[0].videos.upload({ attributes })
81 await waitJobs(servers)
84 it('Should have two video channels when getting my information', async () => {
85 userInfo = await servers[0].users.getMyInfo()
87 expect(userInfo.videoChannels).to.be.an('array')
88 expect(userInfo.videoChannels).to.have.lengthOf(2)
90 const videoChannels = userInfo.videoChannels
91 expect(videoChannels[0].name).to.equal('root_channel')
92 expect(videoChannels[0].displayName).to.equal('Main root channel')
94 expect(videoChannels[1].name).to.equal('second_video_channel')
95 expect(videoChannels[1].displayName).to.equal('second video channel')
96 expect(videoChannels[1].description).to.equal('super video channel description')
97 expect(videoChannels[1].support).to.equal('super video channel support text')
99 accountName = userInfo.account.name + '@' + userInfo.account.host
102 it('Should have two video channels when getting account channels on server 1', async function () {
103 const body = await servers[0].channels.listByAccount({ accountName })
104 expect(body.total).to.equal(2)
106 const videoChannels = body.data
108 expect(videoChannels).to.be.an('array')
109 expect(videoChannels).to.have.lengthOf(2)
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')
120 it('Should paginate and sort account channels', async function () {
122 const body = await servers[0].channels.listByAccount({
129 expect(body.total).to.equal(2)
130 expect(body.data).to.have.lengthOf(1)
132 const videoChannel: VideoChannel = body.data[0]
133 expect(videoChannel.name).to.equal('root_channel')
137 const body = await servers[0].channels.listByAccount({
144 expect(body.total).to.equal(2)
145 expect(body.data).to.have.lengthOf(1)
146 expect(body.data[0].name).to.equal('second_video_channel')
150 const body = await servers[0].channels.listByAccount({
157 expect(body.total).to.equal(2)
158 expect(body.data).to.have.lengthOf(1)
159 expect(body.data[0].name).to.equal('root_channel')
163 it('Should have one video channel when getting account channels on server 2', async function () {
164 const body = await servers[1].channels.listByAccount({ accountName })
166 expect(body.total).to.equal(1)
167 expect(body.data).to.be.an('array')
168 expect(body.data).to.have.lengthOf(1)
170 const videoChannel = body.data[0]
171 expect(videoChannel.name).to.equal('second_video_channel')
172 expect(videoChannel.displayName).to.equal('second video channel')
173 expect(videoChannel.description).to.equal('super video channel description')
174 expect(videoChannel.support).to.equal('super video channel support text')
177 it('Should list video channels', async function () {
178 const body = await servers[0].channels.list({ start: 1, count: 1, sort: '-name' })
180 expect(body.total).to.equal(2)
181 expect(body.data).to.be.an('array')
182 expect(body.data).to.have.lengthOf(1)
183 expect(body.data[0].name).to.equal('root_channel')
184 expect(body.data[0].displayName).to.equal('Main root channel')
187 it('Should update video channel', async function () {
190 const videoChannelAttributes = {
191 displayName: 'video channel updated',
192 description: 'video channel description updated',
193 support: 'support updated'
196 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
198 await waitJobs(servers)
201 it('Should have video channel updated', async function () {
202 for (const server of servers) {
203 const body = await server.channels.list({ start: 0, count: 1, sort: '-name' })
205 expect(body.total).to.equal(2)
206 expect(body.data).to.be.an('array')
207 expect(body.data).to.have.lengthOf(1)
209 expect(body.data[0].name).to.equal('second_video_channel')
210 expect(body.data[0].displayName).to.equal('video channel updated')
211 expect(body.data[0].description).to.equal('video channel description updated')
212 expect(body.data[0].support).to.equal('support updated')
216 it('Should not have updated the video support field', async function () {
217 for (const server of servers) {
218 const video = await server.videos.get({ id: videoUUID })
219 expect(video.support).to.equal('video support field')
223 it('Should update another accounts video channel', async function () {
226 const result = await servers[0].users.generate('second_user')
227 secondUserChannelName = result.userChannelName
229 await servers[0].videos.quickUpload({ name: 'video', token: result.token })
231 const videoChannelAttributes = {
232 displayName: 'video channel updated',
233 description: 'video channel description updated',
234 support: 'support updated'
237 await servers[0].channels.update({ channelName: secondUserChannelName, attributes: videoChannelAttributes })
239 await waitJobs(servers)
242 it('Should have another accounts video channel updated', async function () {
243 for (const server of servers) {
244 const body = await server.channels.get({ channelName: `${secondUserChannelName}@${servers[0].host}` })
246 expect(body.displayName).to.equal('video channel updated')
247 expect(body.description).to.equal('video channel description updated')
248 expect(body.support).to.equal('support updated')
252 it('Should update the channel support field and update videos too', async function () {
255 const videoChannelAttributes = {
256 support: 'video channel support text updated',
257 bulkVideosSupportUpdate: true
260 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
262 await waitJobs(servers)
264 for (const server of servers) {
265 const video = await server.videos.get({ id: videoUUID })
266 expect(video.support).to.equal(videoChannelAttributes.support)
270 it('Should update video channel avatar', async function () {
273 const fixture = 'avatar.png'
275 await servers[0].channels.updateImage({
276 channelName: 'second_video_channel',
281 await waitJobs(servers)
283 for (const server of servers) {
284 const videoChannel = await findChannel(server, secondVideoChannelId)
285 const expectedSizes = ACTOR_IMAGES_SIZE[ActorImageType.AVATAR]
287 expect(videoChannel.avatars.length).to.equal(expectedSizes.length, 'Expected avatars to be generated in all sizes')
289 for (const avatar of videoChannel.avatars) {
290 avatarPaths[server.port] = avatar.path
291 await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatarPaths[server.port], '.png')
292 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
294 const row = await server.sql.getActorImage(basename(avatarPaths[server.port]))
296 expect(expectedSizes.some(({ height, width }) => row.height === height && row.width === width)).to.equal(true)
301 it('Should update video channel banner', async function () {
304 const fixture = 'banner.jpg'
306 await servers[0].channels.updateImage({
307 channelName: 'second_video_channel',
312 await waitJobs(servers)
314 for (const server of servers) {
315 const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
317 bannerPaths[server.port] = videoChannel.banners[0].path
318 await testImage(server.url, 'banner-resized', bannerPaths[server.port])
319 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
321 const row = await server.sql.getActorImage(basename(bannerPaths[server.port]))
322 expect(row.height).to.equal(ACTOR_IMAGES_SIZE[ActorImageType.BANNER][0].height)
323 expect(row.width).to.equal(ACTOR_IMAGES_SIZE[ActorImageType.BANNER][0].width)
327 it('Should still correctly list channels', async function () {
329 const body = await servers[0].channels.list({ start: 1, count: 1, sort: 'createdAt' })
331 expect(body.total).to.equal(3)
332 expect(body.data).to.have.lengthOf(1)
333 expect(body.data[0].name).to.equal('second_video_channel')
337 const body = await servers[0].channels.listByAccount({ accountName, start: 1, count: 1, sort: 'createdAt' })
339 expect(body.total).to.equal(2)
340 expect(body.data).to.have.lengthOf(1)
341 expect(body.data[0].name).to.equal('second_video_channel')
345 it('Should delete the video channel avatar', async function () {
347 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
349 await waitJobs(servers)
351 for (const server of servers) {
352 const videoChannel = await findChannel(server, secondVideoChannelId)
353 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
355 expect(videoChannel.avatars).to.be.empty
359 it('Should delete the video channel banner', async function () {
362 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
364 await waitJobs(servers)
366 for (const server of servers) {
367 const videoChannel = await findChannel(server, secondVideoChannelId)
368 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
370 expect(videoChannel.banners).to.be.empty
374 it('Should list the second video channel videos', async function () {
377 for (const server of servers) {
378 const channelURI = 'second_video_channel@localhost:' + servers[0].port
379 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
381 expect(total).to.equal(1)
382 expect(data).to.be.an('array')
383 expect(data).to.have.lengthOf(1)
384 expect(data[0].name).to.equal('my video name')
388 it('Should change the video channel of a video', async function () {
391 await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
393 await waitJobs(servers)
396 it('Should list the first video channel videos', async function () {
399 for (const server of servers) {
401 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
402 const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
403 expect(total).to.equal(0)
407 const channelURI = 'root_channel@localhost:' + servers[0].port
408 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
409 expect(total).to.equal(1)
411 expect(data).to.be.an('array')
412 expect(data).to.have.lengthOf(1)
413 expect(data[0].name).to.equal('my video name')
418 it('Should delete video channel', async function () {
419 await servers[0].channels.delete({ channelName: 'second_video_channel' })
422 it('Should have video channel deleted', async function () {
423 const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
425 expect(body.total).to.equal(2)
426 expect(body.data).to.be.an('array')
427 expect(body.data).to.have.lengthOf(2)
428 expect(body.data[0].displayName).to.equal('Main root channel')
429 expect(body.data[1].displayName).to.equal('video channel updated')
432 it('Should create the main channel with a suffix if there is a conflict', async function () {
434 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
435 const created = await servers[0].channels.create({ attributes: videoChannel })
436 totoChannel = created.id
440 await servers[0].users.create({ username: 'toto', password: 'password' })
441 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
443 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
444 expect(videoChannels[0].name).to.equal('toto_channel-1')
448 it('Should report correct channel views per days', async function () {
452 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
454 for (const channel of data) {
455 expect(channel).to.haveOwnProperty('viewsPerDay')
456 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
458 for (const v of channel.viewsPerDay) {
459 expect(v.date).to.be.an('string')
460 expect(v.views).to.equal(0)
466 // video has been posted on channel servers[0].store.videoChannel.id since last update
467 await servers[0].views.simulateView({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
468 await servers[0].views.simulateView({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
470 // Wait the repeatable job
473 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
474 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
475 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
479 it('Should report correct total views count', async function () {
480 // check if there's the property
482 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
484 for (const channel of data) {
485 expect(channel).to.haveOwnProperty('totalViews')
486 expect(channel.totalViews).to.be.a('number')
490 // Check if the totalViews count can be updated
492 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
493 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
494 expect(channelWithView.totalViews).to.equal(2)
498 it('Should report correct videos count', async function () {
499 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
501 const totoChannel = data.find(c => c.name === 'toto_channel')
502 const rootChannel = data.find(c => c.name === 'root_channel')
504 expect(rootChannel.videosCount).to.equal(1)
505 expect(totoChannel.videosCount).to.equal(0)
508 it('Should search among account video channels', async function () {
510 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
511 expect(body.total).to.equal(1)
513 const channels = body.data
514 expect(channels).to.have.lengthOf(1)
518 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
519 expect(body.total).to.equal(0)
521 const channels = body.data
522 expect(channels).to.have.lengthOf(0)
526 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
529 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
530 await waitJobs(servers)
532 for (const server of servers) {
533 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
535 expect(data[0].name).to.equal('toto_channel')
536 expect(data[1].name).to.equal('root_channel')
539 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
540 await waitJobs(servers)
542 for (const server of servers) {
543 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
545 expect(data[0].name).to.equal('root_channel')
546 expect(data[1].name).to.equal('toto_channel')
550 after(async function () {
551 await cleanupTests(servers)