Skip to content

Commit

Permalink
Add plugin API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Jul 24, 2019
1 parent 9b47484 commit 0907120
Show file tree
Hide file tree
Showing 19 changed files with 481 additions and 79 deletions.
1 change: 1 addition & 0 deletions config/test-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ storage:
torrents: 'test1/torrents/'
captions: 'test1/captions/'
cache: 'test1/cache/'
plugins: 'test1/plugins/'

admin:
email: '[email protected]'
Expand Down
1 change: 1 addition & 0 deletions config/test-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ storage:
torrents: 'test2/torrents/'
captions: 'test2/captions/'
cache: 'test2/cache/'
plugins: 'test2/plugins/'

admin:
email: '[email protected]'
Expand Down
1 change: 1 addition & 0 deletions config/test-3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ storage:
torrents: 'test3/torrents/'
captions: 'test3/captions/'
cache: 'test3/cache/'
plugins: 'test3/plugins/'

admin:
email: '[email protected]'
Expand Down
1 change: 1 addition & 0 deletions config/test-4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ storage:
torrents: 'test4/torrents/'
captions: 'test4/captions/'
cache: 'test4/cache/'
plugins: 'test4/plugins/'

admin:
email: '[email protected]'
Expand Down
1 change: 1 addition & 0 deletions config/test-5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ storage:
torrents: 'test5/torrents/'
captions: 'test5/captions/'
cache: 'test5/cache/'
plugins: 'test5/plugins/'

admin:
email: '[email protected]'
Expand Down
1 change: 1 addition & 0 deletions config/test-6.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ storage:
torrents: 'test6/torrents/'
captions: 'test6/captions/'
cache: 'test6/cache/'
plugins: 'test6/plugins/'

admin:
email: '[email protected]'
Expand Down
9 changes: 6 additions & 3 deletions server/controllers/api/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model
import { logger } from '../../helpers/logger'
import { listAvailablePluginsFromIndex } from '../../lib/plugins/plugin-index'
import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model'
import { RegisteredSettings } from '../../../shared/models/plugins/register-setting.model'

const pluginRouter = express.Router()

Expand Down Expand Up @@ -103,9 +104,11 @@ export {

async function listPlugins (req: express.Request, res: express.Response) {
const pluginType = req.query.pluginType
const uninstalled = req.query.uninstalled

const resultList = await PluginModel.listForApi({
pluginType,
uninstalled,
start: req.query.start,
count: req.query.count,
sort: req.query.sort
Expand Down Expand Up @@ -161,9 +164,9 @@ async function uninstallPlugin (req: express.Request, res: express.Response) {
function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)

return res.json({
settings
})
const json: RegisteredSettings = { settings }

return res.json(json)
}

async function updatePluginSettings (req: express.Request, res: express.Response) {
Expand Down
2 changes: 1 addition & 1 deletion server/lib/plugins/plugin-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList)
sort,
pluginType,
search,
currentPeerTubeEngine: PEERTUBE_VERSION
currentPeerTubeEngine: options.currentPeerTubeEngine || PEERTUBE_VERSION
}

const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins'
Expand Down
13 changes: 8 additions & 5 deletions server/lib/plugins/plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createReadStream, createWriteStream } from 'fs'
import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants'
import { PluginType } from '../../../shared/models/plugins/plugin.type'
import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn'
import { outputFile } from 'fs-extra'
import { outputFile, readJSON } from 'fs-extra'
import { RegisterSettingOptions } from '../../../shared/models/plugins/register-setting.model'
import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model'
import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model'
Expand Down Expand Up @@ -174,7 +174,7 @@ export class PluginManager implements ServerHook {
const pluginType = PluginModel.getTypeFromNpmName(npmName)
const pluginName = PluginModel.normalizePluginName(npmName)

const packageJSON = this.getPackageJSON(pluginName, pluginType)
const packageJSON = await this.getPackageJSON(pluginName, pluginType)
if (!isPackageJSONValid(packageJSON, pluginType)) {
throw new Error('PackageJSON is invalid.')
}
Expand Down Expand Up @@ -251,7 +251,7 @@ export class PluginManager implements ServerHook {

logger.info('Registering plugin or theme %s.', npmName)

const packageJSON = this.getPackageJSON(plugin.name, plugin.type)
const packageJSON = await this.getPackageJSON(plugin.name, plugin.type)
const pluginPath = this.getPluginPath(plugin.name, plugin.type)

if (!isPackageJSONValid(packageJSON, plugin.type)) {
Expand Down Expand Up @@ -286,7 +286,10 @@ export class PluginManager implements ServerHook {
private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) {
const npmName = PluginModel.buildNpmName(plugin.name, plugin.type)

const library: PluginLibrary = require(join(pluginPath, packageJSON.library))
// Delete cache if needed
const modulePath = join(pluginPath, packageJSON.library)
delete require.cache[modulePath]
const library: PluginLibrary = require(modulePath)

if (!isLibraryCodeValid(library)) {
throw new Error('Library code is not valid (miss register or unregister function)')
Expand Down Expand Up @@ -350,7 +353,7 @@ export class PluginManager implements ServerHook {
private getPackageJSON (pluginName: string, pluginType: PluginType) {
const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json')

return require(pluginPath) as PluginPackageJson
return readJSON(pluginPath) as Promise<PluginPackageJson>
}

private getPluginPath (pluginName: string, pluginType: PluginType) {
Expand Down
6 changes: 4 additions & 2 deletions server/lib/plugins/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ async function installNpmPlugin (npmName: string, version?: string) {
let toInstall = npmName
if (version) toInstall += `@${version}`

await execYarn('add ' + toInstall)
const { stdout } = await execYarn('add ' + toInstall)

logger.debug('Added a yarn package.', { yarnStdout: stdout })
}

async function installNpmPluginFromDisk (path: string) {
Expand Down Expand Up @@ -46,7 +48,7 @@ async function execYarn (command: string) {
await outputJSON(pluginPackageJSON, {})
}

await execShell(`yarn ${command}`, { cwd: pluginDirectory })
return execShell(`yarn ${command}`, { cwd: pluginDirectory })
} catch (result) {
logger.error('Cannot exec yarn.', { command, err: result.err, stderr: result.stderr })

Expand Down
3 changes: 3 additions & 0 deletions server/middlewares/validators/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ const listAvailablePluginsValidator = [
query('pluginType')
.optional()
.custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
query('currentPeerTubeEngine')
.optional()
.custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),

(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
Expand Down
14 changes: 13 additions & 1 deletion server/tests/api/check-params/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ describe('Test server plugins API validators', function () {
const path = '/api/v1/plugins/available'
const baseQuery = {
search: 'super search',
pluginType: PluginType.PLUGIN
pluginType: PluginType.PLUGIN,
currentPeerTubeEngine: '1.2.3'
}

it('Should fail with an invalid token', async function () {
Expand Down Expand Up @@ -198,6 +199,17 @@ describe('Test server plugins API validators', function () {
})
})

it('Should fail with an invalid current peertube engine', async function () {
const query = immutableAssign(baseQuery, { currentPeerTubeEngine: '1.0' })

await makeGetRequest({
url: server.url,
path,
token: server.accessToken,
query
})
})

it('Should success with the correct parameters', async function () {
await makeGetRequest({
url: server.url,
Expand Down
Loading

0 comments on commit 0907120

Please sign in to comment.