From 5e2b2e2775421cd98286d6e2f75cf38aae7a212c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 10 Apr 2020 15:07:54 +0200 Subject: Add ability for plugins to add custom routes --- .../fixtures/peertube-plugin-test-five/main.js | 21 +++++ .../peertube-plugin-test-five/package.json | 20 +++++ server/tests/plugins/index.ts | 1 + server/tests/plugins/plugin-router.ts | 91 ++++++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 server/tests/fixtures/peertube-plugin-test-five/main.js create mode 100644 server/tests/fixtures/peertube-plugin-test-five/package.json create mode 100644 server/tests/plugins/plugin-router.ts (limited to 'server/tests') diff --git a/server/tests/fixtures/peertube-plugin-test-five/main.js b/server/tests/fixtures/peertube-plugin-test-five/main.js new file mode 100644 index 000000000..c1435b928 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-five/main.js @@ -0,0 +1,21 @@ +async function register ({ + getRouter +}) { + const router = getRouter() + router.get('/ping', (req, res) => res.json({ message: 'pong' })) + + router.post('/form/post/mirror', (req, res) => { + res.json(req.body) + }) +} + +async function unregister () { + return +} + +module.exports = { + register, + unregister +} + +// ########################################################################### diff --git a/server/tests/fixtures/peertube-plugin-test-five/package.json b/server/tests/fixtures/peertube-plugin-test-five/package.json new file mode 100644 index 000000000..1f5d65d9d --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-five/package.json @@ -0,0 +1,20 @@ +{ + "name": "peertube-plugin-test-five", + "version": "0.0.1", + "description": "Plugin test 5", + "engine": { + "peertube": ">=1.3.0" + }, + "keywords": [ + "peertube", + "plugin" + ], + "homepage": "https://github.com/Chocobozzz/PeerTube", + "author": "Chocobozzz", + "bugs": "https://github.com/Chocobozzz/PeerTube/issues", + "library": "./main.js", + "staticDirs": {}, + "css": [], + "clientScripts": [], + "translations": {} +} diff --git a/server/tests/plugins/index.ts b/server/tests/plugins/index.ts index 9c9499a79..1414e7e58 100644 --- a/server/tests/plugins/index.ts +++ b/server/tests/plugins/index.ts @@ -3,3 +3,4 @@ import './filter-hooks' import './translations' import './video-constants' import './plugin-helpers' +import './plugin-router' diff --git a/server/tests/plugins/plugin-router.ts b/server/tests/plugins/plugin-router.ts new file mode 100644 index 000000000..cf4130f4b --- /dev/null +++ b/server/tests/plugins/plugin-router.ts @@ -0,0 +1,91 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import 'mocha' +import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers' +import { + getPluginTestPath, + installPlugin, + makeGetRequest, + makePostBodyRequest, + setAccessTokensToServers, uninstallPlugin +} from '../../../shared/extra-utils' +import { expect } from 'chai' + +describe('Test plugin helpers', function () { + let server: ServerInfo + const basePaths = [ + '/plugins/test-five/router/', + '/plugins/test-five/0.0.1/router/' + ] + + before(async function () { + this.timeout(30000) + + server = await flushAndRunServer(1) + await setAccessTokensToServers([ server ]) + + await installPlugin({ + url: server.url, + accessToken: server.accessToken, + path: getPluginTestPath('-five') + }) + }) + + it('Should answer "pong"', async function () { + for (const path of basePaths) { + const res = await makeGetRequest({ + url: server.url, + path: path + 'ping', + statusCodeExpected: 200 + }) + + expect(res.body.message).to.equal('pong') + } + }) + + it('Should mirror post body', async function () { + const body = { + hello: 'world', + riri: 'fifi', + loulou: 'picsou' + } + + for (const path of basePaths) { + const res = await makePostBodyRequest({ + url: server.url, + path: path + 'form/post/mirror', + fields: body, + statusCodeExpected: 200 + }) + + expect(res.body).to.deep.equal(body) + } + }) + + it('Should remove the plugin and remove the routes', async function () { + await uninstallPlugin({ + url: server.url, + accessToken: server.accessToken, + npmName: 'peertube-plugin-test-five' + }) + + for (const path of basePaths) { + await makeGetRequest({ + url: server.url, + path: path + 'ping', + statusCodeExpected: 404 + }) + + await makePostBodyRequest({ + url: server.url, + path: path + 'ping', + fields: {}, + statusCodeExpected: 404 + }) + } + }) + + after(async function () { + await cleanupTests([ server ]) + }) +}) -- cgit v1.2.3