2015-03-24 00:54:37 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('underscore');
|
|
|
|
var test_helper = require('../../support/test_helper');
|
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');
|
|
|
|
|
|
|
|
var BaseController = require('../../../lib/cartodb/controllers/base');
|
|
|
|
var windshaft = require('windshaft');
|
2012-07-18 17:00:24 +08:00
|
|
|
|
2015-09-25 19:31:51 +08:00
|
|
|
describe('req2params', 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';
|
|
|
|
|
2015-09-17 00:09:39 +08:00
|
|
|
|
|
|
|
var baseController;
|
|
|
|
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);
|
|
|
|
|
|
|
|
baseController = new BaseController(authApi, pgConnection);
|
|
|
|
});
|
|
|
|
|
2016-01-29 02:44:25 +08:00
|
|
|
|
2015-09-25 19:31:51 +08:00
|
|
|
it('can be found in server_options', function(){
|
2015-09-17 00:09:39 +08:00
|
|
|
assert.ok(_.isFunction(baseController.req2params));
|
2012-07-18 17:00:24 +08:00
|
|
|
});
|
|
|
|
|
2015-09-16 00:16:50 +08:00
|
|
|
function prepareRequest(req) {
|
|
|
|
req.profiler = {
|
|
|
|
done: function() {}
|
|
|
|
};
|
2015-07-08 21:34:46 +08:00
|
|
|
req.context = { user: 'localhost' };
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2015-09-25 19:31:51 +08:00
|
|
|
it('cleans up request', function(done){
|
2015-07-08 21:34:46 +08:00
|
|
|
var req = {headers: { host:'localhost' }, query: {dbuser:'hacker',dbname:'secret'}};
|
2015-09-17 00:09:39 +08:00
|
|
|
baseController.req2params(prepareRequest(req), function(err, req) {
|
2013-12-06 17:20:32 +08:00
|
|
|
if ( err ) { done(err); return; }
|
2012-07-18 17:00:24 +08:00
|
|
|
assert.ok(_.isObject(req.query), 'request has query');
|
|
|
|
assert.ok(!req.query.hasOwnProperty('dbuser'), 'dbuser was removed from query');
|
|
|
|
assert.ok(req.hasOwnProperty('params'), 'request has params');
|
2015-03-26 20:05:35 +08:00
|
|
|
assert.ok(!req.params.hasOwnProperty('interactivity'), 'request params do not have interactivity');
|
2014-02-21 01:03:43 +08:00
|
|
|
assert.equal(req.params.dbname, test_database, 'could forge dbname: '+ req.params.dbname);
|
|
|
|
assert.ok(req.params.dbuser === test_pubuser, 'could inject dbuser ('+req.params.dbuser+')');
|
2012-07-18 17:00:24 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-25 19:31:51 +08:00
|
|
|
it('sets dbname from redis metadata', function(done){
|
2015-07-08 21:34:46 +08:00
|
|
|
var req = {headers: { host:'localhost' }, query: {} };
|
2015-09-17 00:09:39 +08:00
|
|
|
baseController.req2params(prepareRequest(req), function(err, req) {
|
2013-12-06 17:20:32 +08:00
|
|
|
if ( err ) { done(err); return; }
|
2012-07-18 17:00:24 +08:00
|
|
|
assert.ok(_.isObject(req.query), 'request has query');
|
|
|
|
assert.ok(!req.query.hasOwnProperty('dbuser'), 'dbuser was removed from query');
|
|
|
|
assert.ok(req.hasOwnProperty('params'), 'request has params');
|
2015-03-26 20:05:35 +08:00
|
|
|
assert.ok(!req.params.hasOwnProperty('interactivity'), 'request params do not have interactivity');
|
2014-02-21 01:03:43 +08:00
|
|
|
assert.equal(req.params.dbname, test_database);
|
|
|
|
assert.ok(req.params.dbuser === test_pubuser, 'could inject dbuser ('+req.params.dbuser+')');
|
2012-07-18 17:00:24 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-25 19:31:51 +08:00
|
|
|
it('sets also dbuser for authenticated requests', function(done){
|
2015-07-08 21:34:46 +08:00
|
|
|
var req = {headers: { host:'localhost' }, query: {map_key: '1234'} };
|
2015-09-17 00:09:39 +08:00
|
|
|
baseController.req2params(prepareRequest(req), function(err, req) {
|
2013-12-06 17:20:32 +08:00
|
|
|
if ( err ) { done(err); return; }
|
2012-07-18 17:00:24 +08:00
|
|
|
assert.ok(_.isObject(req.query), 'request has query');
|
|
|
|
assert.ok(!req.query.hasOwnProperty('dbuser'), 'dbuser was removed from query');
|
|
|
|
assert.ok(req.hasOwnProperty('params'), 'request has params');
|
2015-03-26 20:05:35 +08:00
|
|
|
assert.ok(!req.params.hasOwnProperty('interactivity'), 'request params do not have interactivity');
|
2014-02-21 01:03:43 +08:00
|
|
|
assert.equal(req.params.dbname, test_database);
|
|
|
|
assert.equal(req.params.dbuser, test_user);
|
2015-09-16 00:16:50 +08:00
|
|
|
|
|
|
|
req = {
|
|
|
|
headers: {
|
|
|
|
host:'localhost'
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
map_key: '1235'
|
|
|
|
}
|
|
|
|
};
|
2015-09-17 00:09:39 +08:00
|
|
|
baseController.req2params(prepareRequest(req), function(err, req) {
|
2012-07-18 17:00:24 +08:00
|
|
|
// wrong key resets params to no user
|
2014-02-21 01:03:43 +08:00
|
|
|
assert.ok(req.params.dbuser === test_pubuser, 'could inject dbuser ('+req.params.dbuser+')');
|
2012-07-18 17:00:24 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-04-24 21:10:58 +08:00
|
|
|
|
2015-09-25 19:31:51 +08:00
|
|
|
it('it should extend params with decoded lzma', function(done) {
|
2015-03-24 00:54:37 +08:00
|
|
|
var qo = {
|
2015-03-24 01:03:19 +08:00
|
|
|
config: {
|
|
|
|
version: '1.3.0'
|
|
|
|
}
|
2015-03-24 00:54:37 +08:00
|
|
|
};
|
|
|
|
test_helper.lzma_compress_to_base64(JSON.stringify(qo), 1, function(err, data) {
|
|
|
|
var req = {
|
|
|
|
headers: {
|
|
|
|
host:'localhost'
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
non_included: 'toberemoved',
|
|
|
|
api_key: 'test',
|
|
|
|
style: 'override',
|
|
|
|
lzma: data
|
|
|
|
}
|
|
|
|
};
|
2015-09-17 00:09:39 +08:00
|
|
|
baseController.req2params(prepareRequest(req), function(err, req) {
|
2015-03-24 00:54:37 +08:00
|
|
|
if ( err ) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var query = req.params;
|
2015-03-24 01:03:19 +08:00
|
|
|
assert.deepEqual(qo.config, query.config);
|
2015-03-24 00:54:37 +08:00
|
|
|
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
|
|
|
});
|