plugins.ts 7.3 KB

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