From 89cd12756035a146bbcc4db78cd3cd64f2f3d88d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 19 Jul 2019 17:30:41 +0200 Subject: Add hook filters tests --- .../fixtures/peertube-plugin-test-two/main.js | 21 ++++++++ .../fixtures/peertube-plugin-test-two/package.json | 19 +++++++ server/tests/fixtures/peertube-plugin-test/main.js | 62 +++++++++++++++------- 3 files changed, 84 insertions(+), 18 deletions(-) create mode 100644 server/tests/fixtures/peertube-plugin-test-two/main.js create mode 100644 server/tests/fixtures/peertube-plugin-test-two/package.json (limited to 'server/tests/fixtures') diff --git a/server/tests/fixtures/peertube-plugin-test-two/main.js b/server/tests/fixtures/peertube-plugin-test-two/main.js new file mode 100644 index 000000000..71c11b2ba --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-two/main.js @@ -0,0 +1,21 @@ +async function register ({ registerHook, registerSetting, settingsManager, storageManager, peertubeHelpers }) { + registerHook({ + target: 'filter:api.videos.list.params', + handler: obj => addToCount(obj) + }) +} + +async function unregister () { + return +} + +module.exports = { + register, + unregister +} + +// ############################################################################ + +function addToCount (obj) { + return Object.assign({}, obj, { count: obj.count + 1 }) +} diff --git a/server/tests/fixtures/peertube-plugin-test-two/package.json b/server/tests/fixtures/peertube-plugin-test-two/package.json new file mode 100644 index 000000000..52ebb5ac1 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-two/package.json @@ -0,0 +1,19 @@ +{ + "name": "peertube-plugin-test-two", + "version": "0.0.1", + "description": "Plugin test 2", + "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": [] +} diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js index fae0ef948..c5317ab41 100644 --- a/server/tests/fixtures/peertube-plugin-test/main.js +++ b/server/tests/fixtures/peertube-plugin-test/main.js @@ -1,23 +1,52 @@ -async function register ({ registerHook, registerSetting, settingsManager, storageManager }) { - const defaultAdmin = 'PeerTube admin' +async function register ({ registerHook, registerSetting, settingsManager, storageManager, peertubeHelpers }) { + const actionHooks = [ + 'action:application.listening', + + 'action:api.video.updated', + 'action:api.video.deleted', + 'action:api.video.uploaded', + 'action:api.video.viewed', + + 'action:api.video-thread.created', + 'action:api.video-comment-reply.created', + 'action:api.video-comment.deleted' + ] + + for (const h of actionHooks) { + registerHook({ + target: h, + handler: () => peertubeHelpers.logger.debug('Run hook %s.', h) + }) + } registerHook({ - target: 'action:application.listening', - handler: () => displayHelloWorld(settingsManager, defaultAdmin) + target: 'filter:api.videos.list.params', + handler: obj => addToCount(obj) }) - registerSetting({ - name: 'admin-name', - label: 'Admin name', - type: 'input', - default: defaultAdmin + registerHook({ + target: 'filter:api.videos.list.result', + handler: obj => ({ data: obj.data, total: obj.total + 1 }) }) - const value = await storageManager.getData('toto') - console.log(value) - console.log(value.coucou) + registerHook({ + target: 'filter:api.video.get.result', + handler: video => { + video.name += ' <3' - await storageManager.storeData('toto', { coucou: 'hello' + new Date() }) + return video + } + }) + + registerHook({ + target: 'filter:api.video.upload.accept.result', + handler: ({ accepted }, { videoBody }) => { + if (accepted !== false) return { accepted: true } + if (videoBody.name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '} + + return { accepted: true } + } + }) } async function unregister () { @@ -31,9 +60,6 @@ module.exports = { // ############################################################################ -async function displayHelloWorld (settingsManager, defaultAdmin) { - let value = await settingsManager.getSetting('admin-name') - if (!value) value = defaultAdmin - - console.log('hello world ' + value) +function addToCount (obj) { + return Object.assign({}, obj, { count: obj.count + 1 }) } -- cgit v1.2.3