From 2769876fb26742f5cc8aa4b761be7bafca97d18d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 17 Mar 2022 09:09:06 +0100 Subject: [PATCH] Fix client html cache on theme update --- server/lib/client-html.ts | 5 +- server/lib/plugins/plugin-manager.ts | 8 +- server/tests/api/check-params/video-editor.ts | 3 + server/tests/api/server/plugins.ts | 108 ++++++++++-------- .../server-commands/server/config-command.ts | 15 +++ 5 files changed, 87 insertions(+), 52 deletions(-) diff --git a/server/lib/client-html.ts b/server/lib/client-html.ts index 0142b248b..38697401a 100644 --- a/server/lib/client-html.ts +++ b/server/lib/client-html.ts @@ -12,7 +12,6 @@ import { HTMLServerConfig } from '@shared/models' import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n' import { HttpStatusCode } from '../../shared/models/http/http-error-codes' import { VideoPlaylistPrivacy, VideoPrivacy } from '../../shared/models/videos' -import { isTestInstance } from '../helpers/core-utils' import { logger } from '../helpers/logger' import { CONFIG } from '../initializers/config' import { @@ -231,7 +230,7 @@ class ClientHtml { static async getEmbedHTML () { const path = ClientHtml.getEmbedPath() - if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] + if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] const buffer = await readFile(path) const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig() @@ -303,7 +302,7 @@ class ClientHtml { private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) { const path = ClientHtml.getIndexPath(req, res, paramLang) - if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] + if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] const buffer = await readFile(path) const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig() diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 39e7f9a5b..1305f660f 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts @@ -277,6 +277,8 @@ export class PluginManager implements ServerHook { logger.info('Regenerating registered plugin CSS to global file.') await this.regeneratePluginGlobalCSS() } + + ClientHtml.invalidCache() } // ###################### Installation ###################### @@ -419,6 +421,8 @@ export class PluginManager implements ServerHook { } await this.addTranslations(plugin, npmName, packageJSON.translations) + + ClientHtml.invalidCache() } private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJSON) { @@ -473,8 +477,6 @@ export class PluginManager implements ServerHook { // ###################### CSS ###################### private resetCSSGlobalFile () { - ClientHtml.invalidCache() - return outputFile(PLUGIN_GLOBAL_CSS_PATH, '') } @@ -482,8 +484,6 @@ export class PluginManager implements ServerHook { for (const cssPath of cssRelativePaths) { await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH) } - - ClientHtml.invalidCache() } private concatFiles (input: string, output: string) { diff --git a/server/tests/api/check-params/video-editor.ts b/server/tests/api/check-params/video-editor.ts index 1fd22c497..5f5faf8fb 100644 --- a/server/tests/api/check-params/video-editor.ts +++ b/server/tests/api/check-params/video-editor.ts @@ -122,7 +122,10 @@ describe('Test video editor API validator', function () { }) it('Should fail with an already in transcoding state video', async function () { + this.timeout(30000) + const { uuid } = await server.videos.quickUpload({ name: 'transcoded video' }) + await waitJobs([ server ]) await server.jobs.pauseJobQueue() await server.videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' }) diff --git a/server/tests/api/server/plugins.ts b/server/tests/api/server/plugins.ts index 76d3e2481..8aa34fb15 100644 --- a/server/tests/api/server/plugins.ts +++ b/server/tests/api/server/plugins.ts @@ -95,15 +95,15 @@ describe('Test plugins', function () { }) it('Should have the plugin loaded in the configuration', async function () { - const config = await server.config.getConfig() + 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 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') + 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 () { @@ -113,8 +113,9 @@ describe('Test plugins', function () { } }) - const config = await server.config.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 () { @@ -228,45 +229,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.sql.setPluginVersion('hello-world', '0.0.1') + async function testUpdate (type: 'plugin' | 'theme', name: string) { + // Fake update our plugin version + await server.sql.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 server.run() + // 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 }) + + return body.data.find(p => p.name === name) + } + + { + 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') + } + + { + await command.update({ npmName: `peertube-${type}-${name}` }) + + 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 command.update({ npmName: 'peertube-plugin-hello-world' }) - - const body = await command.list({ pluginType: PluginType.PLUGIN }) - - const plugin = body.data[0] - expect(plugin.version).to.equal(oldVersion) - - const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world') - expect(updatedPackageJSON.version).to.equal(oldVersion) - } + await testUpdate('theme', 'background-red') + await testUpdate('plugin', 'hello-world') }) it('Should uninstall the plugin', async function () { @@ -293,15 +311,15 @@ describe('Test plugins', function () { }) it('Should have updated the configuration', async function () { - const config = await server.config.getConfig() + for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) { + expect(config.theme.default).to.equal('default') - 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 () { diff --git a/shared/server-commands/server/config-command.ts b/shared/server-commands/server/config-command.ts index e47a0d346..1dd6e1ea4 100644 --- a/shared/server-commands/server/config-command.ts +++ b/shared/server-commands/server/config-command.ts @@ -123,6 +123,21 @@ export class ConfigCommand extends AbstractCommand { }) } + async getIndexHTMLConfig (options: OverrideCommandOptions = {}) { + const text = await this.getRequestText({ + ...options, + + path: '/', + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + + const match = text.match('') + + // We parse the string twice, first to extract the string and then to extract the JSON + return JSON.parse(JSON.parse(match[1])) as ServerConfig + } + getAbout (options: OverrideCommandOptions = {}) { const path = '/api/v1/config/about'