From ae2abfd3aed3e75d39a316b49b914d187faa7475 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 7 Jul 2021 10:33:49 +0200 Subject: [PATCH] Introduce plugins command --- server/tests/api/check-params/plugins.ts | 5 +- .../api/notifications/admin-notifications.ts | 31 +- server/tests/api/server/plugins.ts | 188 +++-------- server/tests/api/users/users.ts | 3 +- server/tests/cli/plugins.ts | 4 +- server/tests/external-plugins/auth-ldap.ts | 28 +- .../external-plugins/auto-block-videos.ts | 19 +- server/tests/external-plugins/auto-mute.ts | 40 +-- server/tests/plugins/action-hooks.ts | 9 +- server/tests/plugins/external-auth.ts | 47 +-- server/tests/plugins/filter-hooks.ts | 16 +- server/tests/plugins/html-injection.ts | 33 +- server/tests/plugins/id-and-pass-auth.ts | 36 +-- server/tests/plugins/plugin-helpers.ts | 24 +- server/tests/plugins/plugin-router.ts | 27 +- server/tests/plugins/plugin-storage.ts | 29 +- server/tests/plugins/plugin-transcoding.ts | 26 +- server/tests/plugins/plugin-unloading.ts | 32 +- server/tests/plugins/translations.ts | 46 +-- server/tests/plugins/video-constants.ts | 28 +- shared/extra-utils/index.ts | 1 - shared/extra-utils/server/index.ts | 2 + shared/extra-utils/server/plugins-command.ts | 245 +++++++++++++++ shared/extra-utils/server/plugins.ts | 297 +----------------- shared/extra-utils/server/servers.ts | 3 + shared/extra-utils/shared/abstract-command.ts | 26 +- 26 files changed, 480 insertions(+), 765 deletions(-) create mode 100644 shared/extra-utils/server/plugins-command.ts diff --git a/server/tests/api/check-params/plugins.ts b/server/tests/api/check-params/plugins.ts index a833fe6ff..d372221d0 100644 --- a/server/tests/api/check-params/plugins.ts +++ b/server/tests/api/check-params/plugins.ts @@ -10,7 +10,6 @@ import { createUser, flushAndRunServer, immutableAssign, - installPlugin, makeGetRequest, makePostBodyRequest, makePutBodyRequest, @@ -50,13 +49,13 @@ describe('Test server plugins API validators', function () { userAccessToken = await userLogin(server, user) { - const res = await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: npmPlugin }) + const res = await server.pluginsCommand.install({ npmName: npmPlugin }) const plugin = res.body as PeerTubePlugin npmVersion = plugin.version } { - const res = await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: themePlugin }) + const res = await server.pluginsCommand.install({ npmName: themePlugin }) const plugin = res.body as PeerTubePlugin themeVersion = plugin.version } diff --git a/server/tests/api/notifications/admin-notifications.ts b/server/tests/api/notifications/admin-notifications.ts index 91681c9d6..da9767b74 100644 --- a/server/tests/api/notifications/admin-notifications.ts +++ b/server/tests/api/notifications/admin-notifications.ts @@ -2,18 +2,20 @@ import 'mocha' import { expect } from 'chai' -import { MockJoinPeerTubeVersions } from '@shared/extra-utils' -import { PluginType } from '@shared/models' -import { cleanupTests, installPlugin, setPluginLatestVersion, setPluginVersion, wait } from '../../../../shared/extra-utils' -import { ServerInfo } from '../../../../shared/extra-utils/index' -import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email' import { CheckerBaseParams, checkNewPeerTubeVersion, checkNewPluginVersion, - prepareNotificationsTest -} from '../../../../shared/extra-utils/users/user-notifications' -import { UserNotification, UserNotificationType } from '../../../../shared/models/users' + cleanupTests, + MockJoinPeerTubeVersions, + MockSmtpServer, + prepareNotificationsTest, + ServerInfo, + setPluginLatestVersion, + setPluginVersion, + wait +} from '@shared/extra-utils' +import { PluginType, UserNotification, UserNotificationType } from '@shared/models' describe('Test admin notifications', function () { let server: ServerInfo @@ -58,17 +60,8 @@ describe('Test admin notifications', function () { token: server.accessToken } - 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' - }) + await server.pluginsCommand.install({ npmName: 'peertube-plugin-hello-world' }) + await server.pluginsCommand.install({ npmName: 'peertube-theme-background-red' }) }) describe('Latest PeerTube version notification', function () { diff --git a/server/tests/api/server/plugins.ts b/server/tests/api/server/plugins.ts index 6b61c7c33..1536997d5 100644 --- a/server/tests/api/server/plugins.ts +++ b/server/tests/api/server/plugins.ts @@ -9,34 +9,25 @@ import { flushAndRunServer, getConfig, getMyUserInformation, - getPlugin, - getPluginPackageJSON, - getPluginTestPath, - getPublicSettings, - installPlugin, killallServers, - listAvailablePlugins, - listPlugins, + PluginsCommand, reRunServer, ServerInfo, setAccessTokensToServers, setPluginVersion, testHelloWorldRegisteredSettings, - uninstallPlugin, updateCustomSubConfig, updateMyUser, - updatePlugin, - updatePluginPackageJSON, - updatePluginSettings, wait, waitUntilLog } from '@shared/extra-utils' -import { PeerTubePlugin, PeerTubePluginIndex, PluginPackageJson, PluginType, PublicServerSetting, ServerConfig, User } from '@shared/models' +import { PluginType, ServerConfig, User } from '@shared/models' const expect = chai.expect describe('Test plugins', function () { let server: ServerInfo = null + let command: PluginsCommand before(async function () { this.timeout(30000) @@ -54,60 +45,51 @@ describe('Test plugins', 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) } { - 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 } }) @@ -115,17 +97,8 @@ describe('Test plugins', function () { 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' - }) + 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 () { @@ -161,45 +134,38 @@ describe('Test plugins', function () { 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 body = await command.list({ count: 2, start: 0, sort: 'name' }) - const data: PeerTubePlugin[] = res.body.data + const data = body 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') } }) @@ -208,9 +174,8 @@ describe('Test plugins', function () { }) 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 + const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' }) + const publicSettings = body.publicSettings expect(Object.keys(publicSettings)).to.have.lengthOf(1) expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ]) @@ -222,9 +187,7 @@ describe('Test plugins', function () { 'admin-name': 'Cid' } - await updatePluginSettings({ - url: server.url, - accessToken: server.accessToken, + await command.updateSettings({ npmName: 'peertube-plugin-hello-world', settings }) @@ -238,13 +201,7 @@ describe('Test plugins', function () { 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') @@ -262,13 +219,7 @@ describe('Test plugins', function () { } { - 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') @@ -295,92 +246,59 @@ describe('Test plugins', function () { await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1') // Fake update package.json - const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world') + const packageJSON = await command.getPackageJSON('peertube-plugin-hello-world') const oldVersion = packageJSON.version packageJSON.version = '0.0.1' - await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON) + await command.updatePackageJSON('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] + const body = await command.list({ pluginType: PluginType.PLUGIN }) + 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') } { - 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 - }) + await command.update({ npmName: 'peertube-plugin-hello-world' }) - const plugin: PeerTubePlugin = res.body.data[0] + const body = await command.list({ pluginType: PluginType.PLUGIN }) + const plugin = body.data[0] expect(plugin.version).to.equal(oldVersion) - const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world') + const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world') expect(updatedPackageJSON.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 res = await listPlugins({ - url: server.url, - accessToken: server.accessToken, - pluginType: PluginType.PLUGIN - }) + await command.uninstall({ npmName: 'peertube-plugin-hello-world' }) - expect(res.body.total).to.equal(0) - expect(res.body.data).to.have.lengthOf(0) + 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 - }) + const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true }) + expect(body.total).to.equal(1) + expect(body.data).to.have.lengthOf(1) - expect(res.body.total).to.equal(1) - expect(res.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 () { @@ -406,21 +324,13 @@ describe('Test plugins', function () { this.timeout(60000) async function check () { - const res = await listPlugins({ - url: server.url, - accessToken: server.accessToken, - pluginType: PluginType.PLUGIN - }) - - const plugins: PeerTubePlugin[] = res.body.data - + const body = await command.list({ pluginType: PluginType.PLUGIN }) + const plugins = body.data expect(plugins.find(p => p.name === 'test-broken')).to.not.exist } - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-broken'), + await command.install({ + path: PluginsCommand.getPluginTestPath('-broken'), expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index 92927ea97..ba4183e08 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts @@ -23,7 +23,6 @@ import { getUsersListPaginationAndSort, getVideoChannel, getVideosList, - installPlugin, killallServers, login, logout, @@ -75,7 +74,7 @@ describe('Test users', function () { await setAccessTokensToServers([ server ]) - await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-theme-background-red' }) + await server.pluginsCommand.install({ npmName: 'peertube-theme-background-red' }) }) describe('OAuth client', function () { diff --git a/server/tests/cli/plugins.ts b/server/tests/cli/plugins.ts index efdc20748..7b8746a5d 100644 --- a/server/tests/cli/plugins.ts +++ b/server/tests/cli/plugins.ts @@ -6,8 +6,8 @@ import { cleanupTests, flushAndRunServer, getConfig, - getPluginTestPath, killallServers, + PluginsCommand, reRunServer, ServerInfo, setAccessTokensToServers @@ -27,7 +27,7 @@ describe('Test plugin scripts', function () { it('Should install a plugin from stateless CLI', async function () { this.timeout(60000) - const packagePath = getPluginTestPath() + const packagePath = PluginsCommand.getPluginTestPath() await server.cliCommand.execWithEnv(`npm run plugin:install -- --plugin-path ${packagePath}`) }) diff --git a/server/tests/external-plugins/auth-ldap.ts b/server/tests/external-plugins/auth-ldap.ts index e4eae7e8c..0d4edbee0 100644 --- a/server/tests/external-plugins/auth-ldap.ts +++ b/server/tests/external-plugins/auth-ldap.ts @@ -3,17 +3,7 @@ import 'mocha' import { expect } from 'chai' import { User } from '@shared/models/users/user.model' -import { - blockUser, - getMyUserInformation, - installPlugin, - setAccessTokensToServers, - unblockUser, - uninstallPlugin, - updatePluginSettings, - uploadVideo, - userLogin -} from '../../../shared/extra-utils' +import { blockUser, getMyUserInformation, setAccessTokensToServers, unblockUser, uploadVideo, userLogin } from '../../../shared/extra-utils' import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers' describe('Official plugin auth-ldap', function () { @@ -27,11 +17,7 @@ describe('Official plugin auth-ldap', function () { server = await flushAndRunServer(1) await setAccessTokensToServers([ server ]) - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - npmName: 'peertube-plugin-auth-ldap' - }) + await server.pluginsCommand.install({ npmName: 'peertube-plugin-auth-ldap' }) }) it('Should not login with without LDAP settings', async function () { @@ -39,9 +25,7 @@ describe('Official plugin auth-ldap', function () { }) it('Should not login with bad LDAP settings', async function () { - await updatePluginSettings({ - url: server.url, - accessToken: server.accessToken, + await server.pluginsCommand.updateSettings({ npmName: 'peertube-plugin-auth-ldap', settings: { 'bind-credentials': 'GoodNewsEveryone', @@ -59,9 +43,7 @@ describe('Official plugin auth-ldap', function () { }) it('Should not login with good LDAP settings but wrong username/password', async function () { - await updatePluginSettings({ - url: server.url, - accessToken: server.accessToken, + await server.pluginsCommand.updateSettings({ npmName: 'peertube-plugin-auth-ldap', settings: { 'bind-credentials': 'GoodNewsEveryone', @@ -114,7 +96,7 @@ describe('Official plugin auth-ldap', function () { }) it('Should not login if the plugin is uninstalled', async function () { - await uninstallPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-plugin-auth-ldap' }) + await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-auth-ldap' }) await userLogin(server, { username: 'fry@planetexpress.com', password: 'fry' }, 400) }) diff --git a/server/tests/external-plugins/auto-block-videos.ts b/server/tests/external-plugins/auto-block-videos.ts index 18ea17d78..6baf37566 100644 --- a/server/tests/external-plugins/auto-block-videos.ts +++ b/server/tests/external-plugins/auto-block-videos.ts @@ -7,11 +7,9 @@ import { doubleFollow, getBlacklistedVideosList, getVideosList, - installPlugin, MockBlocklist, removeVideoFromBlacklist, setAccessTokensToServers, - updatePluginSettings, uploadVideoAndGetId, wait } from '../../../shared/extra-utils' @@ -28,11 +26,8 @@ async function check (server: ServerInfo, videoUUID: string, exists = true) { const video = res.body.data.find(v => v.uuid === videoUUID) - if (exists) { - expect(video).to.not.be.undefined - } else { - expect(video).to.be.undefined - } + if (exists) expect(video).to.not.be.undefined + else expect(video).to.be.undefined } describe('Official plugin auto-block videos', function () { @@ -49,11 +44,7 @@ describe('Official plugin auto-block videos', function () { await setAccessTokensToServers(servers) for (const server of servers) { - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - npmName: 'peertube-plugin-auto-block-videos' - }) + await server.pluginsCommand.install({ npmName: 'peertube-plugin-auto-block-videos' }) } blocklistServer = new MockBlocklist() @@ -78,9 +69,7 @@ describe('Official plugin auto-block videos', function () { }) it('Should update plugin settings', async function () { - await updatePluginSettings({ - url: servers[0].url, - accessToken: servers[0].accessToken, + await servers[0].pluginsCommand.updateSettings({ npmName: 'peertube-plugin-auto-block-videos', settings: { 'blocklist-urls': `http://localhost:${port}/blocklist`, diff --git a/server/tests/external-plugins/auto-mute.ts b/server/tests/external-plugins/auto-mute.ts index 09355d932..8fcf94452 100644 --- a/server/tests/external-plugins/auto-mute.ts +++ b/server/tests/external-plugins/auto-mute.ts @@ -2,30 +2,24 @@ import 'mocha' import { expect } from 'chai' +import { HttpStatusCode } from '@shared/core-utils' import { addAccountToServerBlocklist, addServerToAccountBlocklist, - removeAccountFromServerBlocklist -} from '@shared/extra-utils/users/blocklist' -import { + cleanupTests, doubleFollow, + flushAndRunMultipleServers, getVideosList, - installPlugin, + killallServers, makeGetRequest, MockBlocklist, + removeAccountFromServerBlocklist, + reRunServer, + ServerInfo, setAccessTokensToServers, - updatePluginSettings, uploadVideoAndGetId, wait -} from '../../../shared/extra-utils' -import { - cleanupTests, - flushAndRunMultipleServers, - killallServers, - reRunServer, - ServerInfo -} from '../../../shared/extra-utils/server/servers' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +} from '@shared/extra-utils' describe('Official plugin auto-mute', function () { const autoMuteListPath = '/plugins/auto-mute/router/api/v1/mute-list' @@ -40,11 +34,7 @@ describe('Official plugin auto-mute', function () { await setAccessTokensToServers(servers) for (const server of servers) { - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - npmName: 'peertube-plugin-auto-mute' - }) + await server.pluginsCommand.install({ npmName: 'peertube-plugin-auto-mute' }) } blocklistServer = new MockBlocklist() @@ -57,9 +47,7 @@ describe('Official plugin auto-mute', function () { }) it('Should update plugin settings', async function () { - await updatePluginSettings({ - url: servers[0].url, - accessToken: servers[0].accessToken, + await servers[0].pluginsCommand.updateSettings({ npmName: 'peertube-plugin-auto-mute', settings: { 'blocklist-urls': `http://localhost:${port}/blocklist`, @@ -185,9 +173,7 @@ describe('Official plugin auto-mute', function () { }) it('Should enable auto mute list', async function () { - await updatePluginSettings({ - url: servers[0].url, - accessToken: servers[0].accessToken, + await servers[0].pluginsCommand.updateSettings({ npmName: 'peertube-plugin-auto-mute', settings: { 'blocklist-urls': '', @@ -206,9 +192,7 @@ describe('Official plugin auto-mute', function () { it('Should mute an account on server 1, and server 2 auto mutes it', async function () { this.timeout(20000) - await updatePluginSettings({ - url: servers[1].url, - accessToken: servers[1].accessToken, + await servers[1].pluginsCommand.updateSettings({ npmName: 'peertube-plugin-auto-mute', settings: { 'blocklist-urls': 'http://localhost:' + servers[0].port + autoMuteListPath, diff --git a/server/tests/plugins/action-hooks.ts b/server/tests/plugins/action-hooks.ts index 0f57ef7fe..0de5b523b 100644 --- a/server/tests/plugins/action-hooks.ts +++ b/server/tests/plugins/action-hooks.ts @@ -11,8 +11,7 @@ import { createUser, createVideoPlaylist, deleteVideoComment, - getPluginTestPath, - installPlugin, + PluginsCommand, registerUser, removeUser, setAccessTokensToServers, @@ -49,11 +48,7 @@ describe('Test plugin action hooks', function () { await setAccessTokensToServers(servers) await setDefaultVideoChannel(servers) - await installPlugin({ - url: servers[0].url, - accessToken: servers[0].accessToken, - path: getPluginTestPath() - }) + await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath() }) killallServers([ servers[0] ]) diff --git a/server/tests/plugins/external-auth.ts b/server/tests/plugins/external-auth.ts index 5addb45c7..424302786 100644 --- a/server/tests/plugins/external-auth.ts +++ b/server/tests/plugins/external-auth.ts @@ -2,27 +2,26 @@ import 'mocha' import { expect } from 'chai' -import { ServerConfig, User, UserRole } from '@shared/models' +import { HttpStatusCode } from '@shared/core-utils' import { + cleanupTests, + createUser, decodeQueryString, + flushAndRunServer, getConfig, - getExternalAuth, getMyUserInformation, - getPluginTestPath, - installPlugin, loginUsingExternalToken, logout, + PluginsCommand, refreshToken, + ServerInfo, setAccessTokensToServers, - uninstallPlugin, updateMyUser, - wait, userLogin, - updatePluginSettings, - createUser -} from '../../../shared/extra-utils' -import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' + wait, + waitUntilLog +} from '@shared/extra-utils' +import { ServerConfig, User, UserRole } from '@shared/models' async function loginExternal (options: { server: ServerInfo @@ -33,13 +32,12 @@ async function loginExternal (options: { statusCodeExpected?: HttpStatusCode statusCodeExpectedStep2?: HttpStatusCode }) { - const res = await getExternalAuth({ - url: options.server.url, + const res = await options.server.pluginsCommand.getExternalAuth({ npmName: options.npmName, npmVersion: '0.0.1', authName: options.authName, query: options.query, - statusCodeExpected: options.statusCodeExpected || HttpStatusCode.FOUND_302 + expectedStatus: options.statusCodeExpected || HttpStatusCode.FOUND_302 }) if (res.status !== HttpStatusCode.FOUND_302) return @@ -75,11 +73,7 @@ describe('Test external auth plugins', function () { await setAccessTokensToServers([ server ]) for (const suffix of [ 'one', 'two', 'three' ]) { - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-external-auth-' + suffix) - }) + await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-external-auth-' + suffix) }) } }) @@ -98,15 +92,14 @@ describe('Test external auth plugins', function () { }) it('Should redirect for a Cyan login', async function () { - const res = await getExternalAuth({ - url: server.url, + const res = await server.pluginsCommand.getExternalAuth({ npmName: 'test-external-auth-one', npmVersion: '0.0.1', authName: 'external-auth-1', query: { username: 'cyan' }, - statusCodeExpected: HttpStatusCode.FOUND_302 + expectedStatus: HttpStatusCode.FOUND_302 }) const location = res.header.location @@ -275,9 +268,7 @@ describe('Test external auth plugins', function () { }) it('Should unregister external-auth-2 and do not login existing Kefka', async function () { - await updatePluginSettings({ - url: server.url, - accessToken: server.accessToken, + await server.pluginsCommand.updateSettings({ npmName: 'peertube-plugin-test-external-auth-one', settings: { disableKefka: true } }) @@ -309,11 +300,7 @@ describe('Test external auth plugins', function () { }) it('Should uninstall the plugin one and do not login Cyan', async function () { - await uninstallPlugin({ - url: server.url, - accessToken: server.accessToken, - npmName: 'peertube-plugin-test-external-auth-one' - }) + await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-external-auth-one' }) await loginExternal({ server, diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts index e2ec5457b..c51e96ab7 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts @@ -14,7 +14,6 @@ import { getAccountVideos, getConfig, getMyVideos, - getPluginTestPath, getVideo, getVideoChannelVideos, getVideoCommentThreads, @@ -23,8 +22,8 @@ import { getVideosListPagination, getVideoThreadComments, getVideoWithToken, - installPlugin, makeRawRequest, + PluginsCommand, registerUser, ServerInfo, setAccessTokensToServers, @@ -63,17 +62,8 @@ describe('Test plugin filter hooks', function () { await setDefaultVideoChannel(servers) await doubleFollow(servers[0], servers[1]) - await installPlugin({ - url: servers[0].url, - accessToken: servers[0].accessToken, - path: getPluginTestPath() - }) - - await installPlugin({ - url: servers[0].url, - accessToken: servers[0].accessToken, - path: getPluginTestPath('-filter-translations') - }) + await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath() }) + await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-filter-translations') }) for (let i = 0; i < 10; i++) { await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i }) diff --git a/server/tests/plugins/html-injection.ts b/server/tests/plugins/html-injection.ts index 4fa8caa3a..80d67ae0e 100644 --- a/server/tests/plugins/html-injection.ts +++ b/server/tests/plugins/html-injection.ts @@ -5,30 +5,31 @@ import * as chai from 'chai' import { cleanupTests, flushAndRunServer, - getPluginsCSS, - installPlugin, makeHTMLRequest, + PluginsCommand, ServerInfo, - setAccessTokensToServers, - uninstallPlugin + setAccessTokensToServers } from '../../../shared/extra-utils' const expect = chai.expect describe('Test plugins HTML injection', function () { let server: ServerInfo = null + let command: PluginsCommand before(async function () { this.timeout(30000) server = await flushAndRunServer(1) await setAccessTokensToServers([ server ]) + + command = server.pluginsCommand }) it('Should not inject global css file in HTML', async function () { { - const res = await getPluginsCSS(server.url) - expect(res.text).to.be.empty + const text = await command.getCSS() + expect(text).to.be.empty } for (const path of [ '/', '/videos/embed/1', '/video-playlists/embed/1' ]) { @@ -40,17 +41,13 @@ describe('Test plugins HTML injection', function () { 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 command.install({ npmName: 'peertube-plugin-hello-world' }) }) it('Should have the correct global css', async function () { { - const res = await getPluginsCSS(server.url) - expect(res.text).to.contain('background-color: red') + const text = await command.getCSS() + expect(text).to.contain('background-color: red') } for (const path of [ '/', '/videos/embed/1', '/video-playlists/embed/1' ]) { @@ -60,15 +57,11 @@ describe('Test plugins HTML injection', function () { }) it('Should have an empty global css on uninstall', async function () { - await uninstallPlugin({ - url: server.url, - accessToken: server.accessToken, - npmName: 'peertube-plugin-hello-world' - }) + await command.uninstall({ npmName: 'peertube-plugin-hello-world' }) { - const res = await getPluginsCSS(server.url) - expect(res.text).to.be.empty + const text = await command.getCSS() + expect(text).to.be.empty } for (const path of [ '/', '/videos/embed/1', '/video-playlists/embed/1' ]) { diff --git a/server/tests/plugins/id-and-pass-auth.ts b/server/tests/plugins/id-and-pass-auth.ts index cbba638c2..545968040 100644 --- a/server/tests/plugins/id-and-pass-auth.ts +++ b/server/tests/plugins/id-and-pass-auth.ts @@ -1,21 +1,25 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import 'mocha' -import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' +import { expect } from 'chai' import { + cleanupTests, + flushAndRunServer, + getConfig, getMyUserInformation, - getPluginTestPath, - installPlugin, + getUsersList, + login, logout, + PluginsCommand, + refreshToken, + ServerInfo, setAccessTokensToServers, - uninstallPlugin, updateMyUser, userLogin, wait, - login, refreshToken, getConfig, updatePluginSettings, getUsersList -} from '../../../shared/extra-utils' -import { User, UserRole, ServerConfig } from '@shared/models' -import { expect } from 'chai' + waitUntilLog +} from '@shared/extra-utils' +import { ServerConfig, User, UserRole } from '@shared/models' describe('Test id and pass auth plugins', function () { let server: ServerInfo @@ -33,11 +37,7 @@ describe('Test id and pass auth plugins', function () { await setAccessTokensToServers([ server ]) for (const suffix of [ 'one', 'two', 'three' ]) { - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-id-pass-auth-' + suffix) - }) + await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-id-pass-auth-' + suffix) }) } }) @@ -180,9 +180,7 @@ describe('Test id and pass auth plugins', function () { }) it('Should unregister spyro-auth and do not login existing Spyro', async function () { - await updatePluginSettings({ - url: server.url, - accessToken: server.accessToken, + await server.pluginsCommand.updateSettings({ npmName: 'peertube-plugin-test-id-pass-auth-one', settings: { disableSpyro: true } }) @@ -204,11 +202,7 @@ describe('Test id and pass auth plugins', function () { }) it('Should uninstall the plugin one and do not login existing Crash', async function () { - await uninstallPlugin({ - url: server.url, - accessToken: server.accessToken, - npmName: 'peertube-plugin-test-id-pass-auth-one' - }) + await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-id-pass-auth-one' }) await userLogin(server, { username: 'crash', password: 'crash password' }, 400) }) diff --git a/server/tests/plugins/plugin-helpers.ts b/server/tests/plugins/plugin-helpers.ts index 0296d6eb7..0e0f61638 100644 --- a/server/tests/plugins/plugin-helpers.ts +++ b/server/tests/plugins/plugin-helpers.ts @@ -1,23 +1,25 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import 'mocha' +import { expect } from 'chai' +import { HttpStatusCode } from '@shared/core-utils' import { checkVideoFilesWereRemoved, + cleanupTests, doubleFollow, - getPluginTestPath, + flushAndRunMultipleServers, getVideo, - installPlugin, + getVideosList, + makeGetRequest, makePostBodyRequest, + PluginsCommand, + ServerInfo, setAccessTokensToServers, uploadVideoAndGetId, viewVideo, - getVideosList, waitJobs, - makeGetRequest -} from '../../../shared/extra-utils' -import { cleanupTests, flushAndRunMultipleServers, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' -import { expect } from 'chai' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' + waitUntilLog +} from '@shared/extra-utils' function postCommand (server: ServerInfo, command: string, bodyArg?: object) { const body = { command } @@ -42,11 +44,7 @@ describe('Test plugin helpers', function () { await doubleFollow(servers[0], servers[1]) - await installPlugin({ - url: servers[0].url, - accessToken: servers[0].accessToken, - path: getPluginTestPath('-four') - }) + await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-four') }) }) describe('Logger', function () { diff --git a/server/tests/plugins/plugin-router.ts b/server/tests/plugins/plugin-router.ts index 24e6a1e83..81e18dabd 100644 --- a/server/tests/plugins/plugin-router.ts +++ b/server/tests/plugins/plugin-router.ts @@ -1,16 +1,17 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import 'mocha' -import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers' +import { expect } from 'chai' +import { HttpStatusCode } from '@shared/core-utils' import { - getPluginTestPath, - installPlugin, + cleanupTests, + flushAndRunServer, makeGetRequest, makePostBodyRequest, - setAccessTokensToServers, uninstallPlugin -} from '../../../shared/extra-utils' -import { expect } from 'chai' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' + PluginsCommand, + ServerInfo, + setAccessTokensToServers +} from '@shared/extra-utils' describe('Test plugin helpers', function () { let server: ServerInfo @@ -25,11 +26,7 @@ describe('Test plugin helpers', function () { server = await flushAndRunServer(1) await setAccessTokensToServers([ server ]) - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-five') - }) + await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-five') }) }) it('Should answer "pong"', async function () { @@ -85,11 +82,7 @@ describe('Test plugin helpers', function () { }) it('Should remove the plugin and remove the routes', async function () { - await uninstallPlugin({ - url: server.url, - accessToken: server.accessToken, - npmName: 'peertube-plugin-test-five' - }) + await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-five' }) for (const path of basePaths) { await makeGetRequest({ diff --git a/server/tests/plugins/plugin-storage.ts b/server/tests/plugins/plugin-storage.ts index 3c46b2585..4c65463f2 100644 --- a/server/tests/plugins/plugin-storage.ts +++ b/server/tests/plugins/plugin-storage.ts @@ -7,13 +7,14 @@ import { join } from 'path' import { HttpStatusCode } from '@shared/core-utils' import { buildServerDirectory, - getPluginTestPath, - installPlugin, + cleanupTests, + flushAndRunServer, makeGetRequest, + PluginsCommand, + ServerInfo, setAccessTokensToServers, - uninstallPlugin -} from '../../../shared/extra-utils' -import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' + waitUntilLog +} from '@shared/extra-utils' describe('Test plugin storage', function () { let server: ServerInfo @@ -24,11 +25,7 @@ describe('Test plugin storage', function () { server = await flushAndRunServer(1) await setAccessTokensToServers([ server ]) - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-six') - }) + await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-six') }) }) describe('DB storage', function () { @@ -76,22 +73,14 @@ describe('Test plugin storage', function () { }) it('Should still have the file after an uninstallation', async function () { - await uninstallPlugin({ - url: server.url, - accessToken: server.accessToken, - npmName: 'peertube-plugin-test-six' - }) + await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-six' }) const content = await getFileContent() expect(content).to.equal('Prince Ali') }) it('Should still have the file after the reinstallation', async function () { - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-six') - }) + await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-six') }) const content = await getFileContent() expect(content).to.equal('Prince Ali') diff --git a/server/tests/plugins/plugin-transcoding.ts b/server/tests/plugins/plugin-transcoding.ts index eefb2294d..f1ff91077 100644 --- a/server/tests/plugins/plugin-transcoding.ts +++ b/server/tests/plugins/plugin-transcoding.ts @@ -4,25 +4,25 @@ import 'mocha' import { expect } from 'chai' import { join } from 'path' import { getAudioStream, getVideoFileFPS, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils' -import { ServerConfig, VideoDetails, VideoPrivacy } from '@shared/models' import { buildServerDirectory, + cleanupTests, createLive, + flushAndRunServer, getConfig, - getPluginTestPath, getVideo, - installPlugin, + PluginsCommand, sendRTMPStreamInVideo, + ServerInfo, setAccessTokensToServers, setDefaultVideoChannel, testFfmpegStreamError, - uninstallPlugin, updateCustomSubConfig, uploadVideoAndGetId, waitJobs, waitUntilLivePublished -} from '../../../shared/extra-utils' -import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers' +} from '@shared/extra-utils' +import { ServerConfig, VideoDetails, VideoPrivacy } from '@shared/models' async function createLiveWrapper (server: ServerInfo) { const liveAttributes = { @@ -109,11 +109,7 @@ describe('Test transcoding plugins', function () { } before(async function () { - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-transcoding-one') - }) + await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-transcoding-one') }) }) it('Should have the appropriate available profiles', async function () { @@ -225,7 +221,7 @@ describe('Test transcoding plugins', function () { it('Should default to the default profile if the specified profile does not exist', async function () { this.timeout(240000) - await uninstallPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-plugin-test-transcoding-one' }) + await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-transcoding-one' }) const res = await getConfig(server.url) const config = res.body as ServerConfig @@ -244,11 +240,7 @@ describe('Test transcoding plugins', function () { describe('When using a plugin adding new encoders', function () { before(async function () { - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-transcoding-two') - }) + await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-transcoding-two') }) await updateConf(server, 'test-vod-profile', 'test-live-profile') }) diff --git a/server/tests/plugins/plugin-unloading.ts b/server/tests/plugins/plugin-unloading.ts index 74ca82e2f..f430f82b8 100644 --- a/server/tests/plugins/plugin-unloading.ts +++ b/server/tests/plugins/plugin-unloading.ts @@ -1,18 +1,9 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import 'mocha' -import { - cleanupTests, - flushAndRunServer, - getPluginTestPath, - makeGetRequest, - installPlugin, - uninstallPlugin, - ServerInfo, - setAccessTokensToServers -} from '../../../shared/extra-utils' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' import { expect } from 'chai' +import { HttpStatusCode } from '@shared/core-utils' +import { cleanupTests, flushAndRunServer, makeGetRequest, PluginsCommand, ServerInfo, setAccessTokensToServers } from '@shared/extra-utils' describe('Test plugins module unloading', function () { let server: ServerInfo = null @@ -25,11 +16,7 @@ describe('Test plugins module unloading', function () { server = await flushAndRunServer(1) await setAccessTokensToServers([ server ]) - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-unloading') - }) + await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-unloading') }) }) it('Should return a numeric value', async function () { @@ -54,11 +41,7 @@ describe('Test plugins module unloading', function () { }) it('Should uninstall the plugin and free the route', async function () { - await uninstallPlugin({ - url: server.url, - accessToken: server.accessToken, - npmName: 'peertube-plugin-test-unloading' - }) + await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-unloading' }) await makeGetRequest({ url: server.url, @@ -68,11 +51,8 @@ describe('Test plugins module unloading', function () { }) it('Should return a different numeric value', async function () { - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-unloading') - }) + await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-unloading') }) + const res = await makeGetRequest({ url: server.url, path: requestPath, diff --git a/server/tests/plugins/translations.ts b/server/tests/plugins/translations.ts index 9fd2ba1c5..0e11a0b53 100644 --- a/server/tests/plugins/translations.ts +++ b/server/tests/plugins/translations.ts @@ -1,20 +1,15 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import * as chai from 'chai' import 'mocha' +import * as chai from 'chai' +import { PluginsCommand, setAccessTokensToServers } from '../../../shared/extra-utils' import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers' -import { - getPluginTestPath, - getPluginTranslations, - installPlugin, - setAccessTokensToServers, - uninstallPlugin -} from '../../../shared/extra-utils' const expect = chai.expect describe('Test plugin translations', function () { let server: ServerInfo + let command: PluginsCommand before(async function () { this.timeout(30000) @@ -22,29 +17,22 @@ describe('Test plugin translations', function () { server = await flushAndRunServer(1) await setAccessTokensToServers([ server ]) - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath() - }) + command = server.pluginsCommand - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-filter-translations') - }) + await command.install({ path: PluginsCommand.getPluginTestPath() }) + await command.install({ path: PluginsCommand.getPluginTestPath('-filter-translations') }) }) it('Should not have translations for locale pt', async function () { - const res = await getPluginTranslations({ url: server.url, locale: 'pt' }) + const body = await command.getTranslations({ locale: 'pt' }) - expect(res.body).to.deep.equal({}) + expect(body).to.deep.equal({}) }) it('Should have translations for locale fr', async function () { - const res = await getPluginTranslations({ url: server.url, locale: 'fr-FR' }) + const body = await command.getTranslations({ locale: 'fr-FR' }) - expect(res.body).to.deep.equal({ + expect(body).to.deep.equal({ 'peertube-plugin-test': { Hi: 'Coucou' }, @@ -55,9 +43,9 @@ describe('Test plugin translations', function () { }) it('Should have translations of locale it', async function () { - const res = await getPluginTranslations({ url: server.url, locale: 'it-IT' }) + const body = await command.getTranslations({ locale: 'it-IT' }) - expect(res.body).to.deep.equal({ + expect(body).to.deep.equal({ 'peertube-plugin-test-filter-translations': { 'Hello world': 'Ciao, mondo!' } @@ -65,12 +53,12 @@ describe('Test plugin translations', function () { }) it('Should remove the plugin and remove the locales', async function () { - await uninstallPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-plugin-test-filter-translations' }) + await command.uninstall({ npmName: 'peertube-plugin-test-filter-translations' }) { - const res = await getPluginTranslations({ url: server.url, locale: 'fr-FR' }) + const body = await command.getTranslations({ locale: 'fr-FR' }) - expect(res.body).to.deep.equal({ + expect(body).to.deep.equal({ 'peertube-plugin-test': { Hi: 'Coucou' } @@ -78,9 +66,9 @@ describe('Test plugin translations', function () { } { - const res = await getPluginTranslations({ url: server.url, locale: 'it-IT' }) + const body = await command.getTranslations({ locale: 'it-IT' }) - expect(res.body).to.deep.equal({}) + expect(body).to.deep.equal({}) } }) diff --git a/server/tests/plugins/video-constants.ts b/server/tests/plugins/video-constants.ts index eb014c596..4124e8a52 100644 --- a/server/tests/plugins/video-constants.ts +++ b/server/tests/plugins/video-constants.ts @@ -1,22 +1,24 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import * as chai from 'chai' import 'mocha' -import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers' +import * as chai from 'chai' +import { HttpStatusCode } from '@shared/core-utils' import { + cleanupTests, createVideoPlaylist, - getPluginTestPath, + flushAndRunServer, getVideo, getVideoCategories, getVideoLanguages, - getVideoLicences, getVideoPlaylistPrivacies, getVideoPrivacies, - installPlugin, + getVideoLicences, + getVideoPlaylistPrivacies, + getVideoPrivacies, + PluginsCommand, + ServerInfo, setAccessTokensToServers, - uninstallPlugin, uploadVideo -} from '../../../shared/extra-utils' -import { VideoDetails, VideoPlaylistPrivacy } from '../../../shared/models/videos' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +} from '@shared/extra-utils' +import { VideoDetails, VideoPlaylistPrivacy } from '@shared/models' const expect = chai.expect @@ -29,11 +31,7 @@ describe('Test plugin altering video constants', function () { server = await flushAndRunServer(1) await setAccessTokensToServers([ server ]) - await installPlugin({ - url: server.url, - accessToken: server.accessToken, - path: getPluginTestPath('-video-constants') - }) + await server.pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-video-constants') }) }) it('Should have updated languages', async function () { @@ -117,7 +115,7 @@ describe('Test plugin altering video constants', function () { }) it('Should uninstall the plugin and reset languages, categories, licences and privacies', async function () { - await uninstallPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-plugin-test-video-constants' }) + await server.pluginsCommand.uninstall({ npmName: 'peertube-plugin-test-video-constants' }) { const res = await getVideoLanguages(server.url) diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index 652779eea..cf6418249 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -15,7 +15,6 @@ export * from './requests/requests' export * from './server/clients' export * from './server/config' -export * from './server/plugins' export * from './server/servers' export * from './users/accounts' diff --git a/shared/extra-utils/server/index.ts b/shared/extra-utils/server/index.ts index b5b6b2116..e602fec7e 100644 --- a/shared/extra-utils/server/index.ts +++ b/shared/extra-utils/server/index.ts @@ -4,3 +4,5 @@ export * from './follows-command' export * from './follows' export * from './jobs' export * from './jobs-command' +export * from './plugins-command' +export * from './plugins' diff --git a/shared/extra-utils/server/plugins-command.ts b/shared/extra-utils/server/plugins-command.ts new file mode 100644 index 000000000..f06e58a22 --- /dev/null +++ b/shared/extra-utils/server/plugins-command.ts @@ -0,0 +1,245 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import { readJSON, writeJSON } from 'fs-extra' +import { join } from 'path' +import { root } from '@server/helpers/core-utils' +import { HttpStatusCode } from '@shared/core-utils' +import { + PeerTubePlugin, + PeerTubePluginIndex, + PeertubePluginIndexList, + PluginPackageJson, + PluginTranslation, + PluginType, + PublicServerSetting, + RegisteredServerSettings, + ResultList +} from '@shared/models' +import { buildServerDirectory } from '../miscs' +import { AbstractCommand, OverrideCommandOptions } from '../shared' + +export class PluginsCommand extends AbstractCommand { + + static getPluginTestPath (suffix = '') { + return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix) + } + + list (options: OverrideCommandOptions & { + start?: number + count?: number + sort?: string + pluginType?: PluginType + uninstalled?: boolean + }) { + const { start, count, sort, pluginType, uninstalled } = options + const path = '/api/v1/plugins' + + return this.getRequestBody>({ + ...options, + + path, + query: { + start, + count, + sort, + pluginType, + uninstalled + }, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + listAvailable (options: OverrideCommandOptions & { + start?: number + count?: number + sort?: string + pluginType?: PluginType + currentPeerTubeEngine?: string + search?: string + expectedStatus?: HttpStatusCode + }) { + const { start, count, sort, pluginType, search, currentPeerTubeEngine } = options + const path = '/api/v1/plugins/available' + + const query: PeertubePluginIndexList = { + start, + count, + sort, + pluginType, + currentPeerTubeEngine, + search + } + + return this.getRequestBody>({ + ...options, + + path, + query, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + get (options: OverrideCommandOptions & { + npmName: string + }) { + const path = '/api/v1/plugins/' + options.npmName + + return this.getRequestBody({ + ...options, + + path, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + updateSettings (options: OverrideCommandOptions & { + npmName: string + settings: any + }) { + const { npmName, settings } = options + const path = '/api/v1/plugins/' + npmName + '/settings' + + return this.putBodyRequest({ + ...options, + + path, + fields: { settings }, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + getRegisteredSettings (options: OverrideCommandOptions & { + npmName: string + }) { + const path = '/api/v1/plugins/' + options.npmName + '/registered-settings' + + return this.getRequestBody({ + ...options, + + path, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + getPublicSettings (options: OverrideCommandOptions & { + npmName: string + }) { + const { npmName } = options + const path = '/api/v1/plugins/' + npmName + '/public-settings' + + return this.getRequestBody({ + ...options, + + path, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + getTranslations (options: OverrideCommandOptions & { + locale: string + }) { + const { locale } = options + const path = '/plugins/translations/' + locale + '.json' + + return this.getRequestBody({ + ...options, + + path, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + install (options: OverrideCommandOptions & { + path?: string + npmName?: string + }) { + const { npmName, path } = options + const apiPath = '/api/v1/plugins/install' + + return this.postBodyRequest({ + ...options, + + path: apiPath, + fields: { npmName, path }, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + update (options: OverrideCommandOptions & { + path?: string + npmName?: string + }) { + const { npmName, path } = options + const apiPath = '/api/v1/plugins/update' + + return this.postBodyRequest({ + ...options, + + path: apiPath, + fields: { npmName, path }, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + uninstall (options: OverrideCommandOptions & { + npmName: string + }) { + const { npmName } = options + const apiPath = '/api/v1/plugins/uninstall' + + return this.postBodyRequest({ + ...options, + + path: apiPath, + fields: { npmName }, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + getCSS (options: OverrideCommandOptions = {}) { + const path = '/plugins/global.css' + + return this.getRequestText({ + ...options, + + path, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + getExternalAuth (options: OverrideCommandOptions & { + npmName: string + npmVersion: string + authName: string + query?: any + }) { + const { npmName, npmVersion, authName, query } = options + + const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName + + return this.getRequest({ + ...options, + + path, + query, + defaultExpectedStatus: HttpStatusCode.OK_200, + redirects: 0 + }) + } + + updatePackageJSON (npmName: string, json: any) { + const path = this.getPackageJSONPath(npmName) + + return writeJSON(path, json) + } + + getPackageJSON (npmName: string): Promise { + const path = this.getPackageJSONPath(npmName) + + return readJSON(path) + } + + private getPackageJSONPath (npmName: string) { + return buildServerDirectory(this.server, join('plugins', 'node_modules', npmName, 'package.json')) + } +} diff --git a/shared/extra-utils/server/plugins.ts b/shared/extra-utils/server/plugins.ts index d53e5b382..1084ea4f4 100644 --- a/shared/extra-utils/server/plugins.ts +++ b/shared/extra-utils/server/plugins.ts @@ -1,307 +1,18 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import { expect } from 'chai' -import { readJSON, writeJSON } from 'fs-extra' -import { join } from 'path' -import { RegisteredServerSettings } from '@shared/models' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -import { PeertubePluginIndexList } from '../../models/plugins/plugin-index/peertube-plugin-index-list.model' -import { PluginType } from '../../models/plugins/plugin.type' -import { buildServerDirectory, root } from '../miscs/miscs' -import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' -import { ServerInfo } from './servers' - -function listPlugins (parameters: { - url: string - accessToken: string - start?: number - count?: number - sort?: string - pluginType?: PluginType - uninstalled?: boolean - expectedStatus?: HttpStatusCode -}) { - const { url, accessToken, start, count, sort, pluginType, uninstalled, expectedStatus = HttpStatusCode.OK_200 } = parameters - const path = '/api/v1/plugins' - - return makeGetRequest({ - url, - path, - token: accessToken, - query: { - start, - count, - sort, - pluginType, - uninstalled - }, - statusCodeExpected: expectedStatus - }) -} - -function listAvailablePlugins (parameters: { - url: string - accessToken: string - start?: number - count?: number - sort?: string - pluginType?: PluginType - currentPeerTubeEngine?: string - search?: string - expectedStatus?: HttpStatusCode -}) { - const { - url, - accessToken, - start, - count, - sort, - pluginType, - search, - currentPeerTubeEngine, - expectedStatus = HttpStatusCode.OK_200 - } = parameters - const path = '/api/v1/plugins/available' - - const query: PeertubePluginIndexList = { - start, - count, - sort, - pluginType, - currentPeerTubeEngine, - search - } - - return makeGetRequest({ - url, - path, - token: accessToken, - query, - statusCodeExpected: expectedStatus - }) -} - -function getPlugin (parameters: { - url: string - accessToken: string - npmName: string - expectedStatus?: HttpStatusCode -}) { - const { url, accessToken, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters - const path = '/api/v1/plugins/' + npmName - - return makeGetRequest({ - url, - path, - token: accessToken, - statusCodeExpected: expectedStatus - }) -} - -function updatePluginSettings (parameters: { - url: string - accessToken: string - npmName: string - settings: any - expectedStatus?: HttpStatusCode -}) { - const { url, accessToken, npmName, settings, expectedStatus = HttpStatusCode.NO_CONTENT_204 } = parameters - const path = '/api/v1/plugins/' + npmName + '/settings' - - return makePutBodyRequest({ - url, - path, - token: accessToken, - fields: { settings }, - statusCodeExpected: expectedStatus - }) -} - -function getPluginRegisteredSettings (parameters: { - url: string - accessToken: string - npmName: string - expectedStatus?: HttpStatusCode -}) { - const { url, accessToken, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters - const path = '/api/v1/plugins/' + npmName + '/registered-settings' - - return makeGetRequest({ - url, - path, - token: accessToken, - statusCodeExpected: expectedStatus - }) -} +import { ServerInfo } from '../server/servers' async function testHelloWorldRegisteredSettings (server: ServerInfo) { - const res = await getPluginRegisteredSettings({ - url: server.url, - accessToken: server.accessToken, - npmName: 'peertube-plugin-hello-world' - }) - - const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings + const body = await server.pluginsCommand.getRegisteredSettings({ npmName: 'peertube-plugin-hello-world' }) + const registeredSettings = body.registeredSettings expect(registeredSettings).to.have.length.at.least(1) const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name') expect(adminNameSettings).to.not.be.undefined } -function getPublicSettings (parameters: { - url: string - npmName: string - expectedStatus?: HttpStatusCode -}) { - const { url, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters - const path = '/api/v1/plugins/' + npmName + '/public-settings' - - return makeGetRequest({ - url, - path, - statusCodeExpected: expectedStatus - }) -} - -function getPluginTranslations (parameters: { - url: string - locale: string - expectedStatus?: HttpStatusCode -}) { - const { url, locale, expectedStatus = HttpStatusCode.OK_200 } = parameters - const path = '/plugins/translations/' + locale + '.json' - - return makeGetRequest({ - url, - path, - statusCodeExpected: expectedStatus - }) -} - -function installPlugin (parameters: { - url: string - accessToken: string - path?: string - npmName?: string - expectedStatus?: HttpStatusCode -}) { - const { url, accessToken, npmName, path, expectedStatus = HttpStatusCode.OK_200 } = parameters - const apiPath = '/api/v1/plugins/install' - - return makePostBodyRequest({ - url, - path: apiPath, - token: accessToken, - fields: { npmName, path }, - statusCodeExpected: expectedStatus - }) -} - -function updatePlugin (parameters: { - url: string - accessToken: string - path?: string - npmName?: string - expectedStatus?: HttpStatusCode -}) { - const { url, accessToken, npmName, path, expectedStatus = HttpStatusCode.OK_200 } = parameters - const apiPath = '/api/v1/plugins/update' - - return makePostBodyRequest({ - url, - path: apiPath, - token: accessToken, - fields: { npmName, path }, - statusCodeExpected: expectedStatus - }) -} - -function uninstallPlugin (parameters: { - url: string - accessToken: string - npmName: string - expectedStatus?: HttpStatusCode -}) { - const { url, accessToken, npmName, expectedStatus = HttpStatusCode.NO_CONTENT_204 } = parameters - const apiPath = '/api/v1/plugins/uninstall' - - return makePostBodyRequest({ - url, - path: apiPath, - token: accessToken, - fields: { npmName }, - statusCodeExpected: expectedStatus - }) -} - -function getPluginsCSS (url: string) { - const path = '/plugins/global.css' - - return makeGetRequest({ - url, - path, - statusCodeExpected: HttpStatusCode.OK_200 - }) -} - -function getPackageJSONPath (server: ServerInfo, npmName: string) { - return buildServerDirectory(server, join('plugins', 'node_modules', npmName, 'package.json')) -} - -function updatePluginPackageJSON (server: ServerInfo, npmName: string, json: any) { - const path = getPackageJSONPath(server, npmName) - - return writeJSON(path, json) -} - -function getPluginPackageJSON (server: ServerInfo, npmName: string) { - const path = getPackageJSONPath(server, npmName) - - return readJSON(path) -} - -function getPluginTestPath (suffix = '') { - return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix) -} - -function getExternalAuth (options: { - url: string - npmName: string - npmVersion: string - authName: string - query?: any - statusCodeExpected?: HttpStatusCode -}) { - const { url, npmName, npmVersion, authName, statusCodeExpected, query } = options - - const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName - - return makeGetRequest({ - url, - path, - query, - statusCodeExpected: statusCodeExpected || HttpStatusCode.OK_200, - redirects: 0 - }) -} - export { - listPlugins, - listAvailablePlugins, - installPlugin, - getPluginTranslations, - getPluginsCSS, - updatePlugin, - getPlugin, - uninstallPlugin, - testHelloWorldRegisteredSettings, - updatePluginSettings, - getPluginRegisteredSettings, - getPackageJSONPath, - updatePluginPackageJSON, - getPluginPackageJSON, - getPluginTestPath, - getPublicSettings, - getExternalAuth + testHelloWorldRegisteredSettings } diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 5511ce0b0..79d6b7b1a 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -20,6 +20,7 @@ import { ContactFormCommand } from './contact-form-command' import { DebugCommand } from './debug-command' import { FollowsCommand } from './follows-command' import { JobsCommand } from './jobs-command' +import { PluginsCommand } from './plugins-command' interface ServerInfo { app: ChildProcess @@ -85,6 +86,7 @@ interface ServerInfo { debugCommand?: DebugCommand followsCommand?: FollowsCommand jobsCommand?: JobsCommand + pluginsCommand?: PluginsCommand } function parallelTests () { @@ -302,6 +304,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] server.debugCommand = new DebugCommand(server) server.followsCommand = new FollowsCommand(server) server.jobsCommand = new JobsCommand(server) + server.pluginsCommand = new PluginsCommand(server) res(server) }) diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index 3815fab0e..dd4598a91 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts @@ -16,6 +16,7 @@ interface GetCommandOptions extends CommonCommandOptions { query?: { [ id: string ]: any } contentType?: string accept?: string + redirects?: number } abstract class AbstractCommand { @@ -44,6 +45,19 @@ abstract class AbstractCommand { return unwrapText(this.getRequest(options)) } + protected getRequest (options: GetCommandOptions) { + const { redirects, query, contentType, accept } = options + + return makeGetRequest({ + ...this.buildCommonRequestOptions(options), + + redirects, + query, + contentType, + accept + }) + } + protected deleteRequest (options: CommonCommandOptions) { return makeDeleteRequest(this.buildCommonRequestOptions(options)) } @@ -85,18 +99,6 @@ abstract class AbstractCommand { statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus } } - - private getRequest (options: GetCommandOptions) { - const { query, contentType, accept } = options - - return makeGetRequest({ - ...this.buildCommonRequestOptions(options), - - query, - contentType, - accept - }) - } } export { -- 2.41.0