From 44896d7569a97313ca2ddab79d9bde3240d0c893 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Thu, 13 Jul 2017 16:17:47 -0400 Subject: [PATCH] add predicate that checks for sources=['whosonfirst'] --- .../is_request_sources_only_whosonfirst.js | 3 + .../is_request_sources_only_whosonfirst.js | 115 ++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 controller/predicates/is_request_sources_only_whosonfirst.js create mode 100644 test/unit/controller/predicates/is_request_sources_only_whosonfirst.js diff --git a/controller/predicates/is_request_sources_only_whosonfirst.js b/controller/predicates/is_request_sources_only_whosonfirst.js new file mode 100644 index 00000000..855c1197 --- /dev/null +++ b/controller/predicates/is_request_sources_only_whosonfirst.js @@ -0,0 +1,3 @@ +const _ = require('lodash'); + +module.exports = (req, res) => _.isEqual(_.get(req, 'clean.sources', []), ['whosonfirst']); diff --git a/test/unit/controller/predicates/is_request_sources_only_whosonfirst.js b/test/unit/controller/predicates/is_request_sources_only_whosonfirst.js new file mode 100644 index 00000000..bbf1aa44 --- /dev/null +++ b/test/unit/controller/predicates/is_request_sources_only_whosonfirst.js @@ -0,0 +1,115 @@ +'use strict'; + +const _ = require('lodash'); +const is_request_sources_only_whosonfirst = require('../../../../controller/predicates/is_request_sources_only_whosonfirst'); + +module.exports.tests = {}; + +module.exports.tests.interface = (test, common) => { + test('valid interface', (t) => { + t.ok(_.isFunction(is_request_sources_only_whosonfirst), 'is_request_sources_only_whosonfirst is a function'); + t.end(); + }); +}; + +module.exports.tests.true_conditions = (test, common) => { + test('sources only \'whosonfirst\' should return true', (t) => { + const req = { + clean: { + sources: [ + 'whosonfirst' + ] + } + }; + + t.ok(is_request_sources_only_whosonfirst(req)); + t.end(); + + }); + +}; + +module.exports.tests.false_conditions = (test, common) => { + test('undefined req should return false', (t) => { + const req = { + clean: { + sources: [ + 'not whosonfirst' + ] + } + }; + + t.notOk(is_request_sources_only_whosonfirst(req)); + t.end(); + + }); + + test('undefined req.clean should return false', (t) => { + const req = {}; + + t.notOk(is_request_sources_only_whosonfirst(req)); + t.end(); + + }); + + test('undefined req.clean.sources should return false', (t) => { + const req = { + clean: {} + }; + + t.notOk(is_request_sources_only_whosonfirst(req)); + t.end(); + + }); + + test('empty req.clean.sources should return false', (t) => { + const req = { + clean: { + sources: [] + } + }; + + t.notOk(is_request_sources_only_whosonfirst(req)); + t.end(); + + }); + + test('sources not \'whosonfirst\' should return false', (t) => { + const req = { + clean: { + sources: [ + 'not whosonfirst' + ] + } + }; + + t.notOk(is_request_sources_only_whosonfirst(req)); + t.end(); + + }); + + test('sources other than \'whosonfirst\' should return false', (t) => { + const req = { + clean: { + sources: [ + 'whosonfirst', 'not whosonfirst' + ] + } + }; + + t.notOk(is_request_sources_only_whosonfirst(req)); + t.end(); + + }); + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`GET /is_request_sources_only_whosonfirst ${name}`, testFunction); + } + + for( const testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +};