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/extra-utils'
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
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 the channel support field and update videos too', async function () {
225 const videoChannelAttributes = {
226 support: 'video channel support text updated',
227 bulkVideosSupportUpdate: true
230 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
232 await waitJobs(servers)
234 for (const server of servers) {
235 const video = await server.videos.get({ id: videoUUID })
236 expect(video.support).to.equal(videoChannelAttributes.support)
240 it('Should update video channel avatar', async function () {
243 const fixture = 'avatar.png'
245 await servers[0].channels.updateImage({
246 channelName: 'second_video_channel',
251 await waitJobs(servers)
253 for (const server of servers) {
254 const videoChannel = await findChannel(server, secondVideoChannelId)
256 avatarPaths[server.port] = videoChannel.avatar.path
257 await testImage(server.url, 'avatar-resized', avatarPaths[server.port], '.png')
258 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
260 const row = await server.sql.getActorImage(basename(avatarPaths[server.port]))
261 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height)
262 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width)
266 it('Should update video channel banner', async function () {
269 const fixture = 'banner.jpg'
271 await servers[0].channels.updateImage({
272 channelName: 'second_video_channel',
277 await waitJobs(servers)
279 for (const server of servers) {
280 const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
282 bannerPaths[server.port] = videoChannel.banner.path
283 await testImage(server.url, 'banner-resized', bannerPaths[server.port])
284 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
286 const row = await server.sql.getActorImage(basename(bannerPaths[server.port]))
287 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height)
288 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width)
292 it('Should delete the video channel avatar', async function () {
295 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
297 await waitJobs(servers)
299 for (const server of servers) {
300 const videoChannel = await findChannel(server, secondVideoChannelId)
301 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
303 expect(videoChannel.avatar).to.be.null
307 it('Should delete the video channel banner', async function () {
310 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
312 await waitJobs(servers)
314 for (const server of servers) {
315 const videoChannel = await findChannel(server, secondVideoChannelId)
316 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
318 expect(videoChannel.banner).to.be.null
322 it('Should list the second video channel videos', async function () {
325 for (const server of servers) {
326 const channelURI = 'second_video_channel@localhost:' + servers[0].port
327 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
329 expect(total).to.equal(1)
330 expect(data).to.be.an('array')
331 expect(data).to.have.lengthOf(1)
332 expect(data[0].name).to.equal('my video name')
336 it('Should change the video channel of a video', async function () {
339 await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
341 await waitJobs(servers)
344 it('Should list the first video channel videos', async function () {
347 for (const server of servers) {
349 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
350 const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
351 expect(total).to.equal(0)
355 const channelURI = 'root_channel@localhost:' + servers[0].port
356 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
357 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')
366 it('Should delete video channel', async function () {
367 await servers[0].channels.delete({ channelName: 'second_video_channel' })
370 it('Should have video channel deleted', async function () {
371 const body = await servers[0].channels.list({ start: 0, count: 10 })
373 expect(body.total).to.equal(1)
374 expect(body.data).to.be.an('array')
375 expect(body.data).to.have.lengthOf(1)
376 expect(body.data[0].displayName).to.equal('Main root channel')
379 it('Should create the main channel with an uuid if there is a conflict', async function () {
381 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
382 const created = await servers[0].channels.create({ attributes: videoChannel })
383 totoChannel = created.id
387 await servers[0].users.create({ username: 'toto', password: 'password' })
388 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
390 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
391 const videoChannel = videoChannels[0]
392 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}/)
396 it('Should report correct channel views per days', async function () {
400 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
402 for (const channel of data) {
403 expect(channel).to.haveOwnProperty('viewsPerDay')
404 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
406 for (const v of channel.viewsPerDay) {
407 expect(v.date).to.be.an('string')
408 expect(v.views).to.equal(0)
414 // video has been posted on channel servers[0].store.videoChannel.id since last update
415 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
416 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
418 // Wait the repeatable job
421 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
422 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
423 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
427 it('Should report correct videos count', async function () {
428 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
430 const totoChannel = data.find(c => c.name === 'toto_channel')
431 const rootChannel = data.find(c => c.name === 'root_channel')
433 expect(rootChannel.videosCount).to.equal(1)
434 expect(totoChannel.videosCount).to.equal(0)
437 it('Should search among account video channels', async function () {
439 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
440 expect(body.total).to.equal(1)
442 const channels = body.data
443 expect(channels).to.have.lengthOf(1)
447 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
448 expect(body.total).to.equal(0)
450 const channels = body.data
451 expect(channels).to.have.lengthOf(0)
455 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
458 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
459 await waitJobs(servers)
461 for (const server of servers) {
462 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
464 expect(data[0].name).to.equal('toto_channel')
465 expect(data[1].name).to.equal('root_channel')
468 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
469 await waitJobs(servers)
471 for (const server of servers) {
472 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
474 expect(data[0].name).to.equal('root_channel')
475 expect(data[1].name).to.equal('toto_channel')
479 after(async function () {
480 await cleanupTests(servers)