mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
485 lines
11 KiB
485 lines
11 KiB
8 years ago
|
'use strict';
|
||
|
|
||
8 years ago
|
const Joi = require('joi');
|
||
|
const schema = require('../../schema');
|
||
|
|
||
|
function validate(config) {
|
||
|
Joi.validate(config, schema, (err, value) => {
|
||
|
if (err) {
|
||
|
throw new Error(err.details[0].message);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
8 years ago
|
|
||
|
module.exports.tests = {};
|
||
|
|
||
8 years ago
|
module.exports.tests.completely_valid = (test, common) => {
|
||
|
test('all valid configuration elements should not throw error', (t) => {
|
||
8 years ago
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
legacyUrl: 'legacyUrl value',
|
||
|
accessLog: 'accessLog value',
|
||
|
relativeScores: true,
|
||
|
localization: {
|
||
|
flipNumberAndStreetCountries: ['ABC', 'DEF']
|
||
8 years ago
|
},
|
||
|
requestRetries: 19
|
||
8 years ago
|
},
|
||
|
esclient: {
|
||
|
requestTimeout: 17
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.doesNotThrow(validate.bind(config));
|
||
8 years ago
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('basic valid configuration should not throw error', (t) => {
|
||
8 years ago
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value'
|
||
|
},
|
||
|
esclient: {
|
||
|
requestTimeout: 17
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.doesNotThrow(validate.bind(config));
|
||
8 years ago
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
|
};
|
||
|
|
||
8 years ago
|
module.exports.tests.api_validation = (test, common) => {
|
||
|
test('config without api should throw error', (t) => {
|
||
8 years ago
|
var config = {
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /"api" is required/, 'api should exist');
|
||
8 years ago
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('config without unknown field in api should not throw error', (t) => {
|
||
8 years ago
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
unknown: 'unknown value'
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.doesNotThrow(validate.bind(null, config), 'unknown properties should be allowed');
|
||
8 years ago
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('non-string api.version should throw error', (t) => {
|
||
8 years ago
|
[null, 17, {}, [], true].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: value,
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value'
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /"version" must be a string/);
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('non-string api.indexName should throw error', (t) => {
|
||
8 years ago
|
[null, 17, {}, [], true].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: value,
|
||
|
host: 'host value'
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /"indexName" must be a string/);
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('non-string api.host should throw error', (t) => {
|
||
8 years ago
|
[null, 17, {}, [], true].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: value
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /"host" must be a string/);
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('non-string api.legacyUrl should throw error', (t) => {
|
||
8 years ago
|
[null, 17, {}, [], true].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
legacyUrl: value
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /"legacyUrl" must be a string/);
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('non-string api.accessLog should throw error', (t) => {
|
||
8 years ago
|
[null, 17, {}, [], true].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
accessLog: value
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /"accessLog" must be a string/);
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('non-boolean api.relativeScores should throw error', (t) => {
|
||
8 years ago
|
[null, 17, {}, [], 'string'].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
relativeScores: value
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /"relativeScores" must be a boolean/);
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('non-object api.localization should throw error', (t) => {
|
||
8 years ago
|
[null, 17, false, [], 'string'].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
localization: value
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /"localization" must be an object/);
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('unknown properties in api.localization should throw error', (t) => {
|
||
8 years ago
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
localization: {
|
||
|
unknown_property: 'value'
|
||
|
}
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /"unknown_property" is not allowed/);
|
||
8 years ago
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('non-array api.localization.flipNumberAndStreetCountries should throw error', (t) => {
|
||
8 years ago
|
[null, 17, {}, false, 'string'].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
localization: {
|
||
|
flipNumberAndStreetCountries: value
|
||
|
}
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(
|
||
|
validate.bind(null, config),
|
||
|
/"flipNumberAndStreetCountries" must be an array/);
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('non-string api.localization.flipNumberAndStreetCountries elements should throw error', (t) => {
|
||
8 years ago
|
[null, 17, {}, false, []].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
localization: {
|
||
|
flipNumberAndStreetCountries: [value]
|
||
|
}
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /"0" must be a string/);
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('non-3-char api.localization.flipNumberAndStreetCountries elements should throw error', (t) => {
|
||
8 years ago
|
['AB', 'ABCD'].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
localization: {
|
||
|
flipNumberAndStreetCountries: [value]
|
||
|
}
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(validate.bind(null, config), /fails to match the required pattern/);
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('config with non-number api.requestRetries should throw error', (t) => {
|
||
8 years ago
|
[null, 'string', {}, [], false].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
requestRetries: value
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(
|
||
|
validate.bind(null, config),
|
||
|
/"requestRetries" must be a number/, 'api.requestRetries should be a number');
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('config with non-integer api.requestRetries should throw error', (t) => {
|
||
8 years ago
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
requestRetries: 17.3
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(
|
||
|
validate.bind(null, config),
|
||
|
/"requestRetries" must be an integer/, 'api.requestRetries should be an integer');
|
||
8 years ago
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('config with negative api.requestRetries should throw error', (t) => {
|
||
8 years ago
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value',
|
||
|
requestRetries: -1
|
||
|
},
|
||
|
esclient: {}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(
|
||
|
validate.bind(null, config),
|
||
|
/"requestRetries" must be larger than or equal to 0/, 'api.requestRetries must be positive');
|
||
8 years ago
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
module.exports.tests.esclient_validation = (test, common) => {
|
||
|
test('config without esclient should throw error', (t) => {
|
||
8 years ago
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value'
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(
|
||
|
validate.bind(null, config),
|
||
|
/"esclient" is required/, 'esclient should exist');
|
||
8 years ago
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('config with non-object esclient should throw error', (t) => {
|
||
8 years ago
|
[null, 17, [], 'string', true].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value'
|
||
|
},
|
||
|
esclient: value
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(
|
||
|
validate.bind(null, config),
|
||
|
/"esclient" must be an object/, 'esclient should be an object');
|
||
8 years ago
|
|
||
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('config with non-number esclient.requestTimeout should throw error', (t) => {
|
||
8 years ago
|
[null, 'string', {}, [], false].forEach((value) => {
|
||
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value'
|
||
|
},
|
||
|
esclient: {
|
||
|
requestTimeout: value
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(
|
||
|
validate.bind(null, config),
|
||
|
/"requestTimeout" must be a number/, 'esclient.requestTimeout should be a number');
|
||
|
|
||
8 years ago
|
});
|
||
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('config with non-integer esclient.requestTimeout should throw error', (t) => {
|
||
8 years ago
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value'
|
||
|
},
|
||
|
esclient: {
|
||
|
requestTimeout: 17.3
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(
|
||
|
validate.bind(null, config),
|
||
|
/"requestTimeout" must be an integer/, 'esclient.requestTimeout should be an integer');
|
||
8 years ago
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
8 years ago
|
test('config with negative esclient.requestTimeout should throw error', (t) => {
|
||
8 years ago
|
var config = {
|
||
|
api: {
|
||
|
version: 'version value',
|
||
|
indexName: 'index name value',
|
||
|
host: 'host value'
|
||
|
},
|
||
|
esclient: {
|
||
|
requestTimeout: -1
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
t.throws(
|
||
|
validate.bind(null, config),
|
||
|
/"requestTimeout" must be larger than or equal to 0/, 'esclient.requestTimeout must be positive');
|
||
8 years ago
|
|
||
|
t.end();
|
||
|
|
||
|
});
|
||
|
|
||
|
};
|
||
|
|
||
8 years ago
|
module.exports.all = (tape, common) => {
|
||
8 years ago
|
|
||
|
function test(name, testFunction) {
|
||
|
return tape('configValidation: ' + name, testFunction);
|
||
|
}
|
||
|
|
||
|
for( var testCase in module.exports.tests ){
|
||
|
module.exports.tests[testCase](test, common);
|
||
|
}
|
||
|
};
|