-/* tslint:disable:no-unused-expression */
-
-import 'mocha'
-import * as chai from 'chai'
+/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
+
+import { expect } from 'chai'
+import { pathExists, remove } from 'fs-extra'
+import { join } from 'path'
+import { SQLCommand, testHelloWorldRegisteredSettings } from '@server/tests/shared'
+import { wait } from '@shared/core-utils'
+import { HttpStatusCode, PluginType } from '@shared/models'
import {
cleanupTests,
- closeAllSequelize,
- flushAndRunServer,
- getConfig, getMyUserInformation, getPluginPackageJSON,
- getPlugin,
- getPluginRegisteredSettings,
- getPluginsCSS,
- installPlugin, killallServers,
- listAvailablePlugins,
- listPlugins, reRunServer,
- ServerInfo,
- setAccessTokensToServers,
- setPluginVersion, uninstallPlugin,
- updateCustomSubConfig, updateMyUser, updatePluginPackageJSON, updatePlugin,
- updatePluginSettings,
- wait
-} from '../../../../shared/extra-utils'
-import { PluginType } from '../../../../shared/models/plugins/plugin.type'
-import { PeerTubePluginIndex } from '../../../../shared/models/plugins/peertube-plugin-index.model'
-import { ServerConfig } from '../../../../shared/models/server'
-import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model'
-import { User } from '../../../../shared/models/users'
-import { PluginPackageJson } from '../../../../shared/models/plugins/plugin-package-json.model'
-import { RegisteredServerSettings } from '../../../../shared/models/plugins/register-server-setting.model'
-
-const expect = chai.expect
+ createSingleServer,
+ killallServers,
+ makeGetRequest,
+ PeerTubeServer,
+ PluginsCommand,
+ setAccessTokensToServers
+} from '@shared/server-commands'
describe('Test plugins', function () {
- let server: ServerInfo = null
+ let server: PeerTubeServer
+ let sqlCommand: SQLCommand
+ let command: PluginsCommand
before(async function () {
this.timeout(30000)
index: { check_latest_versions_interval: '5 seconds' }
}
}
- server = await flushAndRunServer(1, configOverride)
+ server = await createSingleServer(1, configOverride)
await setAccessTokensToServers([ server ])
+
+ command = server.plugins
+
+ sqlCommand = new SQLCommand(server)
})
it('Should list and search available plugins and themes', async function () {
this.timeout(30000)
{
- const res = await listAvailablePlugins({
- url: server.url,
- accessToken: server.accessToken,
+ const body = await command.listAvailable({
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)
+ expect(body.total).to.be.at.least(1)
+ expect(body.data).to.have.lengthOf(1)
}
{
- const res1 = await listAvailablePlugins({
- url: server.url,
- accessToken: server.accessToken,
+ const body1 = await command.listAvailable({
count: 2,
start: 0,
sort: 'npmName'
})
- const data1: PeerTubePluginIndex[] = res1.body.data
+ expect(body1.total).to.be.at.least(2)
- expect(res1.body.total).to.be.at.least(2)
+ const data1 = body1.data
expect(data1).to.have.lengthOf(2)
- const res2 = await listAvailablePlugins({
- url: server.url,
- accessToken: server.accessToken,
+ const body2 = await command.listAvailable({
count: 2,
start: 0,
sort: '-npmName'
})
- const data2: PeerTubePluginIndex[] = res2.body.data
+ expect(body2.total).to.be.at.least(2)
- expect(res2.body.total).to.be.at.least(2)
+ const data2 = body2.data
expect(data2).to.have.lengthOf(2)
- expect(data1[0].npmName).to.not.equal(data2[ 0 ].npmName)
+ expect(data1[0].npmName).to.not.equal(data2[0].npmName)
}
{
- const res = await listAvailablePlugins({
- url: server.url,
- accessToken: server.accessToken,
+ const body = await command.listAvailable({
count: 10,
start: 0,
pluginType: PluginType.THEME,
search: 'background-red',
currentPeerTubeEngine: '1.0.0'
})
- const data: PeerTubePluginIndex[] = res.body.data
- const p = data.find(p => p.npmName === 'peertube-theme-background-red')
+ const p = body.data.find(p => p.npmName === 'peertube-theme-background-red')
expect(p).to.be.undefined
}
})
- it('Should have an empty global css', async function () {
- const res = await getPluginsCSS(server.url)
-
- expect(res.text).to.be.empty
- })
-
it('Should install a plugin and a theme', async function () {
this.timeout(30000)
- await installPlugin({
- url: server.url,
- accessToken: server.accessToken,
- npmName: 'peertube-plugin-hello-world'
- })
-
- await installPlugin({
- url: server.url,
- accessToken: server.accessToken,
- npmName: 'peertube-theme-background-red'
- })
- })
-
- it('Should have the correct global css', async function () {
- const res = await getPluginsCSS(server.url)
-
- expect(res.text).to.contain('--mainBackgroundColor')
+ await command.install({ npmName: 'peertube-plugin-hello-world' })
+ await command.install({ npmName: 'peertube-theme-background-red' })
})
it('Should have the plugin loaded in the configuration', async function () {
- 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
+ for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
+ const theme = config.theme.registered.find(r => r.name === 'background-red')
+ expect(theme).to.not.be.undefined
+ expect(theme.npmName).to.equal('peertube-theme-background-red')
+
+ const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
+ expect(plugin).to.not.be.undefined
+ expect(plugin.npmName).to.equal('peertube-plugin-hello-world')
+ }
})
it('Should update the default theme in the configuration', async function () {
- await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
-
- const res = await getConfig(server.url)
- const config: ServerConfig = res.body
+ await server.config.updateCustomSubConfig({
+ newConfig: {
+ theme: { default: 'background-red' }
+ }
+ })
- expect(config.theme.default).to.equal('background-red')
+ for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
+ expect(config.theme.default).to.equal('background-red')
+ }
})
it('Should update my default theme', async function () {
- await updateMyUser({
- url: server.url,
- accessToken: server.accessToken,
- theme: 'background-red'
- })
+ await server.users.updateMe({ theme: 'background-red' })
- const res = await getMyUserInformation(server.url, server.accessToken)
- expect((res.body as User).theme).to.equal('background-red')
+ const user = await server.users.getMyInfo()
+ expect(user.theme).to.equal('background-red')
})
it('Should list plugins and themes', async function () {
{
- const res = await listPlugins({
- url: server.url,
- accessToken: server.accessToken,
+ const body = await command.list({
count: 1,
start: 0,
pluginType: PluginType.THEME
})
- const data: PeerTubePlugin[] = res.body.data
+ expect(body.total).to.be.at.least(1)
- expect(res.body.total).to.be.at.least(1)
+ const data = body.data
expect(data).to.have.lengthOf(1)
expect(data[0].name).to.equal('background-red')
}
{
- const res = await listPlugins({
- url: server.url,
- accessToken: server.accessToken,
+ const { data } = await command.list({
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,
+ const body = await command.list({
count: 2,
start: 1,
sort: 'name'
})
- const data: PeerTubePlugin[] = res.body.data
- expect(data[0].name).to.equal('hello-world')
+ expect(body.data[0].name).to.equal('hello-world')
}
})
it('Should get registered settings', async function () {
- const res = await getPluginRegisteredSettings({
- url: server.url,
- accessToken: server.accessToken,
- npmName: 'peertube-plugin-hello-world'
- })
-
- const settings = (res.body as RegisteredServerSettings).settings
+ await testHelloWorldRegisteredSettings(server)
+ })
- expect(settings).to.have.length.at.least(1)
+ it('Should get public settings', async function () {
+ const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
+ const publicSettings = body.publicSettings
- const adminNameSettings = settings.find(s => s.name === 'admin-name')
- expect(adminNameSettings).to.not.be.undefined
+ expect(Object.keys(publicSettings)).to.have.lengthOf(1)
+ expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
+ expect(publicSettings['user-name']).to.be.null
})
it('Should update the settings', async function () {
'admin-name': 'Cid'
}
- await updatePluginSettings({
- url: server.url,
- accessToken: server.accessToken,
+ await command.updateSettings({
npmName: 'peertube-plugin-hello-world',
settings
})
})
+ it('Should have watched settings changes', async function () {
+ await server.servers.waitUntilLog('Settings changed!')
+ })
+
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
+ const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
expect(plugin.type).to.equal(PluginType.PLUGIN)
expect(plugin.name).to.equal('hello-world')
}
{
- const res = await getPlugin({
- url: server.url,
- accessToken: server.accessToken,
- npmName: 'peertube-theme-background-red'
- })
-
- const plugin: PeerTubePlugin = res.body
+ const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
expect(plugin.type).to.equal(PluginType.THEME)
expect(plugin.name).to.equal('background-red')
})
it('Should update the plugin and the theme', async function () {
- this.timeout(30000)
+ this.timeout(180000)
// 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')
+ async function testUpdate (type: 'plugin' | 'theme', name: string) {
+ // Fake update our plugin version
+ await sqlCommand.setPluginVersion(name, '0.0.1')
- // Fake update package.json
- const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
- const oldVersion = packageJSON.version
+ // Fake update package.json
+ const packageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
+ const oldVersion = packageJSON.version
- packageJSON.version = '0.0.1'
- await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
+ packageJSON.version = '0.0.1'
+ await command.updatePackageJSON(`peertube-${type}-${name}`, packageJSON)
- // Restart the server to take into account this change
- killallServers([ server ])
- await reRunServer(server)
+ // Restart the server to take into account this change
+ await killallServers([ server ])
+ await server.run()
- {
- const res = await listPlugins({
- url: server.url,
- accessToken: server.accessToken,
- pluginType: PluginType.PLUGIN
- })
-
- const plugin: PeerTubePlugin = res.body.data[0]
+ const checkConfig = async (version: string) => {
+ for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
+ expect(config[type].registered.find(r => r.name === name).version).to.equal(version)
+ }
+ }
- expect(plugin.version).to.equal('0.0.1')
- expect(plugin.latestVersion).to.exist
- expect(plugin.latestVersion).to.not.equal('0.0.1')
- }
+ const getPluginFromAPI = async () => {
+ const body = await command.list({ pluginType: type === 'plugin' ? PluginType.PLUGIN : PluginType.THEME })
- {
- await updatePlugin({
- url: server.url,
- accessToken: server.accessToken,
- npmName: 'peertube-plugin-hello-world'
- })
+ return body.data.find(p => p.name === name)
+ }
- const res = await listPlugins({
- url: server.url,
- accessToken: server.accessToken,
- pluginType: PluginType.PLUGIN
- })
+ {
+ const plugin = await getPluginFromAPI()
+ expect(plugin.version).to.equal('0.0.1')
+ expect(plugin.latestVersion).to.exist
+ expect(plugin.latestVersion).to.not.equal('0.0.1')
- const plugin: PeerTubePlugin = res.body.data[0]
+ await checkConfig('0.0.1')
+ }
- expect(plugin.version).to.equal(oldVersion)
+ {
+ await command.update({ npmName: `peertube-${type}-${name}` })
- const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
- expect(updatedPackageJSON.version).to.equal(oldVersion)
- }
- })
+ const plugin = await getPluginFromAPI()
+ expect(plugin.version).to.equal(oldVersion)
- it('Should uninstall the plugin', async function () {
- await uninstallPlugin({
- url: server.url,
- accessToken: server.accessToken,
- npmName: 'peertube-plugin-hello-world'
- })
+ const updatedPackageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
+ expect(updatedPackageJSON.version).to.equal(oldVersion)
- const res = await listPlugins({
- url: server.url,
- accessToken: server.accessToken,
- pluginType: PluginType.PLUGIN
- })
+ await checkConfig(oldVersion)
+ }
+ }
- expect(res.body.total).to.equal(0)
- expect(res.body.data).to.have.lengthOf(0)
+ await testUpdate('theme', 'background-red')
+ await testUpdate('plugin', 'hello-world')
})
- it('Should have an empty global css', async function () {
- const res = await getPluginsCSS(server.url)
+ it('Should uninstall the plugin', async function () {
+ await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
- expect(res.text).to.be.empty
+ const body = await command.list({ pluginType: PluginType.PLUGIN })
+ expect(body.total).to.equal(0)
+ expect(body.data).to.have.lengthOf(0)
})
it('Should list uninstalled plugins', async function () {
- 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 body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
+ expect(body.total).to.equal(1)
+ expect(body.data).to.have.lengthOf(1)
- const plugin: PeerTubePlugin = res.body.data[0]
+ const plugin = body.data[0]
expect(plugin.name).to.equal('hello-world')
expect(plugin.enabled).to.be.false
expect(plugin.uninstalled).to.be.true
})
it('Should uninstall the theme', async function () {
- await uninstallPlugin({
- url: server.url,
- accessToken: server.accessToken,
- npmName: 'peertube-theme-background-red'
- })
+ await command.uninstall({ npmName: 'peertube-theme-background-red' })
})
it('Should have updated the configuration', async function () {
- // get /config (default theme + registered themes + registered plugins)
- const res = await getConfig(server.url)
- const config: ServerConfig = res.body
-
- expect(config.theme.default).to.equal('default')
+ for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
+ expect(config.theme.default).to.equal('default')
- const theme = config.theme.registered.find(r => r.name === 'background-red')
- expect(theme).to.be.undefined
+ 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
+ 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')
+ const user = await server.users.getMyInfo()
+ expect(user.theme).to.equal('instance-default')
+ })
+
+ it('Should not install a broken plugin', async function () {
+ this.timeout(60000)
+
+ async function check () {
+ const body = await command.list({ pluginType: PluginType.PLUGIN })
+ const plugins = body.data
+ expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
+ }
+
+ await command.install({
+ path: PluginsCommand.getPluginTestPath('-broken'),
+ expectedStatus: HttpStatusCode.BAD_REQUEST_400
+ })
+
+ await check()
+
+ await killallServers([ server ])
+ await server.run()
+
+ await check()
+ })
+
+ it('Should rebuild native modules on Node ABI change', async function () {
+ this.timeout(60000)
+
+ const removeNativeModule = async () => {
+ await remove(join(baseNativeModule, 'build'))
+ await remove(join(baseNativeModule, 'prebuilds'))
+ }
+
+ await command.install({ path: PluginsCommand.getPluginTestPath('-native') })
+
+ await makeGetRequest({
+ url: server.url,
+ path: '/plugins/test-native/router',
+ expectedStatus: HttpStatusCode.NO_CONTENT_204
+ })
+
+ const query = `UPDATE "application" SET "nodeABIVersion" = 1`
+ await sqlCommand.updateQuery(query)
+
+ const baseNativeModule = server.servers.buildDirectory(join('plugins', 'node_modules', 'a-native-example'))
+
+ await removeNativeModule()
+ await server.kill()
+ await server.run()
+
+ await wait(3000)
+
+ expect(await pathExists(join(baseNativeModule, 'build'))).to.be.true
+ expect(await pathExists(join(baseNativeModule, 'prebuilds'))).to.be.true
+
+ await makeGetRequest({
+ url: server.url,
+ path: '/plugins/test-native/router',
+ expectedStatus: HttpStatusCode.NO_CONTENT_204
+ })
+
+ await removeNativeModule()
+
+ await server.kill()
+ await server.run()
+
+ expect(await pathExists(join(baseNativeModule, 'build'))).to.be.false
+ expect(await pathExists(join(baseNativeModule, 'prebuilds'))).to.be.false
+
+ await makeGetRequest({
+ url: server.url,
+ path: '/plugins/test-native/router',
+ expectedStatus: HttpStatusCode.NOT_FOUND_404
+ })
})
after(async function () {
- await closeAllSequelize([ server ])
+ await sqlCommand.cleanup()
+
await cleanupTests([ server ])
})
})