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'
12 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
14 function listPlugins (parameters: {
20 pluginType?: PluginType
22 expectedStatus?: HttpStatusCode
24 const { url, accessToken, start, count, sort, pluginType, uninstalled, expectedStatus = HttpStatusCode.OK_200 } = parameters
25 const path = '/api/v1/plugins'
27 return makeGetRequest({
38 statusCodeExpected: expectedStatus
42 function listAvailablePlugins (parameters: {
48 pluginType?: PluginType
49 currentPeerTubeEngine?: string
51 expectedStatus?: HttpStatusCode
61 currentPeerTubeEngine,
62 expectedStatus = HttpStatusCode.OK_200
64 const path = '/api/v1/plugins/available'
66 const query: PeertubePluginIndexList = {
71 currentPeerTubeEngine,
75 return makeGetRequest({
80 statusCodeExpected: expectedStatus
84 function getPlugin (parameters: {
88 expectedStatus?: HttpStatusCode
90 const { url, accessToken, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters
91 const path = '/api/v1/plugins/' + npmName
93 return makeGetRequest({
97 statusCodeExpected: expectedStatus
101 function updatePluginSettings (parameters: {
106 expectedStatus?: HttpStatusCode
108 const { url, accessToken, npmName, settings, expectedStatus = HttpStatusCode.NO_CONTENT_204 } = parameters
109 const path = '/api/v1/plugins/' + npmName + '/settings'
111 return makePutBodyRequest({
115 fields: { settings },
116 statusCodeExpected: expectedStatus
120 function getPluginRegisteredSettings (parameters: {
124 expectedStatus?: HttpStatusCode
126 const { url, accessToken, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters
127 const path = '/api/v1/plugins/' + npmName + '/registered-settings'
129 return makeGetRequest({
133 statusCodeExpected: expectedStatus
137 async function testHelloWorldRegisteredSettings (server: ServerInfo) {
138 const res = await getPluginRegisteredSettings({
140 accessToken: server.accessToken,
141 npmName: 'peertube-plugin-hello-world'
144 const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings
146 expect(registeredSettings).to.have.length.at.least(1)
148 const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name')
149 expect(adminNameSettings).to.not.be.undefined
152 function getPublicSettings (parameters: {
155 expectedStatus?: HttpStatusCode
157 const { url, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters
158 const path = '/api/v1/plugins/' + npmName + '/public-settings'
160 return makeGetRequest({
163 statusCodeExpected: expectedStatus
167 function getPluginTranslations (parameters: {
170 expectedStatus?: HttpStatusCode
172 const { url, locale, expectedStatus = HttpStatusCode.OK_200 } = parameters
173 const path = '/plugins/translations/' + locale + '.json'
175 return makeGetRequest({
178 statusCodeExpected: expectedStatus
182 function installPlugin (parameters: {
187 expectedStatus?: HttpStatusCode
189 const { url, accessToken, npmName, path, expectedStatus = HttpStatusCode.OK_200 } = parameters
190 const apiPath = '/api/v1/plugins/install'
192 return makePostBodyRequest({
196 fields: { npmName, path },
197 statusCodeExpected: expectedStatus
201 function updatePlugin (parameters: {
206 expectedStatus?: HttpStatusCode
208 const { url, accessToken, npmName, path, expectedStatus = HttpStatusCode.OK_200 } = parameters
209 const apiPath = '/api/v1/plugins/update'
211 return makePostBodyRequest({
215 fields: { npmName, path },
216 statusCodeExpected: expectedStatus
220 function uninstallPlugin (parameters: {
224 expectedStatus?: HttpStatusCode
226 const { url, accessToken, npmName, expectedStatus = HttpStatusCode.NO_CONTENT_204 } = parameters
227 const apiPath = '/api/v1/plugins/uninstall'
229 return makePostBodyRequest({
234 statusCodeExpected: expectedStatus
238 function getPluginsCSS (url: string) {
239 const path = '/plugins/global.css'
241 return makeGetRequest({
244 statusCodeExpected: HttpStatusCode.OK_200
248 function getPackageJSONPath (server: ServerInfo, npmName: string) {
249 return buildServerDirectory(server, join('plugins', 'node_modules', npmName, 'package.json'))
252 function updatePluginPackageJSON (server: ServerInfo, npmName: string, json: any) {
253 const path = getPackageJSONPath(server, npmName)
255 return writeJSON(path, json)
258 function getPluginPackageJSON (server: ServerInfo, npmName: string) {
259 const path = getPackageJSONPath(server, npmName)
261 return readJSON(path)
264 function getPluginTestPath (suffix = '') {
265 return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix)
268 function getExternalAuth (options: {
274 statusCodeExpected?: HttpStatusCode
276 const { url, npmName, npmVersion, authName, statusCodeExpected, query } = options
278 const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName
280 return makeGetRequest({
284 statusCodeExpected: statusCodeExpected || HttpStatusCode.OK_200,
291 listAvailablePlugins,
293 getPluginTranslations,
298 testHelloWorldRegisteredSettings,
299 updatePluginSettings,
300 getPluginRegisteredSettings,
302 updatePluginPackageJSON,
303 getPluginPackageJSON,