Browse Source

added interpolation service to schema

refactored similar service tests to iterate list of services instead of duplicating many tests for each service
pull/936/head
Stephen Hess 8 years ago
parent
commit
3c8bfa196e
  1. 5
      schema.js
  2. 656
      test/unit/schema.js

5
schema.js

@ -41,6 +41,11 @@ module.exports = Joi.object().keys({
url: Joi.string().uri({ scheme: /https?/ }), url: Joi.string().uri({ scheme: /https?/ }),
timeout: Joi.number().integer().optional().default(250).min(0), timeout: Joi.number().integer().optional().default(250).min(0),
retries: Joi.number().integer().optional().default(3).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).requiredKeys('url')
}).unknown(false).default({}), // default api.services to an empty object }).unknown(false).default({}), // default api.services to an empty object
defaultParameters: Joi.object().keys({ defaultParameters: Joi.object().keys({

656
test/unit/schema.js

@ -2,6 +2,7 @@
const Joi = require('joi'); const Joi = require('joi');
const schema = require('../../schema'); const schema = require('../../schema');
const _ = require('lodash');
module.exports.tests = {}; module.exports.tests = {};
@ -540,72 +541,30 @@ module.exports.tests.api_services_validation = (test, common) => {
}; };
module.exports.tests.placeholder_service_validation = (test, common) => { module.exports.tests.service_validation = (test, common) => {
test('timeout and retries not specified should default to 250 and 3', (t) => { // these tests apply for all the individual service definitions
const config = { const services = ['pip', 'placeholder', 'language', 'interpolation'];
api: {
version: 'version value',
indexName: 'index name value',
host: 'host value',
services: {
placeholder: {
url: 'http://localhost'
}
}
},
esclient: {}
};
const result = Joi.validate(config, schema); test('timeout and retries not specified should default to 250 and 3', (t) => {
services.forEach(service => {
t.equals(result.value.api.services.placeholder.timeout, 250); const config = {
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 = {
api: { api: {
version: 'version value', version: 'version value',
indexName: 'index name value', indexName: 'index name value',
host: 'host value', host: 'host value',
services: { services: {}
placeholder: {
url: value
}
}
}, },
esclient: {} esclient: {}
}; };
config.api.services[service] = {
url: 'http://localhost'
};
const result = Joi.validate(config, schema); const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1); t.equals(result.value.api.services[service].timeout, 250);
t.equals(result.error.details[0].message, '"url" must be a string'); 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) => { test('when api.services.service is defined, url is required', (t) => {
['ftp', 'git', 'unknown'].forEach((scheme) => { services.forEach(service => {
var config = { const config = {
api: { api: {
version: 'version value', version: 'version value',
indexName: 'index name value', indexName: 'index name value',
host: 'host value', host: 'host value',
services: { services: {}
placeholder: {
url: `${scheme}://localhost`
}
}
}, },
esclient: {} esclient: {}
}; };
config.api.services[service] = {};
const result = Joi.validate(config, schema); const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1); 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(); t.end();
}); });
test('non-url children of api.services.placeholder should be disallowed', (t) => { test('non-string api.services.pip.url should throw error', (t) => {
var config = { services.forEach(service => {
api: { [null, 17, {}, [], true].forEach(value => {
version: 'version value', const config = {
indexName: 'index name value', api: {
host: 'host value', version: 'version value',
services: { indexName: 'index name value',
placeholder: { host: 'host value',
url: 'http://localhost', services: {}
unknown_property: 'value' },
} esclient: {}
} };
},
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: {}
};
const result = Joi.validate(config, schema); config.api.services[service] = {
url: value
};
t.equals(result.error.details.length, 1); const result = Joi.validate(config, schema);
t.equals(result.error.details[0].message, '"url" is required');
t.end();
}); 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) => { test('non-http/https api.services.pip.url should throw error', (t) => {
['ftp', 'git', 'unknown'].forEach((scheme) => { services.forEach(service => {
var config = { ['ftp', 'git', 'unknown'].forEach((scheme) => {
api: { const config = {
version: 'version value', api: {
indexName: 'index name value', version: 'version value',
host: 'host value', indexName: 'index name value',
services: { host: 'host value',
pip: { services: {}
url: `${scheme}://localhost` },
} esclient: {}
} };
},
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');
}); 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(); t.end();
}); });
test('non-number timeout should throw error', (t) => { test('non-url children of api.services.pip should be disallowed', (t) => {
[null, 'string', {}, [], false].forEach((value) => { services.forEach(service => {
const config = { const config = {
api: { api: {
version: 'version value', version: 'version value',
indexName: 'index name value', indexName: 'index name value',
host: 'host value', host: 'host value',
services: { services: {}
pip: {
url: 'http://localhost',
timeout: value
}
}
}, },
esclient: {} esclient: {}
}; };
config.api.services[service] = {
url: 'http://localhost',
unknown_property: 'value'
};
const result = Joi.validate(config, schema); const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1); 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) => { test('non-number timeout should throw error', (t) => {
const config = { services.forEach(service => {
api: { [null, 'string', {}, [], false].forEach((value) => {
version: 'version value', const config = {
indexName: 'index name value', api: {
host: 'host value', version: 'version value',
services: { indexName: 'index name value',
pip: { host: 'host value',
url: 'http://localhost', services: {}
timeout: 17.3 },
} esclient: {}
} };
},
esclient: {}
};
const result = Joi.validate(config, schema); config.api.services[service] = {
url: 'http://localhost',
timeout: value
};
t.equals(result.error.details.length, 1); const result = Joi.validate(config, schema);
t.equals(result.error.details[0].message, '"timeout" must be an integer');
t.end();
}); 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(); t.end();
}); });
test('non-number retries should throw error', (t) => { test('non-integer timeout should throw error', (t) => {
[null, 'string', {}, [], false].forEach((value) => { services.forEach(service => {
const config = { const config = {
api: { api: {
version: 'version value', version: 'version value',
indexName: 'index name value', indexName: 'index name value',
host: 'host value', host: 'host value',
services: { services: {}
pip: {
url: 'http://localhost',
retries: value
}
}
}, },
esclient: {} esclient: {}
}; };
config.api.services[service] = {
url: 'http://localhost',
timeout: 17.3
};
const result = Joi.validate(config, schema); const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1); 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) => { test('negative timeout should throw error', (t) => {
const config = { services.forEach(service => {
api: { const config = {
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 = {
api: { api: {
version: 'version value', version: 'version value',
indexName: 'index name value', indexName: 'index name value',
host: 'host value', host: 'host value',
services: { services: {}
language: {
url: value
}
}
}, },
esclient: {} esclient: {}
}; };
config.api.services[service] = {
url: 'http://localhost',
timeout: -1
};
const result = Joi.validate(config, schema); const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1); 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) => { test('non-number retries should throw error', (t) => {
['ftp', 'git', 'unknown'].forEach((scheme) => { services.forEach(service => {
var config = { [null, 'string', {}, [], false].forEach((value) => {
api: { const config = {
version: 'version value', api: {
indexName: 'index name value', version: 'version value',
host: 'host value', indexName: 'index name value',
services: { host: 'host value',
language: { services: {}
url: `${scheme}://localhost` },
} esclient: {}
} };
},
esclient: {}
};
const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1); config.api.services[service] = {
t.equals(result.error.details[0].message, '"url" must be a valid uri with a scheme matching the https\? pattern'); 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(); t.end();
}); });
test('non-number timeout should throw error', (t) => { test('non-integer retries should throw error', (t) => {
[null, 'string', {}, [], false].forEach((value) => { services.forEach(service => {
const config = { const config = {
api: { api: {
version: 'version value', version: 'version value',
indexName: 'index name value', indexName: 'index name value',
host: 'host value', host: 'host value',
services: { services: {}
language: {
url: 'http://localhost',
timeout: value
}
}
}, },
esclient: {} esclient: {}
}; };
config.api.services[service] = {
url: 'http://localhost',
retries: 17.3
};
const result = Joi.validate(config, schema); const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1); 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) => { test('negative retries should throw error', (t) => {
const config = { services.forEach(service => {
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) => {
const config = { const config = {
api: { api: {
version: 'version value', version: 'version value',
indexName: 'index name value', indexName: 'index name value',
host: 'host value', host: 'host value',
services: { services: {}
language: {
url: 'http://localhost',
retries: value
}
}
}, },
esclient: {} esclient: {}
}; };
config.api.services[service] = {
url: 'http://localhost',
retries: -1
};
const result = Joi.validate(config, schema); const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1); 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) => { module.exports.tests.esclient_validation = (test, common) => {

Loading…
Cancel
Save