From 3c8bfa196e5f872251910d0a6392057ffd0f46fe Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 26 Jul 2017 17:42:40 -0400 Subject: [PATCH] added interpolation service to schema refactored similar service tests to iterate list of services instead of duplicating many tests for each service --- schema.js | 5 + test/unit/schema.js | 656 ++++++++++---------------------------------- 2 files changed, 152 insertions(+), 509 deletions(-) diff --git a/schema.js b/schema.js index a82a9d0f..1c46f95a 100644 --- a/schema.js +++ b/schema.js @@ -41,6 +41,11 @@ module.exports = Joi.object().keys({ url: Joi.string().uri({ scheme: /https?/ }), timeout: Joi.number().integer().optional().default(250).min(0), retries: Joi.number().integer().optional().default(3).min(0), + }).unknown(false).requiredKeys('url'), + interpolation: Joi.object().keys({ + url: Joi.string().uri({ scheme: /https?/ }), + timeout: Joi.number().integer().optional().default(250).min(0), + retries: Joi.number().integer().optional().default(3).min(0), }).unknown(false).requiredKeys('url') }).unknown(false).default({}), // default api.services to an empty object defaultParameters: Joi.object().keys({ diff --git a/test/unit/schema.js b/test/unit/schema.js index 52d8c27e..ea331115 100644 --- a/test/unit/schema.js +++ b/test/unit/schema.js @@ -2,6 +2,7 @@ const Joi = require('joi'); const schema = require('../../schema'); +const _ = require('lodash'); module.exports.tests = {}; @@ -540,72 +541,30 @@ module.exports.tests.api_services_validation = (test, common) => { }; -module.exports.tests.placeholder_service_validation = (test, common) => { - test('timeout and retries not specified should default to 250 and 3', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - placeholder: { - url: 'http://localhost' - } - } - }, - esclient: {} - }; +module.exports.tests.service_validation = (test, common) => { + // these tests apply for all the individual service definitions + const services = ['pip', 'placeholder', 'language', 'interpolation']; - const result = Joi.validate(config, schema); - - t.equals(result.value.api.services.placeholder.timeout, 250); - t.equals(result.value.api.services.placeholder.retries, 3); - t.end(); - - }); - - test('when api.services.placeholder is defined, url is required', (t) => { - var config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - placeholder: { - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"url" is required'); - t.end(); - - }); - - test('non-string api.services.placeholder.url should throw error', (t) => { - [null, 17, {}, [], true].forEach((value) => { - var config = { + test('timeout and retries not specified should default to 250 and 3', (t) => { + services.forEach(service => { + const config = { api: { version: 'version value', indexName: 'index name value', host: 'host value', - services: { - placeholder: { - url: value - } - } + services: {} }, esclient: {} }; + config.api.services[service] = { + url: 'http://localhost' + }; + const result = Joi.validate(config, schema); - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"url" must be a string'); + t.equals(result.value.api.services[service].timeout, 250); + t.equals(result.value.api.services[service].retries, 3); }); @@ -613,125 +572,53 @@ module.exports.tests.placeholder_service_validation = (test, common) => { }); - test('non-http/https api.services.placeholder.url should throw error', (t) => { - ['ftp', 'git', 'unknown'].forEach((scheme) => { - var config = { + test('when api.services.service is defined, url is required', (t) => { + services.forEach(service => { + const config = { api: { version: 'version value', indexName: 'index name value', host: 'host value', - services: { - placeholder: { - url: `${scheme}://localhost` - } - } + services: {} }, esclient: {} }; + config.api.services[service] = {}; + const result = Joi.validate(config, schema); t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"url" must be a valid uri with a scheme matching the https\? pattern'); - + t.equals(result.error.details[0].message, '"url" is required'); }); t.end(); }); - test('non-url children of api.services.placeholder should be disallowed', (t) => { - var config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - placeholder: { - url: 'http://localhost', - unknown_property: 'value' - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"unknown_property" is not allowed'); - t.end(); - - }); - -}; - -module.exports.tests.pip_service_validation = (test, common) => { - test('timeout and retries not specified should default to 250 and 3', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - pip: { - url: 'http://localhost' - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.value.api.services.pip.timeout, 250); - t.equals(result.value.api.services.pip.retries, 3); - t.end(); - - }); - - test('when api.services.pip is defined, url is required', (t) => { - var config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - pip: { - } - } - }, - esclient: {} - }; + test('non-string api.services.pip.url should throw error', (t) => { + services.forEach(service => { + [null, 17, {}, [], true].forEach(value => { + const config = { + api: { + version: 'version value', + indexName: 'index name value', + host: 'host value', + services: {} + }, + esclient: {} + }; - const result = Joi.validate(config, schema); + config.api.services[service] = { + url: value + }; - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"url" is required'); - t.end(); + const result = Joi.validate(config, schema); - }); + t.equals(result.error.details.length, 1); + t.equals(result.error.details[0].message, '"url" must be a string'); - test('non-string api.services.pip.url should throw error', (t) => { - [null, 17, {}, [], true].forEach((value) => { - var config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - pip: { - url: value - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"url" must be a string'); + }); }); @@ -740,77 +627,56 @@ module.exports.tests.pip_service_validation = (test, common) => { }); test('non-http/https api.services.pip.url should throw error', (t) => { - ['ftp', 'git', 'unknown'].forEach((scheme) => { - var config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - pip: { - url: `${scheme}://localhost` - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"url" must be a valid uri with a scheme matching the https\? pattern'); + services.forEach(service => { + ['ftp', 'git', 'unknown'].forEach((scheme) => { + const config = { + api: { + version: 'version value', + indexName: 'index name value', + host: 'host value', + services: {} + }, + esclient: {} + }; - }); + config.api.services[service] = { + url: `${scheme}://localhost` + }; - t.end(); + const result = Joi.validate(config, schema); - }); + t.equals(result.error.details.length, 1); + t.equals(result.error.details[0].message, '"url" must be a valid uri with a scheme matching the https\? pattern'); - test('non-url children of api.services.pip should be disallowed', (t) => { - var config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - pip: { - url: 'http://localhost', - unknown_property: 'value' - } - } - }, - esclient: {} - }; + }); - const result = Joi.validate(config, schema); + }); - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"unknown_property" is not allowed'); t.end(); }); - test('non-number timeout should throw error', (t) => { - [null, 'string', {}, [], false].forEach((value) => { + test('non-url children of api.services.pip should be disallowed', (t) => { + services.forEach(service => { const config = { api: { version: 'version value', indexName: 'index name value', host: 'host value', - services: { - pip: { - url: 'http://localhost', - timeout: value - } - } + services: {} }, esclient: {} }; + config.api.services[service] = { + url: 'http://localhost', + unknown_property: 'value' + }; + const result = Joi.validate(config, schema); t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"timeout" must be a number'); + t.equals(result.error.details[0].message, '"unknown_property" is not allowed'); }); @@ -818,75 +684,58 @@ module.exports.tests.pip_service_validation = (test, common) => { }); - test('non-integer timeout should throw error', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - pip: { - url: 'http://localhost', - timeout: 17.3 - } - } - }, - esclient: {} - }; + test('non-number timeout should throw error', (t) => { + services.forEach(service => { + [null, 'string', {}, [], false].forEach((value) => { + const config = { + api: { + version: 'version value', + indexName: 'index name value', + host: 'host value', + services: {} + }, + esclient: {} + }; - const result = Joi.validate(config, schema); + config.api.services[service] = { + url: 'http://localhost', + timeout: value + }; - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"timeout" must be an integer'); - t.end(); + const result = Joi.validate(config, schema); - }); + t.equals(result.error.details.length, 1); + t.equals(result.error.details[0].message, '"timeout" must be a number'); - test('negative timeout should throw error', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - pip: { - url: 'http://localhost', - timeout: -1 - } - } - }, - esclient: {} - }; + }); - const result = Joi.validate(config, schema); + }); - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"timeout" must be larger than or equal to 0'); t.end(); }); - test('non-number retries should throw error', (t) => { - [null, 'string', {}, [], false].forEach((value) => { + test('non-integer timeout should throw error', (t) => { + services.forEach(service => { const config = { api: { version: 'version value', indexName: 'index name value', host: 'host value', - services: { - pip: { - url: 'http://localhost', - retries: value - } - } + services: {} }, esclient: {} }; + config.api.services[service] = { + url: 'http://localhost', + timeout: 17.3 + }; + const result = Joi.validate(config, schema); t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"retries" must be a number'); + t.equals(result.error.details[0].message, '"timeout" must be an integer'); }); @@ -894,122 +743,27 @@ module.exports.tests.pip_service_validation = (test, common) => { }); - test('non-integer retries should throw error', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - pip: { - url: 'http://localhost', - retries: 17.3 - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"retries" must be an integer'); - t.end(); - - }); - - test('negative retries should throw error', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - pip: { - url: 'http://localhost', - retries: -1 - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"retries" must be larger than or equal to 0'); - t.end(); - - }); - -}; - -module.exports.tests.language_service_validation = (test, common) => { - test('timeout and retries not specified should default to 250 and 3', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - language: { - url: 'http://localhost' - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.value.api.services.language.timeout, 250); - t.equals(result.value.api.services.language.retries, 3); - t.end(); - - }); - - test('when api.services.language is defined, url is required', (t) => { - var config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - language: { - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"url" is required'); - t.end(); - - }); - - test('non-string api.services.language.url should throw error', (t) => { - [null, 17, {}, [], true].forEach((value) => { - var config = { + test('negative timeout should throw error', (t) => { + services.forEach(service => { + const config = { api: { version: 'version value', indexName: 'index name value', host: 'host value', - services: { - language: { - url: value - } - } + services: {} }, esclient: {} }; + config.api.services[service] = { + url: 'http://localhost', + timeout: -1 + }; + const result = Joi.validate(config, schema); t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"url" must be a string'); + t.equals(result.error.details[0].message, '"timeout" must be larger than or equal to 0'); }); @@ -1017,78 +771,58 @@ module.exports.tests.language_service_validation = (test, common) => { }); - test('non-http/https api.services.language.url should throw error', (t) => { - ['ftp', 'git', 'unknown'].forEach((scheme) => { - var config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - language: { - url: `${scheme}://localhost` - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); + test('non-number retries should throw error', (t) => { + services.forEach(service => { + [null, 'string', {}, [], false].forEach((value) => { + const config = { + api: { + version: 'version value', + indexName: 'index name value', + host: 'host value', + services: {} + }, + esclient: {} + }; - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"url" must be a valid uri with a scheme matching the https\? pattern'); + config.api.services[service] = { + url: 'http://localhost', + retries: value + }; - }); + const result = Joi.validate(config, schema); - t.end(); + t.equals(result.error.details.length, 1); + t.equals(result.error.details[0].message, '"retries" must be a number'); - }); + }); - test('non-url children of api.services.language should be disallowed', (t) => { - var config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - language: { - url: 'http://localhost', - unknown_property: 'value' - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); + }); - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"unknown_property" is not allowed'); t.end(); }); - test('non-number timeout should throw error', (t) => { - [null, 'string', {}, [], false].forEach((value) => { + test('non-integer retries should throw error', (t) => { + services.forEach(service => { const config = { api: { version: 'version value', indexName: 'index name value', host: 'host value', - services: { - language: { - url: 'http://localhost', - timeout: value - } - } + services: {} }, esclient: {} }; + config.api.services[service] = { + url: 'http://localhost', + retries: 17.3 + }; + const result = Joi.validate(config, schema); t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"timeout" must be a number'); + t.equals(result.error.details[0].message, '"retries" must be an integer'); }); @@ -1096,75 +830,27 @@ module.exports.tests.language_service_validation = (test, common) => { }); - test('non-integer timeout should throw error', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - language: { - url: 'http://localhost', - timeout: 17.3 - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"timeout" must be an integer'); - t.end(); - - }); - - test('negative timeout should throw error', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - language: { - url: 'http://localhost', - timeout: -1 - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"timeout" must be larger than or equal to 0'); - t.end(); - - }); - - test('non-number retries should throw error', (t) => { - [null, 'string', {}, [], false].forEach((value) => { + test('negative retries should throw error', (t) => { + services.forEach(service => { const config = { api: { version: 'version value', indexName: 'index name value', host: 'host value', - services: { - language: { - url: 'http://localhost', - retries: value - } - } + services: {} }, esclient: {} }; + config.api.services[service] = { + url: 'http://localhost', + retries: -1 + }; + const result = Joi.validate(config, schema); t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"retries" must be a number'); + t.equals(result.error.details[0].message, '"retries" must be larger than or equal to 0'); }); @@ -1172,54 +858,6 @@ module.exports.tests.language_service_validation = (test, common) => { }); - test('non-integer retries should throw error', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - language: { - url: 'http://localhost', - retries: 17.3 - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"retries" must be an integer'); - t.end(); - - }); - - test('negative retries should throw error', (t) => { - const config = { - api: { - version: 'version value', - indexName: 'index name value', - host: 'host value', - services: { - language: { - url: 'http://localhost', - retries: -1 - } - } - }, - esclient: {} - }; - - const result = Joi.validate(config, schema); - - t.equals(result.error.details.length, 1); - t.equals(result.error.details[0].message, '"retries" must be larger than or equal to 0'); - t.end(); - - }); - }; module.exports.tests.esclient_validation = (test, common) => {