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.

133 lines
3.6 KiB

const proxyquire = require('proxyquire').noCallThru();
const mock_logger = require('pelias-mock-logger');
module.exports.tests = {};
module.exports.tests.sanitize = (test, common) => {
test('verify that no sanitizers were called when should_execute returns false', (t) => {
t.plan(1);
const logger = mock_logger();
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
// were all called correctly
const defer_to_addressit = proxyquire('../../../sanitizer/defer_to_addressit', {
'../sanitizer/_text_addressit': () => {
t.fail('_text_addressit should not have been called');
},
'pelias-logger': logger
})(() => false);
defer_to_addressit({}, {}, () => {
t.equals(logger.getInfoMessages().length, 0);
t.end();
});
});
test('verify that _text_addressit sanitizer was called when should_execute returns true', (t) => {
t.plan(2);
const logger = mock_logger();
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
// were all called correctly
const defer_to_addressit = proxyquire('../../../sanitizer/defer_to_addressit', {
'../sanitizer/_text_addressit': () => {
t.pass('_text_addressit should have been called');
return { errors: [], warnings: [] };
},
'pelias-logger': logger,
'../helper/logging': {
isDNT: () => false
}
})(() => true);
const req = {
path: '/v1/search',
clean: {
text: 'this is the query text'
}
};
defer_to_addressit(req, {}, () => {
t.deepEquals(logger.getInfoMessages(), ['fallback queryText: this is the query text']);
t.end();
});
});
test('query should not be logged if path != \'/v1/search\'', (t) => {
t.plan(2);
const logger = mock_logger();
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
// were all called correctly
const defer_to_addressit = proxyquire('../../../sanitizer/defer_to_addressit', {
'../sanitizer/_text_addressit': () => {
t.pass('_text_addressit should have been called');
return { errors: [], warnings: [] };
},
'pelias-logger': logger
})(() => true);
const req = {
path: 'not /v1/search',
clean: {
text: 'this is the query text'
}
};
defer_to_addressit(req, {}, () => {
t.deepEquals(logger.getInfoMessages(), []);
t.end();
});
});
test('query should be logged as [text removed] if private', (t) => {
t.plan(2);
const logger = mock_logger();
// rather than re-verify the functionality of all the sanitizers, this test just verifies that they
// were all called correctly
const defer_to_addressit = proxyquire('../../../sanitizer/defer_to_addressit', {
'../sanitizer/_text_addressit': () => {
t.pass('_text_addressit should have been called');
return { errors: [], warnings: [] };
},
'pelias-logger': logger,
'../helper/logging': {
isDNT: () => true
}
})(() => true);
const req = {
path: '/v1/search',
clean: {
text: 'this is the query text'
}
};
defer_to_addressit(req, {}, () => {
t.deepEquals(logger.getInfoMessages(), ['fallback queryText: [text removed]']);
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape(`SANITIZE /defer_to_addressit ${name}`, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};