diff --git a/controller/predicates/is_request_sources_includes_whosonfirst.js b/controller/predicates/is_request_sources_includes_whosonfirst.js new file mode 100644 index 00000000..0f43921b --- /dev/null +++ b/controller/predicates/is_request_sources_includes_whosonfirst.js @@ -0,0 +1,17 @@ +const _ = require('lodash'); +const Debug = require('../../helper/debug'); +const debugLog = new Debug('controller:predicates:is_request_sources_includes_whosonfirst'); +const stackTraceLine = require('../../helper/stackTraceLine'); + +// returns true IFF 'whosonfirst' is included in the requested sources +module.exports = (req, res) => { + const is_request_sources_includes_whosonfirst = _.includes( + _.get(req, 'clean.sources', []), + 'whosonfirst' + ); + debugLog.push(req, () => ({ + reply: is_request_sources_includes_whosonfirst, + stack_trace: stackTraceLine() + })); + return is_request_sources_includes_whosonfirst; +}; diff --git a/controller/predicates/is_request_sources_undefined.js b/controller/predicates/is_request_sources_undefined.js new file mode 100644 index 00000000..504a5f14 --- /dev/null +++ b/controller/predicates/is_request_sources_undefined.js @@ -0,0 +1,17 @@ +const _ = require('lodash'); +const Debug = require('../../helper/debug'); +const debugLog = new Debug('controller:predicates:is_request_sources_undefined'); +const stackTraceLine = require('../../helper/stackTraceLine'); + +// returns true IFF there are no requested sources +module.exports = (req, res) => { + const is_request_sources_undefined = _.isEqual( + _.get(req, 'clean.sources', []), + [] + ); + debugLog.push(req, () => ({ + reply: is_request_sources_undefined, + stack_trace: stackTraceLine() + })); + return is_request_sources_undefined; +}; diff --git a/routes/v1.js b/routes/v1.js index eb217086..d0f5a99a 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -80,6 +80,9 @@ const hasRequestCategories = require('../controller/predicates/has_request_param const isOnlyNonAdminLayers = require('../controller/predicates/is_only_non_admin_layers'); // this can probably be more generalized const isRequestSourcesOnlyWhosOnFirst = require('../controller/predicates/is_request_sources_only_whosonfirst'); +// const isRequestSourcesIncludesWhosOnFirst = require('../controller/predicates/is_request_sources_includes_whosonfirst'); +// const isRequestSourcesUndefined = require('../controller/predicates/is_request_sources_undefined'); + const hasRequestParameter = require('../controller/predicates/has_request_parameter'); const hasParsedTextProperties = require('../controller/predicates/has_parsed_text_properties'); @@ -166,7 +169,16 @@ function addRoutes(app, peliasConfig) { hasRequestCategories ) ), - isRequestSourcesOnlyWhosOnFirst + any( + isRequestSourcesOnlyWhosOnFirst, + all( + isAdminOnlyAnalysis, + any( + isRequestSourcesUndefined, + isRequestSourcesIncludesWhosOnFirst + ) + ) + ) ); // execute placeholder if libpostal identified address parts but ids need to diff --git a/test/unit/controller/predicates/is_request_sources_includes_whosonfirst.js b/test/unit/controller/predicates/is_request_sources_includes_whosonfirst.js new file mode 100644 index 00000000..d3e51004 --- /dev/null +++ b/test/unit/controller/predicates/is_request_sources_includes_whosonfirst.js @@ -0,0 +1,92 @@ +const _ = require('lodash'); +const is_request_sources_includes_whosonfirst = require('../../../../controller/predicates/is_request_sources_includes_whosonfirst'); + +module.exports.tests = {}; + +module.exports.tests.interface = (test, common) => { + test('valid interface', (t) => { + t.ok(_.isFunction(is_request_sources_includes_whosonfirst), 'is_request_sources_includes_whosonfirst is a function'); + t.end(); + }); +}; + +module.exports.tests.true_conditions = (test, common) => { + test('sources includes \'whosonfirst\' should return true', (t) => { + const req = { + clean: { + sources: [ + 'whosonfirst', + 'not whosonfirst' + ] + } + }; + + t.ok(is_request_sources_includes_whosonfirst(req)); + t.end(); + + }); + + test('empty req.clean.sources should return false', (t) => { + const req = { + clean: { + sources: [] + } + }; + + t.notOk(is_request_sources_includes_whosonfirst(req)); + t.end(); + + }); + +}; + +module.exports.tests.false_conditions = (test, common) => { + test('undefined req should return false', (t) => { + t.notOk(is_request_sources_includes_whosonfirst(undefined)); + t.end(); + + }); + + test('undefined req.clean should return false', (t) => { + const req = {}; + + t.notOk(is_request_sources_includes_whosonfirst(req)); + t.end(); + + }); + + test('undefined req.clean.sources should return false', (t) => { + const req = { + clean: {} + }; + + t.notOk(is_request_sources_includes_whosonfirst(req)); + t.end(); + + }); + + test('sources not \'whosonfirst\' should return false', (t) => { + const req = { + clean: { + sources: [ + 'not whosonfirst' + ] + } + }; + + t.notOk(is_request_sources_includes_whosonfirst(req)); + t.end(); + + }) + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`GET /is_request_sources_includes_whosonfirst ${name}`, testFunction); + } + + for( const testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/controller/predicates/is_request_sources_undefined.js b/test/unit/controller/predicates/is_request_sources_undefined.js new file mode 100644 index 00000000..d7486bb8 --- /dev/null +++ b/test/unit/controller/predicates/is_request_sources_undefined.js @@ -0,0 +1,78 @@ +const _ = require('lodash'); +const is_request_sources_undefined = require('../../../../controller/predicates/is_request_sources_undefined'); + +module.exports.tests = {}; + +module.exports.tests.interface = (test, common) => { + test('valid interface', (t) => { + t.ok(_.isFunction(is_request_sources_undefined), 'is_request_sources_undefined is a function'); + t.end(); + }); +}; + +module.exports.tests.true_conditions = (test, common) => { + test('undefined req should return true', (t) => { + + t.ok(is_request_sources_undefined(undefined)); + t.end(); + + }); + + test('undefined req.clean should return true', (t) => { + const req = {}; + + t.ok(is_request_sources_undefined(req)); + t.end(); + + }); + + test('undefined req.clean.sources should return true', (t) => { + const req = { + clean: {} + }; + + t.ok(is_request_sources_undefined(req)); + t.end(); + + }); + + test('empty req.clean.sources should return true', (t) => { + const req = { + clean: { + sources: [] + } + }; + + t.ok(is_request_sources_undefined(req)); + t.end(); + + }); + +}; + +module.exports.tests.false_conditions = (test, common) => { + test('sources not empty should return false', (t) => { + const req = { + clean: { + sources: [ + 'not empty' + ] + } + }; + + t.notOk(is_request_sources_undefined(req)); + t.end(); + + }); + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`GET /is_request_sources_undefined ${name}`, testFunction); + } + + for( const testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/run.js b/test/unit/run.js index 404cd86b..ae93bd4f 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -29,7 +29,9 @@ var tests = [ require('./controller/predicates/is_admin_only_analysis'), require('./controller/predicates/is_coarse_reverse'), require('./controller/predicates/is_only_non_admin_layers'), + require('./controller/predicates/is_request_sources_includes_whosonfirst'), require('./controller/predicates/is_request_sources_only_whosonfirst'), + require('./controller/predicates/is_request_sources_undefined'), require('./helper/debug'), require('./helper/diffPlaces'), require('./helper/fieldValue'),