plugins.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import { pathExists, remove } from 'fs-extra/esm'
  4. import { join } from 'path'
  5. import { wait } from '@peertube/peertube-core-utils'
  6. import { HttpStatusCode, PluginType } from '@peertube/peertube-models'
  7. import {
  8. cleanupTests,
  9. createSingleServer,
  10. killallServers,
  11. makeGetRequest,
  12. PeerTubeServer,
  13. PluginsCommand,
  14. setAccessTokensToServers
  15. } from '@peertube/peertube-server-commands'
  16. import { SQLCommand } from '@tests/shared/sql-command.js'
  17. import { testHelloWorldRegisteredSettings } from '@tests/shared/plugins.js'
  18. describe('Test plugins', function () {
  19. let server: PeerTubeServer
  20. let sqlCommand: SQLCommand
  21. let command: PluginsCommand
  22. before(async function () {
  23. this.timeout(30000)
  24. const configOverride = {
  25. plugins: {
  26. index: { check_latest_versions_interval: '5 seconds' }
  27. }
  28. }
  29. server = await createSingleServer(1, configOverride)
  30. await setAccessTokensToServers([ server ])
  31. command = server.plugins
  32. sqlCommand = new SQLCommand(server)
  33. })
  34. it('Should list and search available plugins and themes', async function () {
  35. this.timeout(30000)
  36. {
  37. const body = await command.listAvailable({
  38. count: 1,
  39. start: 0,
  40. pluginType: PluginType.THEME,
  41. search: 'background-red'
  42. })
  43. expect(body.total).to.be.at.least(1)
  44. expect(body.data).to.have.lengthOf(1)
  45. }
  46. {
  47. const body1 = await command.listAvailable({
  48. count: 2,
  49. start: 0,
  50. sort: 'npmName'
  51. })
  52. expect(body1.total).to.be.at.least(2)
  53. const data1 = body1.data
  54. expect(data1).to.have.lengthOf(2)
  55. const body2 = await command.listAvailable({
  56. count: 2,
  57. start: 0,
  58. sort: '-npmName'
  59. })
  60. expect(body2.total).to.be.at.least(2)
  61. const data2 = body2.data
  62. expect(data2).to.have.lengthOf(2)
  63. expect(data1[0].npmName).to.not.equal(data2[0].npmName)
  64. }
  65. {
  66. const body = await command.listAvailable({
  67. count: 10,
  68. start: 0,
  69. pluginType: PluginType.THEME,
  70. search: 'background-red',
  71. currentPeerTubeEngine: '1.0.0'
  72. })
  73. const p = body.data.find(p => p.npmName === 'peertube-theme-background-red')
  74. expect(p).to.be.undefined
  75. }
  76. })
  77. it('Should install a plugin and a theme', async function () {
  78. this.timeout(30000)
  79. await command.install({ npmName: 'peertube-plugin-hello-world' })
  80. await command.install({ npmName: 'peertube-theme-background-red' })
  81. })
  82. it('Should have the plugin loaded in the configuration', async function () {
  83. for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
  84. const theme = config.theme.registered.find(r => r.name === 'background-red')
  85. expect(theme).to.not.be.undefined
  86. expect(theme.npmName).to.equal('peertube-theme-background-red')
  87. const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
  88. expect(plugin).to.not.be.undefined
  89. expect(plugin.npmName).to.equal('peertube-plugin-hello-world')
  90. }
  91. })
  92. it('Should update the default theme in the configuration', async function () {
  93. await server.config.updateExistingConfig({
  94. newConfig: {
  95. theme: { default: 'background-red' }
  96. }
  97. })
  98. for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
  99. expect(config.theme.default).to.equal('background-red')
  100. }
  101. })
  102. it('Should update my default theme', async function () {
  103. await server.users.updateMe({ theme: 'background-red' })
  104. const user = await server.users.getMyInfo()
  105. expect(user.theme).to.equal('background-red')
  106. })
  107. it('Should list plugins and themes', async function () {
  108. {
  109. const body = await command.list({
  110. count: 1,
  111. start: 0,
  112. pluginType: PluginType.THEME
  113. })
  114. expect(body.total).to.be.at.least(1)
  115. const data = body.data
  116. expect(data).to.have.lengthOf(1)
  117. expect(data[0].name).to.equal('background-red')
  118. }
  119. {
  120. const { data } = await command.list({
  121. count: 2,
  122. start: 0,
  123. sort: 'name'
  124. })
  125. expect(data[0].name).to.equal('background-red')
  126. expect(data[1].name).to.equal('hello-world')
  127. }
  128. {
  129. const body = await command.list({
  130. count: 2,
  131. start: 1,
  132. sort: 'name'
  133. })
  134. expect(body.data[0].name).to.equal('hello-world')
  135. }
  136. })
  137. it('Should get registered settings', async function () {
  138. await testHelloWorldRegisteredSettings(server)
  139. })
  140. it('Should get public settings', async function () {
  141. const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
  142. const publicSettings = body.publicSettings
  143. expect(Object.keys(publicSettings)).to.have.lengthOf(1)
  144. expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
  145. expect(publicSettings['user-name']).to.be.null
  146. })
  147. it('Should update the settings', async function () {
  148. const settings = {
  149. 'admin-name': 'Cid'
  150. }
  151. await command.updateSettings({
  152. npmName: 'peertube-plugin-hello-world',
  153. settings
  154. })
  155. })
  156. it('Should have watched settings changes', async function () {
  157. await server.servers.waitUntilLog('Settings changed!')
  158. })
  159. it('Should get a plugin and a theme', async function () {
  160. {
  161. const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
  162. expect(plugin.type).to.equal(PluginType.PLUGIN)
  163. expect(plugin.name).to.equal('hello-world')
  164. expect(plugin.description).to.exist
  165. expect(plugin.homepage).to.exist
  166. expect(plugin.uninstalled).to.be.false
  167. expect(plugin.enabled).to.be.true
  168. expect(plugin.description).to.exist
  169. expect(plugin.version).to.exist
  170. expect(plugin.peertubeEngine).to.exist
  171. expect(plugin.createdAt).to.exist
  172. expect(plugin.settings).to.not.be.undefined
  173. expect(plugin.settings['admin-name']).to.equal('Cid')
  174. }
  175. {
  176. const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
  177. expect(plugin.type).to.equal(PluginType.THEME)
  178. expect(plugin.name).to.equal('background-red')
  179. expect(plugin.description).to.exist
  180. expect(plugin.homepage).to.exist
  181. expect(plugin.uninstalled).to.be.false
  182. expect(plugin.enabled).to.be.true
  183. expect(plugin.description).to.exist
  184. expect(plugin.version).to.exist
  185. expect(plugin.peertubeEngine).to.exist
  186. expect(plugin.createdAt).to.exist
  187. expect(plugin.settings).to.be.null
  188. }
  189. })
  190. it('Should update the plugin and the theme', async function () {
  191. this.timeout(180000)
  192. // Wait the scheduler that get the latest plugins versions
  193. await wait(6000)
  194. async function testUpdate (type: 'plugin' | 'theme', name: string) {
  195. // Fake update our plugin version
  196. await sqlCommand.setPluginVersion(name, '0.0.1')
  197. // Fake update package.json
  198. const packageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
  199. const oldVersion = packageJSON.version
  200. packageJSON.version = '0.0.1'
  201. await command.updatePackageJSON(`peertube-${type}-${name}`, packageJSON)
  202. // Restart the server to take into account this change
  203. await killallServers([ server ])
  204. await server.run()
  205. const checkConfig = async (version: string) => {
  206. for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
  207. expect(config[type].registered.find(r => r.name === name).version).to.equal(version)
  208. }
  209. }
  210. const getPluginFromAPI = async () => {
  211. const body = await command.list({ pluginType: type === 'plugin' ? PluginType.PLUGIN : PluginType.THEME })
  212. return body.data.find(p => p.name === name)
  213. }
  214. {
  215. const plugin = await getPluginFromAPI()
  216. expect(plugin.version).to.equal('0.0.1')
  217. expect(plugin.latestVersion).to.exist
  218. expect(plugin.latestVersion).to.not.equal('0.0.1')
  219. await checkConfig('0.0.1')
  220. }
  221. {
  222. await command.update({ npmName: `peertube-${type}-${name}` })
  223. const plugin = await getPluginFromAPI()
  224. expect(plugin.version).to.equal(oldVersion)
  225. const updatedPackageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
  226. expect(updatedPackageJSON.version).to.equal(oldVersion)
  227. await checkConfig(oldVersion)
  228. }
  229. }
  230. await testUpdate('theme', 'background-red')
  231. await testUpdate('plugin', 'hello-world')
  232. })
  233. it('Should uninstall the plugin', async function () {
  234. await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
  235. const body = await command.list({ pluginType: PluginType.PLUGIN })
  236. expect(body.total).to.equal(0)
  237. expect(body.data).to.have.lengthOf(0)
  238. })
  239. it('Should list uninstalled plugins', async function () {
  240. const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
  241. expect(body.total).to.equal(1)
  242. expect(body.data).to.have.lengthOf(1)
  243. const plugin = body.data[0]
  244. expect(plugin.name).to.equal('hello-world')
  245. expect(plugin.enabled).to.be.false
  246. expect(plugin.uninstalled).to.be.true
  247. })
  248. it('Should uninstall the theme', async function () {
  249. await command.uninstall({ npmName: 'peertube-theme-background-red' })
  250. })
  251. it('Should have updated the configuration', async function () {
  252. for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
  253. expect(config.theme.default).to.equal('default')
  254. const theme = config.theme.registered.find(r => r.name === 'background-red')
  255. expect(theme).to.be.undefined
  256. const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
  257. expect(plugin).to.be.undefined
  258. }
  259. })
  260. it('Should have updated the user theme', async function () {
  261. const user = await server.users.getMyInfo()
  262. expect(user.theme).to.equal('instance-default')
  263. })
  264. it('Should not install a broken plugin', async function () {
  265. this.timeout(60000)
  266. async function check () {
  267. const body = await command.list({ pluginType: PluginType.PLUGIN })
  268. const plugins = body.data
  269. expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
  270. }
  271. await command.install({
  272. path: PluginsCommand.getPluginTestPath('-broken'),
  273. expectedStatus: HttpStatusCode.BAD_REQUEST_400
  274. })
  275. await check()
  276. await killallServers([ server ])
  277. await server.run()
  278. await check()
  279. })
  280. it('Should rebuild native modules on Node ABI change', async function () {
  281. this.timeout(60000)
  282. const removeNativeModule = async () => {
  283. await remove(join(baseNativeModule, 'build'))
  284. await remove(join(baseNativeModule, 'prebuilds'))
  285. }
  286. await command.install({ path: PluginsCommand.getPluginTestPath('-native') })
  287. await makeGetRequest({
  288. url: server.url,
  289. path: '/plugins/test-native/router',
  290. expectedStatus: HttpStatusCode.NO_CONTENT_204
  291. })
  292. const query = `UPDATE "application" SET "nodeABIVersion" = 1`
  293. await sqlCommand.updateQuery(query)
  294. const baseNativeModule = server.servers.buildDirectory(join('plugins', 'node_modules', 'a-native-example'))
  295. await removeNativeModule()
  296. await server.kill()
  297. await server.run()
  298. await wait(3000)
  299. expect(await pathExists(join(baseNativeModule, 'build'))).to.be.true
  300. expect(await pathExists(join(baseNativeModule, 'prebuilds'))).to.be.true
  301. await makeGetRequest({
  302. url: server.url,
  303. path: '/plugins/test-native/router',
  304. expectedStatus: HttpStatusCode.NO_CONTENT_204
  305. })
  306. await removeNativeModule()
  307. await server.kill()
  308. await server.run()
  309. expect(await pathExists(join(baseNativeModule, 'build'))).to.be.false
  310. expect(await pathExists(join(baseNativeModule, 'prebuilds'))).to.be.false
  311. await makeGetRequest({
  312. url: server.url,
  313. path: '/plugins/test-native/router',
  314. expectedStatus: HttpStatusCode.NOT_FOUND_404
  315. })
  316. })
  317. after(async function () {
  318. await sqlCommand.cleanup()
  319. await cleanupTests([ server ])
  320. })
  321. })