Adds tests for named maps authentication for tiles

This commit is contained in:
Raul Ochoa 2015-09-22 19:48:08 +02:00
parent b617bb0277
commit 14e6cb05b3

View File

@ -3,13 +3,14 @@ var RedisPool = require('redis-mpool');
var querystring = require('querystring');
var assert = require('../support/assert');
var mapnik = require('windshaft').mapnik;
var CartodbWindshaft = require(__dirname + '/../../lib/cartodb/server');
var serverOptions = require(__dirname + '/../../lib/cartodb/server_options');
var server = new CartodbWindshaft(serverOptions);
var TemplateMaps = require('../../lib/cartodb/backends/template_maps.js');
var NamedMapsCacheEntry = require('../../lib/cartodb/cache/model/named_maps_entry');
describe('named static maps', function() {
describe('named maps authentication', function() {
// configure redis pool instance to use in tests
var redisPool = new RedisPool(global.environment.redis);
@ -125,9 +126,9 @@ describe('named static maps', function() {
});
});
function getStaticMap(name, options, callback) {
function getNamedTile(name, z, x, y, options, callback) {
var url = '/api/v1/map/static/named/' + name + '/640/480.png';
var url = '/api/v1/map/named/' + name + '/all/' + [z,x,y].join('/') + '.png';
if (options.params) {
url = url + '?' + querystring.stringify(options.params);
}
@ -136,7 +137,8 @@ describe('named static maps', function() {
method: 'GET',
headers: {
host: username
}
},
encoding: 'binary'
};
var statusCode = options.status || 200;
@ -152,7 +154,79 @@ describe('named static maps', function() {
requestOptions,
expectedResponse,
function (res, err) {
return callback(err, res);
var img;
if (!err && res.headers['content-type'] === 'image/png') {
img = mapnik.Image.fromBytes(new Buffer(res.body, 'binary'));
}
return callback(err, res, img);
}
);
}
it('should return a 404 error for nonexistent template name', function (done) {
var nonexistentName = 'nonexistent';
getNamedTile(nonexistentName, 0, 0, 0, { status: 404 }, function(err, res) {
assert.ok(!err);
assert.deepEqual(
JSON.parse(res.body),
{ errors: ["Template '" + nonexistentName + "' of user '" + username + "' not found"] }
);
done();
});
});
it('should return 403 if not properly authorized', function(done) {
getNamedTile(tokenAuthTemplateName, 0, 0, 0, { status: 403 }, function(err, res) {
assert.ok(!err);
assert.deepEqual(JSON.parse(res.body), { errors: ['Unauthorized template instantiation'] });
done();
});
});
it('should return 200 if properly authorized', function(done) {
getNamedTile(tokenAuthTemplateName, 0, 0, 0, { params: { auth_token: 'valid1' } }, function(err, res, img) {
assert.equal(img.width(), 256);
assert.equal(img.height(), 256);
assert.ok(!err);
test_helper.checkSurrogateKey(res, new NamedMapsCacheEntry(username, tokenAuthTemplateName).key());
done();
});
});
function getStaticMap(name, options, callback) {
var url = '/api/v1/map/static/named/' + name + '/640/480.png';
if (options.params) {
url = url + '?' + querystring.stringify(options.params);
}
var requestOptions = {
url: url,
method: 'GET',
headers: {
host: username
},
encoding: 'binary'
};
var statusCode = options.status || 200;
var expectedResponse = {
status: statusCode,
headers: {
'Content-Type': statusCode === 200 ? 'image/png' : 'application/json; charset=utf-8'
}
};
assert.response(server,
requestOptions,
expectedResponse,
function (res, err) {
var img;
if (!err && res.headers['content-type'] === 'image/png') {
img = mapnik.Image.fromBytes(new Buffer(res.body, 'binary'));
}
return callback(err, res, img);
}
);
}
@ -178,8 +252,12 @@ describe('named static maps', function() {
});
it('should return 200 if properly authorized', function(done) {
getStaticMap(tokenAuthTemplateName, { params: { auth_token: 'valid1' } }, function(err, res) {
getStaticMap(tokenAuthTemplateName, { params: { auth_token: 'valid1' } }, function(err, res, img) {
assert.ok(!err);
assert.equal(img.width(), 640);
assert.equal(img.height(), 480);
test_helper.checkSurrogateKey(res, new NamedMapsCacheEntry(username, tokenAuthTemplateName).key());
done();
});