X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Ftests%2Fapi%2Fserver%2Fplugins.ts;h=199d205c79e86cf0ac3fb2197cd2866b44239a0e;hb=d102de1b38f2877463529c3b27bd35ffef4fd8bf;hp=655cf00fae7679e255ea23e54c7c0e063ff6a6f4;hpb=9293139fde7091e9badcafa9b570b83cea9a10ad;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/server/plugins.ts b/server/tests/api/server/plugins.ts index 655cf00fa..199d205c7 100644 --- a/server/tests/api/server/plugins.ts +++ b/server/tests/api/server/plugins.ts @@ -1,28 +1,24 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import 'mocha' -import * as chai from 'chai' -import { HttpStatusCode } from '@shared/core-utils' +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, - flushAndRunServer, - getMyUserInformation, + createSingleServer, killallServers, + makeGetRequest, + PeerTubeServer, PluginsCommand, - reRunServer, - ServerInfo, - setAccessTokensToServers, - testHelloWorldRegisteredSettings, - updateMyUser, - wait, - waitUntilLog -} from '@shared/extra-utils' -import { PluginType, User } from '@shared/models' - -const expect = chai.expect + 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 () { @@ -33,10 +29,12 @@ describe('Test plugins', function () { index: { check_latest_versions_interval: '5 seconds' } } } - server = await flushAndRunServer(1, configOverride) + server = await createSingleServer(1, configOverride) await setAccessTokensToServers([ server ]) - command = server.pluginsCommand + command = server.plugins + + sqlCommand = new SQLCommand(server) }) it('Should list and search available plugins and themes', async function () { @@ -100,35 +98,34 @@ describe('Test plugins', function () { }) it('Should have the plugin loaded in the configuration', async function () { - const config = await server.configCommand.getConfig() - - 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 server.configCommand.updateCustomSubConfig({ + await server.config.updateCustomSubConfig({ newConfig: { theme: { default: 'background-red' } } }) - const config = await server.configCommand.getConfig() - 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 () { @@ -146,13 +143,12 @@ describe('Test plugins', function () { } { - const body = await command.list({ + const { data } = await command.list({ count: 2, start: 0, sort: 'name' }) - const data = body expect(data[0].name).to.equal('background-red') expect(data[1].name).to.equal('hello-world') } @@ -195,7 +191,7 @@ describe('Test plugins', function () { it('Should have watched settings changes', async function () { this.timeout(10000) - await waitUntilLog(server, 'Settings changed!') + await server.servers.waitUntilLog('Settings changed!') }) it('Should get a plugin and a theme', async function () { @@ -236,45 +232,62 @@ describe('Test plugins', function () { }) it('Should update the plugin and the theme', async function () { - this.timeout(90000) + this.timeout(180000) // Wait the scheduler that get the latest plugins versions await wait(6000) - // Fake update our plugin version - await server.sqlCommand.setPluginVersion('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 = await command.getPackageJSON('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 command.updatePackageJSON('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 - await killallServers([ server ]) - await reRunServer(server) + // Restart the server to take into account this change + await killallServers([ server ]) + await server.run() - { - const body = await command.list({ pluginType: PluginType.PLUGIN }) + 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) + } + } - const plugin = 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') - } + const getPluginFromAPI = async () => { + const body = await command.list({ pluginType: type === 'plugin' ? PluginType.PLUGIN : PluginType.THEME }) - { - await command.update({ npmName: 'peertube-plugin-hello-world' }) + return body.data.find(p => p.name === name) + } - const body = await command.list({ 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') + + await checkConfig('0.0.1') + } - const plugin = body.data[0] - expect(plugin.version).to.equal(oldVersion) + { + await command.update({ npmName: `peertube-${type}-${name}` }) - const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world') - expect(updatedPackageJSON.version).to.equal(oldVersion) + const plugin = await getPluginFromAPI() + expect(plugin.version).to.equal(oldVersion) + + const updatedPackageJSON = await command.getPackageJSON(`peertube-${type}-${name}`) + expect(updatedPackageJSON.version).to.equal(oldVersion) + + await checkConfig(oldVersion) + } } + + await testUpdate('theme', 'background-red') + await testUpdate('plugin', 'hello-world') }) it('Should uninstall the plugin', async function () { @@ -301,20 +314,20 @@ describe('Test plugins', function () { }) it('Should have updated the configuration', async function () { - const config = await server.configCommand.getConfig() - - 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 () { @@ -334,12 +347,65 @@ describe('Test plugins', function () { await check() await killallServers([ server ]) - await reRunServer(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 sqlCommand.cleanup() + await cleanupTests([ server ]) }) })