2015-03-24 00:54:37 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('underscore');
|
2012-07-18 17:00:24 +08:00
|
|
|
|
2015-09-17 00:09:39 +08:00
|
|
|
var RedisPool = require('redis-mpool');
|
|
|
|
var cartodbRedis = require('cartodb-redis');
|
|
|
|
var PgConnection = require('../../../lib/cartodb/backends/pg_connection');
|
|
|
|
var AuthApi = require('../../../lib/cartodb/api/auth_api');
|
|
|
|
var TemplateMaps = require('../../../lib/cartodb/backends/template_maps');
|
|
|
|
|
2017-09-25 19:40:22 +08:00
|
|
|
const cleanUpQueryParamsMiddleware = require('../../../lib/cartodb/middleware/context/clean-up-query-params');
|
|
|
|
const authorizeMiddleware = require('../../../lib/cartodb/middleware/context/authorize');
|
|
|
|
const dbConnSetupMiddleware = require('../../../lib/cartodb/middleware/context/db-conn-setup');
|
2017-10-02 18:09:19 +08:00
|
|
|
const localsMiddleware = require('../../../lib/cartodb/middleware/context/locals');
|
2017-09-25 19:40:22 +08:00
|
|
|
|
2015-09-17 00:09:39 +08:00
|
|
|
var windshaft = require('windshaft');
|
2012-07-18 17:00:24 +08:00
|
|
|
|
2017-09-22 23:56:47 +08:00
|
|
|
describe('prepare-context', function() {
|
2014-02-21 01:03:43 +08:00
|
|
|
|
|
|
|
var test_user = _.template(global.environment.postgres_auth_user, {user_id:1});
|
|
|
|
var test_pubuser = global.environment.postgres.user;
|
|
|
|
var test_database = test_user + '_db';
|
|
|
|
|
2017-09-25 19:40:22 +08:00
|
|
|
let cleanUpQueryParams;
|
|
|
|
let dbConnSetup;
|
|
|
|
let authorize;
|
2015-09-17 00:09:39 +08:00
|
|
|
|
|
|
|
before(function() {
|
|
|
|
var redisPool = new RedisPool(global.environment.redis);
|
|
|
|
var mapStore = new windshaft.storage.MapStore();
|
|
|
|
var metadataBackend = cartodbRedis({pool: redisPool});
|
|
|
|
var pgConnection = new PgConnection(metadataBackend);
|
|
|
|
var templateMaps = new TemplateMaps(redisPool);
|
|
|
|
var authApi = new AuthApi(pgConnection, metadataBackend, mapStore, templateMaps);
|
|
|
|
|
2017-09-25 19:40:22 +08:00
|
|
|
cleanUpQueryParams = cleanUpQueryParamsMiddleware();
|
|
|
|
authorize = authorizeMiddleware(authApi);
|
|
|
|
dbConnSetup = dbConnSetupMiddleware(pgConnection);
|
2015-09-17 00:09:39 +08:00
|
|
|
});
|
|
|
|
|
2016-01-29 02:44:25 +08:00
|
|
|
|
2015-09-25 19:31:51 +08:00
|
|
|
it('can be found in server_options', function(){
|
2017-09-25 19:40:22 +08:00
|
|
|
assert.ok(_.isFunction(authorize));
|
|
|
|
assert.ok(_.isFunction(dbConnSetup));
|
|
|
|
assert.ok(_.isFunction(cleanUpQueryParams));
|
2012-07-18 17:00:24 +08:00
|
|
|
});
|
|
|
|
|
2017-10-02 18:09:19 +08:00
|
|
|
function prepareRequest(req, res) {
|
2015-09-16 00:16:50 +08:00
|
|
|
req.profiler = {
|
|
|
|
done: function() {}
|
|
|
|
};
|
2015-07-08 21:34:46 +08:00
|
|
|
req.context = { user: 'localhost' };
|
2017-10-02 18:09:19 +08:00
|
|
|
res.locals = {};
|
|
|
|
|
|
|
|
return {req, res};
|
2015-07-08 21:34:46 +08:00
|
|
|
}
|
|
|
|
|
2017-10-02 18:09:19 +08:00
|
|
|
it('res.locals are created', function(done) {
|
|
|
|
let req = {};
|
|
|
|
let res = {};
|
|
|
|
|
|
|
|
({req, res} = prepareRequest(req, res));
|
|
|
|
|
|
|
|
localsMiddleware(req, res, function(err) {
|
|
|
|
if ( err ) { done(err); return; }
|
|
|
|
assert.ok(res.hasOwnProperty('locals'), 'response has locals');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-25 19:31:51 +08:00
|
|
|
it('cleans up request', function(done){
|
2017-10-02 18:09:19 +08:00
|
|
|
var req = {headers: { host:'localhost' }, query: {dbuser:'hacker',dbname:'secret'}};
|
|
|
|
var res = {};
|
|
|
|
|
|
|
|
({req, res} = prepareRequest(req, res));
|
|
|
|
|
|
|
|
cleanUpQueryParams(req, res, function(err) {
|
|
|
|
if ( err ) { done(err); return; }
|
|
|
|
assert.ok(_.isObject(req.query), 'request has query');
|
|
|
|
assert.ok(!req.query.hasOwnProperty('dbuser'), 'dbuser was removed from query');
|
|
|
|
assert.ok(res.hasOwnProperty('locals'), 'response has locals');
|
|
|
|
assert.ok(!res.locals.hasOwnProperty('interactivity'), 'response locals do not have interactivity');
|
|
|
|
done();
|
|
|
|
});
|
2012-07-18 17:00:24 +08:00
|
|
|
});
|
|
|
|
|
2015-09-25 19:31:51 +08:00
|
|
|
it('sets dbname from redis metadata', function(done){
|
2017-10-02 18:09:19 +08:00
|
|
|
var req = {headers: { host:'localhost' }, query: {} };
|
|
|
|
var res = {};
|
|
|
|
|
|
|
|
({req, res} = prepareRequest(req, res));
|
|
|
|
|
|
|
|
dbConnSetup(req, res, function(err) {
|
|
|
|
if ( err ) { done(err); return; }
|
|
|
|
assert.ok(_.isObject(req.query), 'request has query');
|
|
|
|
assert.ok(!req.query.hasOwnProperty('dbuser'), 'dbuser was removed from query');
|
|
|
|
assert.ok(res.hasOwnProperty('locals'), 'response has locals');
|
|
|
|
assert.ok(!res.locals.hasOwnProperty('interactivity'), 'response locals do not have interactivity');
|
|
|
|
assert.equal(res.locals.dbname, test_database);
|
|
|
|
assert.ok(res.locals.dbuser === test_pubuser, 'could inject dbuser ('+res.locals.dbuser+')');
|
|
|
|
done();
|
|
|
|
});
|
2012-07-18 17:00:24 +08:00
|
|
|
});
|
|
|
|
|
2015-09-25 19:31:51 +08:00
|
|
|
it('sets also dbuser for authenticated requests', function(done){
|
2017-10-02 18:09:19 +08:00
|
|
|
var req = { headers: { host: 'localhost' }, query: { map_key: '1234' }};
|
2017-09-25 19:40:22 +08:00
|
|
|
var res = {};
|
|
|
|
|
2017-10-02 18:09:19 +08:00
|
|
|
({req, res} = prepareRequest(req, res));
|
|
|
|
|
2017-09-25 19:40:22 +08:00
|
|
|
// FIXME: review authorize-pgconnsetup workflow, It might we are doing authorization twice.
|
2017-10-02 18:09:19 +08:00
|
|
|
authorize(req, res, function (err) {
|
2017-09-25 19:40:22 +08:00
|
|
|
if (err) { done(err); return; }
|
|
|
|
dbConnSetup(req, res, function(err) {
|
|
|
|
if ( err ) { done(err); return; }
|
|
|
|
assert.ok(_.isObject(req.query), 'request has query');
|
|
|
|
assert.ok(!req.query.hasOwnProperty('dbuser'), 'dbuser was removed from query');
|
2017-10-02 18:09:19 +08:00
|
|
|
assert.ok(res.hasOwnProperty('locals'), 'response has locals');
|
|
|
|
assert.ok(!res.locals.hasOwnProperty('interactivity'), 'request params do not have interactivity');
|
|
|
|
assert.equal(res.locals.dbname, test_database);
|
|
|
|
assert.equal(res.locals.dbuser, test_user);
|
2017-09-25 19:40:22 +08:00
|
|
|
|
|
|
|
req = {
|
|
|
|
headers: {
|
|
|
|
host:'localhost'
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
map_key: '1235'
|
2017-10-02 18:28:29 +08:00
|
|
|
}
|
2017-09-25 19:40:22 +08:00
|
|
|
};
|
|
|
|
|
2017-10-02 18:09:19 +08:00
|
|
|
({req, res} = prepareRequest(req, res));
|
|
|
|
|
2017-10-02 18:28:29 +08:00
|
|
|
dbConnSetup(req, res, function(err) {
|
|
|
|
if ( err ) { done(err); return; }
|
2017-09-25 19:40:22 +08:00
|
|
|
// wrong key resets params to no user
|
2017-10-02 18:09:19 +08:00
|
|
|
assert.ok(res.locals.dbuser === test_pubuser, 'could inject dbuser ('+res.locals.dbuser+')');
|
2017-09-25 19:40:22 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2012-07-18 17:00:24 +08:00
|
|
|
});
|
2013-04-24 21:10:58 +08:00
|
|
|
|
2017-08-04 23:30:46 +08:00
|
|
|
it('it should remove invalid params', function(done) {
|
|
|
|
var config = {
|
|
|
|
version: '1.3.0'
|
|
|
|
};
|
|
|
|
var req = {
|
|
|
|
headers: {
|
|
|
|
host:'localhost'
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
non_included: 'toberemoved',
|
|
|
|
api_key: 'test',
|
|
|
|
style: 'override',
|
|
|
|
config: config
|
2017-10-02 18:09:19 +08:00
|
|
|
}
|
2015-03-24 00:54:37 +08:00
|
|
|
};
|
2017-09-22 22:46:39 +08:00
|
|
|
var res = {};
|
2017-09-22 22:26:56 +08:00
|
|
|
|
2017-10-02 18:09:19 +08:00
|
|
|
({req, res} = prepareRequest(req, res));
|
|
|
|
|
|
|
|
cleanUpQueryParams(req, res, function (err) {
|
2017-09-22 22:46:39 +08:00
|
|
|
if ( err ) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2017-09-25 19:40:22 +08:00
|
|
|
|
2017-10-02 18:09:19 +08:00
|
|
|
var query = res.locals;
|
2017-09-22 22:46:39 +08:00
|
|
|
assert.deepEqual(config, query.config);
|
|
|
|
assert.equal('test', query.api_key);
|
|
|
|
assert.equal(undefined, query.non_included);
|
|
|
|
done();
|
2013-04-24 21:10:58 +08:00
|
|
|
});
|
|
|
|
});
|
2015-03-24 00:54:37 +08:00
|
|
|
|
2012-07-18 17:00:24 +08:00
|
|
|
});
|