diff --git a/__tests__/unit/services/plugin-manager/plugin-sandbox.spec.js b/__tests__/unit/services/plugin-manager/plugin-sandbox.spec.js new file mode 100644 index 0000000000..48777da2cb --- /dev/null +++ b/__tests__/unit/services/plugin-manager/plugin-sandbox.spec.js @@ -0,0 +1,63 @@ +import path from 'path' +import { createLocalVue } from '@vue/test-utils' +import { PluginSandbox } from '@/services/plugin-manager/plugin-sandbox' +import { Plugin } from '@/services/plugin-manager/plugin' + +const rootPath = path.resolve(__dirname, '../../../') +const app = createLocalVue() +const plugin = new Plugin({ + rootPath, + fullPath: path.resolve(__dirname), + config: { + permissions: [] + } +}) + +beforeEach(() => { + plugin.config.permissions = [] +}) + +describe('Plugin Sandbox', () => { + it('should parse file with api', async () => { + const sandbox = new PluginSandbox({ + app, + plugin + }) + + await sandbox.install() + + const pluginVM = sandbox.getComponentVM() + expect(pluginVM.run(` + module.exports = walletApi + `)).toBeDefined() + }) + + it('should parse file without api', async () => { + const sandbox = new PluginSandbox({ + app, + plugin + }) + + await sandbox.install() + + const pluginVM = sandbox.getPluginVM() + expect(() => pluginVM.run(` + module.exports = walletApi + `)).toThrowError('walletApi is not defined') + }) + + it('should read permissions', async () => { + plugin.config.permissions = ['ALERTS'] + const sandbox = new PluginSandbox({ + app, + plugin + }) + + await sandbox.install() + + const componentVM = sandbox.getComponentVM() + expect(() => componentVM.run(` + module.exports = walletApi.alert + `)).toBeDefined() + }) +}) diff --git a/__tests__/unit/services/plugin-manager/setup/components-setup.spec.js b/__tests__/unit/services/plugin-manager/setup/components-setup.spec.js index 943666f875..b8f443c1c2 100644 --- a/__tests__/unit/services/plugin-manager/setup/components-setup.spec.js +++ b/__tests__/unit/services/plugin-manager/setup/components-setup.spec.js @@ -31,7 +31,10 @@ const pluginObject = { } const sandbox = { - getVM: jest.fn(() => ({ + getComponentVM: jest.fn(() => ({ + run: jest.fn() + })), + getPluginVM: jest.fn(() => ({ run: jest.fn() })), app: {} diff --git a/src/renderer/services/plugin-manager/plugin-sandbox.js b/src/renderer/services/plugin-manager/plugin-sandbox.js index 32fd32f007..a2ab78ae66 100644 --- a/src/renderer/services/plugin-manager/plugin-sandbox.js +++ b/src/renderer/services/plugin-manager/plugin-sandbox.js @@ -40,7 +40,6 @@ export class PluginSandbox { this.sandbox = {} this.walletApi = {} - this.sandboxes = this.__mapPermissionsToSandbox() } @@ -57,32 +56,38 @@ export class PluginSandbox { } } - getVM ({ loadApi }) { - const fullPath = this.plugin.fullPath - + getComponentVM () { return new NodeVM({ - sandbox: this.getSandbox(loadApi), + sandbox: this.getSandbox(true), require: { builtin: [], context: 'sandbox', - resolve: function (source) { - return path.resolve(fullPath, source) - }, external: { - modules: [ - path.resolve(fullPath), - 'vue/dist/vue.common.js' - ], + modules: [], transitive: true }, root: [ - this.plugin.rootPath, - path.resolve(fullPath) + path.resolve(this.plugin.fullPath) ] } }) } + getPluginVM () { + const { rootPath } = this.plugin + + return new NodeVM({ + sandbox: this.getSandbox(false), + require: { + context: 'sandbox', + resolve: function (source) { + return path.resolve(rootPath, 'node_modules', source) + }, + external: ['vue'] + } + }) + } + async install () { await this.__run(this.sandboxes[PUBLIC.name]) diff --git a/src/renderer/services/plugin-manager/plugin-setup.js b/src/renderer/services/plugin-manager/plugin-setup.js index c28588a2cc..f8c459e264 100644 --- a/src/renderer/services/plugin-manager/plugin-setup.js +++ b/src/renderer/services/plugin-manager/plugin-setup.js @@ -38,7 +38,7 @@ export class PluginSetup { localVue.options._base = localVue this.vue = localVue - this.pluginObject = this.sandbox.getVM(false).run( + this.pluginObject = this.sandbox.getPluginVM().run( fs.readFileSync(path.join(plugin.fullPath, 'src/index.js')), path.join(plugin.fullPath, 'src/index.js') ) diff --git a/src/renderer/services/plugin-manager/setup/components-setup.js b/src/renderer/services/plugin-manager/setup/components-setup.js index 16997e2e9a..ebced74ab2 100644 --- a/src/renderer/services/plugin-manager/setup/components-setup.js +++ b/src/renderer/services/plugin-manager/setup/components-setup.js @@ -23,8 +23,8 @@ export function createComponentsSetup (plugin, pluginObject, sandbox, vue) { vue, fullPath, name: componentName, - pluginVM: sandbox.getVM({ loadApi: false }), - componentVM: sandbox.getVM({ loadApi: true }), + pluginVM: sandbox.getPluginVM(), + componentVM: sandbox.getComponentVM(), logger: sandbox.app.$logger })