1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { readJSON, writeJSON } from 'fs-extra'
5 import { join } from 'path'
6 import { RegisteredServerSettings } from '@shared/models'
7 import { PeertubePluginIndexList } from '../../models/plugins/peertube-plugin-index-list.model'
8 import { PluginType } from '../../models/plugins/plugin.type'
9 import { buildServerDirectory, root } from '../miscs/miscs'
10 import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
11 import { ServerInfo } from './servers'
13 function listPlugins (parameters: {
19 pluginType?: PluginType
21 expectedStatus?: number
23 const { url, accessToken, start, count, sort, pluginType, uninstalled, expectedStatus = 200 } = parameters
24 const path = '/api/v1/plugins'
26 return makeGetRequest({
37 statusCodeExpected: expectedStatus
41 function listAvailablePlugins (parameters: {
47 pluginType?: PluginType
48 currentPeerTubeEngine?: string
50 expectedStatus?: number
52 const { url, accessToken, start, count, sort, pluginType, search, currentPeerTubeEngine, expectedStatus = 200 } = parameters
53 const path = '/api/v1/plugins/available'
55 const query: PeertubePluginIndexList = {
60 currentPeerTubeEngine,
64 return makeGetRequest({
69 statusCodeExpected: expectedStatus
73 function getPlugin (parameters: {
77 expectedStatus?: number
79 const { url, accessToken, npmName, expectedStatus = 200 } = parameters
80 const path = '/api/v1/plugins/' + npmName
82 return makeGetRequest({
86 statusCodeExpected: expectedStatus
90 function updatePluginSettings (parameters: {
95 expectedStatus?: number
97 const { url, accessToken, npmName, settings, expectedStatus = 204 } = parameters
98 const path = '/api/v1/plugins/' + npmName + '/settings'
100 return makePutBodyRequest({
104 fields: { settings },
105 statusCodeExpected: expectedStatus
109 function getPluginRegisteredSettings (parameters: {
113 expectedStatus?: number
115 const { url, accessToken, npmName, expectedStatus = 200 } = parameters
116 const path = '/api/v1/plugins/' + npmName + '/registered-settings'
118 return makeGetRequest({
122 statusCodeExpected: expectedStatus
126 async function testHelloWorldRegisteredSettings (server: ServerInfo) {
127 const res = await getPluginRegisteredSettings({
129 accessToken: server.accessToken,
130 npmName: 'peertube-plugin-hello-world'
133 const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings
135 expect(registeredSettings).to.have.length.at.least(1)
137 const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name')
138 expect(adminNameSettings).to.not.be.undefined
141 function getPublicSettings (parameters: {
144 expectedStatus?: number
146 const { url, npmName, expectedStatus = 200 } = parameters
147 const path = '/api/v1/plugins/' + npmName + '/public-settings'
149 return makeGetRequest({
152 statusCodeExpected: expectedStatus
156 function getPluginTranslations (parameters: {
159 expectedStatus?: number
161 const { url, locale, expectedStatus = 200 } = parameters
162 const path = '/plugins/translations/' + locale + '.json'
164 return makeGetRequest({
167 statusCodeExpected: expectedStatus
171 function installPlugin (parameters: {
176 expectedStatus?: number
178 const { url, accessToken, npmName, path, expectedStatus = 200 } = parameters
179 const apiPath = '/api/v1/plugins/install'
181 return makePostBodyRequest({
185 fields: { npmName, path },
186 statusCodeExpected: expectedStatus
190 function updatePlugin (parameters: {
195 expectedStatus?: number
197 const { url, accessToken, npmName, path, expectedStatus = 200 } = parameters
198 const apiPath = '/api/v1/plugins/update'
200 return makePostBodyRequest({
204 fields: { npmName, path },
205 statusCodeExpected: expectedStatus
209 function uninstallPlugin (parameters: {
213 expectedStatus?: number
215 const { url, accessToken, npmName, expectedStatus = 204 } = parameters
216 const apiPath = '/api/v1/plugins/uninstall'
218 return makePostBodyRequest({
223 statusCodeExpected: expectedStatus
227 function getPluginsCSS (url: string) {
228 const path = '/plugins/global.css'
230 return makeGetRequest({
233 statusCodeExpected: 200
237 function getPackageJSONPath (server: ServerInfo, npmName: string) {
238 return buildServerDirectory(server, join('plugins', 'node_modules', npmName, 'package.json'))
241 function updatePluginPackageJSON (server: ServerInfo, npmName: string, json: any) {
242 const path = getPackageJSONPath(server, npmName)
244 return writeJSON(path, json)
247 function getPluginPackageJSON (server: ServerInfo, npmName: string) {
248 const path = getPackageJSONPath(server, npmName)
250 return readJSON(path)
253 function getPluginTestPath (suffix = '') {
254 return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix)
257 function getExternalAuth (options: {
263 statusCodeExpected?: number
265 const { url, npmName, npmVersion, authName, statusCodeExpected, query } = options
267 const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName
269 return makeGetRequest({
273 statusCodeExpected: statusCodeExpected || 200,
280 listAvailablePlugins,
282 getPluginTranslations,
287 testHelloWorldRegisteredSettings,
288 updatePluginSettings,
289 getPluginRegisteredSettings,
291 updatePluginPackageJSON,
292 getPluginPackageJSON,