123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- /* 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 { PeertubePluginIndexList } from '../../models/plugins/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'
- import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
- 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
- })
- }
- 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
- 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
- }
|