plugins.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* tslint:disable:no-unused-expression */
  2. import 'mocha'
  3. import {
  4. checkBadCountPagination,
  5. checkBadSortPagination,
  6. checkBadStartPagination,
  7. cleanupTests,
  8. createUser,
  9. flushAndRunServer,
  10. immutableAssign,
  11. installPlugin,
  12. makeGetRequest, makePostBodyRequest, makePutBodyRequest,
  13. ServerInfo,
  14. setAccessTokensToServers,
  15. userLogin
  16. } from '../../../../shared/extra-utils'
  17. import { PluginType } from '../../../../shared/models/plugins/plugin.type'
  18. import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model'
  19. describe('Test server plugins API validators', function () {
  20. let server: ServerInfo
  21. let userAccessToken = null
  22. const npmPlugin = 'peertube-plugin-hello-world'
  23. const pluginName = 'hello-world'
  24. let npmVersion: string
  25. const themePlugin = 'peertube-theme-background-red'
  26. const themeName = 'background-red'
  27. let themeVersion: string
  28. // ---------------------------------------------------------------
  29. before(async function () {
  30. this.timeout(30000)
  31. server = await flushAndRunServer(1)
  32. await setAccessTokensToServers([ server ])
  33. const user = {
  34. username: 'user1',
  35. password: 'password'
  36. }
  37. await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
  38. userAccessToken = await userLogin(server, user)
  39. {
  40. const res = await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: npmPlugin })
  41. const plugin = res.body as PeerTubePlugin
  42. npmVersion = plugin.version
  43. }
  44. {
  45. const res = await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: themePlugin })
  46. const plugin = res.body as PeerTubePlugin
  47. themeVersion = plugin.version
  48. }
  49. })
  50. describe('With static plugin routes', function () {
  51. it('Should fail with an unknown plugin name/plugin version', async function () {
  52. const paths = [
  53. '/plugins/' + pluginName + '/0.0.1/static/images/chocobo.png',
  54. '/plugins/' + pluginName + '/0.0.1/client-scripts/client/common-client-plugin.js',
  55. '/themes/' + themeName + '/0.0.1/static/images/chocobo.png',
  56. '/themes/' + themeName + '/0.0.1/client-scripts/client/video-watch-client-plugin.js',
  57. '/themes/' + themeName + '/0.0.1/css/assets/style1.css'
  58. ]
  59. for (const p of paths) {
  60. await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 404 })
  61. }
  62. })
  63. it('Should fail when requesting a plugin in the theme path', async function () {
  64. await makeGetRequest({
  65. url: server.url,
  66. path: '/themes/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png',
  67. statusCodeExpected: 404
  68. })
  69. })
  70. it('Should fail with invalid versions', async function () {
  71. const paths = [
  72. '/plugins/' + pluginName + '/0.0.1.1/static/images/chocobo.png',
  73. '/plugins/' + pluginName + '/0.1/client-scripts/client/common-client-plugin.js',
  74. '/themes/' + themeName + '/1/static/images/chocobo.png',
  75. '/themes/' + themeName + '/0.0.1000a/client-scripts/client/video-watch-client-plugin.js',
  76. '/themes/' + themeName + '/0.a.1/css/assets/style1.css'
  77. ]
  78. for (const p of paths) {
  79. await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 400 })
  80. }
  81. })
  82. it('Should fail with invalid paths', async function () {
  83. const paths = [
  84. '/plugins/' + pluginName + '/' + npmVersion + '/static/images/../chocobo.png',
  85. '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/../client/common-client-plugin.js',
  86. '/themes/' + themeName + '/' + themeVersion + '/static/../images/chocobo.png',
  87. '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/video-watch-client-plugin.js/..',
  88. '/themes/' + themeName + '/' + themeVersion + '/css/../assets/style1.css'
  89. ]
  90. for (const p of paths) {
  91. await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 400 })
  92. }
  93. })
  94. it('Should fail with an unknown static file', async function () {
  95. const paths = [
  96. '/plugins/' + pluginName + '/' + npmVersion + '/static/fake/chocobo.png',
  97. '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/client/fake.js',
  98. '/themes/' + themeName + '/' + themeVersion + '/static/fake/chocobo.png',
  99. '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/fake.js'
  100. ]
  101. for (const p of paths) {
  102. await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 404 })
  103. }
  104. })
  105. it('Should fail with an unknown CSS file', async function () {
  106. await makeGetRequest({
  107. url: server.url,
  108. path: '/themes/' + themeName + '/' + themeVersion + '/css/assets/fake.css',
  109. statusCodeExpected: 404
  110. })
  111. })
  112. it('Should succeed with the correct parameters', async function () {
  113. const paths = [
  114. '/plugins/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png',
  115. '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/client/common-client-plugin.js',
  116. '/themes/' + themeName + '/' + themeVersion + '/static/images/chocobo.png',
  117. '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/video-watch-client-plugin.js',
  118. '/themes/' + themeName + '/' + themeVersion + '/css/assets/style1.css'
  119. ]
  120. for (const p of paths) {
  121. await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 200 })
  122. }
  123. })
  124. })
  125. describe('When listing available plugins/themes', function () {
  126. const path = '/api/v1/plugins/available'
  127. const baseQuery = {
  128. search: 'super search',
  129. pluginType: PluginType.PLUGIN,
  130. currentPeerTubeEngine: '1.2.3'
  131. }
  132. it('Should fail with an invalid token', async function () {
  133. await makeGetRequest({
  134. url: server.url,
  135. path,
  136. token: 'fake_token',
  137. query: baseQuery,
  138. statusCodeExpected: 401
  139. })
  140. })
  141. it('Should fail if the user is not an administrator', async function () {
  142. await makeGetRequest({
  143. url: server.url,
  144. path,
  145. token: userAccessToken,
  146. query: baseQuery,
  147. statusCodeExpected: 403
  148. })
  149. })
  150. it('Should fail with a bad start pagination', async function () {
  151. await checkBadStartPagination(server.url, path, server.accessToken)
  152. })
  153. it('Should fail with a bad count pagination', async function () {
  154. await checkBadCountPagination(server.url, path, server.accessToken)
  155. })
  156. it('Should fail with an incorrect sort', async function () {
  157. await checkBadSortPagination(server.url, path, server.accessToken)
  158. })
  159. it('Should fail with an invalid plugin type', async function () {
  160. const query = immutableAssign(baseQuery, { pluginType: 5 })
  161. await makeGetRequest({
  162. url: server.url,
  163. path,
  164. token: server.accessToken,
  165. query
  166. })
  167. })
  168. it('Should fail with an invalid current peertube engine', async function () {
  169. const query = immutableAssign(baseQuery, { currentPeerTubeEngine: '1.0' })
  170. await makeGetRequest({
  171. url: server.url,
  172. path,
  173. token: server.accessToken,
  174. query
  175. })
  176. })
  177. it('Should success with the correct parameters', async function () {
  178. await makeGetRequest({
  179. url: server.url,
  180. path,
  181. token: server.accessToken,
  182. query: baseQuery,
  183. statusCodeExpected: 200
  184. })
  185. })
  186. })
  187. describe('When listing local plugins/themes', function () {
  188. const path = '/api/v1/plugins'
  189. const baseQuery = {
  190. pluginType: PluginType.THEME
  191. }
  192. it('Should fail with an invalid token', async function () {
  193. await makeGetRequest({
  194. url: server.url,
  195. path,
  196. token: 'fake_token',
  197. query: baseQuery,
  198. statusCodeExpected: 401
  199. })
  200. })
  201. it('Should fail if the user is not an administrator', async function () {
  202. await makeGetRequest({
  203. url: server.url,
  204. path,
  205. token: userAccessToken,
  206. query: baseQuery,
  207. statusCodeExpected: 403
  208. })
  209. })
  210. it('Should fail with a bad start pagination', async function () {
  211. await checkBadStartPagination(server.url, path, server.accessToken)
  212. })
  213. it('Should fail with a bad count pagination', async function () {
  214. await checkBadCountPagination(server.url, path, server.accessToken)
  215. })
  216. it('Should fail with an incorrect sort', async function () {
  217. await checkBadSortPagination(server.url, path, server.accessToken)
  218. })
  219. it('Should fail with an invalid plugin type', async function () {
  220. const query = immutableAssign(baseQuery, { pluginType: 5 })
  221. await makeGetRequest({
  222. url: server.url,
  223. path,
  224. token: server.accessToken,
  225. query
  226. })
  227. })
  228. it('Should success with the correct parameters', async function () {
  229. await makeGetRequest({
  230. url: server.url,
  231. path,
  232. token: server.accessToken,
  233. query: baseQuery,
  234. statusCodeExpected: 200
  235. })
  236. })
  237. })
  238. describe('When getting a plugin or the registered settings or public settings', function () {
  239. const path = '/api/v1/plugins/'
  240. it('Should fail with an invalid token', async function () {
  241. for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) {
  242. await makeGetRequest({
  243. url: server.url,
  244. path: path + suffix,
  245. token: 'fake_token',
  246. statusCodeExpected: 401
  247. })
  248. }
  249. })
  250. it('Should fail if the user is not an administrator', async function () {
  251. for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) {
  252. await makeGetRequest({
  253. url: server.url,
  254. path: path + suffix,
  255. token: userAccessToken,
  256. statusCodeExpected: 403
  257. })
  258. }
  259. })
  260. it('Should fail with an invalid npm name', async function () {
  261. for (const suffix of [ 'toto', 'toto/registered-settings', 'toto/public-settings' ]) {
  262. await makeGetRequest({
  263. url: server.url,
  264. path: path + suffix,
  265. token: server.accessToken,
  266. statusCodeExpected: 400
  267. })
  268. }
  269. for (const suffix of [ 'peertube-plugin-TOTO', 'peertube-plugin-TOTO/registered-settings', 'peertube-plugin-TOTO/public-settings' ]) {
  270. await makeGetRequest({
  271. url: server.url,
  272. path: path + suffix,
  273. token: server.accessToken,
  274. statusCodeExpected: 400
  275. })
  276. }
  277. })
  278. it('Should fail with an unknown plugin', async function () {
  279. for (const suffix of [ 'peertube-plugin-toto', 'peertube-plugin-toto/registered-settings', 'peertube-plugin-toto/public-settings' ]) {
  280. await makeGetRequest({
  281. url: server.url,
  282. path: path + suffix,
  283. token: server.accessToken,
  284. statusCodeExpected: 404
  285. })
  286. }
  287. })
  288. it('Should succeed with the correct parameters', async function () {
  289. for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings`, `${npmPlugin}/public-settings` ]) {
  290. await makeGetRequest({
  291. url: server.url,
  292. path: path + suffix,
  293. token: server.accessToken,
  294. statusCodeExpected: 200
  295. })
  296. }
  297. })
  298. })
  299. describe('When updating plugin settings', function () {
  300. const path = '/api/v1/plugins/'
  301. const settings = { setting1: 'value1' }
  302. it('Should fail with an invalid token', async function () {
  303. await makePutBodyRequest({
  304. url: server.url,
  305. path: path + npmPlugin + '/settings',
  306. fields: { settings },
  307. token: 'fake_token',
  308. statusCodeExpected: 401
  309. })
  310. })
  311. it('Should fail if the user is not an administrator', async function () {
  312. await makePutBodyRequest({
  313. url: server.url,
  314. path: path + npmPlugin + '/settings',
  315. fields: { settings },
  316. token: userAccessToken,
  317. statusCodeExpected: 403
  318. })
  319. })
  320. it('Should fail with an invalid npm name', async function () {
  321. await makePutBodyRequest({
  322. url: server.url,
  323. path: path + 'toto/settings',
  324. fields: { settings },
  325. token: server.accessToken,
  326. statusCodeExpected: 400
  327. })
  328. await makePutBodyRequest({
  329. url: server.url,
  330. path: path + 'peertube-plugin-TOTO/settings',
  331. fields: { settings },
  332. token: server.accessToken,
  333. statusCodeExpected: 400
  334. })
  335. })
  336. it('Should fail with an unknown plugin', async function () {
  337. await makePutBodyRequest({
  338. url: server.url,
  339. path: path + 'peertube-plugin-toto/settings',
  340. fields: { settings },
  341. token: server.accessToken,
  342. statusCodeExpected: 404
  343. })
  344. })
  345. it('Should succeed with the correct parameters', async function () {
  346. await makePutBodyRequest({
  347. url: server.url,
  348. path: path + npmPlugin + '/settings',
  349. fields: { settings },
  350. token: server.accessToken,
  351. statusCodeExpected: 204
  352. })
  353. })
  354. })
  355. describe('When installing/updating/uninstalling a plugin', function () {
  356. const path = '/api/v1/plugins/'
  357. it('Should fail with an invalid token', async function () {
  358. for (const suffix of [ 'install', 'update', 'uninstall' ]) {
  359. await makePostBodyRequest({
  360. url: server.url,
  361. path: path + suffix,
  362. fields: { npmName: npmPlugin },
  363. token: 'fake_token',
  364. statusCodeExpected: 401
  365. })
  366. }
  367. })
  368. it('Should fail if the user is not an administrator', async function () {
  369. for (const suffix of [ 'install', 'update', 'uninstall' ]) {
  370. await makePostBodyRequest({
  371. url: server.url,
  372. path: path + suffix,
  373. fields: { npmName: npmPlugin },
  374. token: userAccessToken,
  375. statusCodeExpected: 403
  376. })
  377. }
  378. })
  379. it('Should fail with an invalid npm name', async function () {
  380. for (const suffix of [ 'install', 'update', 'uninstall' ]) {
  381. await makePostBodyRequest({
  382. url: server.url,
  383. path: path + suffix,
  384. fields: { npmName: 'toto' },
  385. token: server.accessToken,
  386. statusCodeExpected: 400
  387. })
  388. }
  389. for (const suffix of [ 'install', 'update', 'uninstall' ]) {
  390. await makePostBodyRequest({
  391. url: server.url,
  392. path: path + suffix,
  393. fields: { npmName: 'peertube-plugin-TOTO' },
  394. token: server.accessToken,
  395. statusCodeExpected: 400
  396. })
  397. }
  398. })
  399. it('Should succeed with the correct parameters', async function () {
  400. const it = [
  401. { suffix: 'install', status: 200 },
  402. { suffix: 'update', status: 200 },
  403. { suffix: 'uninstall', status: 204 }
  404. ]
  405. for (const obj of it) {
  406. await makePostBodyRequest({
  407. url: server.url,
  408. path: path + obj.suffix,
  409. fields: { npmName: npmPlugin },
  410. token: server.accessToken,
  411. statusCodeExpected: obj.status
  412. })
  413. }
  414. })
  415. })
  416. after(async function () {
  417. await cleanupTests([ server ])
  418. })
  419. })