From 9b474844e85cce916370693cc24f53339a695570 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 19 Jul 2019 10:37:35 +0200 Subject: Add CLI plugins tests --- server/tests/api/check-params/users.ts | 13 +- server/tests/api/server/index.ts | 1 + server/tests/api/server/plugins.ts | 130 +++++++++++++ server/tests/api/users/users.ts | 19 +- server/tests/cli/index.ts | 1 + server/tests/cli/peertube.ts | 202 ++++++++++++--------- server/tests/cli/plugins.ts | 87 +++++++++ server/tests/fixtures/peertube-plugin-test/main.js | 39 ++++ .../fixtures/peertube-plugin-test/package.json | 19 ++ server/tests/index.ts | 1 + server/tests/plugins/action-hooks.ts | 27 +++ server/tests/plugins/filter-hooks.ts | 27 +++ server/tests/plugins/index.ts | 2 + 13 files changed, 484 insertions(+), 84 deletions(-) create mode 100644 server/tests/api/server/plugins.ts create mode 100644 server/tests/cli/plugins.ts create mode 100644 server/tests/fixtures/peertube-plugin-test/main.js create mode 100644 server/tests/fixtures/peertube-plugin-test/package.json create mode 100644 server/tests/plugins/action-hooks.ts create mode 100644 server/tests/plugins/filter-hooks.ts create mode 100644 server/tests/plugins/index.ts (limited to 'server/tests') diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts index 5d62fe2b3..5b788e328 100644 --- a/server/tests/api/check-params/users.ts +++ b/server/tests/api/check-params/users.ts @@ -387,13 +387,24 @@ describe('Test users API validators', function () { } }) + it('Should fail with an invalid theme', async function () { + const fields = { theme: 'invalid' } + await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) + }) + + it('Should fail with an unknown theme', async function () { + const fields = { theme: 'peertube-theme-unknown' } + await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) + }) + it('Should succeed to change password with the correct params', async function () { const fields = { currentPassword: 'my super password', password: 'my super password', nsfwPolicy: 'blur', autoPlayVideo: false, - email: 'super_email@example.com' + email: 'super_email@example.com', + theme: 'default' } await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields, statusCodeExpected: 204 }) diff --git a/server/tests/api/server/index.ts b/server/tests/api/server/index.ts index 94c15e0d0..3daeeb49a 100644 --- a/server/tests/api/server/index.ts +++ b/server/tests/api/server/index.ts @@ -11,3 +11,4 @@ import './reverse-proxy' import './stats' import './tracker' import './no-client' +import './plugins' diff --git a/server/tests/api/server/plugins.ts b/server/tests/api/server/plugins.ts new file mode 100644 index 000000000..9a623c553 --- /dev/null +++ b/server/tests/api/server/plugins.ts @@ -0,0 +1,130 @@ +/* tslint:disable:no-unused-expression */ + +import 'mocha' +import * as chai from 'chai' +import { About } from '../../../../shared/models/server/about.model' +import { CustomConfig } from '../../../../shared/models/server/custom-config.model' +import { + cleanupTests, + deleteCustomConfig, + flushAndRunServer, + getAbout, + getConfig, + getCustomConfig, installPlugin, + killallServers, parallelTests, + registerUser, + reRunServer, ServerInfo, + setAccessTokensToServers, + updateCustomConfig, uploadVideo +} from '../../../../shared/extra-utils' +import { ServerConfig } from '../../../../shared/models' +import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model' + +const expect = chai.expect + +describe('Test plugins', function () { + let server = null + + before(async function () { + this.timeout(30000) + + server = await flushAndRunServer(1) + await setAccessTokensToServers([ server ]) + + { + await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-plugin-hello-world' }) + } + + { + await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-plugin-background-color' }) + } + }) + + it('Should list available plugins and themes', async function () { + // List without filter + // List with filter (plugin and theme) + }) + + it('Should search available plugins', async function () { + // Search with filter (plugin and theme) + // Add pagination + // Add sort + // Add peertube engine + }) + + it('Should have an empty global css', async function () { + // get /global.css + }) + + it('Should install a plugin and a theme', async function () { + + }) + + it('Should have the correct global css', async function () { + // get /global.css + }) + + it('Should have the plugin loaded in the configuration', async function () { + // Check registered themes/plugins + }) + + it('Should update the default theme in the configuration', async function () { + // Update config + }) + + it('Should list plugins and themes', async function () { + // List without filter + // List with filter (theme/plugin) + // List with pagination + // List with sort + }) + + it('Should get a plugin and a theme', async function () { + // Get plugin + // Get theme + }) + + it('Should get registered settings', async function () { + // Get plugin + }) + + it('Should update the settings', async function () { + // Update /settings + + // get /plugin + }) + + it('Should update the plugin and the theme', async function () { + // update BDD -> 0.0.1 + // update package.json (theme + plugin) + // list to check versions + // update plugin + theme + // list to check they have been updated + // check package.json are upgraded too + }) + + it('Should uninstall the plugin', async function () { + // uninstall + // list + }) + + it('Should have an empty global css', async function () { + // get /global.css + }) + + it('Should list uninstalled plugins', async function () { + // { uninstalled: true } + }) + + it('Should uninstall the theme', async function () { + // Uninstall + }) + + it('Should have updated the configuration', async function () { + // get /config (default theme + registered themes + registered plugins) + }) + + after(async function () { + await cleanupTests([ server ]) + }) +}) diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index 6fc2a070f..3a3fabb4c 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts @@ -18,7 +18,7 @@ import { getUsersList, getUsersListPaginationAndSort, getVideoChannel, - getVideosList, + getVideosList, installPlugin, login, makePutBodyRequest, rateVideo, @@ -57,6 +57,8 @@ describe('Test users', function () { server = await flushAndRunServer(1) await setAccessTokensToServers([ server ]) + + await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: 'peertube-theme-background-red' }) }) describe('OAuth client', function () { @@ -551,6 +553,21 @@ describe('Test users', function () { expect(user.account.displayName).to.equal('new display name') expect(user.account.description).to.equal('my super description updated') }) + + it('Should be able to update my theme', async function () { + for (const theme of [ 'background-red', 'default', 'instance-default' ]) { + await updateMyUser({ + url: server.url, + accessToken: accessTokenUser, + theme + }) + + const res = await getMyUserInformation(server.url, accessTokenUser) + const body: User = res.body + + expect(body.theme).to.equal(theme) + } + }) }) describe('Updating another user', function () { diff --git a/server/tests/cli/index.ts b/server/tests/cli/index.ts index c6b7ec078..5af286fe2 100644 --- a/server/tests/cli/index.ts +++ b/server/tests/cli/index.ts @@ -3,5 +3,6 @@ import './create-import-video-file-job' import './create-transcoding-job' import './optimize-old-videos' import './peertube' +import './plugins' import './reset-password' import './update-host' diff --git a/server/tests/cli/peertube.ts b/server/tests/cli/peertube.ts index d73e27564..b8c0b1f79 100644 --- a/server/tests/cli/peertube.ts +++ b/server/tests/cli/peertube.ts @@ -43,133 +43,171 @@ describe('Test CLI wrapper', function () { } }) - it('Should display no selected instance', async function () { - this.timeout(60000) + describe('Authentication and instance selection', function () { - const env = getEnvCli(server) - const stdout = await execCLI(`${env} ${cmd} --help`) + it('Should display no selected instance', async function () { + this.timeout(60000) - expect(stdout).to.contain('no instance selected') - }) + const env = getEnvCli(server) + const stdout = await execCLI(`${env} ${cmd} --help`) - it('Should add a user', async function () { - this.timeout(60000) + expect(stdout).to.contain('no instance selected') + }) - const env = getEnvCli(server) - await execCLI(`${env} ${cmd} auth add -u ${server.url} -U user_1 -p super_password`) - }) + it('Should add a user', async function () { + this.timeout(60000) - it('Should default to this user', async function () { - this.timeout(60000) + const env = getEnvCli(server) + await execCLI(`${env} ${cmd} auth add -u ${server.url} -U user_1 -p super_password`) + }) - const env = getEnvCli(server) - const stdout = await execCLI(`${env} ${cmd} --help`) + it('Should default to this user', async function () { + this.timeout(60000) - expect(stdout).to.contain(`instance ${server.url} selected`) - }) + const env = getEnvCli(server) + const stdout = await execCLI(`${env} ${cmd} --help`) - it('Should remember the user', async function () { - this.timeout(60000) + expect(stdout).to.contain(`instance ${server.url} selected`) + }) - const env = getEnvCli(server) - const stdout = await execCLI(`${env} ${cmd} auth list`) + it('Should remember the user', async function () { + this.timeout(60000) - expect(stdout).to.contain(server.url) + const env = getEnvCli(server) + const stdout = await execCLI(`${env} ${cmd} auth list`) + + expect(stdout).to.contain(server.url) + }) }) - it('Should upload a video', async function () { - this.timeout(60000) + describe('Video upload/import', function () { - const env = getEnvCli(server) + it('Should upload a video', async function () { + this.timeout(60000) - const fixture = buildAbsoluteFixturePath('60fps_720p_small.mp4') + const env = getEnvCli(server) - const params = `-f ${fixture} --video-name 'test upload' --channel-name user_channel --support 'support_text'` + const fixture = buildAbsoluteFixturePath('60fps_720p_small.mp4') - await execCLI(`${env} ${cmd} upload ${params}`) - }) + const params = `-f ${fixture} --video-name 'test upload' --channel-name user_channel --support 'support_text'` - it('Should have the video uploaded', async function () { - const res = await getVideosList(server.url) + await execCLI(`${env} ${cmd} upload ${params}`) + }) - expect(res.body.total).to.equal(1) + it('Should have the video uploaded', async function () { + const res = await getVideosList(server.url) - const videos: Video[] = res.body.data + expect(res.body.total).to.equal(1) - const video: VideoDetails = (await getVideo(server.url, videos[0].uuid)).body + const videos: Video[] = res.body.data - expect(video.name).to.equal('test upload') - expect(video.support).to.equal('support_text') - expect(video.channel.name).to.equal('user_channel') - }) + const video: VideoDetails = (await getVideo(server.url, videos[ 0 ].uuid)).body - it('Should import a video', async function () { - this.timeout(60000) + expect(video.name).to.equal('test upload') + expect(video.support).to.equal('support_text') + expect(video.channel.name).to.equal('user_channel') + }) - const env = getEnvCli(server) + it('Should import a video', async function () { + this.timeout(60000) - const params = `--target-url ${getYoutubeVideoUrl()} --channel-name user_channel` + const env = getEnvCli(server) - await execCLI(`${env} ${cmd} import ${params}`) - }) + const params = `--target-url ${getYoutubeVideoUrl()} --channel-name user_channel` - it('Should have imported the video', async function () { - this.timeout(60000) + await execCLI(`${env} ${cmd} import ${params}`) + }) - await waitJobs([ server ]) + it('Should have imported the video', async function () { + this.timeout(60000) - const res = await getVideosList(server.url) + await waitJobs([ server ]) - expect(res.body.total).to.equal(2) + const res = await getVideosList(server.url) - const videos: Video[] = res.body.data - const video = videos.find(v => v.name === 'small video - youtube') - expect(video).to.not.be.undefined + expect(res.body.total).to.equal(2) - const videoDetails: VideoDetails = (await getVideo(server.url, video.id)).body - expect(videoDetails.channel.name).to.equal('user_channel') - expect(videoDetails.support).to.equal('super support text') - expect(videoDetails.nsfw).to.be.false + const videos: Video[] = res.body.data + const video = videos.find(v => v.name === 'small video - youtube') + expect(video).to.not.be.undefined - // So we can reimport it - await removeVideo(server.url, userAccessToken, video.id) - }) + const videoDetails: VideoDetails = (await getVideo(server.url, video.id)).body + expect(videoDetails.channel.name).to.equal('user_channel') + expect(videoDetails.support).to.equal('super support text') + expect(videoDetails.nsfw).to.be.false - it('Should import and override some imported attributes', async function () { - this.timeout(60000) + // So we can reimport it + await removeVideo(server.url, userAccessToken, video.id) + }) - const env = getEnvCli(server) + it('Should import and override some imported attributes', async function () { + this.timeout(60000) - const params = `--target-url ${getYoutubeVideoUrl()} --channel-name user_channel --video-name toto --nsfw --support support` + const env = getEnvCli(server) - await execCLI(`${env} ${cmd} import ${params}`) + const params = `--target-url ${getYoutubeVideoUrl()} --channel-name user_channel --video-name toto --nsfw --support support` - await waitJobs([ server ]) + await execCLI(`${env} ${cmd} import ${params}`) - { - const res = await getVideosList(server.url) - expect(res.body.total).to.equal(2) + await waitJobs([ server ]) - const videos: Video[] = res.body.data - const video = videos.find(v => v.name === 'toto') - expect(video).to.not.be.undefined + { + const res = await getVideosList(server.url) + expect(res.body.total).to.equal(2) - const videoDetails: VideoDetails = (await getVideo(server.url, video.id)).body - expect(videoDetails.channel.name).to.equal('user_channel') - expect(videoDetails.support).to.equal('support') - expect(videoDetails.nsfw).to.be.true - expect(videoDetails.commentsEnabled).to.be.true - } + const videos: Video[] = res.body.data + const video = videos.find(v => v.name === 'toto') + expect(video).to.not.be.undefined + + const videoDetails: VideoDetails = (await getVideo(server.url, video.id)).body + expect(videoDetails.channel.name).to.equal('user_channel') + expect(videoDetails.support).to.equal('support') + expect(videoDetails.nsfw).to.be.true + expect(videoDetails.commentsEnabled).to.be.true + } + }) }) - it('Should remove the auth user', async function () { - const env = getEnvCli(server) + describe('Admin auth', function () { + + it('Should remove the auth user', async function () { + const env = getEnvCli(server) + + await execCLI(`${env} ${cmd} auth del ${server.url}`) + + const stdout = await execCLI(`${env} ${cmd} --help`) + + expect(stdout).to.contain('no instance selected') + }) + + it('Should add the admin user', async function () { + const env = getEnvCli(server) + await execCLI(`${env} ${cmd} auth add -u ${server.url} -U root -p test${server.internalServerNumber}`) + }) + }) + + describe('Manage plugins', function () { + + it('Should install a plugin', async function () { + this.timeout(60000) + + const env = getEnvCli(server) + await execCLI(`${env} ${cmd} plugins install --npm-name peertube-plugin-hello-world`) + }) + + it('Should list installed plugins', async function () { + const env = getEnvCli(server) + const res = await execCLI(`${env} ${cmd} plugins list`) - await execCLI(`${env} ${cmd} auth del ${server.url}`) + expect(res).to.contain('peertube-plugin-hello-world') + }) - const stdout = await execCLI(`${env} ${cmd} --help`) + it('Should uninstall the plugin', async function () { + const env = getEnvCli(server) + const res = await execCLI(`${env} ${cmd} plugins uninstall --npm-name peertube-plugin-hello-world`) - expect(stdout).to.contain('no instance selected') + expect(res).to.not.contain('peertube-plugin-hello-world') + }) }) after(async function () { diff --git a/server/tests/cli/plugins.ts b/server/tests/cli/plugins.ts new file mode 100644 index 000000000..d7bf8a690 --- /dev/null +++ b/server/tests/cli/plugins.ts @@ -0,0 +1,87 @@ +/* tslint:disable:no-unused-expression */ + +import 'mocha' +import { + cleanupTests, + execCLI, + flushAndRunServer, + getConfig, + getEnvCli, killallServers, + reRunServer, + root, + ServerInfo, + setAccessTokensToServers +} from '../../../shared/extra-utils' +import { join } from 'path' +import { ServerConfig } from '../../../shared/models/server' +import { expect } from 'chai' + +describe('Test plugin scripts', function () { + let server: ServerInfo + + before(async function () { + this.timeout(30000) + + server = await flushAndRunServer(1) + await setAccessTokensToServers([ server ]) + }) + + it('Should install a plugin from stateless CLI', async function () { + this.timeout(60000) + + const packagePath = join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test') + + const env = getEnvCli(server) + await execCLI(`${env} npm run plugin:install -- --plugin-path ${packagePath}`) + }) + + it('Should install a theme from stateless CLI', async function () { + this.timeout(60000) + + const env = getEnvCli(server) + await execCLI(`${env} npm run plugin:install -- --npm-name peertube-theme-background-red`) + }) + + it('Should have the theme and the plugin registered when we restart peertube', async function () { + this.timeout(30000) + + killallServers([ server ]) + await reRunServer(server) + + const res = await getConfig(server.url) + const config: ServerConfig = res.body + + const plugin = config.plugin.registered + .find(p => p.name === 'test') + expect(plugin).to.not.be.undefined + + const theme = config.theme.registered + .find(t => t.name === 'background-red') + expect(theme).to.not.be.undefined + }) + + it('Should uninstall a plugin from stateless CLI', async function () { + this.timeout(60000) + + const env = getEnvCli(server) + await execCLI(`${env} npm run plugin:uninstall -- --npm-name peertube-plugin-test`) + }) + + it('Should have removed the plugin on another peertube restart', async function () { + this.timeout(30000) + + killallServers([ server ]) + await reRunServer(server) + + const res = await getConfig(server.url) + const config: ServerConfig = res.body + + const plugin = config.plugin.registered + .find(p => p.name === 'test') + expect(plugin).to.be.undefined + }) + + after(async function () { + await cleanupTests([ server ]) + }) +}) diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js new file mode 100644 index 000000000..fae0ef948 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test/main.js @@ -0,0 +1,39 @@ +async function register ({ registerHook, registerSetting, settingsManager, storageManager }) { + const defaultAdmin = 'PeerTube admin' + + registerHook({ + target: 'action:application.listening', + handler: () => displayHelloWorld(settingsManager, defaultAdmin) + }) + + registerSetting({ + name: 'admin-name', + label: 'Admin name', + type: 'input', + default: defaultAdmin + }) + + const value = await storageManager.getData('toto') + console.log(value) + console.log(value.coucou) + + await storageManager.storeData('toto', { coucou: 'hello' + new Date() }) +} + +async function unregister () { + return +} + +module.exports = { + register, + unregister +} + +// ############################################################################ + +async function displayHelloWorld (settingsManager, defaultAdmin) { + let value = await settingsManager.getSetting('admin-name') + if (!value) value = defaultAdmin + + console.log('hello world ' + value) +} diff --git a/server/tests/fixtures/peertube-plugin-test/package.json b/server/tests/fixtures/peertube-plugin-test/package.json new file mode 100644 index 000000000..9d6fe5c90 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test/package.json @@ -0,0 +1,19 @@ +{ + "name": "peertube-plugin-test", + "version": "0.0.1", + "description": "Plugin test", + "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/index.ts b/server/tests/index.ts index ed16d65dd..8bddcfc7c 100644 --- a/server/tests/index.ts +++ b/server/tests/index.ts @@ -3,3 +3,4 @@ import './client' import './feeds/' import './cli/' import './api/' +import './plugins/' diff --git a/server/tests/plugins/action-hooks.ts b/server/tests/plugins/action-hooks.ts new file mode 100644 index 000000000..8abab98c2 --- /dev/null +++ b/server/tests/plugins/action-hooks.ts @@ -0,0 +1,27 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers' +import { setAccessTokensToServers } from '../../../shared/extra-utils' + +const expect = chai.expect + +describe('Test plugin filter hooks', function () { + let server: ServerInfo + + before(async function () { + this.timeout(30000) + server = await flushAndRunServer(1) + + await setAccessTokensToServers([ server ]) + }) + + it('Should execute ', async function () { + + }) + + after(async function () { + await cleanupTests([ server ]) + }) +}) diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts new file mode 100644 index 000000000..8abab98c2 --- /dev/null +++ b/server/tests/plugins/filter-hooks.ts @@ -0,0 +1,27 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers' +import { setAccessTokensToServers } from '../../../shared/extra-utils' + +const expect = chai.expect + +describe('Test plugin filter hooks', function () { + let server: ServerInfo + + before(async function () { + this.timeout(30000) + server = await flushAndRunServer(1) + + await setAccessTokensToServers([ server ]) + }) + + it('Should execute ', async function () { + + }) + + after(async function () { + await cleanupTests([ server ]) + }) +}) diff --git a/server/tests/plugins/index.ts b/server/tests/plugins/index.ts new file mode 100644 index 000000000..b640ecc9e --- /dev/null +++ b/server/tests/plugins/index.ts @@ -0,0 +1,2 @@ +export * from './action-hooks' +export * from './filter-hooks' -- cgit v1.2.3