PeerTube/server/tests/api/server/plugins.ts

445 lines
13 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 */
2019-07-19 03:37:35 -05:00
import 'mocha'
import * as chai from 'chai'
import {
cleanupTests,
2019-07-19 07:36:04 -05:00
closeAllSequelize,
2019-07-19 03:37:35 -05:00
flushAndRunServer,
2020-01-31 09:56:52 -06:00
getConfig,
getMyUserInformation,
2019-07-19 07:36:04 -05:00
getPlugin,
2020-01-31 09:56:52 -06:00
getPluginPackageJSON,
2019-07-19 07:36:04 -05:00
getPluginRegisteredSettings,
getPluginsCSS,
2020-01-31 09:56:52 -06:00
getPublicSettings,
installPlugin,
killallServers,
2019-07-19 07:36:04 -05:00
listAvailablePlugins,
2020-01-31 09:56:52 -06:00
listPlugins,
reRunServer,
2019-07-19 07:36:04 -05:00
ServerInfo,
2019-07-19 03:37:35 -05:00
setAccessTokensToServers,
2020-01-31 09:56:52 -06:00
setPluginVersion,
uninstallPlugin,
updateCustomSubConfig,
updateMyUser,
updatePlugin,
updatePluginPackageJSON,
2019-07-19 07:36:04 -05:00
updatePluginSettings,
2020-04-30 02:28:39 -05:00
wait,
waitUntilLog
2019-07-19 03:37:35 -05:00
} from '../../../../shared/extra-utils'
2019-07-19 07:36:04 -05:00
import { PluginType } from '../../../../shared/models/plugins/plugin.type'
import { PeerTubePluginIndex } from '../../../../shared/models/plugins/peertube-plugin-index.model'
import { ServerConfig } from '../../../../shared/models/server'
2019-07-19 03:37:35 -05:00
import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model'
2019-07-19 07:36:04 -05:00
import { User } from '../../../../shared/models/users'
import { PluginPackageJson } from '../../../../shared/models/plugins/plugin-package-json.model'
2019-07-24 04:17:42 -05:00
import { RegisteredServerSettings } from '../../../../shared/models/plugins/register-server-setting.model'
2019-07-26 02:35:43 -05:00
import { PublicServerSetting } from '../../../../shared/models/plugins/public-server.setting'
2019-07-19 03:37:35 -05:00
const expect = chai.expect
describe('Test plugins', function () {
2019-07-19 07:36:04 -05:00
let server: ServerInfo = null
2019-07-19 03:37:35 -05:00
before(async function () {
this.timeout(30000)
2019-07-19 07:36:04 -05:00
const configOverride = {
plugins: {
index: { check_latest_versions_interval: '5 seconds' }
}
}
server = await flushAndRunServer(1, configOverride)
2019-07-19 03:37:35 -05:00
await setAccessTokensToServers([ server ])
2019-07-19 07:36:04 -05:00
})
it('Should list and search available plugins and themes', async function () {
this.timeout(30000)
2019-07-19 03:37:35 -05:00
{
2019-07-19 07:36:04 -05:00
const res = await listAvailablePlugins({
url: server.url,
accessToken: server.accessToken,
count: 1,
start: 0,
pluginType: PluginType.THEME,
search: 'background-red'
})
expect(res.body.total).to.be.at.least(1)
expect(res.body.data).to.have.lengthOf(1)
2019-07-19 03:37:35 -05:00
}
{
2019-07-19 07:36:04 -05:00
const res1 = await listAvailablePlugins({
url: server.url,
accessToken: server.accessToken,
count: 2,
start: 0,
sort: 'npmName'
})
const data1: PeerTubePluginIndex[] = res1.body.data
expect(res1.body.total).to.be.at.least(2)
expect(data1).to.have.lengthOf(2)
const res2 = await listAvailablePlugins({
url: server.url,
accessToken: server.accessToken,
count: 2,
start: 0,
sort: '-npmName'
})
const data2: PeerTubePluginIndex[] = res2.body.data
expect(res2.body.total).to.be.at.least(2)
expect(data2).to.have.lengthOf(2)
2020-01-31 09:56:52 -06:00
expect(data1[0].npmName).to.not.equal(data2[0].npmName)
2019-07-19 03:37:35 -05:00
}
2019-07-19 07:36:04 -05:00
{
const res = await listAvailablePlugins({
url: server.url,
accessToken: server.accessToken,
count: 10,
start: 0,
pluginType: PluginType.THEME,
search: 'background-red',
currentPeerTubeEngine: '1.0.0'
})
const data: PeerTubePluginIndex[] = res.body.data
2019-07-19 03:37:35 -05:00
2019-07-19 07:36:04 -05:00
const p = data.find(p => p.npmName === 'peertube-theme-background-red')
expect(p).to.be.undefined
}
2019-07-19 03:37:35 -05:00
})
it('Should have an empty global css', async function () {
2019-07-19 07:36:04 -05:00
const res = await getPluginsCSS(server.url)
expect(res.text).to.be.empty
2019-07-19 03:37:35 -05:00
})
it('Should install a plugin and a theme', async function () {
2019-07-19 07:36:04 -05:00
this.timeout(30000)
await installPlugin({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-plugin-hello-world'
})
2019-07-19 03:37:35 -05:00
2019-07-19 07:36:04 -05:00
await installPlugin({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-theme-background-red'
})
2019-07-19 03:37:35 -05:00
})
it('Should have the correct global css', async function () {
2019-07-19 07:36:04 -05:00
const res = await getPluginsCSS(server.url)
2020-04-30 02:28:39 -05:00
expect(res.text).to.contain('background-color: red')
2019-07-19 03:37:35 -05:00
})
it('Should have the plugin loaded in the configuration', async function () {
2019-07-19 07:36:04 -05:00
const res = await getConfig(server.url)
const config: ServerConfig = res.body
const theme = config.theme.registered.find(r => r.name === 'background-red')
expect(theme).to.not.be.undefined
const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
expect(plugin).to.not.be.undefined
2019-07-19 03:37:35 -05:00
})
it('Should update the default theme in the configuration', async function () {
2019-07-19 07:36:04 -05:00
await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
const res = await getConfig(server.url)
const config: ServerConfig = res.body
expect(config.theme.default).to.equal('background-red')
2019-07-19 03:37:35 -05:00
})
2019-07-19 07:36:04 -05:00
it('Should update my default theme', async function () {
await updateMyUser({
url: server.url,
accessToken: server.accessToken,
theme: 'background-red'
})
const res = await getMyUserInformation(server.url, server.accessToken)
expect((res.body as User).theme).to.equal('background-red')
2019-07-19 03:37:35 -05:00
})
2019-07-19 07:36:04 -05:00
it('Should list plugins and themes', async function () {
{
const res = await listPlugins({
url: server.url,
accessToken: server.accessToken,
count: 1,
start: 0,
pluginType: PluginType.THEME
})
const data: PeerTubePlugin[] = res.body.data
expect(res.body.total).to.be.at.least(1)
expect(data).to.have.lengthOf(1)
expect(data[0].name).to.equal('background-red')
}
{
const res = await listPlugins({
url: server.url,
accessToken: server.accessToken,
count: 2,
start: 0,
sort: 'name'
})
const data: PeerTubePlugin[] = res.body.data
expect(data[0].name).to.equal('background-red')
expect(data[1].name).to.equal('hello-world')
}
{
const res = await listPlugins({
url: server.url,
accessToken: server.accessToken,
count: 2,
start: 1,
sort: 'name'
})
const data: PeerTubePlugin[] = res.body.data
expect(data[0].name).to.equal('hello-world')
}
2019-07-19 03:37:35 -05:00
})
it('Should get registered settings', async function () {
2019-07-19 07:36:04 -05:00
const res = await getPluginRegisteredSettings({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-plugin-hello-world'
})
2019-07-26 02:35:43 -05:00
const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings
2019-07-19 07:36:04 -05:00
2019-07-26 02:35:43 -05:00
expect(registeredSettings).to.have.length.at.least(1)
2019-07-19 07:36:04 -05:00
2019-07-26 02:35:43 -05:00
const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name')
2019-07-19 07:36:04 -05:00
expect(adminNameSettings).to.not.be.undefined
2019-07-19 03:37:35 -05:00
})
2019-07-26 02:35:43 -05:00
it('Should get public settings', async function () {
const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })
const publicSettings = (res.body as PublicServerSetting).publicSettings
expect(Object.keys(publicSettings)).to.have.lengthOf(1)
expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
expect(publicSettings['user-name']).to.be.null
})
2019-07-19 03:37:35 -05:00
it('Should update the settings', async function () {
2019-07-19 07:36:04 -05:00
const settings = {
'admin-name': 'Cid'
}
await updatePluginSettings({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-plugin-hello-world',
settings
})
})
2020-04-30 02:28:39 -05:00
it('Should have watched settings changes', async function () {
this.timeout(10000)
await waitUntilLog(server, 'Settings changed!')
})
2019-07-19 07:36:04 -05:00
it('Should get a plugin and a theme', async function () {
{
const res = await getPlugin({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-plugin-hello-world'
})
const plugin: PeerTubePlugin = res.body
expect(plugin.type).to.equal(PluginType.PLUGIN)
expect(plugin.name).to.equal('hello-world')
expect(plugin.description).to.exist
expect(plugin.homepage).to.exist
expect(plugin.uninstalled).to.be.false
expect(plugin.enabled).to.be.true
expect(plugin.description).to.exist
expect(plugin.version).to.exist
expect(plugin.peertubeEngine).to.exist
expect(plugin.createdAt).to.exist
expect(plugin.settings).to.not.be.undefined
expect(plugin.settings['admin-name']).to.equal('Cid')
}
{
const res = await getPlugin({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-theme-background-red'
})
const plugin: PeerTubePlugin = res.body
2019-07-19 03:37:35 -05:00
2019-07-19 07:36:04 -05:00
expect(plugin.type).to.equal(PluginType.THEME)
expect(plugin.name).to.equal('background-red')
expect(plugin.description).to.exist
expect(plugin.homepage).to.exist
expect(plugin.uninstalled).to.be.false
expect(plugin.enabled).to.be.true
expect(plugin.description).to.exist
expect(plugin.version).to.exist
expect(plugin.peertubeEngine).to.exist
expect(plugin.createdAt).to.exist
expect(plugin.settings).to.be.null
}
2019-07-19 03:37:35 -05:00
})
it('Should update the plugin and the theme', async function () {
2019-07-19 07:36:04 -05:00
this.timeout(30000)
// Wait the scheduler that get the latest plugins versions
await wait(6000)
// Fake update our plugin version
await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
// Fake update package.json
const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
const oldVersion = packageJSON.version
packageJSON.version = '0.0.1'
await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
// Restart the server to take into account this change
killallServers([ server ])
await reRunServer(server)
{
const res = await listPlugins({
url: server.url,
accessToken: server.accessToken,
pluginType: PluginType.PLUGIN
})
const plugin: PeerTubePlugin = res.body.data[0]
expect(plugin.version).to.equal('0.0.1')
expect(plugin.latestVersion).to.exist
expect(plugin.latestVersion).to.not.equal('0.0.1')
}
{
await updatePlugin({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-plugin-hello-world'
})
const res = await listPlugins({
url: server.url,
accessToken: server.accessToken,
pluginType: PluginType.PLUGIN
})
const plugin: PeerTubePlugin = res.body.data[0]
expect(plugin.version).to.equal(oldVersion)
const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
expect(updatedPackageJSON.version).to.equal(oldVersion)
}
2019-07-19 03:37:35 -05:00
})
it('Should uninstall the plugin', async function () {
2019-07-19 07:36:04 -05:00
await uninstallPlugin({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-plugin-hello-world'
})
const res = await listPlugins({
url: server.url,
accessToken: server.accessToken,
pluginType: PluginType.PLUGIN
})
expect(res.body.total).to.equal(0)
expect(res.body.data).to.have.lengthOf(0)
2019-07-19 03:37:35 -05:00
})
it('Should have an empty global css', async function () {
2019-07-19 07:36:04 -05:00
const res = await getPluginsCSS(server.url)
expect(res.text).to.be.empty
2019-07-19 03:37:35 -05:00
})
it('Should list uninstalled plugins', async function () {
2019-07-19 07:36:04 -05:00
const res = await listPlugins({
url: server.url,
accessToken: server.accessToken,
pluginType: PluginType.PLUGIN,
uninstalled: true
})
expect(res.body.total).to.equal(1)
expect(res.body.data).to.have.lengthOf(1)
const plugin: PeerTubePlugin = res.body.data[0]
expect(plugin.name).to.equal('hello-world')
expect(plugin.enabled).to.be.false
expect(plugin.uninstalled).to.be.true
2019-07-19 03:37:35 -05:00
})
it('Should uninstall the theme', async function () {
2019-07-19 07:36:04 -05:00
await uninstallPlugin({
url: server.url,
accessToken: server.accessToken,
npmName: 'peertube-theme-background-red'
})
2019-07-19 03:37:35 -05:00
})
it('Should have updated the configuration', async function () {
// get /config (default theme + registered themes + registered plugins)
2019-07-19 07:36:04 -05:00
const res = await getConfig(server.url)
const config: ServerConfig = res.body
expect(config.theme.default).to.equal('default')
const theme = config.theme.registered.find(r => r.name === 'background-red')
expect(theme).to.be.undefined
const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
expect(plugin).to.be.undefined
})
it('Should have updated the user theme', async function () {
const res = await getMyUserInformation(server.url, server.accessToken)
expect((res.body as User).theme).to.equal('instance-default')
2019-07-19 03:37:35 -05:00
})
after(async function () {
2019-07-19 07:36:04 -05:00
await closeAllSequelize([ server ])
2019-07-19 03:37:35 -05:00
await cleanupTests([ server ])
})
})