mirror of https://github.com/pelias/api.git
Peter Johnson
10 years ago
41 changed files with 1831 additions and 275 deletions
@ -0,0 +1,41 @@
|
||||
|
||||
var service = { mget: require('../service/mget') }; |
||||
var geojsonify = require('../helper/geojsonify').search; |
||||
|
||||
function setup( backend ){ |
||||
|
||||
// allow overriding of dependencies
|
||||
backend = backend || require('../src/backend'); |
||||
|
||||
function controller( req, res, next ){ |
||||
|
||||
var query = req.clean.ids.map( function(id) { |
||||
return { |
||||
_index: 'pelias', |
||||
_type: id.type, |
||||
_id: id.id |
||||
}; |
||||
}); |
||||
|
||||
service.mget( backend, query, function( err, docs ){ |
||||
|
||||
// error handler
|
||||
if( err ){ return next( err ); } |
||||
|
||||
// convert docs to geojson
|
||||
var geojson = geojsonify( docs ); |
||||
|
||||
// response envelope
|
||||
geojson.date = new Date().getTime(); |
||||
|
||||
// respond
|
||||
return res.status(200).json( geojson ); |
||||
|
||||
}); |
||||
|
||||
} |
||||
|
||||
return controller; |
||||
} |
||||
|
||||
module.exports = setup; |
@ -0,0 +1,40 @@
|
||||
|
||||
var service = { suggest: require('../service/suggest') }; |
||||
var geojsonify = require('../helper/geojsonify').suggest; |
||||
|
||||
function setup( backend, query ){ |
||||
|
||||
// allow overriding of dependencies
|
||||
backend = backend || require('../src/backend'); |
||||
query = query || require('../query/suggest'); |
||||
|
||||
function controller( req, res, next ){ |
||||
|
||||
// backend command
|
||||
var cmd = { |
||||
index: 'pelias', |
||||
body: query( req.clean ) |
||||
}; |
||||
|
||||
// query backend
|
||||
service.suggest( backend, cmd, function( err, docs ){ |
||||
|
||||
// error handler
|
||||
if( err ){ return next( err ); } |
||||
|
||||
// convert docs to geojson
|
||||
var geojson = geojsonify( docs ); |
||||
|
||||
// response envelope
|
||||
geojson.date = new Date().getTime(); |
||||
|
||||
// respond
|
||||
return res.status(200).json( geojson ); |
||||
}); |
||||
|
||||
} |
||||
|
||||
return controller; |
||||
} |
||||
|
||||
module.exports = setup; |
@ -0,0 +1,97 @@
|
||||
# valid doc query |
||||
|
||||
*Generated: Thu Nov 06 2014 11:44:19 GMT-0500 (EST)* |
||||
## Request |
||||
```javascript |
||||
{ |
||||
"protocol": "http:", |
||||
"host": "localhost", |
||||
"method": "GET", |
||||
"port": 3100, |
||||
"path": "/doc?id=geoname:4221195&id=geoname:4193595" |
||||
} |
||||
``` |
||||
|
||||
## Response |
||||
```javascript |
||||
Status: 200 |
||||
{ |
||||
"x-powered-by": "mapzen", |
||||
"charset": "utf8", |
||||
"cache-control": "public,max-age=60", |
||||
"server": "Pelias/0.0.0", |
||||
"access-control-allow-origin": "*", |
||||
"access-control-allow-methods": "GET", |
||||
"access-control-allow-headers": "X-Requested-With,content-type", |
||||
"access-control-allow-credentials": "true", |
||||
"content-type": "application/json; charset=utf-8", |
||||
"content-length": "555", |
||||
"etag": "W/\"22b-dd736629\"", |
||||
"date": "Thu, 06 Nov 2014 16:44:19 GMT", |
||||
"connection": "close" |
||||
} |
||||
``` |
||||
```javascript |
||||
{ |
||||
"type": "FeatureCollection", |
||||
"features": [ |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.9207, |
||||
34.36094 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"name": "Sanders Grove Cemetery", |
||||
"admin0": "United States", |
||||
"admin1": "Georgia", |
||||
"admin2": "Hart County", |
||||
"text": "Sanders Grove Cemetery, Hart County, Georgia" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-83.94213, |
||||
33.32262 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"name": "Etheredge Cemetery", |
||||
"admin0": "United States", |
||||
"admin1": "Georgia", |
||||
"admin2": "Butts County", |
||||
"text": "Etheredge Cemetery, Butts County, Georgia" |
||||
} |
||||
} |
||||
], |
||||
"date": 1415292259726 |
||||
} |
||||
``` |
||||
|
||||
## Tests |
||||
|
||||
### âś“ 200 ok |
||||
```javascript |
||||
response.statusCode.should.equal 200 |
||||
``` |
||||
|
||||
### âś“ valid geojson |
||||
```javascript |
||||
json.type.should.equal 'FeatureCollection' |
||||
json.features.should.be.instanceof Array |
||||
``` |
||||
|
||||
### âś“ valid response |
||||
```javascript |
||||
now = new Date().getTime() |
||||
should.exist json |
||||
should.not.exist json.error |
||||
json.date.should.be.within now-5000, now+5000 |
||||
``` |
||||
|
@ -0,0 +1,80 @@
|
||||
# valid doc query |
||||
|
||||
*Generated: Thu Nov 06 2014 11:44:20 GMT-0500 (EST)* |
||||
## Request |
||||
```javascript |
||||
{ |
||||
"protocol": "http:", |
||||
"host": "localhost", |
||||
"method": "GET", |
||||
"port": 3100, |
||||
"path": "/doc?id=geoname:4221195" |
||||
} |
||||
``` |
||||
|
||||
## Response |
||||
```javascript |
||||
Status: 200 |
||||
{ |
||||
"x-powered-by": "mapzen", |
||||
"charset": "utf8", |
||||
"cache-control": "public,max-age=60", |
||||
"server": "Pelias/0.0.0", |
||||
"access-control-allow-origin": "*", |
||||
"access-control-allow-methods": "GET", |
||||
"access-control-allow-headers": "X-Requested-With,content-type", |
||||
"access-control-allow-credentials": "true", |
||||
"content-type": "application/json; charset=utf-8", |
||||
"content-length": "311", |
||||
"etag": "W/\"137-1644173e\"", |
||||
"date": "Thu, 06 Nov 2014 16:44:20 GMT", |
||||
"connection": "close" |
||||
} |
||||
``` |
||||
```javascript |
||||
{ |
||||
"type": "FeatureCollection", |
||||
"features": [ |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.9207, |
||||
34.36094 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"name": "Sanders Grove Cemetery", |
||||
"admin0": "United States", |
||||
"admin1": "Georgia", |
||||
"admin2": "Hart County", |
||||
"text": "Sanders Grove Cemetery, Hart County, Georgia" |
||||
} |
||||
} |
||||
], |
||||
"date": 1415292260057 |
||||
} |
||||
``` |
||||
|
||||
## Tests |
||||
|
||||
### âś“ valid geojson |
||||
```javascript |
||||
json.type.should.equal 'FeatureCollection' |
||||
json.features.should.be.instanceof Array |
||||
``` |
||||
|
||||
### âś“ valid response |
||||
```javascript |
||||
now = new Date().getTime() |
||||
should.exist json |
||||
should.not.exist json.error |
||||
json.date.should.be.within now-5000, now+5000 |
||||
``` |
||||
|
||||
### âś“ 200 ok |
||||
```javascript |
||||
response.statusCode.should.equal 200 |
||||
``` |
||||
|
@ -0,0 +1,223 @@
|
||||
# valid suggest query |
||||
|
||||
*Generated: Thu Nov 06 2014 11:44:20 GMT-0500 (EST)* |
||||
## Request |
||||
```javascript |
||||
{ |
||||
"protocol": "http:", |
||||
"host": "localhost", |
||||
"method": "GET", |
||||
"port": 3100, |
||||
"path": "/suggest/nearby?input=a&lat=29.49136&lon=-82.50622" |
||||
} |
||||
``` |
||||
|
||||
## Response |
||||
```javascript |
||||
Status: 200 |
||||
{ |
||||
"x-powered-by": "mapzen", |
||||
"charset": "utf8", |
||||
"cache-control": "public,max-age=60", |
||||
"server": "Pelias/0.0.0", |
||||
"access-control-allow-origin": "*", |
||||
"access-control-allow-methods": "GET", |
||||
"access-control-allow-headers": "X-Requested-With,content-type", |
||||
"access-control-allow-credentials": "true", |
||||
"content-type": "application/json; charset=utf-8", |
||||
"content-length": "2034", |
||||
"etag": "W/\"Do9VJ5hCbynTxDjtm5fNlg==\"", |
||||
"date": "Thu, 06 Nov 2014 16:44:19 GMT", |
||||
"connection": "close" |
||||
} |
||||
``` |
||||
```javascript |
||||
{ |
||||
"type": "FeatureCollection", |
||||
"features": [ |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.05231, |
||||
29.17998 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Abiding Hope E V Lutheran Church, Marion County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145572" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.10231, |
||||
29.21942 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Abundant Harvest Ministries, Marion County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145578" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.50622, |
||||
29.49136 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Adam, Alachua County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145612" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.75374, |
||||
35.17789 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Adams Branch, Transylvania County, North Carolina", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4452189" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.83012, |
||||
29.4783 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Adamsville Cemetery, Levy County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145634" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.01511, |
||||
35.17289 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Africa School (historical), Spartanburg County, South Carolina", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4569065" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.20426, |
||||
29.25192 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Agape Baptist Church, Marion County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145673" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.14954, |
||||
29.19248 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Agnew Cemetery, Marion County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145677" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.75429, |
||||
35.16928 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Aiken Mountain, Transylvania County, North Carolina", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4452268" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.15912, |
||||
29.47877 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Alachua County Fire Rescue Station 31, Alachua County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4152402" |
||||
} |
||||
} |
||||
], |
||||
"date": 1415292259785 |
||||
} |
||||
``` |
||||
|
||||
## Tests |
||||
|
||||
### âś“ 200 ok |
||||
```javascript |
||||
response.statusCode.should.equal 200 |
||||
``` |
||||
|
||||
### âś“ valid response |
||||
```javascript |
||||
now = new Date().getTime() |
||||
should.exist json |
||||
should.not.exist json.error |
||||
json.date.should.be.within now-5000, now+5000 |
||||
``` |
||||
|
||||
### âś“ valid geojson |
||||
```javascript |
||||
json.type.should.equal 'FeatureCollection' |
||||
json.features.should.be.instanceof Array |
||||
``` |
||||
|
@ -0,0 +1,73 @@
|
||||
// validate inputs, convert types and apply defaults
|
||||
// id generally looks like 'geoname:4163334' (type:id)
|
||||
// so, both type and id are required fields.
|
||||
|
||||
function sanitize( req ){ |
||||
|
||||
req.clean = req.clean || {}; |
||||
var params = req.query; |
||||
var indeces = require('../query/indeces'); |
||||
var delim = ':'; |
||||
|
||||
// ensure params is a valid object
|
||||
if( Object.prototype.toString.call( params ) !== '[object Object]' ){ |
||||
params = {}; |
||||
} |
||||
|
||||
var errormessage = function(fieldname, message) { |
||||
return { |
||||
'error': true, |
||||
'message': message || ('invalid param \''+ fieldname + '\': text length, must be >0') |
||||
} |
||||
}; |
||||
|
||||
if(('string' === typeof params.id && !params.id.length) || params.id === undefined){ |
||||
return errormessage('id'); |
||||
} |
||||
|
||||
if( params && params.id && params.id.length ){ |
||||
req.clean.ids = []; |
||||
params.ids = Array.isArray(params.id) ? params.id : [params.id]; |
||||
|
||||
// de-dupe
|
||||
params.ids = params.ids.filter(function(item, pos) { |
||||
return params.ids.indexOf(item) == pos; |
||||
}); |
||||
|
||||
for (var i=0; i<params.ids.length; i++) { |
||||
var thisparam = params.ids[i]; |
||||
|
||||
// basic format/ presence of ':'
|
||||
if(thisparam.indexOf(delim) === -1) { |
||||
return errormessage(null, 'invalid: must be of the format type:id for ex: \'geoname:4163334\''); |
||||
} |
||||
|
||||
var param_index = thisparam.indexOf(delim); |
||||
var type = thisparam.substring(0, param_index ); |
||||
var id = thisparam.substring(param_index + 1); |
||||
|
||||
// id text
|
||||
if('string' !== typeof id || !id.length){ |
||||
return errormessage(thisparam); |
||||
} |
||||
// type text
|
||||
if('string' !== typeof type || !type.length){ |
||||
return errormessage(thisparam); |
||||
} |
||||
// type text must be one of the indeces
|
||||
if(indeces.indexOf(type) == -1){ |
||||
return errormessage('type', type + ' is invalid. It must be one of these values - [' + indeces.join(", ") + ']'); |
||||
} |
||||
req.clean.ids.push({ |
||||
id: id, |
||||
type: type |
||||
}); |
||||
} |
||||
} |
||||
|
||||
return { 'error': false }; |
||||
|
||||
} |
||||
|
||||
// export function
|
||||
module.exports = sanitize; |
@ -0,0 +1,23 @@
|
||||
|
||||
var logger = require('../src/logger'), |
||||
_sanitize = require('../sanitiser/_sanitize'), |
||||
sanitizers = { |
||||
id: require('../sanitiser/_id') |
||||
}; |
||||
|
||||
var sanitize = function(req, cb) { _sanitize(req, sanitizers, cb); } |
||||
|
||||
// export sanitize for testing
|
||||
module.exports.sanitize = sanitize; |
||||
|
||||
// middleware
|
||||
module.exports.middleware = function( req, res, next ){ |
||||
sanitize( req, function( err, clean ){ |
||||
if( err ){ |
||||
res.status(400); // 400 Bad Request
|
||||
return next(err); |
||||
} |
||||
req.clean = clean; |
||||
next(); |
||||
}); |
||||
}; |
@ -0,0 +1,43 @@
|
||||
|
||||
/** |
||||
|
||||
query must be an array of hashes, structured like so: |
||||
|
||||
{ |
||||
_index: 'myindex', |
||||
_type: 'mytype', |
||||
_id: 'myid' |
||||
} |
||||
|
||||
**/ |
||||
|
||||
function service( backend, query, cb ){ |
||||
|
||||
// backend command
|
||||
var cmd = { |
||||
body: { |
||||
docs: query |
||||
} |
||||
}; |
||||
|
||||
// query new backend
|
||||
backend().client.mget( cmd, function( err, data ){ |
||||
|
||||
// handle backend errors
|
||||
if( err ){ return cb( err ); } |
||||
|
||||
// map returned documents
|
||||
var docs = []; |
||||
if( data && Array.isArray(data.docs) ){ |
||||
docs = data.docs.map( function( doc ){ |
||||
return doc._source; |
||||
}); |
||||
} |
||||
|
||||
// fire callback
|
||||
return cb( null, docs ); |
||||
}); |
||||
|
||||
} |
||||
|
||||
module.exports = service; |
@ -0,0 +1,30 @@
|
||||
|
||||
/** |
||||
|
||||
cmd can be any valid ES query command |
||||
|
||||
**/ |
||||
|
||||
function service( backend, cmd, cb ){ |
||||
|
||||
// query new backend
|
||||
backend().client.search( cmd, function( err, data ){ |
||||
|
||||
// handle backend errors
|
||||
if( err ){ return cb( err ); } |
||||
|
||||
// map returned documents
|
||||
var docs = []; |
||||
if( data && data.hits && data.hits.total && Array.isArray(data.hits.hits)){ |
||||
docs = data.hits.hits.map( function( hit ){ |
||||
return hit._source; |
||||
}); |
||||
} |
||||
|
||||
// fire callback
|
||||
return cb( null, docs ); |
||||
}); |
||||
|
||||
} |
||||
|
||||
module.exports = service; |
@ -0,0 +1,28 @@
|
||||
|
||||
/** |
||||
|
||||
cmd can be any valid ES suggest command |
||||
|
||||
**/ |
||||
|
||||
function service( backend, cmd, cb ){ |
||||
|
||||
// query new backend
|
||||
backend().client.suggest( cmd, function( err, data ){ |
||||
|
||||
// handle backend errors
|
||||
if( err ){ return cb( err ); } |
||||
|
||||
// map returned documents
|
||||
var docs = []; |
||||
if( data && Array.isArray( data.pelias ) && data.pelias.length ){ |
||||
docs = data['pelias'][0].options || []; |
||||
} |
||||
|
||||
// fire callback
|
||||
return cb( null, docs ); |
||||
}); |
||||
|
||||
} |
||||
|
||||
module.exports = service; |
@ -0,0 +1,16 @@
|
||||
|
||||
#> valid doc query |
||||
path: '/doc?id=geoname:4221195&id=geoname:4193595' |
||||
|
||||
#? 200 ok |
||||
response.statusCode.should.equal 200 |
||||
|
||||
#? valid response |
||||
now = new Date().getTime() |
||||
should.exist json |
||||
should.not.exist json.error |
||||
json.date.should.be.within now-5000, now+5000 |
||||
|
||||
#? valid geojson |
||||
json.type.should.equal 'FeatureCollection' |
||||
json.features.should.be.instanceof Array |
@ -0,0 +1,16 @@
|
||||
|
||||
#> valid doc query |
||||
path: '/doc?id=geoname:4221195' |
||||
|
||||
#? 200 ok |
||||
response.statusCode.should.equal 200 |
||||
|
||||
#? valid response |
||||
now = new Date().getTime() |
||||
should.exist json |
||||
should.not.exist json.error |
||||
json.date.should.be.within now-5000, now+5000 |
||||
|
||||
#? valid geojson |
||||
json.type.should.equal 'FeatureCollection' |
||||
json.features.should.be.instanceof Array |
@ -0,0 +1,16 @@
|
||||
|
||||
#> valid suggest query |
||||
path: '/suggest/nearby?input=a&lat=29.49136&lon=-82.50622' |
||||
|
||||
#? 200 ok |
||||
response.statusCode.should.equal 200 |
||||
|
||||
#? valid response |
||||
now = new Date().getTime() |
||||
should.exist json |
||||
should.not.exist json.error |
||||
json.date.should.be.within now-5000, now+5000 |
||||
|
||||
#? valid geojson |
||||
json.type.should.equal 'FeatureCollection' |
||||
json.features.should.be.instanceof Array |
@ -0,0 +1,92 @@
|
||||
|
||||
var setup = require('../../../controller/doc'), |
||||
mockBackend = require('../mock/backend'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.equal(typeof setup(), 'function', 'setup returns a controller'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test controller (backend success)
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
|
||||
// expected geojson features for 'client/doc/ok/1' fixture
|
||||
var expected = [{ |
||||
type: 'Feature', |
||||
geometry: { |
||||
type: 'Point', |
||||
coordinates: [ -50.5, 100.1 ] |
||||
}, |
||||
properties: { |
||||
name: 'test name1', |
||||
admin0: 'country1', |
||||
admin1: 'state1', |
||||
admin2: 'city1' |
||||
} |
||||
}, { |
||||
type: 'Feature', |
||||
geometry: { |
||||
type: 'Point', |
||||
coordinates: [ -51.5, 100.2 ] |
||||
}, |
||||
properties: { |
||||
name: 'test name2', |
||||
admin0: 'country2', |
||||
admin1: 'state2', |
||||
admin2: 'city2' |
||||
} |
||||
}]; |
||||
|
||||
test('functional success', function(t) { |
||||
var backend = mockBackend( 'client/doc/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command'); |
||||
}); |
||||
var controller = setup( backend ); |
||||
var res = { |
||||
status: function( code ){ |
||||
t.equal(code, 200, 'status set'); |
||||
return res; |
||||
}, |
||||
json: function( json ){ |
||||
t.equal(typeof json, 'object', 'returns json'); |
||||
t.equal(typeof json.date, 'number', 'date set'); |
||||
t.equal(json.type, 'FeatureCollection', 'valid geojson'); |
||||
t.true(Array.isArray(json.features), 'features is array'); |
||||
t.deepEqual(json.features, expected, 'values correctly mapped'); |
||||
t.end(); |
||||
} |
||||
}; |
||||
controller( { clean: { ids: [ {'id' : 123, 'type': 'a' } ] } }, res ); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test controller (backend failure)
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
test('functional failure', function(t) { |
||||
var backend = mockBackend( 'client/doc/fail/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'b' } ] } }, 'correct backend command'); |
||||
}); |
||||
var controller = setup( backend ); |
||||
var next = function( message ){ |
||||
t.equal(message,'a backend error occurred','error passed to errorHandler'); |
||||
t.end(); |
||||
}; |
||||
controller( { clean: { ids: [ {'id' : 123, 'type': 'b' } ] } }, undefined, next ); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('GET /doc ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,91 @@
|
||||
|
||||
var setup = require('../../../controller/suggest'), |
||||
mockBackend = require('../mock/backend'), |
||||
mockQuery = require('../mock/query'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.equal(typeof setup(), 'function', 'setup returns a controller'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test controller (backend success)
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
|
||||
// expected geojson features for 'client/suggest/ok/1' fixture
|
||||
var expected = [{ |
||||
type: 'Feature', |
||||
geometry: { |
||||
type: 'Point', |
||||
coordinates: [ 101, -10.1 ] |
||||
}, |
||||
properties: { |
||||
id: 'mockid1', |
||||
type: 'mocktype', |
||||
value: 1 |
||||
} |
||||
}, { |
||||
type: 'Feature', |
||||
geometry: { |
||||
type: 'Point', |
||||
coordinates: [ 101, -10.1 ] |
||||
}, |
||||
properties: { |
||||
id: 'mockid2', |
||||
type: 'mocktype', |
||||
value: 2 |
||||
} |
||||
}]; |
||||
|
||||
test('functional success', function(t) { |
||||
var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); |
||||
}); |
||||
var controller = setup( backend, mockQuery() ); |
||||
var res = { |
||||
status: function( code ){ |
||||
t.equal(code, 200, 'status set'); |
||||
return res; |
||||
}, |
||||
json: function( json ){ |
||||
t.equal(typeof json, 'object', 'returns json'); |
||||
t.equal(typeof json.date, 'number', 'date set'); |
||||
t.equal(json.type, 'FeatureCollection', 'valid geojson'); |
||||
t.true(Array.isArray(json.features), 'features is array'); |
||||
t.deepEqual(json.features, expected, 'values correctly mapped'); |
||||
t.end(); |
||||
} |
||||
}; |
||||
controller( { clean: { a: 'b' } }, res ); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test controller (backend failure)
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
test('functional failure', function(t) { |
||||
var backend = mockBackend( 'client/suggest/fail/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); |
||||
}); |
||||
var controller = setup( backend, mockQuery() ); |
||||
var next = function( message ){ |
||||
t.equal(message,'a backend error occurred','error passed to errorHandler'); |
||||
t.end(); |
||||
}; |
||||
controller( { clean: { a: 'b' } }, undefined, next ); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('GET /suggest ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,159 @@
|
||||
|
||||
var doc = require('../../../sanitiser/doc'), |
||||
_sanitize = doc.sanitize, |
||||
middleware = doc.middleware, |
||||
indeces = require('../../../query/indeces'), |
||||
delimiter = ':', |
||||
defaultLengthError = function(input) { return 'invalid param \''+ input + '\': text length, must be >0' }, |
||||
defaultFormatError = 'invalid: must be of the format type:id for ex: \'geoname:4163334\'', |
||||
defaultError = 'invalid param \'id\': text length, must be >0', |
||||
defaultMissingTypeError = function(input) {
|
||||
var type = input.split(delimiter)[0]; |
||||
return type + ' is invalid. It must be one of these values - [' + indeces.join(", ") + ']'}, |
||||
defaultClean = { ids: [ { id: '123', type: 'geoname' } ] }, |
||||
sanitize = function(query, cb) { _sanitize({'query':query}, cb); }, |
||||
inputs = { |
||||
valid: [ 'geoname:1', 'osmnode:2', 'admin0:53', 'osmway:44', 'geoname:5' ], |
||||
invalid: [ ':', '', '::', 'geoname:', ':234', 'gibberish:23' ] |
||||
}; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('sanitize interface', function(t) { |
||||
t.equal(typeof sanitize, 'function', 'sanitize is a function'); |
||||
t.equal(sanitize.length, 2, 'sanitize interface'); |
||||
t.end(); |
||||
}); |
||||
test('middleware interface', function(t) { |
||||
t.equal(typeof middleware, 'function', 'middleware is a function'); |
||||
t.equal(middleware.length, 3, 'sanitizee has a valid middleware'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.sanitize_id = function(test, common) { |
||||
test('invalid input', function(t) { |
||||
inputs.invalid.forEach( function( input ){ |
||||
sanitize({ id: input }, function( err, clean ){ |
||||
switch (err) { |
||||
case defaultError: |
||||
t.equal(err, defaultError, input + ' is invalid input'); break; |
||||
case defaultLengthError(input): |
||||
t.equal(err, defaultLengthError(input), input + ' is invalid (missing id/type)'); break; |
||||
case defaultFormatError: |
||||
t.equal(err, defaultFormatError, input + ' is invalid (invalid format)'); break; |
||||
case defaultMissingTypeError(input): |
||||
t.equal(err, defaultMissingTypeError(input), input + ' is an unknown type'); break; |
||||
default: break; |
||||
} |
||||
t.equal(clean, undefined, 'clean not set'); |
||||
}); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('valid input', function(t) { |
||||
inputs.valid.forEach( function( input ){ |
||||
var input_parts = input.split(delimiter); |
||||
var expected = { ids: [ { id: input_parts[1], type: input_parts[0] } ] }; |
||||
sanitize({ id: input }, function( err, clean ){ |
||||
t.equal(err, undefined, 'no error (' + input + ')' ); |
||||
t.deepEqual(clean, expected, 'clean set correctly (' + input + ')'); |
||||
}); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
|
||||
module.exports.tests.sanitize_ids = function(test, common) { |
||||
test('invalid input', function(t) { |
||||
sanitize({ id: inputs.invalid }, function( err, clean ){ |
||||
var input = inputs.invalid[0]; // since it breaks on the first invalid element
|
||||
switch (err) { |
||||
case defaultError: |
||||
t.equal(err, defaultError, input + ' is invalid input'); break; |
||||
case defaultLengthError(input): |
||||
t.equal(err, defaultLengthError(input), input + ' is invalid (missing id/type)'); break; |
||||
case defaultFormatError: |
||||
t.equal(err, defaultFormatError, input + ' is invalid (invalid format)'); break; |
||||
case defaultMissingTypeError(input): |
||||
t.equal(err, defaultMissingTypeError(input), input + ' is an unknown type'); break; |
||||
default: break; |
||||
} |
||||
t.equal(clean, undefined, 'clean not set'); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('valid input', function(t) { |
||||
var expected={}; |
||||
expected.ids = []; |
||||
inputs.valid.forEach( function( input ){ |
||||
var input_parts = input.split(delimiter); |
||||
expected.ids.push({ id: input_parts[1], type: input_parts[0] }); |
||||
}); |
||||
sanitize({ id: inputs.valid }, function( err, clean ){ |
||||
t.equal(err, undefined, 'no error' ); |
||||
t.deepEqual(clean, expected, 'clean set correctly'); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.de_dupe = function(test, common) { |
||||
var expected = { ids: [ { id: '1', type: 'geoname' }, { id: '2', type: 'osmnode' } ] }; |
||||
test('duplicate ids', function(t) { |
||||
sanitize( { id: ['geoname:1', 'osmnode:2', 'geoname:1'] }, function( err, clean ){ |
||||
t.equal(err, undefined, 'no error' ); |
||||
t.deepEqual(clean, expected, 'clean set correctly'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.invalid_params = function(test, common) { |
||||
test('invalid params', function(t) { |
||||
sanitize( undefined, function( err, clean ){ |
||||
t.equal(err, defaultError, 'handle invalid params gracefully'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.middleware_failure = function(test, common) { |
||||
test('middleware failure', function(t) { |
||||
var res = { status: function( code ){ |
||||
t.equal(code, 400, 'status set'); |
||||
}}; |
||||
var next = function( message ){ |
||||
t.equal(message, defaultError); |
||||
t.end(); |
||||
}; |
||||
middleware( {}, res, next ); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.middleware_success = function(test, common) { |
||||
test('middleware success', function(t) { |
||||
var req = { query: { id: 'geoname' + delimiter + '123' }}; |
||||
var next = function( message ){ |
||||
t.equal(message, undefined, 'no error message set'); |
||||
t.deepEqual(req.clean, defaultClean); |
||||
t.end(); |
||||
}; |
||||
middleware( req, undefined, next ); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE /doc ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,82 @@
|
||||
|
||||
var setup = require('../../../service/mget'), |
||||
mockBackend = require('../mock/backend'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
|
||||
var expected = [ |
||||
{ |
||||
value: 1, |
||||
center_point: { lat: 100.1, lon: -50.5 }, |
||||
name: { default: 'test name1' }, |
||||
admin0: 'country1', admin1: 'state1', admin2: 'city1' |
||||
},
|
||||
{ |
||||
value: 2, |
||||
center_point: { lat: 100.2, lon: -51.5 }, |
||||
name: { default: 'test name2' }, |
||||
admin0: 'country2', admin1: 'state2', admin2: 'city2' |
||||
} |
||||
]; |
||||
|
||||
test('valid query', function(t) { |
||||
var backend = mockBackend( 'client/doc/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command'); |
||||
}); |
||||
setup( backend, [ { _id: 123, _index: 'pelias', _type: 'a' } ], function(err, data) { |
||||
t.true(Array.isArray(data), 'returns an array'); |
||||
data.forEach(function(d) { |
||||
t.true(typeof d === 'object', 'valid object'); |
||||
}); |
||||
t.deepEqual(data, expected, 'values correctly mapped') |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
|
||||
test('invalid query', function(t) { |
||||
var invalid_queries = [ |
||||
{ _id: 123, _index: 'pelias' }, |
||||
{ _id: 123, _type: 'a' }, |
||||
{ _index: 'pelias', _type: 'a' }, |
||||
{ } |
||||
]; |
||||
|
||||
var backend = mockBackend( 'client/doc/fail/1', function( cmd ){ |
||||
t.notDeepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'incorrect backend command'); |
||||
}); |
||||
invalid_queries.forEach(function(query) { |
||||
setup( backend, [ query ], function(err, data) { |
||||
t.equal(err, 'a backend error occurred','error passed to errorHandler'); |
||||
t.equal(data, undefined, 'data is undefined'); |
||||
}); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SERVICE /mget ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,82 @@
|
||||
|
||||
var setup = require('../../../service/search'), |
||||
mockBackend = require('../mock/backend'); |
||||
|
||||
var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' }; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
|
||||
var expected = [ |
||||
{ |
||||
value: 1, |
||||
center_point: { lat: 100.1, lon: -50.5 }, |
||||
name: { default: 'test name1' }, |
||||
admin0: 'country1', admin1: 'state1', admin2: 'city1' |
||||
},
|
||||
{ |
||||
value: 2, |
||||
center_point: { lat: 100.2, lon: -51.5 }, |
||||
name: { default: 'test name2' }, |
||||
admin0: 'country2', admin1: 'state2', admin2: 'city2' |
||||
} |
||||
]; |
||||
|
||||
test('valid ES query', function(t) { |
||||
var backend = mockBackend( 'client/search/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, example_valid_es_query, 'no change to the command'); |
||||
}); |
||||
setup( backend, example_valid_es_query, function(err, data) { |
||||
t.true(Array.isArray(data), 'returns an array'); |
||||
data.forEach(function(d) { |
||||
t.true(typeof d === 'object', 'valid object'); |
||||
}); |
||||
t.deepEqual(data, expected, 'values correctly mapped') |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
|
||||
test('invalid ES query', function(t) { |
||||
var invalid_queries = [ |
||||
{ }, |
||||
{ foo: 'bar' } |
||||
]; |
||||
|
||||
var backend = mockBackend( 'client/search/fail/1', function( cmd ){ |
||||
t.notDeepEqual(cmd, example_valid_es_query, 'incorrect backend command'); |
||||
}); |
||||
invalid_queries.forEach(function(query) { |
||||
setup( backend, [ query ], function(err, data) { |
||||
t.equal(err, 'a backend error occurred','error passed to errorHandler'); |
||||
t.equal(data, undefined, 'data is undefined'); |
||||
}); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SERVICE /search ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,79 @@
|
||||
|
||||
var setup = require('../../../service/suggest'), |
||||
mockBackend = require('../mock/backend'); |
||||
|
||||
var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' }; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
|
||||
var mockPayload = function(id){ |
||||
return {
|
||||
id: 'mocktype/mockid'+id, |
||||
geo: '101,-10.1' |
||||
} |
||||
}; |
||||
|
||||
var expected = [
|
||||
{ value: 1, payload: mockPayload(1) },
|
||||
{ value: 2, payload: mockPayload(2) }
|
||||
]; |
||||
|
||||
test('valid ES query', function(t) { |
||||
var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, example_valid_es_query, 'no change to the command'); |
||||
}); |
||||
setup( backend, example_valid_es_query, function(err, data) { |
||||
t.true(Array.isArray(data), 'returns an array'); |
||||
data.forEach(function(d) { |
||||
t.true(typeof d === 'object', 'valid object'); |
||||
}); |
||||
t.deepEqual(data, expected, 'values correctly mapped') |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
|
||||
test('invalid ES query', function(t) { |
||||
var invalid_queries = [ |
||||
{ }, |
||||
{ foo: 'bar' } |
||||
]; |
||||
|
||||
var backend = mockBackend( 'client/suggest/fail/1', function( cmd ){ |
||||
t.notDeepEqual(cmd, example_valid_es_query, 'incorrect backend command'); |
||||
}); |
||||
invalid_queries.forEach(function(query) { |
||||
setup( backend, [ query ], function(err, data) { |
||||
t.equal(err, 'a backend error occurred','error passed to errorHandler'); |
||||
t.equal(data, undefined, 'data is undefined'); |
||||
}); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SERVICE /suggest ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue