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