]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/server/plugins.ts
hls-plugin: destroy hls upon third err
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / server / plugins.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
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
13 function listPlugins (parameters: {
14 url: string
15 accessToken: string
16 start?: number
17 count?: number
18 sort?: string
19 pluginType?: PluginType
20 uninstalled?: boolean
21 expectedStatus?: number
22 }) {
23 const { url, accessToken, start, count, sort, pluginType, uninstalled, expectedStatus = 200 } = parameters
24 const path = '/api/v1/plugins'
25
26 return makeGetRequest({
27 url,
28 path,
29 token: accessToken,
30 query: {
31 start,
32 count,
33 sort,
34 pluginType,
35 uninstalled
36 },
37 statusCodeExpected: expectedStatus
38 })
39 }
40
41 function listAvailablePlugins (parameters: {
42 url: string
43 accessToken: string
44 start?: number
45 count?: number
46 sort?: string
47 pluginType?: PluginType
48 currentPeerTubeEngine?: string
49 search?: string
50 expectedStatus?: number
51 }) {
52 const { url, accessToken, start, count, sort, pluginType, search, currentPeerTubeEngine, expectedStatus = 200 } = parameters
53 const path = '/api/v1/plugins/available'
54
55 const query: PeertubePluginIndexList = {
56 start,
57 count,
58 sort,
59 pluginType,
60 currentPeerTubeEngine,
61 search
62 }
63
64 return makeGetRequest({
65 url,
66 path,
67 token: accessToken,
68 query,
69 statusCodeExpected: expectedStatus
70 })
71 }
72
73 function getPlugin (parameters: {
74 url: string
75 accessToken: string
76 npmName: string
77 expectedStatus?: number
78 }) {
79 const { url, accessToken, npmName, expectedStatus = 200 } = parameters
80 const path = '/api/v1/plugins/' + npmName
81
82 return makeGetRequest({
83 url,
84 path,
85 token: accessToken,
86 statusCodeExpected: expectedStatus
87 })
88 }
89
90 function updatePluginSettings (parameters: {
91 url: string
92 accessToken: string
93 npmName: string
94 settings: any
95 expectedStatus?: number
96 }) {
97 const { url, accessToken, npmName, settings, expectedStatus = 204 } = parameters
98 const path = '/api/v1/plugins/' + npmName + '/settings'
99
100 return makePutBodyRequest({
101 url,
102 path,
103 token: accessToken,
104 fields: { settings },
105 statusCodeExpected: expectedStatus
106 })
107 }
108
109 function getPluginRegisteredSettings (parameters: {
110 url: string
111 accessToken: string
112 npmName: string
113 expectedStatus?: number
114 }) {
115 const { url, accessToken, npmName, expectedStatus = 200 } = parameters
116 const path = '/api/v1/plugins/' + npmName + '/registered-settings'
117
118 return makeGetRequest({
119 url,
120 path,
121 token: accessToken,
122 statusCodeExpected: expectedStatus
123 })
124 }
125
126 async function testHelloWorldRegisteredSettings (server: ServerInfo) {
127 const res = await getPluginRegisteredSettings({
128 url: server.url,
129 accessToken: server.accessToken,
130 npmName: 'peertube-plugin-hello-world'
131 })
132
133 const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings
134
135 expect(registeredSettings).to.have.length.at.least(1)
136
137 const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name')
138 expect(adminNameSettings).to.not.be.undefined
139 }
140
141 function getPublicSettings (parameters: {
142 url: string
143 npmName: string
144 expectedStatus?: number
145 }) {
146 const { url, npmName, expectedStatus = 200 } = parameters
147 const path = '/api/v1/plugins/' + npmName + '/public-settings'
148
149 return makeGetRequest({
150 url,
151 path,
152 statusCodeExpected: expectedStatus
153 })
154 }
155
156 function getPluginTranslations (parameters: {
157 url: string
158 locale: string
159 expectedStatus?: number
160 }) {
161 const { url, locale, expectedStatus = 200 } = parameters
162 const path = '/plugins/translations/' + locale + '.json'
163
164 return makeGetRequest({
165 url,
166 path,
167 statusCodeExpected: expectedStatus
168 })
169 }
170
171 function installPlugin (parameters: {
172 url: string
173 accessToken: string
174 path?: string
175 npmName?: string
176 expectedStatus?: number
177 }) {
178 const { url, accessToken, npmName, path, expectedStatus = 200 } = parameters
179 const apiPath = '/api/v1/plugins/install'
180
181 return makePostBodyRequest({
182 url,
183 path: apiPath,
184 token: accessToken,
185 fields: { npmName, path },
186 statusCodeExpected: expectedStatus
187 })
188 }
189
190 function updatePlugin (parameters: {
191 url: string
192 accessToken: string
193 path?: string
194 npmName?: string
195 expectedStatus?: number
196 }) {
197 const { url, accessToken, npmName, path, expectedStatus = 200 } = parameters
198 const apiPath = '/api/v1/plugins/update'
199
200 return makePostBodyRequest({
201 url,
202 path: apiPath,
203 token: accessToken,
204 fields: { npmName, path },
205 statusCodeExpected: expectedStatus
206 })
207 }
208
209 function uninstallPlugin (parameters: {
210 url: string
211 accessToken: string
212 npmName: string
213 expectedStatus?: number
214 }) {
215 const { url, accessToken, npmName, expectedStatus = 204 } = parameters
216 const apiPath = '/api/v1/plugins/uninstall'
217
218 return makePostBodyRequest({
219 url,
220 path: apiPath,
221 token: accessToken,
222 fields: { npmName },
223 statusCodeExpected: expectedStatus
224 })
225 }
226
227 function getPluginsCSS (url: string) {
228 const path = '/plugins/global.css'
229
230 return makeGetRequest({
231 url,
232 path,
233 statusCodeExpected: 200
234 })
235 }
236
237 function getPackageJSONPath (server: ServerInfo, npmName: string) {
238 return buildServerDirectory(server, join('plugins', 'node_modules', npmName, 'package.json'))
239 }
240
241 function updatePluginPackageJSON (server: ServerInfo, npmName: string, json: any) {
242 const path = getPackageJSONPath(server, npmName)
243
244 return writeJSON(path, json)
245 }
246
247 function getPluginPackageJSON (server: ServerInfo, npmName: string) {
248 const path = getPackageJSONPath(server, npmName)
249
250 return readJSON(path)
251 }
252
253 function getPluginTestPath (suffix = '') {
254 return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix)
255 }
256
257 function getExternalAuth (options: {
258 url: string
259 npmName: string
260 npmVersion: string
261 authName: string
262 query?: any
263 statusCodeExpected?: number
264 }) {
265 const { url, npmName, npmVersion, authName, statusCodeExpected, query } = options
266
267 const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName
268
269 return makeGetRequest({
270 url,
271 path,
272 query,
273 statusCodeExpected: statusCodeExpected || 200,
274 redirects: 0
275 })
276 }
277
278 export {
279 listPlugins,
280 listAvailablePlugins,
281 installPlugin,
282 getPluginTranslations,
283 getPluginsCSS,
284 updatePlugin,
285 getPlugin,
286 uninstallPlugin,
287 testHelloWorldRegisteredSettings,
288 updatePluginSettings,
289 getPluginRegisteredSettings,
290 getPackageJSONPath,
291 updatePluginPackageJSON,
292 getPluginPackageJSON,
293 getPluginTestPath,
294 getPublicSettings,
295 getExternalAuth
296 }