1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { basename } from 'path'
5 import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
6 import { SQLCommand, 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 async function findChannel (server: PeerTubeServer, channelId: number) {
21 const body = await server.channels.list({ sort: '-name' })
23 return body.data.find(c => c.id === channelId)
26 describe('Test video channels', function () {
27 let servers: PeerTubeServer[]
28 let sqlCommands: SQLCommand[] = []
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])
51 sqlCommands = servers.map(s => new SQLCommand(s))
54 it('Should have one video channel (created with root)', async () => {
55 const body = await servers[0].channels.list({ start: 0, count: 2 })
57 expect(body.total).to.equal(1)
58 expect(body.data).to.be.an('array')
59 expect(body.data).to.have.lengthOf(1)
62 it('Should create another video channel', async function () {
66 const videoChannel = {
67 name: 'second_video_channel',
68 displayName: 'second video channel',
69 description: 'super video channel description',
70 support: 'super video channel support text'
72 const created = await servers[0].channels.create({ attributes: videoChannel })
73 secondVideoChannelId = created.id
76 // The channel is 1 is propagated to servers 2
78 const attributes = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
79 const { uuid } = await servers[0].videos.upload({ attributes })
83 await waitJobs(servers)
86 it('Should have two video channels when getting my information', async () => {
87 userInfo = await servers[0].users.getMyInfo()
89 expect(userInfo.videoChannels).to.be.an('array')
90 expect(userInfo.videoChannels).to.have.lengthOf(2)
92 const videoChannels = userInfo.videoChannels
93 expect(videoChannels[0].name).to.equal('root_channel')
94 expect(videoChannels[0].displayName).to.equal('Main root channel')
96 expect(videoChannels[1].name).to.equal('second_video_channel')
97 expect(videoChannels[1].displayName).to.equal('second video channel')
98 expect(videoChannels[1].description).to.equal('super video channel description')
99 expect(videoChannels[1].support).to.equal('super video channel support text')
101 accountName = userInfo.account.name + '@' + userInfo.account.host
104 it('Should have two video channels when getting account channels on server 1', async function () {
105 const body = await servers[0].channels.listByAccount({ accountName })
106 expect(body.total).to.equal(2)
108 const videoChannels = body.data
110 expect(videoChannels).to.be.an('array')
111 expect(videoChannels).to.have.lengthOf(2)
113 expect(videoChannels[0].name).to.equal('root_channel')
114 expect(videoChannels[0].displayName).to.equal('Main root channel')
116 expect(videoChannels[1].name).to.equal('second_video_channel')
117 expect(videoChannels[1].displayName).to.equal('second video channel')
118 expect(videoChannels[1].description).to.equal('super video channel description')
119 expect(videoChannels[1].support).to.equal('super video channel support text')
122 it('Should paginate and sort account channels', async function () {
124 const body = await servers[0].channels.listByAccount({
131 expect(body.total).to.equal(2)
132 expect(body.data).to.have.lengthOf(1)
134 const videoChannel: VideoChannel = body.data[0]
135 expect(videoChannel.name).to.equal('root_channel')
139 const body = await servers[0].channels.listByAccount({
146 expect(body.total).to.equal(2)
147 expect(body.data).to.have.lengthOf(1)
148 expect(body.data[0].name).to.equal('second_video_channel')
152 const body = await servers[0].channels.listByAccount({
159 expect(body.total).to.equal(2)
160 expect(body.data).to.have.lengthOf(1)
161 expect(body.data[0].name).to.equal('root_channel')
165 it('Should have one video channel when getting account channels on server 2', async function () {
166 const body = await servers[1].channels.listByAccount({ accountName })
168 expect(body.total).to.equal(1)
169 expect(body.data).to.be.an('array')
170 expect(body.data).to.have.lengthOf(1)
172 const videoChannel = body.data[0]
173 expect(videoChannel.name).to.equal('second_video_channel')
174 expect(videoChannel.displayName).to.equal('second video channel')
175 expect(videoChannel.description).to.equal('super video channel description')
176 expect(videoChannel.support).to.equal('super video channel support text')
179 it('Should list video channels', async function () {
180 const body = await servers[0].channels.list({ start: 1, count: 1, sort: '-name' })
182 expect(body.total).to.equal(2)
183 expect(body.data).to.be.an('array')
184 expect(body.data).to.have.lengthOf(1)
185 expect(body.data[0].name).to.equal('root_channel')
186 expect(body.data[0].displayName).to.equal('Main root channel')
189 it('Should update video channel', async function () {
192 const videoChannelAttributes = {
193 displayName: 'video channel updated',
194 description: 'video channel description updated',
195 support: 'support updated'
198 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
200 await waitJobs(servers)
203 it('Should have video channel updated', async function () {
204 for (const server of servers) {
205 const body = await server.channels.list({ start: 0, count: 1, sort: '-name' })
207 expect(body.total).to.equal(2)
208 expect(body.data).to.be.an('array')
209 expect(body.data).to.have.lengthOf(1)
211 expect(body.data[0].name).to.equal('second_video_channel')
212 expect(body.data[0].displayName).to.equal('video channel updated')
213 expect(body.data[0].description).to.equal('video channel description updated')
214 expect(body.data[0].support).to.equal('support updated')
218 it('Should not have updated the video support field', async function () {
219 for (const server of servers) {
220 const video = await server.videos.get({ id: videoUUID })
221 expect(video.support).to.equal('video support field')
225 it('Should update another accounts video channel', async function () {
228 const result = await servers[0].users.generate('second_user')
229 secondUserChannelName = result.userChannelName
231 await servers[0].videos.quickUpload({ name: 'video', token: result.token })
233 const videoChannelAttributes = {
234 displayName: 'video channel updated',
235 description: 'video channel description updated',
236 support: 'support updated'
239 await servers[0].channels.update({ channelName: secondUserChannelName, attributes: videoChannelAttributes })
241 await waitJobs(servers)
244 it('Should have another accounts video channel updated', async function () {
245 for (const server of servers) {
246 const body = await server.channels.get({ channelName: `${secondUserChannelName}@${servers[0].host}` })
248 expect(body.displayName).to.equal('video channel updated')
249 expect(body.description).to.equal('video channel description updated')
250 expect(body.support).to.equal('support updated')
254 it('Should update the channel support field and update videos too', async function () {
257 const videoChannelAttributes = {
258 support: 'video channel support text updated',
259 bulkVideosSupportUpdate: true
262 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
264 await waitJobs(servers)
266 for (const server of servers) {
267 const video = await server.videos.get({ id: videoUUID })
268 expect(video.support).to.equal(videoChannelAttributes.support)
272 it('Should update video channel avatar', async function () {
275 const fixture = 'avatar.png'
277 await servers[0].channels.updateImage({
278 channelName: 'second_video_channel',
283 await waitJobs(servers)
285 for (let i = 0; i < servers.length; i++) {
286 const server = servers[i]
288 const videoChannel = await findChannel(server, secondVideoChannelId)
289 const expectedSizes = ACTOR_IMAGES_SIZE[ActorImageType.AVATAR]
291 expect(videoChannel.avatars.length).to.equal(expectedSizes.length, 'Expected avatars to be generated in all sizes')
293 for (const avatar of videoChannel.avatars) {
294 avatarPaths[server.port] = avatar.path
295 await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatarPaths[server.port], '.png')
296 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
298 const row = await sqlCommands[i].getActorImage(basename(avatarPaths[server.port]))
300 expect(expectedSizes.some(({ height, width }) => row.height === height && row.width === width)).to.equal(true)
305 it('Should update video channel banner', async function () {
308 const fixture = 'banner.jpg'
310 await servers[0].channels.updateImage({
311 channelName: 'second_video_channel',
316 await waitJobs(servers)
318 for (let i = 0; i < servers.length; i++) {
319 const server = servers[i]
321 const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
323 bannerPaths[server.port] = videoChannel.banners[0].path
324 await testImage(server.url, 'banner-resized', bannerPaths[server.port])
325 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
327 const row = await sqlCommands[i].getActorImage(basename(bannerPaths[server.port]))
328 expect(row.height).to.equal(ACTOR_IMAGES_SIZE[ActorImageType.BANNER][0].height)
329 expect(row.width).to.equal(ACTOR_IMAGES_SIZE[ActorImageType.BANNER][0].width)
333 it('Should still correctly list channels', async function () {
335 const body = await servers[0].channels.list({ start: 1, count: 1, sort: 'createdAt' })
337 expect(body.total).to.equal(3)
338 expect(body.data).to.have.lengthOf(1)
339 expect(body.data[0].name).to.equal('second_video_channel')
343 const body = await servers[0].channels.listByAccount({ accountName, start: 1, count: 1, sort: 'createdAt' })
345 expect(body.total).to.equal(2)
346 expect(body.data).to.have.lengthOf(1)
347 expect(body.data[0].name).to.equal('second_video_channel')
351 it('Should delete the video channel avatar', async function () {
353 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
355 await waitJobs(servers)
357 for (const server of servers) {
358 const videoChannel = await findChannel(server, secondVideoChannelId)
359 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
361 expect(videoChannel.avatars).to.be.empty
365 it('Should delete the video channel banner', async function () {
368 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
370 await waitJobs(servers)
372 for (const server of servers) {
373 const videoChannel = await findChannel(server, secondVideoChannelId)
374 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
376 expect(videoChannel.banners).to.be.empty
380 it('Should list the second video channel videos', async function () {
383 for (const server of servers) {
384 const channelURI = 'second_video_channel@' + servers[0].host
385 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
387 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')
394 it('Should change the video channel of a video', async function () {
397 await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
399 await waitJobs(servers)
402 it('Should list the first video channel videos', async function () {
405 for (const server of servers) {
407 const secondChannelURI = 'second_video_channel@' + servers[0].host
408 const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
409 expect(total).to.equal(0)
413 const channelURI = 'root_channel@' + servers[0].host
414 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
415 expect(total).to.equal(1)
417 expect(data).to.be.an('array')
418 expect(data).to.have.lengthOf(1)
419 expect(data[0].name).to.equal('my video name')
424 it('Should delete video channel', async function () {
425 await servers[0].channels.delete({ channelName: 'second_video_channel' })
428 it('Should have video channel deleted', async function () {
429 const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
431 expect(body.total).to.equal(2)
432 expect(body.data).to.be.an('array')
433 expect(body.data).to.have.lengthOf(2)
434 expect(body.data[0].displayName).to.equal('Main root channel')
435 expect(body.data[1].displayName).to.equal('video channel updated')
438 it('Should create the main channel with a suffix if there is a conflict', async function () {
440 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
441 const created = await servers[0].channels.create({ attributes: videoChannel })
442 totoChannel = created.id
446 await servers[0].users.create({ username: 'toto', password: 'password' })
447 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
449 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
450 expect(videoChannels[0].name).to.equal('toto_channel-1')
454 it('Should report correct channel views per days', async function () {
458 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
460 for (const channel of data) {
461 expect(channel).to.haveOwnProperty('viewsPerDay')
462 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
464 for (const v of channel.viewsPerDay) {
465 expect(v.date).to.be.an('string')
466 expect(v.views).to.equal(0)
472 // video has been posted on channel servers[0].store.videoChannel.id since last update
473 await servers[0].views.simulateView({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
474 await servers[0].views.simulateView({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
476 // Wait the repeatable job
479 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
480 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
481 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
485 it('Should report correct total views count', async function () {
486 // check if there's the property
488 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
490 for (const channel of data) {
491 expect(channel).to.haveOwnProperty('totalViews')
492 expect(channel.totalViews).to.be.a('number')
496 // Check if the totalViews count can be updated
498 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
499 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
500 expect(channelWithView.totalViews).to.equal(2)
504 it('Should report correct videos count', async function () {
505 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
507 const totoChannel = data.find(c => c.name === 'toto_channel')
508 const rootChannel = data.find(c => c.name === 'root_channel')
510 expect(rootChannel.videosCount).to.equal(1)
511 expect(totoChannel.videosCount).to.equal(0)
514 it('Should search among account video channels', async function () {
516 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
517 expect(body.total).to.equal(1)
519 const channels = body.data
520 expect(channels).to.have.lengthOf(1)
524 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
525 expect(body.total).to.equal(0)
527 const channels = body.data
528 expect(channels).to.have.lengthOf(0)
532 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
535 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
536 await waitJobs(servers)
538 for (const server of servers) {
539 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
541 expect(data[0].name).to.equal('toto_channel')
542 expect(data[1].name).to.equal('root_channel')
545 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
546 await waitJobs(servers)
548 for (const server of servers) {
549 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
551 expect(data[0].name).to.equal('root_channel')
552 expect(data[1].name).to.equal('toto_channel')
556 after(async function () {
557 for (const sqlCommand of sqlCommands) {
558 await sqlCommand.cleanup()
561 await cleanupTests(servers)