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'
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
18 } from '@shared/server-commands'
19 import { User, VideoChannel } from '@shared/models'
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)
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)
286 avatarPaths[server.port] = videoChannel.avatar.path
287 await testImage(server.url, 'avatar-resized', avatarPaths[server.port], '.png')
288 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
290 const row = await server.sql.getActorImage(basename(avatarPaths[server.port]))
291 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height)
292 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width)
296 it('Should update video channel banner', async function () {
299 const fixture = 'banner.jpg'
301 await servers[0].channels.updateImage({
302 channelName: 'second_video_channel',
307 await waitJobs(servers)
309 for (const server of servers) {
310 const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
312 bannerPaths[server.port] = videoChannel.banner.path
313 await testImage(server.url, 'banner-resized', bannerPaths[server.port])
314 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
316 const row = await server.sql.getActorImage(basename(bannerPaths[server.port]))
317 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height)
318 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width)
322 it('Should delete the video channel avatar', async function () {
325 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
327 await waitJobs(servers)
329 for (const server of servers) {
330 const videoChannel = await findChannel(server, secondVideoChannelId)
331 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
333 expect(videoChannel.avatar).to.be.null
337 it('Should delete the video channel banner', async function () {
340 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
342 await waitJobs(servers)
344 for (const server of servers) {
345 const videoChannel = await findChannel(server, secondVideoChannelId)
346 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
348 expect(videoChannel.banner).to.be.null
352 it('Should list the second video channel videos', async function () {
355 for (const server of servers) {
356 const channelURI = 'second_video_channel@localhost:' + servers[0].port
357 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
359 expect(total).to.equal(1)
360 expect(data).to.be.an('array')
361 expect(data).to.have.lengthOf(1)
362 expect(data[0].name).to.equal('my video name')
366 it('Should change the video channel of a video', async function () {
369 await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
371 await waitJobs(servers)
374 it('Should list the first video channel videos', async function () {
377 for (const server of servers) {
379 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
380 const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
381 expect(total).to.equal(0)
385 const channelURI = 'root_channel@localhost:' + servers[0].port
386 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
387 expect(total).to.equal(1)
389 expect(data).to.be.an('array')
390 expect(data).to.have.lengthOf(1)
391 expect(data[0].name).to.equal('my video name')
396 it('Should delete video channel', async function () {
397 await servers[0].channels.delete({ channelName: 'second_video_channel' })
400 it('Should have video channel deleted', async function () {
401 const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
403 expect(body.total).to.equal(2)
404 expect(body.data).to.be.an('array')
405 expect(body.data).to.have.lengthOf(2)
406 expect(body.data[0].displayName).to.equal('Main root channel')
407 expect(body.data[1].displayName).to.equal('video channel updated')
410 it('Should create the main channel with an uuid if there is a conflict', async function () {
412 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
413 const created = await servers[0].channels.create({ attributes: videoChannel })
414 totoChannel = created.id
418 await servers[0].users.create({ username: 'toto', password: 'password' })
419 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
421 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
422 const videoChannel = videoChannels[0]
423 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}/)
427 it('Should report correct channel views per days', async function () {
431 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
433 for (const channel of data) {
434 expect(channel).to.haveOwnProperty('viewsPerDay')
435 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
437 for (const v of channel.viewsPerDay) {
438 expect(v.date).to.be.an('string')
439 expect(v.views).to.equal(0)
445 // video has been posted on channel servers[0].store.videoChannel.id since last update
446 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
447 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
449 // Wait the repeatable job
452 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
453 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
454 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
458 it('Should report correct videos count', async function () {
459 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
461 const totoChannel = data.find(c => c.name === 'toto_channel')
462 const rootChannel = data.find(c => c.name === 'root_channel')
464 expect(rootChannel.videosCount).to.equal(1)
465 expect(totoChannel.videosCount).to.equal(0)
468 it('Should search among account video channels', async function () {
470 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
471 expect(body.total).to.equal(1)
473 const channels = body.data
474 expect(channels).to.have.lengthOf(1)
478 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
479 expect(body.total).to.equal(0)
481 const channels = body.data
482 expect(channels).to.have.lengthOf(0)
486 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
489 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
490 await waitJobs(servers)
492 for (const server of servers) {
493 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
495 expect(data[0].name).to.equal('toto_channel')
496 expect(data[1].name).to.equal('root_channel')
499 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
500 await waitJobs(servers)
502 for (const server of servers) {
503 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
505 expect(data[0].name).to.equal('root_channel')
506 expect(data[1].name).to.equal('toto_channel')
510 after(async function () {
511 await cleanupTests(servers)