Tweak error messages on missing redis variables, update tests

This commit is contained in:
Sandro Santilli 2013-11-11 15:56:58 +01:00
parent 8d1b394df1
commit bf340e684a
2 changed files with 14 additions and 15 deletions

View File

@ -26,6 +26,9 @@ module.exports = function() {
tagged_mapview_key: "user:<%= username %>:mapviews:stat_tag:<%= stat_tag %>"
};
me.userFromHostname = function(hostname) {
return hostname.split('.')[0];
}
/**
* Get the database name for this particular subdomain/username
@ -35,22 +38,18 @@ module.exports = function() {
*/
me.getDatabase = function(req, callback) {
// strip subdomain from header host
var username = req.headers.host.split('.')[0]
var username = me.userFromHostname(req.headers.host);
var redisKey = _.template(this.user_key, {username: username});
this.retrieve(this.user_metadata_db, redisKey, 'database_name', function(err, dbname) {
if ( err ) callback(err, null);
else if ( dbname === null ) {
callback(new Error("missing " + username + "'s dbname in redis (try CARTODB/script/restore_redis)"), null);
callback(new Error("missing " + username + "'s database_name in redis (try CARTODB/script/restore_redis)"), null);
}
else callback(err, dbname);
});
};
me.userFromHostname = function(hostname) {
return hostname.split('.')[0];
}
/**
* Increment mapview count for a user
*
@ -88,13 +87,13 @@ module.exports = function() {
*/
me.getId= function(req, callback) {
// strip subdomain from header host
var username = req.headers.host.split('.')[0];
var username = me.userFromHostname(req.headers.host);
var redisKey = _.template(this.user_key, {username: username});
this.retrieve(this.user_metadata_db, redisKey, 'id', function(err, dbname) {
if ( err ) callback(err, null);
else if ( dbname === null ) {
callback(new Error("missing " + username + "'s dbuser in redis (try CARTODB/script/restore_redis)"), null);
callback(new Error("missing " + username + "'s id in redis (try CARTODB/script/restore_redis)"), null);
}
else callback(err, dbname);
});
@ -108,13 +107,13 @@ module.exports = function() {
*/
me.getDatabaseHost= function(req, callback) {
// strip subdomain from header host
var username = req.headers.host.split('.')[0];
var username = me.userFromHostname(req.headers.host);
var redisKey = _.template(this.user_key, {username: username});
this.retrieve(this.user_metadata_db, redisKey, 'database_host', function(err, dbname) {
if ( err ) callback(err, null);
else if ( dbname === null ) {
callback(new Error("missing " + username + "'s dbuser in redis (try CARTODB/script/restore_redis)"), null);
callback(new Error("missing " + username + "'s database_host in redis (try CARTODB/script/restore_redis)"), null);
}
else callback(err, dbname);
});
@ -128,7 +127,7 @@ module.exports = function() {
*/
me.checkMapKey = function(req, callback) {
// strip subdomain from header host
var username = req.headers.host.split('.')[0];
var username = me.userFromHostname(req.headers.host);
var redisKey = "rails:users:" + username;
this.retrieve(this.user_metadata_db, redisKey, "map_key", function(err, val) {
var valid = 0;

View File

@ -140,7 +140,7 @@ suite('server', function() {
// FIXME: should be 401 Unauthorized
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
assert.deepEqual(JSON.parse(res.body),
{error:"missing unknown_user's dbname in redis (try CARTODB/script/restore_redis)"});
{error:"missing unknown_user's database_name in redis (try CARTODB/script/restore_redis)"});
done();
});
});
@ -493,7 +493,7 @@ suite('server', function() {
// FIXME: should be 401 Unauthorized
assert.equal(res.statusCode, 500, res.statusCode + ': ' + res.body);
assert.deepEqual(JSON.parse(res.body),
{error:"missing unknown_user's dbname in redis (try CARTODB/script/restore_redis)"});
{error:"missing unknown_user's database_name in redis (try CARTODB/script/restore_redis)"});
done();
});
});
@ -576,7 +576,7 @@ suite('server', function() {
// FIXME: should be 401 Unauthorized
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
assert.deepEqual(JSON.parse(res.body),
{error:"missing unknown_user's dbname in redis (try CARTODB/script/restore_redis)"});
{error:"missing unknown_user's database_name in redis (try CARTODB/script/restore_redis)"});
done();
});
});
@ -727,7 +727,7 @@ suite('server', function() {
// FIXME: should be 401 Unauthorized
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
assert.deepEqual(JSON.parse(res.body),
{error:"missing unknown_user's dbname in redis (try CARTODB/script/restore_redis)"});
{error:"missing unknown_user's database_name in redis (try CARTODB/script/restore_redis)"});
done();
});
});