PeerTube/server/tests/api/videos/video-channels.ts

552 lines
18 KiB
TypeScript
Raw Normal View History

2020-01-31 09:56:52 -06:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2017-10-24 12:41:30 -05:00
2022-08-17 08:44:32 -05:00
import { expect } from 'chai'
2021-04-08 04:23:45 -05:00
import { basename } from 'path'
import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
import { testFileExistsOrNot, testImage } from '@server/tests/shared'
import { wait } from '@shared/core-utils'
import { ActorImageType, User, VideoChannel } from '@shared/models'
import {
2019-04-24 08:10:37 -05:00
cleanupTests,
2021-07-16 02:47:51 -05:00
createMultipleServers,
2021-07-16 07:27:30 -05:00
doubleFollow,
2021-07-16 02:47:51 -05:00
PeerTubeServer,
2021-07-13 07:23:01 -05:00
setAccessTokensToServers,
setDefaultAccountAvatar,
setDefaultVideoChannel,
2021-07-13 07:23:01 -05:00
waitJobs
} from '@shared/server-commands'
2017-10-24 12:41:30 -05:00
2021-07-16 02:47:51 -05:00
async function findChannel (server: PeerTubeServer, channelId: number) {
2021-07-16 02:04:35 -05:00
const body = await server.channels.list({ sort: '-name' })
2021-04-07 03:36:13 -05:00
2021-07-09 04:21:30 -05:00
return body.data.find(c => c.id === channelId)
2021-04-07 03:36:13 -05:00
}
describe('Test video channels', function () {
2021-07-16 02:47:51 -05:00
let servers: PeerTubeServer[]
2017-10-24 12:41:30 -05:00
let userInfo: User
2018-05-11 08:10:13 -05:00
let secondVideoChannelId: number
let totoChannel: number
2018-05-11 08:10:13 -05:00
let videoUUID: string
let accountName: string
let secondUserChannelName: string
2017-10-24 12:41:30 -05:00
2021-07-01 09:47:14 -05:00
const avatarPaths: { [ port: number ]: string } = {}
const bannerPaths: { [ port: number ]: string } = {}
2017-10-24 12:41:30 -05:00
before(async function () {
2019-04-26 01:50:52 -05:00
this.timeout(60000)
2017-10-24 12:41:30 -05:00
2021-07-16 02:47:51 -05:00
servers = await createMultipleServers(2)
await setAccessTokensToServers(servers)
await setDefaultVideoChannel(servers)
await setDefaultAccountAvatar(servers)
2018-04-24 10:05:32 -05:00
await doubleFollow(servers[0], servers[1])
2017-10-24 12:41:30 -05:00
})
it('Should have one video channel (created with root)', async () => {
2021-07-16 02:04:35 -05:00
const body = await servers[0].channels.list({ start: 0, count: 2 })
2017-10-24 12:41:30 -05:00
2021-07-09 04:21:30 -05:00
expect(body.total).to.equal(1)
expect(body.data).to.be.an('array')
expect(body.data).to.have.lengthOf(1)
2017-10-24 12:41:30 -05:00
})
it('Should create another video channel', async function () {
2022-07-04 06:34:03 -05:00
this.timeout(30000)
2018-05-11 08:10:13 -05:00
{
const videoChannel = {
2018-08-17 08:45:42 -05:00
name: 'second_video_channel',
2018-05-11 08:10:13 -05:00
displayName: 'second video channel',
description: 'super video channel description',
support: 'super video channel support text'
}
2021-07-16 02:04:35 -05:00
const created = await servers[0].channels.create({ attributes: videoChannel })
2021-07-09 04:21:30 -05:00
secondVideoChannelId = created.id
2017-10-24 12:41:30 -05:00
}
// The channel is 1 is propagated to servers 2
2018-05-11 08:10:13 -05:00
{
2021-07-15 03:02:54 -05:00
const attributes = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
2021-07-16 02:04:35 -05:00
const { uuid } = await servers[0].videos.upload({ attributes })
2021-07-15 03:02:54 -05:00
videoUUID = uuid
2018-05-11 08:10:13 -05:00
}
await waitJobs(servers)
2017-10-24 12:41:30 -05:00
})
it('Should have two video channels when getting my information', async () => {
2021-07-16 02:04:35 -05:00
userInfo = await servers[0].users.getMyInfo()
2017-10-24 12:41:30 -05:00
expect(userInfo.videoChannels).to.be.an('array')
expect(userInfo.videoChannels).to.have.lengthOf(2)
const videoChannels = userInfo.videoChannels
2018-08-17 08:45:42 -05:00
expect(videoChannels[0].name).to.equal('root_channel')
expect(videoChannels[0].displayName).to.equal('Main root channel')
expect(videoChannels[1].name).to.equal('second_video_channel')
2018-01-11 04:40:18 -06:00
expect(videoChannels[1].displayName).to.equal('second video channel')
2017-10-24 12:41:30 -05:00
expect(videoChannels[1].description).to.equal('super video channel description')
expect(videoChannels[1].support).to.equal('super video channel support text')
accountName = userInfo.account.name + '@' + userInfo.account.host
2017-10-24 12:41:30 -05:00
})
it('Should have two video channels when getting account channels on server 1', async function () {
2021-07-16 02:04:35 -05:00
const body = await servers[0].channels.listByAccount({ accountName })
2021-07-09 04:21:30 -05:00
expect(body.total).to.equal(2)
2021-07-09 04:21:30 -05:00
const videoChannels = body.data
expect(videoChannels).to.be.an('array')
expect(videoChannels).to.have.lengthOf(2)
2017-10-24 12:41:30 -05:00
2018-08-17 08:45:42 -05:00
expect(videoChannels[0].name).to.equal('root_channel')
expect(videoChannels[0].displayName).to.equal('Main root channel')
expect(videoChannels[1].name).to.equal('second_video_channel')
2018-01-11 04:40:18 -06:00
expect(videoChannels[1].displayName).to.equal('second video channel')
2017-10-24 12:41:30 -05:00
expect(videoChannels[1].description).to.equal('super video channel description')
expect(videoChannels[1].support).to.equal('super video channel support text')
})
it('Should paginate and sort account channels', async function () {
{
2021-07-16 02:04:35 -05:00
const body = await servers[0].channels.listByAccount({
accountName,
start: 0,
count: 1,
sort: 'createdAt'
})
2021-07-09 04:21:30 -05:00
expect(body.total).to.equal(2)
expect(body.data).to.have.lengthOf(1)
2021-07-09 04:21:30 -05:00
const videoChannel: VideoChannel = body.data[0]
expect(videoChannel.name).to.equal('root_channel')
}
{
2021-07-16 02:04:35 -05:00
const body = await servers[0].channels.listByAccount({
accountName,
start: 0,
count: 1,
sort: '-createdAt'
})
2021-07-09 04:21:30 -05:00
expect(body.total).to.equal(2)
expect(body.data).to.have.lengthOf(1)
expect(body.data[0].name).to.equal('second_video_channel')
}
{
2021-07-16 02:04:35 -05:00
const body = await servers[0].channels.listByAccount({
accountName,
start: 1,
count: 1,
sort: '-createdAt'
})
2021-07-09 04:21:30 -05:00
expect(body.total).to.equal(2)
expect(body.data).to.have.lengthOf(1)
expect(body.data[0].name).to.equal('root_channel')
}
})
it('Should have one video channel when getting account channels on server 2', async function () {
2021-07-16 02:04:35 -05:00
const body = await servers[1].channels.listByAccount({ accountName })
2021-07-09 04:21:30 -05:00
expect(body.total).to.equal(1)
expect(body.data).to.be.an('array')
expect(body.data).to.have.lengthOf(1)
2017-10-24 12:41:30 -05:00
2021-07-09 04:21:30 -05:00
const videoChannel = body.data[0]
expect(videoChannel.name).to.equal('second_video_channel')
expect(videoChannel.displayName).to.equal('second video channel')
expect(videoChannel.description).to.equal('super video channel description')
expect(videoChannel.support).to.equal('super video channel support text')
2017-10-24 12:41:30 -05:00
})
it('Should list video channels', async function () {
2021-07-16 02:04:35 -05:00
const body = await servers[0].channels.list({ start: 1, count: 1, sort: '-name' })
2017-10-24 12:41:30 -05:00
2021-07-09 04:21:30 -05:00
expect(body.total).to.equal(2)
expect(body.data).to.be.an('array')
expect(body.data).to.have.lengthOf(1)
expect(body.data[0].name).to.equal('root_channel')
expect(body.data[0].displayName).to.equal('Main root channel')
2017-10-24 12:41:30 -05:00
})
it('Should update video channel', async function () {
this.timeout(15000)
2017-10-24 12:41:30 -05:00
const videoChannelAttributes = {
2018-04-26 09:11:38 -05:00
displayName: 'video channel updated',
description: 'video channel description updated',
support: 'support updated'
2017-10-24 12:41:30 -05:00
}
2021-07-16 02:04:35 -05:00
await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
await waitJobs(servers)
2017-10-24 12:41:30 -05:00
})
it('Should have video channel updated', async function () {
for (const server of servers) {
2021-07-16 02:04:35 -05:00
const body = await server.channels.list({ start: 0, count: 1, sort: '-name' })
2021-07-09 04:21:30 -05:00
expect(body.total).to.equal(2)
expect(body.data).to.be.an('array')
expect(body.data).to.have.lengthOf(1)
expect(body.data[0].name).to.equal('second_video_channel')
expect(body.data[0].displayName).to.equal('video channel updated')
expect(body.data[0].description).to.equal('video channel description updated')
expect(body.data[0].support).to.equal('support updated')
}
})
it('Should not have updated the video support field', async function () {
for (const server of servers) {
2021-07-16 02:04:35 -05:00
const video = await server.videos.get({ id: videoUUID })
expect(video.support).to.equal('video support field')
}
})
it('Should update another accounts video channel', async function () {
this.timeout(15000)
const result = await servers[0].users.generate('second_user')
secondUserChannelName = result.userChannelName
await servers[0].videos.quickUpload({ name: 'video', token: result.token })
const videoChannelAttributes = {
displayName: 'video channel updated',
description: 'video channel description updated',
support: 'support updated'
}
await servers[0].channels.update({ channelName: secondUserChannelName, attributes: videoChannelAttributes })
await waitJobs(servers)
})
it('Should have another accounts video channel updated', async function () {
for (const server of servers) {
const body = await server.channels.get({ channelName: `${secondUserChannelName}@${servers[0].host}` })
expect(body.displayName).to.equal('video channel updated')
expect(body.description).to.equal('video channel description updated')
expect(body.support).to.equal('support updated')
}
})
it('Should update the channel support field and update videos too', async function () {
this.timeout(35000)
const videoChannelAttributes = {
support: 'video channel support text updated',
bulkVideosSupportUpdate: true
}
2021-07-16 02:04:35 -05:00
await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
await waitJobs(servers)
for (const server of servers) {
2021-07-16 02:04:35 -05:00
const video = await server.videos.get({ id: videoUUID })
expect(video.support).to.equal(videoChannelAttributes.support)
}
2017-10-24 12:41:30 -05:00
})
it('Should update video channel avatar', async function () {
2021-04-07 03:36:13 -05:00
this.timeout(15000)
const fixture = 'avatar.png'
2021-07-16 02:04:35 -05:00
await servers[0].channels.updateImage({
2021-07-09 04:21:30 -05:00
channelName: 'second_video_channel',
2021-04-07 03:36:13 -05:00
fixture,
type: 'avatar'
})
await waitJobs(servers)
2021-04-07 03:36:13 -05:00
for (const server of servers) {
const videoChannel = await findChannel(server, secondVideoChannelId)
const expectedSizes = ACTOR_IMAGES_SIZE[ActorImageType.AVATAR]
2021-04-07 03:36:13 -05:00
expect(videoChannel.avatars.length).to.equal(expectedSizes.length, 'Expected avatars to be generated in all sizes')
2021-04-08 04:23:45 -05:00
for (const avatar of videoChannel.avatars) {
avatarPaths[server.port] = avatar.path
await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatarPaths[server.port], '.png')
await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
const row = await server.sql.getActorImage(basename(avatarPaths[server.port]))
expect(expectedSizes.some(({ height, width }) => row.height === height && row.width === width)).to.equal(true)
}
2021-04-07 03:36:13 -05:00
}
})
2021-04-07 03:36:13 -05:00
it('Should update video channel banner', async function () {
this.timeout(15000)
const fixture = 'banner.jpg'
2021-07-16 02:04:35 -05:00
await servers[0].channels.updateImage({
2021-07-09 04:21:30 -05:00
channelName: 'second_video_channel',
2021-04-07 03:36:13 -05:00
fixture,
type: 'banner'
})
await waitJobs(servers)
for (const server of servers) {
2021-07-16 02:04:35 -05:00
const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
bannerPaths[server.port] = videoChannel.banners[0].path
2021-07-01 09:47:14 -05:00
await testImage(server.url, 'banner-resized', bannerPaths[server.port])
await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
2021-04-08 04:23:45 -05:00
2021-07-16 02:04:35 -05:00
const row = await server.sql.getActorImage(basename(bannerPaths[server.port]))
expect(row.height).to.equal(ACTOR_IMAGES_SIZE[ActorImageType.BANNER][0].height)
expect(row.width).to.equal(ACTOR_IMAGES_SIZE[ActorImageType.BANNER][0].width)
2021-04-07 03:36:13 -05:00
}
})
2022-03-21 03:26:48 -05:00
it('Should still correctly list channels', async function () {
{
const body = await servers[0].channels.list({ start: 1, count: 1, sort: 'createdAt' })
expect(body.total).to.equal(3)
expect(body.data).to.have.lengthOf(1)
expect(body.data[0].name).to.equal('second_video_channel')
}
{
const body = await servers[0].channels.listByAccount({ accountName, start: 1, count: 1, sort: 'createdAt' })
expect(body.total).to.equal(2)
expect(body.data).to.have.lengthOf(1)
expect(body.data[0].name).to.equal('second_video_channel')
}
})
2021-04-07 03:36:13 -05:00
it('Should delete the video channel avatar', async function () {
this.timeout(15000)
2021-07-16 02:04:35 -05:00
await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
2021-04-07 03:36:13 -05:00
await waitJobs(servers)
for (const server of servers) {
const videoChannel = await findChannel(server, secondVideoChannelId)
2021-07-01 09:47:14 -05:00
await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
2021-04-07 03:36:13 -05:00
expect(videoChannel.avatars).to.be.empty
}
})
2021-04-07 03:36:13 -05:00
it('Should delete the video channel banner', async function () {
this.timeout(15000)
2021-07-16 02:04:35 -05:00
await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
2017-10-24 12:41:30 -05:00
2021-04-07 03:36:13 -05:00
await waitJobs(servers)
for (const server of servers) {
const videoChannel = await findChannel(server, secondVideoChannelId)
2021-07-01 09:47:14 -05:00
await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
2021-04-07 03:36:13 -05:00
expect(videoChannel.banners).to.be.empty
2021-04-07 03:36:13 -05:00
}
2017-10-24 12:41:30 -05:00
})
2018-05-11 08:10:13 -05:00
it('Should list the second video channel videos', async function () {
2018-04-25 03:21:38 -05:00
this.timeout(10000)
for (const server of servers) {
2022-12-09 04:14:47 -06:00
const channelURI = 'second_video_channel@' + servers[0].host
2021-07-16 03:42:24 -05:00
const { total, data } = await server.videos.listByChannel({ handle: channelURI })
2021-07-15 03:02:54 -05:00
expect(total).to.equal(1)
expect(data).to.be.an('array')
expect(data).to.have.lengthOf(1)
expect(data[0].name).to.equal('my video name')
2018-05-11 08:10:13 -05:00
}
})
it('Should change the video channel of a video', async function () {
this.timeout(10000)
2021-07-16 02:04:35 -05:00
await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
2018-05-11 08:10:13 -05:00
await waitJobs(servers)
2018-05-11 08:10:13 -05:00
})
it('Should list the first video channel videos', async function () {
this.timeout(10000)
for (const server of servers) {
2021-07-15 03:02:54 -05:00
{
2022-12-09 04:14:47 -06:00
const secondChannelURI = 'second_video_channel@' + servers[0].host
2021-07-16 03:42:24 -05:00
const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
2021-07-15 03:02:54 -05:00
expect(total).to.equal(0)
}
{
2022-12-09 04:14:47 -06:00
const channelURI = 'root_channel@' + servers[0].host
2021-07-16 03:42:24 -05:00
const { total, data } = await server.videos.listByChannel({ handle: channelURI })
2021-07-15 03:02:54 -05:00
expect(total).to.equal(1)
expect(data).to.be.an('array')
expect(data).to.have.lengthOf(1)
expect(data[0].name).to.equal('my video name')
}
2018-04-25 03:21:38 -05:00
}
})
it('Should delete video channel', async function () {
2021-07-16 02:04:35 -05:00
await servers[0].channels.delete({ channelName: 'second_video_channel' })
2017-10-24 12:41:30 -05:00
})
it('Should have video channel deleted', async function () {
const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
2017-10-24 12:41:30 -05:00
expect(body.total).to.equal(2)
2021-07-09 04:21:30 -05:00
expect(body.data).to.be.an('array')
expect(body.data).to.have.lengthOf(2)
2021-07-09 04:21:30 -05:00
expect(body.data[0].displayName).to.equal('Main root channel')
expect(body.data[1].displayName).to.equal('video channel updated')
2018-08-17 08:45:42 -05:00
})
2022-08-03 04:49:39 -05:00
it('Should create the main channel with a suffix if there is a conflict', async function () {
2018-08-17 08:45:42 -05:00
{
const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
2021-07-16 02:04:35 -05:00
const created = await servers[0].channels.create({ attributes: videoChannel })
2021-07-09 04:21:30 -05:00
totoChannel = created.id
2018-08-17 08:45:42 -05:00
}
{
2021-07-16 02:04:35 -05:00
await servers[0].users.create({ username: 'toto', password: 'password' })
const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
2018-08-17 08:45:42 -05:00
2021-07-16 02:04:35 -05:00
const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
2022-08-03 04:49:39 -05:00
expect(videoChannels[0].name).to.equal('toto_channel-1')
2018-08-17 08:45:42 -05:00
}
2017-10-24 12:41:30 -05:00
})
2020-06-16 07:13:01 -05:00
it('Should report correct channel views per days', async function () {
this.timeout(10000)
2020-03-27 16:26:39 -05:00
{
2021-07-16 02:04:35 -05:00
const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
2020-06-16 07:13:01 -05:00
2021-07-09 04:21:30 -05:00
for (const channel of data) {
2020-03-27 16:26:39 -05:00
expect(channel).to.haveOwnProperty('viewsPerDay')
expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
2020-06-16 07:13:01 -05:00
for (const v of channel.viewsPerDay) {
2020-03-27 16:26:39 -05:00
expect(v.date).to.be.an('string')
expect(v.views).to.equal(0)
2020-06-16 07:13:01 -05:00
}
}
2020-03-27 16:26:39 -05:00
}
{
2021-07-16 02:04:35 -05:00
// video has been posted on channel servers[0].store.videoChannel.id since last update
await servers[0].views.simulateView({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
await servers[0].views.simulateView({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
2020-03-27 16:26:39 -05:00
// Wait the repeatable job
await wait(8000)
2021-07-16 02:04:35 -05:00
const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
2020-03-27 16:26:39 -05:00
expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
}
})
it('Should report correct total views count', async function () {
// check if there's the property
{
const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
for (const channel of data) {
expect(channel).to.haveOwnProperty('totalViews')
expect(channel.totalViews).to.be.a('number')
}
}
// Check if the totalViews count can be updated
{
const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
expect(channelWithView.totalViews).to.equal(2)
}
})
2020-06-16 07:13:01 -05:00
it('Should report correct videos count', async function () {
2021-07-16 02:04:35 -05:00
const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
2020-06-16 07:13:01 -05:00
2021-07-09 04:21:30 -05:00
const totoChannel = data.find(c => c.name === 'toto_channel')
const rootChannel = data.find(c => c.name === 'root_channel')
2020-06-16 07:13:01 -05:00
expect(rootChannel.videosCount).to.equal(1)
expect(totoChannel.videosCount).to.equal(0)
})
it('Should search among account video channels', async function () {
{
2021-07-16 02:04:35 -05:00
const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
2021-07-09 04:21:30 -05:00
expect(body.total).to.equal(1)
2021-07-09 04:21:30 -05:00
const channels = body.data
expect(channels).to.have.lengthOf(1)
}
{
2021-07-16 02:04:35 -05:00
const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
2021-07-09 04:21:30 -05:00
expect(body.total).to.equal(0)
2021-07-09 04:21:30 -05:00
const channels = body.data
expect(channels).to.have.lengthOf(0)
}
})
it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
this.timeout(30000)
2021-07-16 02:04:35 -05:00
await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
await waitJobs(servers)
for (const server of servers) {
2021-07-16 02:04:35 -05:00
const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
2021-07-09 04:21:30 -05:00
expect(data[0].name).to.equal('toto_channel')
expect(data[1].name).to.equal('root_channel')
}
2021-07-16 02:04:35 -05:00
await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
await waitJobs(servers)
for (const server of servers) {
2021-07-16 02:04:35 -05:00
const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
2021-07-09 04:21:30 -05:00
expect(data[0].name).to.equal('root_channel')
expect(data[1].name).to.equal('toto_channel')
}
})
2019-04-24 08:10:37 -05:00
after(async function () {
await cleanupTests(servers)
2017-10-24 12:41:30 -05:00
})
})