]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/server/plugins.ts
hls-plugin: destroy hls upon third err
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / server / plugins.ts
CommitLineData
353f8bc0
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import { expect } from 'chai'
09071200 4import { readJSON, writeJSON } from 'fs-extra'
09071200 5import { join } from 'path'
353f8bc0 6import { RegisteredServerSettings } from '@shared/models'
ca5c612b
C
7import { PeertubePluginIndexList } from '../../models/plugins/peertube-plugin-index-list.model'
8import { PluginType } from '../../models/plugins/plugin.type'
9import { buildServerDirectory, root } from '../miscs/miscs'
10import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
11import { ServerInfo } from './servers'
8d2be0ed
C
12
13function listPlugins (parameters: {
a1587156
C
14 url: string
15 accessToken: string
16 start?: number
17 count?: number
18 sort?: string
19 pluginType?: PluginType
20 uninstalled?: boolean
8d2be0ed
C
21 expectedStatus?: number
22}) {
09071200 23 const { url, accessToken, start, count, sort, pluginType, uninstalled, expectedStatus = 200 } = parameters
8d2be0ed
C
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,
09071200
C
34 pluginType,
35 uninstalled
8d2be0ed
C
36 },
37 statusCodeExpected: expectedStatus
38 })
39}
40
09071200 41function listAvailablePlugins (parameters: {
a1587156
C
42 url: string
43 accessToken: string
44 start?: number
45 count?: number
46 sort?: string
47 pluginType?: PluginType
48 currentPeerTubeEngine?: string
09071200
C
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
8d2be0ed 73function getPlugin (parameters: {
a1587156
C
74 url: string
75 accessToken: string
76 npmName: string
8d2be0ed
C
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
09071200 90function updatePluginSettings (parameters: {
a1587156
C
91 url: string
92 accessToken: string
93 npmName: string
94 settings: any
8d2be0ed
C
95 expectedStatus?: number
96}) {
09071200 97 const { url, accessToken, npmName, settings, expectedStatus = 204 } = parameters
8d2be0ed
C
98 const path = '/api/v1/plugins/' + npmName + '/settings'
99
09071200 100 return makePutBodyRequest({
8d2be0ed
C
101 url,
102 path,
103 token: accessToken,
09071200 104 fields: { settings },
8d2be0ed
C
105 statusCodeExpected: expectedStatus
106 })
107}
108
109function getPluginRegisteredSettings (parameters: {
a1587156
C
110 url: string
111 accessToken: string
112 npmName: string
8d2be0ed
C
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
353f8bc0
C
126async 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
ba211e73 141function getPublicSettings (parameters: {
a1587156
C
142 url: string
143 npmName: string
ba211e73
C
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
d75db01f 156function getPluginTranslations (parameters: {
a1587156
C
157 url: string
158 locale: string
d75db01f
C
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
8d2be0ed 171function installPlugin (parameters: {
a1587156
C
172 url: string
173 accessToken: string
174 path?: string
8d2be0ed
C
175 npmName?: string
176 expectedStatus?: number
177}) {
b5f919ac 178 const { url, accessToken, npmName, path, expectedStatus = 200 } = parameters
8d2be0ed
C
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
b5f919ac 190function updatePlugin (parameters: {
a1587156
C
191 url: string
192 accessToken: string
193 path?: string
b5f919ac
C
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
8d2be0ed 209function uninstallPlugin (parameters: {
a1587156
C
210 url: string
211 accessToken: string
8d2be0ed
C
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
09071200
C
227function getPluginsCSS (url: string) {
228 const path = '/plugins/global.css'
229
230 return makeGetRequest({
231 url,
232 path,
233 statusCodeExpected: 200
234 })
235}
236
237function getPackageJSONPath (server: ServerInfo, npmName: string) {
ca5c612b 238 return buildServerDirectory(server, join('plugins', 'node_modules', npmName, 'package.json'))
09071200
C
239}
240
241function updatePluginPackageJSON (server: ServerInfo, npmName: string, json: any) {
242 const path = getPackageJSONPath(server, npmName)
243
244 return writeJSON(path, json)
245}
246
247function getPluginPackageJSON (server: ServerInfo, npmName: string) {
248 const path = getPackageJSONPath(server, npmName)
249
250 return readJSON(path)
251}
252
89cd1275
C
253function getPluginTestPath (suffix = '') {
254 return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix)
255}
256
9107d791
C
257function 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
8d2be0ed
C
278export {
279 listPlugins,
09071200 280 listAvailablePlugins,
8d2be0ed 281 installPlugin,
d75db01f 282 getPluginTranslations,
09071200 283 getPluginsCSS,
b5f919ac 284 updatePlugin,
8d2be0ed
C
285 getPlugin,
286 uninstallPlugin,
353f8bc0 287 testHelloWorldRegisteredSettings,
09071200
C
288 updatePluginSettings,
289 getPluginRegisteredSettings,
290 getPackageJSONPath,
291 updatePluginPackageJSON,
89cd1275 292 getPluginPackageJSON,
ba211e73 293 getPluginTestPath,
9107d791
C
294 getPublicSettings,
295 getExternalAuth
8d2be0ed 296}