2020-11-27 04:41:38 -06:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
|
|
|
|
|
|
|
import { expect } from 'chai'
|
2019-07-19 07:36:04 -05:00
|
|
|
import { readJSON, writeJSON } from 'fs-extra'
|
|
|
|
import { join } from 'path'
|
2020-11-27 04:41:38 -06:00
|
|
|
import { RegisteredServerSettings } from '@shared/models'
|
2021-05-11 05:04:47 -05:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
|
|
|
import { PeertubePluginIndexList } from '../../models/plugins/plugin-index/peertube-plugin-index-list.model'
|
2020-11-24 08:22:56 -06:00
|
|
|
import { PluginType } from '../../models/plugins/plugin.type'
|
|
|
|
import { buildServerDirectory, root } from '../miscs/miscs'
|
|
|
|
import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
|
|
|
|
import { ServerInfo } from './servers'
|
2019-07-11 10:23:24 -05:00
|
|
|
|
|
|
|
function listPlugins (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
|
|
|
start?: number
|
|
|
|
count?: number
|
|
|
|
sort?: string
|
|
|
|
pluginType?: PluginType
|
|
|
|
uninstalled?: boolean
|
2020-12-07 07:32:36 -06:00
|
|
|
expectedStatus?: HttpStatusCode
|
2019-07-11 10:23:24 -05:00
|
|
|
}) {
|
2020-12-07 07:32:36 -06:00
|
|
|
const { url, accessToken, start, count, sort, pluginType, uninstalled, expectedStatus = HttpStatusCode.OK_200 } = parameters
|
2019-07-11 10:23:24 -05:00
|
|
|
const path = '/api/v1/plugins'
|
|
|
|
|
|
|
|
return makeGetRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
token: accessToken,
|
|
|
|
query: {
|
|
|
|
start,
|
|
|
|
count,
|
|
|
|
sort,
|
2019-07-19 07:36:04 -05:00
|
|
|
pluginType,
|
|
|
|
uninstalled
|
2019-07-11 10:23:24 -05:00
|
|
|
},
|
|
|
|
statusCodeExpected: expectedStatus
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-19 07:36:04 -05:00
|
|
|
function listAvailablePlugins (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
|
|
|
start?: number
|
|
|
|
count?: number
|
|
|
|
sort?: string
|
|
|
|
pluginType?: PluginType
|
|
|
|
currentPeerTubeEngine?: string
|
2019-07-19 07:36:04 -05:00
|
|
|
search?: string
|
2020-12-07 07:32:36 -06:00
|
|
|
expectedStatus?: HttpStatusCode
|
2019-07-19 07:36:04 -05:00
|
|
|
}) {
|
2020-12-07 07:32:36 -06:00
|
|
|
const {
|
|
|
|
url,
|
|
|
|
accessToken,
|
|
|
|
start,
|
|
|
|
count,
|
|
|
|
sort,
|
|
|
|
pluginType,
|
|
|
|
search,
|
|
|
|
currentPeerTubeEngine,
|
|
|
|
expectedStatus = HttpStatusCode.OK_200
|
|
|
|
} = parameters
|
2019-07-19 07:36:04 -05:00
|
|
|
const path = '/api/v1/plugins/available'
|
|
|
|
|
|
|
|
const query: PeertubePluginIndexList = {
|
|
|
|
start,
|
|
|
|
count,
|
|
|
|
sort,
|
|
|
|
pluginType,
|
|
|
|
currentPeerTubeEngine,
|
|
|
|
search
|
|
|
|
}
|
|
|
|
|
|
|
|
return makeGetRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
token: accessToken,
|
|
|
|
query,
|
|
|
|
statusCodeExpected: expectedStatus
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:23:24 -05:00
|
|
|
function getPlugin (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
|
|
|
npmName: string
|
2020-12-07 07:32:36 -06:00
|
|
|
expectedStatus?: HttpStatusCode
|
2019-07-11 10:23:24 -05:00
|
|
|
}) {
|
2020-12-07 07:32:36 -06:00
|
|
|
const { url, accessToken, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters
|
2019-07-11 10:23:24 -05:00
|
|
|
const path = '/api/v1/plugins/' + npmName
|
|
|
|
|
|
|
|
return makeGetRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
token: accessToken,
|
|
|
|
statusCodeExpected: expectedStatus
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-19 07:36:04 -05:00
|
|
|
function updatePluginSettings (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
|
|
|
npmName: string
|
|
|
|
settings: any
|
2020-12-07 07:32:36 -06:00
|
|
|
expectedStatus?: HttpStatusCode
|
2019-07-11 10:23:24 -05:00
|
|
|
}) {
|
2020-12-07 07:32:36 -06:00
|
|
|
const { url, accessToken, npmName, settings, expectedStatus = HttpStatusCode.NO_CONTENT_204 } = parameters
|
2019-07-11 10:23:24 -05:00
|
|
|
const path = '/api/v1/plugins/' + npmName + '/settings'
|
|
|
|
|
2019-07-19 07:36:04 -05:00
|
|
|
return makePutBodyRequest({
|
2019-07-11 10:23:24 -05:00
|
|
|
url,
|
|
|
|
path,
|
|
|
|
token: accessToken,
|
2019-07-19 07:36:04 -05:00
|
|
|
fields: { settings },
|
2019-07-11 10:23:24 -05:00
|
|
|
statusCodeExpected: expectedStatus
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPluginRegisteredSettings (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
|
|
|
npmName: string
|
2020-12-07 07:32:36 -06:00
|
|
|
expectedStatus?: HttpStatusCode
|
2019-07-11 10:23:24 -05:00
|
|
|
}) {
|
2020-12-07 07:32:36 -06:00
|
|
|
const { url, accessToken, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters
|
2019-07-11 10:23:24 -05:00
|
|
|
const path = '/api/v1/plugins/' + npmName + '/registered-settings'
|
|
|
|
|
|
|
|
return makeGetRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
token: accessToken,
|
|
|
|
statusCodeExpected: expectedStatus
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-27 04:41:38 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-26 02:35:43 -05:00
|
|
|
function getPublicSettings (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
npmName: string
|
2020-12-07 07:32:36 -06:00
|
|
|
expectedStatus?: HttpStatusCode
|
2019-07-26 02:35:43 -05:00
|
|
|
}) {
|
2020-12-07 07:32:36 -06:00
|
|
|
const { url, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters
|
2019-07-26 02:35:43 -05:00
|
|
|
const path = '/api/v1/plugins/' + npmName + '/public-settings'
|
|
|
|
|
|
|
|
return makeGetRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
statusCodeExpected: expectedStatus
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-26 07:44:50 -05:00
|
|
|
function getPluginTranslations (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
locale: string
|
2020-12-07 07:32:36 -06:00
|
|
|
expectedStatus?: HttpStatusCode
|
2019-07-26 07:44:50 -05:00
|
|
|
}) {
|
2020-12-07 07:32:36 -06:00
|
|
|
const { url, locale, expectedStatus = HttpStatusCode.OK_200 } = parameters
|
2019-07-26 07:44:50 -05:00
|
|
|
const path = '/plugins/translations/' + locale + '.json'
|
|
|
|
|
|
|
|
return makeGetRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
statusCodeExpected: expectedStatus
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:23:24 -05:00
|
|
|
function installPlugin (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
|
|
|
path?: string
|
2019-07-11 10:23:24 -05:00
|
|
|
npmName?: string
|
2020-12-07 07:32:36 -06:00
|
|
|
expectedStatus?: HttpStatusCode
|
2019-07-11 10:23:24 -05:00
|
|
|
}) {
|
2020-12-07 07:32:36 -06:00
|
|
|
const { url, accessToken, npmName, path, expectedStatus = HttpStatusCode.OK_200 } = parameters
|
2019-07-11 10:23:24 -05:00
|
|
|
const apiPath = '/api/v1/plugins/install'
|
|
|
|
|
|
|
|
return makePostBodyRequest({
|
|
|
|
url,
|
|
|
|
path: apiPath,
|
|
|
|
token: accessToken,
|
|
|
|
fields: { npmName, path },
|
|
|
|
statusCodeExpected: expectedStatus
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-12 04:39:58 -05:00
|
|
|
function updatePlugin (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
|
|
|
path?: string
|
2019-07-12 04:39:58 -05:00
|
|
|
npmName?: string
|
2020-12-07 07:32:36 -06:00
|
|
|
expectedStatus?: HttpStatusCode
|
2019-07-12 04:39:58 -05:00
|
|
|
}) {
|
2020-12-07 07:32:36 -06:00
|
|
|
const { url, accessToken, npmName, path, expectedStatus = HttpStatusCode.OK_200 } = parameters
|
2019-07-12 04:39:58 -05:00
|
|
|
const apiPath = '/api/v1/plugins/update'
|
|
|
|
|
|
|
|
return makePostBodyRequest({
|
|
|
|
url,
|
|
|
|
path: apiPath,
|
|
|
|
token: accessToken,
|
|
|
|
fields: { npmName, path },
|
|
|
|
statusCodeExpected: expectedStatus
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:23:24 -05:00
|
|
|
function uninstallPlugin (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
2019-07-11 10:23:24 -05:00
|
|
|
npmName: string
|
2020-12-07 07:32:36 -06:00
|
|
|
expectedStatus?: HttpStatusCode
|
2019-07-11 10:23:24 -05:00
|
|
|
}) {
|
2020-12-07 07:32:36 -06:00
|
|
|
const { url, accessToken, npmName, expectedStatus = HttpStatusCode.NO_CONTENT_204 } = parameters
|
2019-07-11 10:23:24 -05:00
|
|
|
const apiPath = '/api/v1/plugins/uninstall'
|
|
|
|
|
|
|
|
return makePostBodyRequest({
|
|
|
|
url,
|
|
|
|
path: apiPath,
|
|
|
|
token: accessToken,
|
|
|
|
fields: { npmName },
|
|
|
|
statusCodeExpected: expectedStatus
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-19 07:36:04 -05:00
|
|
|
function getPluginsCSS (url: string) {
|
|
|
|
const path = '/plugins/global.css'
|
|
|
|
|
|
|
|
return makeGetRequest({
|
|
|
|
url,
|
|
|
|
path,
|
2020-12-07 07:32:36 -06:00
|
|
|
statusCodeExpected: HttpStatusCode.OK_200
|
2019-07-19 07:36:04 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPackageJSONPath (server: ServerInfo, npmName: string) {
|
2020-11-24 08:22:56 -06:00
|
|
|
return buildServerDirectory(server, join('plugins', 'node_modules', npmName, 'package.json'))
|
2019-07-19 07:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-19 10:30:41 -05:00
|
|
|
function getPluginTestPath (suffix = '') {
|
|
|
|
return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix)
|
|
|
|
}
|
|
|
|
|
2020-04-29 02:04:42 -05:00
|
|
|
function getExternalAuth (options: {
|
|
|
|
url: string
|
|
|
|
npmName: string
|
|
|
|
npmVersion: string
|
|
|
|
authName: string
|
|
|
|
query?: any
|
2020-12-07 07:32:36 -06:00
|
|
|
statusCodeExpected?: HttpStatusCode
|
2020-04-29 02:04:42 -05:00
|
|
|
}) {
|
|
|
|
const { url, npmName, npmVersion, authName, statusCodeExpected, query } = options
|
|
|
|
|
|
|
|
const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName
|
|
|
|
|
|
|
|
return makeGetRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
query,
|
2020-12-07 07:32:36 -06:00
|
|
|
statusCodeExpected: statusCodeExpected || HttpStatusCode.OK_200,
|
2020-04-29 02:04:42 -05:00
|
|
|
redirects: 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:23:24 -05:00
|
|
|
export {
|
|
|
|
listPlugins,
|
2019-07-19 07:36:04 -05:00
|
|
|
listAvailablePlugins,
|
2019-07-11 10:23:24 -05:00
|
|
|
installPlugin,
|
2019-07-26 07:44:50 -05:00
|
|
|
getPluginTranslations,
|
2019-07-19 07:36:04 -05:00
|
|
|
getPluginsCSS,
|
2019-07-12 04:39:58 -05:00
|
|
|
updatePlugin,
|
2019-07-11 10:23:24 -05:00
|
|
|
getPlugin,
|
|
|
|
uninstallPlugin,
|
2020-11-27 04:41:38 -06:00
|
|
|
testHelloWorldRegisteredSettings,
|
2019-07-19 07:36:04 -05:00
|
|
|
updatePluginSettings,
|
|
|
|
getPluginRegisteredSettings,
|
|
|
|
getPackageJSONPath,
|
|
|
|
updatePluginPackageJSON,
|
2019-07-19 10:30:41 -05:00
|
|
|
getPluginPackageJSON,
|
2019-07-26 02:35:43 -05:00
|
|
|
getPluginTestPath,
|
2020-04-29 02:04:42 -05:00
|
|
|
getPublicSettings,
|
|
|
|
getExternalAuth
|
2019-07-11 10:23:24 -05:00
|
|
|
}
|