From 2403b96011099c87b74141adae249b0f9303fd9f Mon Sep 17 00:00:00 2001 From: Matteo Figus Date: Wed, 5 Jul 2017 16:48:25 +0100 Subject: [PATCH] Remove plugin detective logic --- package.json | 1 - src/cli/facade/dev.js | 21 +- src/registry/domain/plugins-detective.js | 14 - src/registry/routes/helpers/get-component.js | 94 +-- src/resources/index.js | 3 - .../unit/registry-domain-plugins-detective.js | 42 -- test/unit/registry-routes-component.js | 562 ++++++++++-------- 7 files changed, 364 insertions(+), 373 deletions(-) delete mode 100644 src/registry/domain/plugins-detective.js delete mode 100644 test/unit/registry-domain-plugins-detective.js diff --git a/package.json b/package.json index 98b3c3a0f..f92932059 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,6 @@ "clean-css": "4.1.3", "colors": "1.1.2", "dependency-graph": "0.5.0", - "detective": "4.5.0", "errorhandler": "^1.5.0", "express": "4.15.3", "form-data": "2.1.4", diff --git a/src/cli/facade/dev.js b/src/cli/facade/dev.js index 2edfc893e..689f99900 100644 --- a/src/cli/facade/dev.js +++ b/src/cli/facade/dev.js @@ -93,10 +93,10 @@ module.exports = function(dependencies) { }, error => { if (error) { - const errorDescription = error instanceof SyntaxError || - !!error.message - ? error.message - : error; + const errorDescription = + error instanceof SyntaxError || !!error.message + ? error.message + : error; logger.err( format( strings.errors.cli.PACKAGING_FAIL, @@ -136,12 +136,10 @@ module.exports = function(dependencies) { }); }; - const registerPlugins = function(registry) { + const registerPlugins = registry => { const mockedPlugins = getMockedPlugins(logger, componentsDir); - mockedPlugins.forEach(p => { - registry.register(p); - }); + mockedPlugins.forEach(registry.register); registry.on('request', data => { if (data.errorCode === 'PLUGIN_MISSING_FROM_REGISTRY') { @@ -152,13 +150,6 @@ module.exports = function(dependencies) { colors.blue(strings.commands.cli.MOCK_PLUGIN) ) ); - } else if (data.errorCode === 'PLUGIN_MISSING_FROM_COMPONENT') { - logger.err( - format( - strings.errors.cli.PLUGIN_MISSING_FROM_COMPONENT, - data.errorDetails - ) - ); } }); }; diff --git a/src/registry/domain/plugins-detective.js b/src/registry/domain/plugins-detective.js deleted file mode 100644 index ac18439c3..000000000 --- a/src/registry/domain/plugins-detective.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -module.exports.parse = code => { - const names = []; - const parts = code.split('plugins'); - for (let index = 1; index < parts.length; index++) { - const part = parts[index]; - const parenthesis = part.indexOf('('); - if (part.indexOf('&&') === -1) { - names.push(part.substring(1, parenthesis)); - } - } - return names; -}; diff --git a/src/registry/routes/helpers/get-component.js b/src/registry/routes/helpers/get-component.js index 720ebb62d..ec645aaa0 100644 --- a/src/registry/routes/helpers/get-component.js +++ b/src/registry/routes/helpers/get-component.js @@ -9,7 +9,6 @@ const _ = require('lodash'); const applyDefaultValues = require('./apply-default-values'); const Client = require('../../../../client'); -const detective = require('../../domain/plugins-detective'); const eventsHandler = require('../../domain/events-handler'); const GetComponentRetrievingInfo = require('./get-component-retrieving-info'); const getComponentFallback = require('./get-component-fallback'); @@ -34,13 +33,13 @@ module.exports = function(conf, repository) { retrievingInfo = new GetComponentRetrievingInfo(options); let responseHeaders = {}; - const getLanguage = function() { + const getLanguage = () => { const paramOverride = !!options.parameters && options.parameters['__ocAcceptLanguage']; return paramOverride || options.headers['accept-language']; }; - const callback = function(result) { + const callback = result => { if (result.response.error) { retrievingInfo.extend(result.response); } @@ -55,13 +54,13 @@ module.exports = function(conf, repository) { }; let componentCallbackDone = false; - const conf = options.conf, - acceptLanguage = getLanguage(), - requestedComponent = { - name: options.name, - version: options.version || '', - parameters: options.parameters - }; + const conf = options.conf; + const acceptLanguage = getLanguage(); + const requestedComponent = { + name: options.name, + version: options.version || '', + parameters: options.parameters + }; repository.getComponent( requestedComponent.name, @@ -122,9 +121,9 @@ module.exports = function(conf, repository) { // sanitise and check params const appliedParams = applyDefaultValues( - requestedComponent.parameters, - component.oc.parameters - ), + requestedComponent.parameters, + component.oc.parameters + ), params = sanitiser.sanitiseComponentParameters( appliedParams, component.oc.parameters @@ -144,11 +143,11 @@ module.exports = function(conf, repository) { }); } - const filterCustomHeaders = function( + const filterCustomHeaders = ( headers, requestedVersion, actualVersion - ) { + ) => { const needFiltering = !_.isEmpty(headers) && !_.isEmpty(conf.customHeadersToSkipOnWeakVersion) && @@ -159,7 +158,7 @@ module.exports = function(conf, repository) { : headers; }; - const returnComponent = function(err, data) { + const returnComponent = (err, data) => { if (componentCallbackDone) { return; } @@ -196,7 +195,8 @@ module.exports = function(conf, repository) { ); const isUnrendered = - options.headers.accept === settings.registry.acceptUnrenderedHeader, + options.headers.accept === + settings.registry.acceptUnrenderedHeader, renderMode = isUnrendered ? 'unrendered' : 'rendered'; const response = { @@ -241,16 +241,12 @@ module.exports = function(conf, repository) { }) }); } else { - const cacheKey = format( - '{0}/{1}/template.js', - component.name, - component.version - ), + const cacheKey = `${component.name}/${component.version}/template.js`, cached = cache.get('file-contents', cacheKey), key = component.oc.files.template.hashKey, renderOptions = { href: componentHref, - key: key, + key, version: component.version, name: component.name, templateType: component.oc.files.template.type, @@ -258,7 +254,7 @@ module.exports = function(conf, repository) { renderInfo: component.oc.renderInfo }; - const returnResult = function(template) { + const returnResult = template => { client.renderTemplate( template, data, @@ -277,7 +273,7 @@ module.exports = function(conf, repository) { callback({ status: 200, headers: responseHeaders, - response: _.extend(response, { html: html }) + response: _.extend(response, { html }) }); } ); @@ -320,18 +316,14 @@ module.exports = function(conf, repository) { if (!component.oc.files.dataProvider) { returnComponent(null, {}); } else { - const cacheKey = format( - '{0}/{1}/server.js', - component.name, - component.version - ), + const cacheKey = `${component.name}/${component.version}/server.js`, cached = cache.get('file-contents', cacheKey), domain = Domain.create(), contextObj = { acceptLanguage: acceptLanguageParser.parse(acceptLanguage), baseUrl: conf.baseUrl, env: conf.env, - params: params, + params, plugins: conf.plugins, renderComponent: nestedRenderer.renderComponent, renderComponents: nestedRenderer.renderComponents, @@ -339,7 +331,7 @@ module.exports = function(conf, repository) { staticPath: repository .getStaticFilePath(component.name, component.version, '') .replace('https:', ''), - setHeader: function(header, value) { + setHeader: (header, value) => { if ( !(typeof header === 'string' && typeof value === 'string') ) { @@ -355,15 +347,11 @@ module.exports = function(conf, repository) { templates: repository.getTemplates() }; - const setCallbackTimeout = function() { + const setCallbackTimeout = () => { if (conf.executionTimeout) { setTimeout(() => { - returnComponent({ - message: format( - 'timeout ({0}ms)', - conf.executionTimeout * 1000 - ) - }); + const message = `timeout (${conf.executionTimeout * 1000}ms)`; + returnComponent({ message }); domain.exit(); }, conf.executionTimeout * 1000); } @@ -401,11 +389,11 @@ module.exports = function(conf, repository) { require: RequireWrapper(conf.dependencies), module: { exports: {} }, console: conf.local ? console : { log: _.noop }, - setTimeout: setTimeout, - Buffer: Buffer + setTimeout, + Buffer }; - const handleError = function(err) { + const handleError = err => { if (err.code === 'DEPENDENCY_MISSING_FROM_REGISTRY') { componentCallbackDone = true; @@ -422,28 +410,6 @@ module.exports = function(conf, repository) { }); } - const usedPlugins = detective.parse(dataProcessorJs), - unRegisteredPlugins = _.difference( - usedPlugins, - _.keys(conf.plugins) - ); - - if (!_.isEmpty(unRegisteredPlugins)) { - componentCallbackDone = true; - - return callback({ - status: 501, - response: { - code: 'PLUGIN_MISSING_FROM_COMPONENT', - error: format( - strings.errors.registry.PLUGIN_NOT_FOUND, - unRegisteredPlugins.join(' ,') - ), - missingPlugins: unRegisteredPlugins - } - }); - } - returnComponent(err); }; diff --git a/src/resources/index.js b/src/resources/index.js index ce9a2a119..597084c1e 100644 --- a/src/resources/index.js +++ b/src/resources/index.js @@ -108,9 +108,6 @@ module.exports = { PACKAGING_FAIL: 'an error happened while packaging {0}: {1}', PLUGIN_MISSING_FROM_REGISTRY: 'Looks like you are trying to use a plugin in the dev mode ({0}).\nYou need to mock it doing {1}', - PLUGIN_MISSING_FROM_COMPONENT: - "Looks like you are trying to use a plugin you haven't registered ({0})." + - "\nYou need to register it editing your component's package.json", PORT_IS_BUSY: 'The port {0} is already in use. Specify the optional port parameter to use another port.', PUBLISHING_FAIL: 'An error happened when publishing the component: {0}', diff --git a/test/unit/registry-domain-plugins-detective.js b/test/unit/registry-domain-plugins-detective.js deleted file mode 100644 index f4bbb6f37..000000000 --- a/test/unit/registry-domain-plugins-detective.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict'; - -const expect = require('chai').expect; -const code = require('../fixtures/mocked-components/package-server-with-plugin'); - -describe('registry : domain : plugins-detective', () => { - - const detective = require('../../src/registry/domain/plugins-detective'); - - describe('when detecting plugins usage', () => { - - describe('when plugins not detected', () => { - const code = 'module.exports.data=function(context, callback){ callback(null, {}); };'; - - it('should return blank array', () => { - expect(detective.parse(code)).to.eql([]); - }); - }); - - describe('when plugin detected', () => { - const code = 'module.exports.data=function(context, callback){ callback(null, { bla: context.plugins.bla(\'args\',123)}); };'; - - it('should return plugin names', () => { - expect(detective.parse(code)).to.eql(['bla']); - }); - }); - - describe('when minified plugins call present', () => { - const code = 'var hello="something";module.exports.data=function(a,b){b(null,{bla:a.plugins.bla(\'args\',123)}); };'; - - it('should detect it', () => { - expect(detective.parse(code)).to.eql(['bla']); - }); - }); - - describe('given a component that uses the `getTimezoneOffset` plugin', () => { - it('should detect it', () => { - expect(detective.parse(code)).to.eql(['getTimezoneOffset']); - }); - }); - }); -}); diff --git a/test/unit/registry-routes-component.js b/test/unit/registry-routes-component.js index 84e2975a8..2c414461c 100644 --- a/test/unit/registry-routes-component.js +++ b/test/unit/registry-routes-component.js @@ -5,12 +5,11 @@ const sinon = require('sinon'); const _ = require('lodash'); describe('registry : routes : component', () => { - const ComponentRoute = require('../../src/registry/routes/component'), mockedComponents = require('../fixtures/mocked-components'); let mockedRepository, resJsonStub, resSetStub, statusStub, componentRoute; - const initialise = function(params){ + const initialise = function(params) { resJsonStub = sinon.stub(); resSetStub = sinon.stub(); statusStub = sinon.stub().returns({ json: resJsonStub }); @@ -24,32 +23,37 @@ describe('registry : routes : component', () => { }; describe('when getting a component with server.js execution timeout', () => { - let code, response; - before((done) => { + before(done => { initialise(mockedComponents['timeout-component']); componentRoute = new ComponentRoute({}, mockedRepository); - const resStatus = function(calledCode){ + const resStatus = function(calledCode) { code = calledCode; return { - json: function(calledResponse){ + json: function(calledResponse) { response = calledResponse; done(); } }; }; - componentRoute({ - headers: {}, - params: { componentName: 'timeout-component', componentVersion: '1.X.X' } - }, { - conf: { - baseUrl: 'http://component.com/', - executionTimeout: 0.1 + componentRoute( + { + headers: {}, + params: { + componentName: 'timeout-component', + componentVersion: '1.X.X' + } }, - status: resStatus - }); + { + conf: { + baseUrl: 'http://component.com/', + executionTimeout: 0.1 + }, + status: resStatus + } + ); }); it('should return 500 status code', () => { @@ -57,30 +61,34 @@ describe('registry : routes : component', () => { }); it('should respond with error message', () => { - expect(response.error).to.equal('Component execution error: timeout (100ms)'); + expect(response.error).to.equal( + 'Component execution error: timeout (100ms)' + ); }); - it('should return component\'s name and request version', () => { + it("should return component's name and request version", () => { expect(response.name).to.equal('timeout-component'); expect(response.requestVersion).to.equal('1.X.X'); }); }); describe('when getting a component with a server.js that returns undefined data', () => { - before(() => { initialise(mockedComponents['undefined-component']); componentRoute = new ComponentRoute({}, mockedRepository); - componentRoute({ - headers: {}, - params: { componentName: 'undefined-component' } - }, { - conf: { - baseUrl: 'http://components.com/' + componentRoute( + { + headers: {}, + params: { componentName: 'undefined-component' } }, - status: statusStub - }); + { + conf: { + baseUrl: 'http://components.com/' + }, + status: statusStub + } + ); }); it('should return 500 status code', () => { @@ -88,33 +96,39 @@ describe('registry : routes : component', () => { }); it('should respond with error message for undefined data', () => { - expect(resJsonStub.args[0][0].error).to.equal('Component execution error: data object is undefined'); + expect(resJsonStub.args[0][0].error).to.equal( + 'Component execution error: data object is undefined' + ); }); - it('should return component\'s name and request version', () => { + it("should return component's name and request version", () => { expect(resJsonStub.args[0][0].name).to.equal('undefined-component'); expect(resJsonStub.args[0][0].requestVersion).to.equal(''); }); }); describe('when getting a component with server.js execution errors', () => { - before(() => { initialise(mockedComponents['error-component']); componentRoute = new ComponentRoute({}, mockedRepository); - componentRoute({ - headers: {}, - params: { componentName: 'error-component' } - }, { - conf: { - baseUrl: 'http://components.com/', - plugins: { - a: function(){ return ''; } - } + componentRoute( + { + headers: {}, + params: { componentName: 'error-component' } }, - status: statusStub - }); + { + conf: { + baseUrl: 'http://components.com/', + plugins: { + a: function() { + return ''; + } + } + }, + status: statusStub + } + ); }); it('should return 500 status code', () => { @@ -122,39 +136,42 @@ describe('registry : routes : component', () => { }); it('should respond with error message including missing plugin', () => { - expect(resJsonStub.args[0][0].error).to.equal('Component execution error: c is not defined'); + expect(resJsonStub.args[0][0].error).to.equal( + 'Component execution error: c is not defined' + ); }); - it('should return component\'s name and request version', () => { + it("should return component's name and request version", () => { expect(resJsonStub.args[0][0].name).to.equal('error-component'); expect(resJsonStub.args[0][0].requestVersion).to.equal(''); }); }); describe('when getting a component with server.js asynchronous execution errors', () => { - describe('when has error that gets fired on first execution', () => { - - before((done) => { + before(done => { initialise(mockedComponents['async-error-component']); componentRoute = new ComponentRoute({}, mockedRepository); statusStub.returns({ - json: function(response){ + json: function(response) { resJsonStub(response); done(); } }); - componentRoute({ - headers: {}, - params: { componentName: 'async-error-component' } - }, { - conf: { - baseUrl: 'http://components.com/', - plugins: {} + componentRoute( + { + headers: {}, + params: { componentName: 'async-error-component' } }, - status: statusStub - }); + { + conf: { + baseUrl: 'http://components.com/', + plugins: {} + }, + status: statusStub + } + ); }); it('should return 500 status code', () => { @@ -162,51 +179,58 @@ describe('registry : routes : component', () => { }); it('should respond with error message for component execution error', () => { - expect(resJsonStub.args[0][0].error).to.equal('Component execution error: thisDoesnotExist is not defined'); + expect(resJsonStub.args[0][0].error).to.equal( + 'Component execution error: thisDoesnotExist is not defined' + ); }); - it('should return component\'s name and request version', () => { + it("should return component's name and request version", () => { expect(resJsonStub.args[0][0].name).to.equal('async-error-component'); expect(resJsonStub.args[0][0].requestVersion).to.equal(''); }); }); describe('when has error that gets fired on following executions', () => { - - before((done) => { + before(done => { initialise(mockedComponents['async-error2-component']); componentRoute = new ComponentRoute({}, mockedRepository); statusStub.returns({ - json: function(response){ + json: function(response) { resJsonStub(response); - if(statusStub.args.length>=2){ + if (statusStub.args.length >= 2) { done(); } } }); - componentRoute({ - headers: {}, - params: { componentName: 'async-error2-component' } - }, { - conf: { - baseUrl: 'http://components.com/', - plugins: {} + componentRoute( + { + headers: {}, + params: { componentName: 'async-error2-component' } }, - status: statusStub - }); + { + conf: { + baseUrl: 'http://components.com/', + plugins: {} + }, + status: statusStub + } + ); - componentRoute({ - headers: {}, - params: { componentName: 'async-error2-component' }, - query: { error: true } - }, { - conf: { - baseUrl: 'http://components.com/', - plugins: {} + componentRoute( + { + headers: {}, + params: { componentName: 'async-error2-component' }, + query: { error: true } }, - status: statusStub - }); + { + conf: { + baseUrl: 'http://components.com/', + plugins: {} + }, + status: statusStub + } + ); }); it('should return 200 status code for successful request', () => { @@ -222,10 +246,12 @@ describe('registry : routes : component', () => { }); it('should respond with error message for component execution error', () => { - expect(resJsonStub.args[1][0].error).to.equal('Component execution error: thisDoesnotExist is not defined'); + expect(resJsonStub.args[1][0].error).to.equal( + 'Component execution error: thisDoesnotExist is not defined' + ); }); - it('should return component\'s name and request version for both requests', () => { + it("should return component's name and request version for both requests", () => { expect(resJsonStub.args[0][0].name).to.equal('async-error2-component'); expect(resJsonStub.args[0][0].requestVersion).to.equal(''); expect(resJsonStub.args[1][0].name).to.equal('async-error2-component'); @@ -235,45 +261,47 @@ describe('registry : routes : component', () => { }); describe('when getting a component that implements a plugin', () => { - describe('when plugin not declared in package.json', () => { - before(() => { initialise(mockedComponents['plugin-component']); componentRoute = new ComponentRoute({}, mockedRepository); - componentRoute({ - headers: {}, - params: { componentName: 'plugin-component' } - }, { - conf: { - baseUrl: 'http://components.com/', - plugins: {} + componentRoute( + { + headers: {}, + params: { componentName: 'plugin-component' } }, - status: statusStub - }); + { + conf: { + baseUrl: 'http://components.com/', + plugins: {} + }, + status: statusStub + } + ); }); - it('should return 501 status code', () => { - expect(statusStub.args[0][0]).to.be.equal(501); + it('should return 500 status code', () => { + expect(statusStub.args[0][0]).to.be.equal(500); }); - it('should respond with PLUGIN_MISSING_FROM_COMPONENT error code', () => { - expect(resJsonStub.args[0][0].code).to.equal('PLUGIN_MISSING_FROM_COMPONENT'); + it('should respond with GENERIC_ERROR error code', () => { + expect(resJsonStub.args[0][0].code).to.equal('GENERIC_ERROR'); }); it('should respond with error message including missing plugin', () => { - expect(resJsonStub.args[0][0].error).to.equal('Component is trying to use un-registered plugins: doSomething'); + expect(resJsonStub.args[0][0].error).to.contain( + 'plugins.doSomething is not a function' + ); }); - it('should return component\'s name and request version', () => { + it("should return component's name and request version", () => { expect(resJsonStub.args[0][0].name).to.equal('plugin-component'); expect(resJsonStub.args[0][0].requestVersion).to.equal(''); }); }); describe('when plugin declared in package.json', () => { - beforeEach(() => { const component = _.clone(mockedComponents['plugin-component']); component.package.oc.plugins = ['doSomething']; @@ -282,20 +310,24 @@ describe('registry : routes : component', () => { }); describe('when registry implements plugin', () => { - beforeEach(() => { - componentRoute({ - headers: {}, - params: { componentName: 'plugin-component' } - }, { - conf: { - baseUrl: 'http://components.com/', - plugins: { - doSomething: function(){ return 'hello hello hello my friend'; } - } + componentRoute( + { + headers: {}, + params: { componentName: 'plugin-component' } }, - status: statusStub - }); + { + conf: { + baseUrl: 'http://components.com/', + plugins: { + doSomething: function() { + return 'hello hello hello my friend'; + } + } + }, + status: statusStub + } + ); }); it('should return 200 status code', () => { @@ -303,25 +335,29 @@ describe('registry : routes : component', () => { }); it('should use plugin inside compiledView', () => { - expect(resJsonStub.args[0][0].html).to.contain('hello hello hello my friend John'); + expect(resJsonStub.args[0][0].html).to.contain( + 'hello hello hello my friend John' + ); }); - it('should return component\'s name and request version', () => { + it("should return component's name and request version", () => { expect(resJsonStub.args[0][0].name).to.equal('plugin-component'); expect(resJsonStub.args[0][0].requestVersion).to.equal(''); }); }); describe('when registry does not implement plugin', () => { - beforeEach(() => { - componentRoute({ - headers: {}, - params: { componentName: 'plugin-component' } - }, { - conf: { baseUrl: 'http://components.com/' }, - status: statusStub - }); + componentRoute( + { + headers: {}, + params: { componentName: 'plugin-component' } + }, + { + conf: { baseUrl: 'http://components.com/' }, + status: statusStub + } + ); }); it('should return 501 status code', () => { @@ -329,14 +365,18 @@ describe('registry : routes : component', () => { }); it('should respond with PLUGIN_MISSING_FROM_REGISTRY error code', () => { - expect(resJsonStub.args[0][0].code).to.equal('PLUGIN_MISSING_FROM_REGISTRY'); + expect(resJsonStub.args[0][0].code).to.equal( + 'PLUGIN_MISSING_FROM_REGISTRY' + ); }); it('should respond with error message including missing plugin', () => { - expect(resJsonStub.args[0][0].error).to.equal('registry does not implement plugins: doSomething'); + expect(resJsonStub.args[0][0].error).to.equal( + 'registry does not implement plugins: doSomething' + ); }); - it('should return component\'s name and request version', () => { + it("should return component's name and request version", () => { expect(resJsonStub.args[0][0].name).to.equal('plugin-component'); expect(resJsonStub.args[0][0].requestVersion).to.equal(''); }); @@ -345,27 +385,26 @@ describe('registry : routes : component', () => { }); describe('when getting a component that requires a npm module', () => { - describe('when registry implements dependency', () => { - beforeEach(() => { initialise(mockedComponents['npm-component']); componentRoute = new ComponentRoute({}, mockedRepository); - componentRoute({ - headers: {}, - params: { componentName: 'npm-component' } - }, { - conf: { - local: true, //needed to invalidate the cache - baseUrl: 'http://components.com/', - plugins: {}, - dependencies: [ - 'lodash' - ] + componentRoute( + { + headers: {}, + params: { componentName: 'npm-component' } }, - status: statusStub - }); + { + conf: { + local: true, //needed to invalidate the cache + baseUrl: 'http://components.com/', + plugins: {}, + dependencies: ['lodash'] + }, + status: statusStub + } + ); }); it('should return 200 status code', () => { @@ -376,31 +415,33 @@ describe('registry : routes : component', () => { expect(resJsonStub.args[0][0].html).to.contain('bye John'); }); - it('should return component\'s name and request version', () => { + it("should return component's name and request version", () => { expect(resJsonStub.args[0][0].name).to.equal('npm-component'); expect(resJsonStub.args[0][0].requestVersion).to.equal(''); }); }); describe('when registry does not implement dependency', () => { - beforeEach(() => { initialise(mockedComponents['npm-component']); componentRoute = new ComponentRoute({}, mockedRepository); - componentRoute({ - headers: {}, - params: { componentName: 'npm-component' } - }, { - conf: { - local: true, - hotReloading: true, //needed to invalidate the cache - baseUrl: 'http://components.com/', - plugins: {}, - dependencies: {} + componentRoute( + { + headers: {}, + params: { componentName: 'npm-component' } }, - status: statusStub - }); + { + conf: { + local: true, + hotReloading: true, //needed to invalidate the cache + baseUrl: 'http://components.com/', + plugins: {}, + dependencies: {} + }, + status: statusStub + } + ); }); it('should return 501 status code', () => { @@ -408,14 +449,18 @@ describe('registry : routes : component', () => { }); it('should respond with DEPENDENCY_MISSING_FROM_REGISTRY error code', () => { - expect(resJsonStub.args[0][0].code).to.equal('DEPENDENCY_MISSING_FROM_REGISTRY'); + expect(resJsonStub.args[0][0].code).to.equal( + 'DEPENDENCY_MISSING_FROM_REGISTRY' + ); }); it('should respond with error message including missing dependency', () => { - expect(resJsonStub.args[0][0].error).to.equal('Component is trying to use unavailable dependencies: lodash'); + expect(resJsonStub.args[0][0].error).to.equal( + 'Component is trying to use unavailable dependencies: lodash' + ); }); - it('should return component\'s name and request version', () => { + it("should return component's name and request version", () => { expect(resJsonStub.args[0][0].name).to.equal('npm-component'); expect(resJsonStub.args[0][0].requestVersion).to.equal(''); }); @@ -423,22 +468,27 @@ describe('registry : routes : component', () => { }); describe('when getting a component with server.js that sets custom headers with empty customHeadersToSkipOnWeakVersion', () => { - before(() => { initialise(mockedComponents['response-headers-component']); componentRoute = new ComponentRoute({}, mockedRepository); - componentRoute({ - headers: {}, - params: { componentName: 'response-headers-component', componentVersion: '1.X.X' } - }, { - conf: { - baseUrl: 'http://component.com/', - executionTimeout: 0.1 + componentRoute( + { + headers: {}, + params: { + componentName: 'response-headers-component', + componentVersion: '1.X.X' + } }, - status: statusStub, - set: resSetStub - }); + { + conf: { + baseUrl: 'http://component.com/', + executionTimeout: 0.1 + }, + status: statusStub, + set: resSetStub + } + ); }); it('should return 200 status code', () => { @@ -451,37 +501,44 @@ describe('registry : routes : component', () => { expect(resSetStub.args[0][0]['test-header']).to.equal('test-value'); }); - it('should return component\'s name and request version', () => { - expect(resJsonStub.args[0][0].name).to.equal('response-headers-component'); + it("should return component's name and request version", () => { + expect(resJsonStub.args[0][0].name).to.equal( + 'response-headers-component' + ); expect(resJsonStub.args[0][0].requestVersion).to.equal('1.X.X'); }); }); describe('when getting a component with server.js that sets custom headers with non-empty customHeadersToSkipOnWeakVersion', () => { - - before((done) => { + before(done => { initialise(mockedComponents['response-headers-component']); componentRoute = new ComponentRoute({}, mockedRepository); statusStub.returns({ - json: function(response){ + json: function(response) { resJsonStub(response); done(); } }); - componentRoute({ - headers: {}, - params: { componentName: 'response-headers-component', componentVersion: '1.X.X' } - }, { - conf: { - baseUrl: 'http://component.com/', - executionTimeout: 0.1, - customHeadersToSkipOnWeakVersion: ['test-header'] + componentRoute( + { + headers: {}, + params: { + componentName: 'response-headers-component', + componentVersion: '1.X.X' + } }, - status: statusStub, - set: resSetStub - }); + { + conf: { + baseUrl: 'http://component.com/', + executionTimeout: 0.1, + customHeadersToSkipOnWeakVersion: ['test-header'] + }, + status: statusStub, + set: resSetStub + } + ); }); it('should return 200 status code', () => { @@ -493,16 +550,18 @@ describe('registry : routes : component', () => { expect(resSetStub.called).to.be.false; }); - it('should return component\'s name and request version', () => { - expect(resJsonStub.args[0][0].name).to.equal('response-headers-component'); + it("should return component's name and request version", () => { + expect(resJsonStub.args[0][0].name).to.equal( + 'response-headers-component' + ); expect(resJsonStub.args[0][0].requestVersion).to.equal('1.X.X'); }); }); describe('when getting a simple component with server.js after headers component no custom headers should be set', () => { - - before((done) => { - const headersComponent = mockedComponents['another-response-headers-component']; + before(done => { + const headersComponent = + mockedComponents['another-response-headers-component']; const simpleComponent = mockedComponents['simple-component']; mockedRepository = { @@ -516,14 +575,26 @@ describe('registry : routes : component', () => { // Custom repository initialization to give us two components when invoked twice. // The firts one with custom headers and the second without. - mockedRepository.getCompiledView.onCall(0).yields(null, headersComponent.view); - mockedRepository.getCompiledView.onCall(1).yields(null, simpleComponent.view); - - mockedRepository.getComponent.onCall(0).yields(null, headersComponent.package); - mockedRepository.getComponent.onCall(1).yields(null, simpleComponent.package); - - mockedRepository.getDataProvider.onCall(0).yields(null, headersComponent.data); - mockedRepository.getDataProvider.onCall(1).yields(null, simpleComponent.data); + mockedRepository.getCompiledView + .onCall(0) + .yields(null, headersComponent.view); + mockedRepository.getCompiledView + .onCall(1) + .yields(null, simpleComponent.view); + + mockedRepository.getComponent + .onCall(0) + .yields(null, headersComponent.package); + mockedRepository.getComponent + .onCall(1) + .yields(null, simpleComponent.package); + + mockedRepository.getDataProvider + .onCall(0) + .yields(null, headersComponent.data); + mockedRepository.getDataProvider + .onCall(1) + .yields(null, simpleComponent.data); componentRoute = new ComponentRoute({}, mockedRepository); @@ -531,37 +602,49 @@ describe('registry : routes : component', () => { resSetStub = sinon.stub(); statusStub = sinon.stub().returns({ - json: function(response){ + json: function(response) { resJsonStub(response); - if(statusStub.args.length>=2){ + if (statusStub.args.length >= 2) { done(); } } }); - componentRoute({ - headers: {}, - params: { componentName: 'another-response-headers-component', componentVersion: '1.X.X' } - }, { - conf: { - baseUrl: 'http://component.com/', - executionTimeout: 0.1 + componentRoute( + { + headers: {}, + params: { + componentName: 'another-response-headers-component', + componentVersion: '1.X.X' + } }, - status: statusStub, - set: resSetStub - }); + { + conf: { + baseUrl: 'http://component.com/', + executionTimeout: 0.1 + }, + status: statusStub, + set: resSetStub + } + ); - componentRoute({ - headers: {}, - params: { componentName: 'simple-component', componentVersion: '1.X.X' } - }, { - conf: { - baseUrl: 'http://component.com/', - executionTimeout: 0.1 + componentRoute( + { + headers: {}, + params: { + componentName: 'simple-component', + componentVersion: '1.X.X' + } }, - status: statusStub, - set: resSetStub - }); + { + conf: { + baseUrl: 'http://component.com/', + executionTimeout: 0.1 + }, + status: statusStub, + set: resSetStub + } + ); }); it('should return 200 status code for the first component', () => { @@ -569,14 +652,18 @@ describe('registry : routes : component', () => { }); it('should return "response-headers-component" name for the first component\'s name and request version', () => { - expect(resJsonStub.args[0][0].name).to.equal('another-response-headers-component'); + expect(resJsonStub.args[0][0].name).to.equal( + 'another-response-headers-component' + ); expect(resJsonStub.args[0][0].requestVersion).to.equal('1.X.X'); }); it('should set response headers for the first component', () => { expect(resJsonStub.args[0][0].headers).to.be.undefined; expect(resSetStub.args[0][0]).to.not.be.null; - expect(resSetStub.args[0][0]['another-test-header']).to.equal('another-test-value'); + expect(resSetStub.args[0][0]['another-test-header']).to.equal( + 'another-test-value' + ); }); it('should return 200 status code for the second component', () => { @@ -599,17 +686,23 @@ describe('registry : routes : component', () => { initialise(mockedComponents['response-headers-component']); componentRoute = new ComponentRoute({}, mockedRepository); - componentRoute({ - headers: { accept: 'application/vnd.oc.info+json' }, - params: { componentName: 'response-headers-component', componentVersion: '1.0.0' } - }, { - conf: { - baseUrl: 'http://component.com/', - executionTimeout: 0.1 + componentRoute( + { + headers: { accept: 'application/vnd.oc.info+json' }, + params: { + componentName: 'response-headers-component', + componentVersion: '1.0.0' + } }, - status: statusStub, - set: resSetStub - }); + { + conf: { + baseUrl: 'http://component.com/', + executionTimeout: 0.1 + }, + status: statusStub, + set: resSetStub + } + ); }); it('should return 200 status code', () => { @@ -621,8 +714,10 @@ describe('registry : routes : component', () => { expect(resSetStub.called).to.be.false; }); - it('should return component\'s name and request version', () => { - expect(resJsonStub.args[0][0].name).to.equal('response-headers-component'); + it("should return component's name and request version", () => { + expect(resJsonStub.args[0][0].name).to.equal( + 'response-headers-component' + ); expect(resJsonStub.args[0][0].requestVersion).to.equal('1.0.0'); }); @@ -630,5 +725,4 @@ describe('registry : routes : component', () => { expect(resJsonStub.args[0][0].html).to.be.undefined; }); }); - });