|
|
@ -1,256 +1,235 @@ |
|
|
|
|
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
var fs = require('fs'), |
|
|
|
const setup = require('../../../middleware/changeLanguage'); |
|
|
|
tmp = require('tmp'), |
|
|
|
|
|
|
|
setup = require('../../../middleware/changeLanguage'); |
|
|
|
|
|
|
|
const proxyquire = require('proxyquire').noCallThru(); |
|
|
|
const proxyquire = require('proxyquire').noCallThru(); |
|
|
|
|
|
|
|
const _ = require('lodash'); |
|
|
|
// load middleware using the default pelias config
|
|
|
|
|
|
|
|
var load = function(){ |
|
|
|
|
|
|
|
// adapter is driven by config
|
|
|
|
|
|
|
|
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
|
|
|
|
|
|
|
fs.writeFileSync( tmpfile, '{}', { encoding: 'utf8' } ); |
|
|
|
|
|
|
|
process.env.PELIAS_CONFIG = tmpfile; |
|
|
|
|
|
|
|
var middleware = setup(); |
|
|
|
|
|
|
|
delete process.env.PELIAS_CONFIG; |
|
|
|
|
|
|
|
return middleware; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.tests = {}; |
|
|
|
module.exports.tests = {}; |
|
|
|
|
|
|
|
|
|
|
|
module.exports.tests.interface = function(test, common) { |
|
|
|
module.exports.tests.interface = (test, common) => { |
|
|
|
test('valid interface', function(t) { |
|
|
|
test('valid interface', t => { |
|
|
|
var middleware = load(); |
|
|
|
t.equal(typeof setup, 'function', 'setup is a function'); |
|
|
|
t.equal(typeof middleware, 'function', 'middleware is a function'); |
|
|
|
t.equal(typeof setup(), 'function', 'setup returns a controller'); |
|
|
|
t.equal(middleware.length, 3, 'middleware is a function'); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
t.end(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
module.exports.tests.isLanguageChangeRequired = function(test, common) { |
|
|
|
module.exports.tests.early_exit_conditions = (test, common) => { |
|
|
|
test('invalid query - null req/res', function(t) { |
|
|
|
test('should_execute returning false should not call service', t => { |
|
|
|
var middleware = load(); |
|
|
|
t.plan(2, 'should_execute will assert 2 things'); |
|
|
|
middleware(null, null, t.end); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('invalid query - no results', function(t) { |
|
|
|
|
|
|
|
var req = { language: { iso6393: 'spa' } }; |
|
|
|
|
|
|
|
var res = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var middleware = load(); |
|
|
|
const service = () => { |
|
|
|
middleware(req, res, function(){ |
|
|
|
t.fail('service should not have been called'); |
|
|
|
t.deepEqual( req, { language: { iso6393: 'spa' } } ); |
|
|
|
}; |
|
|
|
t.deepEqual( res, {} ); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('invalid query - empty results', function(t) { |
|
|
|
const should_execute = (req, res) => { |
|
|
|
var req = { language: { iso6393: 'spa' } }; |
|
|
|
t.deepEquals(req, { a: 1 }); |
|
|
|
var res = { data: [] }; |
|
|
|
t.deepEquals(res, { b: 2 }); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var middleware = load(); |
|
|
|
const controller = setup(service, should_execute); |
|
|
|
middleware(req, res, function(){ |
|
|
|
|
|
|
|
t.deepEqual( req, { language: { iso6393: 'spa' } } ); |
|
|
|
|
|
|
|
t.deepEqual( res, { data: [] } ); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('invalid query - no target language', function(t) { |
|
|
|
controller({ a: 1 }, { b: 2 }, () => { }); |
|
|
|
var req = {}; |
|
|
|
|
|
|
|
var res = { data: [] }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var middleware = load(); |
|
|
|
|
|
|
|
middleware(req, res, function(){ |
|
|
|
|
|
|
|
t.deepEqual( req, {} ); |
|
|
|
|
|
|
|
t.deepEqual( res, { data: [] } ); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// check the service is called and response mapped correctly
|
|
|
|
module.exports.tests.error_conditions = (test, common) => { |
|
|
|
module.exports.tests.miss = function(test, common) { |
|
|
|
test('service error should log and call next', t => { |
|
|
|
test('miss', function(t) { |
|
|
|
// (2) req/res were passed to service
|
|
|
|
|
|
|
|
// (1) error was logged
|
|
|
|
|
|
|
|
// (1) res was not modified
|
|
|
|
|
|
|
|
t.plan(4); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const service = (req, res, callback) => { |
|
|
|
|
|
|
|
t.deepEquals(req, { a: 1 } ); |
|
|
|
|
|
|
|
t.deepEquals(res, { b: 2 } ); |
|
|
|
|
|
|
|
callback('this is an error'); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var req = { language: { iso6393: 'spa' } }; |
|
|
|
const logger = require('pelias-mock-logger')(); |
|
|
|
var res = { data: [ |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
layer: 'locality', |
|
|
|
|
|
|
|
name: { default: 'London' }, |
|
|
|
|
|
|
|
parent: { |
|
|
|
|
|
|
|
locality_id: [ 101750367 ], |
|
|
|
|
|
|
|
locality: [ 'London' ] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
layer: 'example', |
|
|
|
|
|
|
|
name: { default: 'London' }, |
|
|
|
|
|
|
|
parent: { |
|
|
|
|
|
|
|
locality_id: [ 101735809 ], |
|
|
|
|
|
|
|
locaity: [ 'London' ] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
]}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var middleware = load(); |
|
|
|
const controller = proxyquire('../../../middleware/changeLanguage', { |
|
|
|
|
|
|
|
'pelias-logger': logger |
|
|
|
|
|
|
|
})(service, () => true); |
|
|
|
|
|
|
|
|
|
|
|
// mock out the transport
|
|
|
|
const req = { a: 1 }; |
|
|
|
middleware.transport.query = function mock( ids, cb ){ |
|
|
|
const res = { b: 2 }; |
|
|
|
t.deepEqual( ids, [ '101735809', '101750367' ] ); |
|
|
|
|
|
|
|
t.equal( typeof cb, 'function' ); |
|
|
|
|
|
|
|
cb( 'error' ); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
middleware(req, res, function(){ |
|
|
|
controller(req, res, () => { |
|
|
|
t.deepEqual( res, { data: [ |
|
|
|
t.ok(logger.isErrorMessage('this is an error')); |
|
|
|
{ |
|
|
|
t.deepEquals(res, { b: 2 }, 'res should not have been modified'); |
|
|
|
layer: 'locality', |
|
|
|
|
|
|
|
name: { default: 'London' }, |
|
|
|
|
|
|
|
parent: { |
|
|
|
|
|
|
|
locality_id: [ 101750367 ], |
|
|
|
|
|
|
|
locality: [ 'London' ] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
layer: 'example', |
|
|
|
|
|
|
|
name: { default: 'London' }, |
|
|
|
|
|
|
|
parent: { |
|
|
|
|
|
|
|
locality_id: [ 101735809 ], |
|
|
|
|
|
|
|
locaity: [ 'London' ] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
]}); |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check the service is called and response mapped correctly
|
|
|
|
}; |
|
|
|
module.exports.tests.hit = function(test, common) { |
|
|
|
|
|
|
|
test('hit', function(t) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var req = { language: { iso6393: 'spa' } }; |
|
|
|
module.exports.tests.success_conditions = (test, common) => { |
|
|
|
var res = { data: [ |
|
|
|
test('translations should be mapped in', t => { |
|
|
|
{ |
|
|
|
// (2) req/res were passed to service
|
|
|
|
layer: 'locality', |
|
|
|
// (1) error was logged
|
|
|
|
name: { default: 'London' }, |
|
|
|
// (1) res was not modified
|
|
|
|
parent: { |
|
|
|
// t.plan(4);
|
|
|
|
locality_id: [ 101750367 ], |
|
|
|
|
|
|
|
locality: [ 'London' ] |
|
|
|
const service = (req, res, callback) => { |
|
|
|
|
|
|
|
const response = { |
|
|
|
|
|
|
|
'1': { |
|
|
|
|
|
|
|
names: { |
|
|
|
|
|
|
|
'requested language': [ |
|
|
|
|
|
|
|
'replacement name for layer1' |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
// this should be ignored
|
|
|
|
|
|
|
|
'another language': [ |
|
|
|
|
|
|
|
'name in another language' |
|
|
|
|
|
|
|
] |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
'2': { |
|
|
|
layer: 'example', |
|
|
|
names: { |
|
|
|
name: { default: 'London' }, |
|
|
|
'requested language': [ |
|
|
|
parent: { |
|
|
|
'replacement name for layer2', |
|
|
|
locality_id: [ 101735809 ], |
|
|
|
// this should be ignored
|
|
|
|
locality: [ 'London' ] |
|
|
|
'another replacement name for layer2' |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
'3': { |
|
|
|
|
|
|
|
names: { |
|
|
|
|
|
|
|
'requested language': [ |
|
|
|
|
|
|
|
'replacement name 1 for layer3' |
|
|
|
|
|
|
|
] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
'4': { |
|
|
|
|
|
|
|
names: { |
|
|
|
|
|
|
|
'requested language': [ |
|
|
|
|
|
|
|
'replacement name 2 for layer3' |
|
|
|
|
|
|
|
] |
|
|
|
} |
|
|
|
} |
|
|
|
]}; |
|
|
|
}, |
|
|
|
|
|
|
|
'10': { |
|
|
|
var middleware = load(); |
|
|
|
// has names but not in the requested language
|
|
|
|
|
|
|
|
names: { |
|
|
|
// mock out the transport
|
|
|
|
'another language': [ |
|
|
|
middleware.transport.query = function mock( ids, cb ){ |
|
|
|
'replacement name for layer4' |
|
|
|
t.deepEqual( ids, [ '101735809', '101750367' ] ); |
|
|
|
] |
|
|
|
t.equal( typeof cb, 'function' ); |
|
|
|
|
|
|
|
cb( null, { |
|
|
|
|
|
|
|
'101750367': { |
|
|
|
|
|
|
|
'names': { |
|
|
|
|
|
|
|
'default':['London'], |
|
|
|
|
|
|
|
'chi':['倫敦'], |
|
|
|
|
|
|
|
'spa':['Londres'], |
|
|
|
|
|
|
|
'eng':['London'], |
|
|
|
|
|
|
|
'hin':['लंदन'], |
|
|
|
|
|
|
|
'ara':['لندن'], |
|
|
|
|
|
|
|
'por':['Londres'], |
|
|
|
|
|
|
|
'ben':['লন্ডন'], |
|
|
|
|
|
|
|
'rus':['Лондон'], |
|
|
|
|
|
|
|
'jpn':['ロンドン'], |
|
|
|
|
|
|
|
'kor':['런던'] |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
'101735809': { |
|
|
|
'11': { |
|
|
|
'names':{ |
|
|
|
// no names
|
|
|
|
'default':['London'], |
|
|
|
} |
|
|
|
'eng':['London'] |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
callback(null, response); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const logger = require('pelias-mock-logger')(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const controller = proxyquire('../../../middleware/changeLanguage', { |
|
|
|
|
|
|
|
'pelias-logger': logger |
|
|
|
|
|
|
|
})(service, () => true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const req = { |
|
|
|
|
|
|
|
clean: { |
|
|
|
|
|
|
|
lang: { |
|
|
|
|
|
|
|
iso6393: 'requested language' |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
middleware(req, res, function(){ |
|
|
|
const res = { |
|
|
|
t.deepEqual( res, { data: [ |
|
|
|
data: [ |
|
|
|
|
|
|
|
// doc with 2 layer names that will be changed
|
|
|
|
{ |
|
|
|
{ |
|
|
|
layer: 'locality', |
|
|
|
name: { |
|
|
|
name: { default: 'Londres' }, |
|
|
|
default: 'original name for 1st result' |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
layer: 'layer1', |
|
|
|
parent: { |
|
|
|
parent: { |
|
|
|
locality_id: [ 101750367 ], |
|
|
|
layer1_id: ['1'], |
|
|
|
locality: [ 'Londres' ] |
|
|
|
layer1: ['original name for layer1'], |
|
|
|
|
|
|
|
layer2_id: ['2'], |
|
|
|
|
|
|
|
layer2: ['original name for layer2'] |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
// not sure how this would sneak in but check anyway
|
|
|
|
|
|
|
|
undefined, |
|
|
|
|
|
|
|
// doc w/o parent
|
|
|
|
|
|
|
|
{}, |
|
|
|
|
|
|
|
// doc with only 1 layer name that will be changed and no default name change
|
|
|
|
{ |
|
|
|
{ |
|
|
|
layer: 'example', |
|
|
|
name: { |
|
|
|
name: { default: 'London' }, |
|
|
|
default: 'original name for 2nd result' |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
layer: 'layer10', |
|
|
|
parent: { |
|
|
|
parent: { |
|
|
|
locality_id: [ 101735809 ], |
|
|
|
layer3_id: ['3', '4'], |
|
|
|
locality: [ 'London' ] |
|
|
|
layer3: ['original name 1 for layer3', 'original name 2 for layer3'], |
|
|
|
|
|
|
|
// requested language not found for this id
|
|
|
|
|
|
|
|
layer10_id: ['10'], |
|
|
|
|
|
|
|
layer10: ['original name for layer10'], |
|
|
|
|
|
|
|
// no names for this id
|
|
|
|
|
|
|
|
layer11_id: ['11'], |
|
|
|
|
|
|
|
layer11: ['original name for layer11'], |
|
|
|
|
|
|
|
// no translations for this id
|
|
|
|
|
|
|
|
layer12_id: ['12'], |
|
|
|
|
|
|
|
layer12: ['original name for layer12'], |
|
|
|
|
|
|
|
// undefined id, will be skipped
|
|
|
|
|
|
|
|
layer13_id: [undefined], |
|
|
|
|
|
|
|
layer13: ['original name for layer13'] |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
]}); |
|
|
|
] |
|
|
|
t.end(); |
|
|
|
}; |
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('empty array name translation should not change the value', t => { |
|
|
|
controller(req, res, () => { |
|
|
|
t.plan(2); |
|
|
|
t.ok(logger.isDebugMessage('[language] [debug] missing translation requested language 10')); |
|
|
|
|
|
|
|
t.ok(logger.isDebugMessage('[language] [debug] missing translation requested language 11')); |
|
|
|
|
|
|
|
t.ok(logger.isDebugMessage('[language] [debug] failed to find translations for 12')); |
|
|
|
|
|
|
|
|
|
|
|
const req = { language: { iso6393: 'ISO3 value' } }; |
|
|
|
t.notOk(logger.hasErrorMessages(), 'there shouldn\'t be any error messages'); |
|
|
|
const res = { |
|
|
|
|
|
|
|
|
|
|
|
t.deepEquals(res, { |
|
|
|
data: [ |
|
|
|
data: [ |
|
|
|
{ |
|
|
|
{ |
|
|
|
layer: 'locality', |
|
|
|
name: { |
|
|
|
name: { default: 'original name' }, |
|
|
|
default: 'replacement name for layer1' |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
layer: 'layer1', |
|
|
|
parent: { |
|
|
|
parent: { |
|
|
|
locality_id: [ 123 ], |
|
|
|
layer1_id: ['1'], |
|
|
|
locality: [ 'original name' ] |
|
|
|
layer1: ['replacement name for layer1'], |
|
|
|
} |
|
|
|
layer2_id: ['2'], |
|
|
|
} |
|
|
|
layer2: ['replacement name for layer2'] |
|
|
|
] |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const changeLanguage = proxyquire('../../../middleware/changeLanguage', { |
|
|
|
|
|
|
|
'../service/language': { |
|
|
|
|
|
|
|
findById: () => ({ |
|
|
|
|
|
|
|
query: (ids, callback) => { |
|
|
|
|
|
|
|
t.deepEquals(ids, ['123']); |
|
|
|
|
|
|
|
callback(null, { |
|
|
|
|
|
|
|
'123': { |
|
|
|
|
|
|
|
'names': { |
|
|
|
|
|
|
|
'ISO3 value':[] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
})(); |
|
|
|
}, |
|
|
|
|
|
|
|
undefined, |
|
|
|
changeLanguage(req, res, () => { |
|
|
|
{}, |
|
|
|
t.deepEqual( res, { data: [ |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
layer: 'locality', |
|
|
|
|
|
|
|
name: { |
|
|
|
name: { |
|
|
|
default: 'original name' |
|
|
|
default: 'original name for 2nd result' |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
layer: 'layer10', |
|
|
|
parent: { |
|
|
|
parent: { |
|
|
|
locality_id: [ 123 ], |
|
|
|
layer3_id: ['3', '4'], |
|
|
|
locality: [ 'original name' ] |
|
|
|
layer3: ['replacement name 1 for layer3', 'replacement name 2 for layer3'], |
|
|
|
|
|
|
|
layer10_id: ['10'], |
|
|
|
|
|
|
|
layer10: ['original name for layer10'], |
|
|
|
|
|
|
|
layer11_id: ['11'], |
|
|
|
|
|
|
|
layer11: ['original name for layer11'], |
|
|
|
|
|
|
|
layer12_id: ['12'], |
|
|
|
|
|
|
|
layer12: ['original name for layer12'], |
|
|
|
|
|
|
|
layer13_id: [undefined], |
|
|
|
|
|
|
|
layer13: ['original name for layer13'] |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
]}); |
|
|
|
] |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -258,13 +237,13 @@ module.exports.tests.hit = function(test, common) { |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
module.exports.all = function (tape, common) { |
|
|
|
module.exports.all = (tape, common) => { |
|
|
|
|
|
|
|
|
|
|
|
function test(name, testFunction) { |
|
|
|
function test(name, testFunction) { |
|
|
|
return tape('[middleware] changeLanguage: ' + name, testFunction); |
|
|
|
return tape(`GET /changeLanguage ${name}`, testFunction); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for( var testCase in module.exports.tests ){ |
|
|
|
for( const testCase in module.exports.tests ){ |
|
|
|
module.exports.tests[testCase](test, common); |
|
|
|
module.exports.tests[testCase](test, common); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|