2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-14 03:08:04 +08:00
|
|
|
var testHelper = require('../support/test-helper');
|
2015-09-29 18:21:11 +08:00
|
|
|
var RedisPool = require('redis-mpool');
|
|
|
|
var querystring = require('querystring');
|
|
|
|
|
|
|
|
var assert = require('../support/assert');
|
2020-04-04 23:34:22 +08:00
|
|
|
const mapnik = require('@carto/mapnik');
|
2020-06-02 17:57:11 +08:00
|
|
|
const createServer = require('../../lib/server');
|
2019-10-07 16:55:26 +08:00
|
|
|
var serverOptions = require('../../lib/server-options');
|
2019-10-07 16:50:14 +08:00
|
|
|
var TemplateMaps = require('../../lib/backends/template-maps');
|
2019-10-07 15:51:51 +08:00
|
|
|
var NamedMapsCacheEntry = require('../../lib/cache/model/named-maps-entry');
|
2015-09-29 18:21:11 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('named maps preview stats', function () {
|
2018-04-16 22:16:23 +08:00
|
|
|
var server;
|
|
|
|
|
|
|
|
before(function () {
|
2020-06-02 17:57:11 +08:00
|
|
|
server = createServer(serverOptions);
|
2018-04-16 22:16:23 +08:00
|
|
|
});
|
2018-04-16 20:09:24 +08:00
|
|
|
|
2015-09-29 18:21:11 +08:00
|
|
|
var redisPool = new RedisPool(global.environment.redis);
|
|
|
|
|
|
|
|
var templateMaps = new TemplateMaps(redisPool, {
|
|
|
|
max_user_templates: global.environment.maxUserTemplates
|
|
|
|
});
|
|
|
|
|
|
|
|
var username = 'localhost';
|
|
|
|
|
|
|
|
var statTag = 'wadus_viz';
|
|
|
|
var templateName = 'with_stats';
|
|
|
|
var template = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: templateName,
|
|
|
|
auth: {
|
|
|
|
method: 'open'
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
placeholders: {
|
|
|
|
color: {
|
|
|
|
type: 'css_color',
|
|
|
|
default: '#cc3300'
|
2015-09-29 18:21:11 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
layergroup: {
|
|
|
|
stat_tag: statTag,
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
sql: 'select 1 cartodb_id, null::geometry the_geom_webmercator',
|
|
|
|
cartocss: '#layer { marker-fill: <%= color %>; }',
|
|
|
|
cartocss_version: '2.3.0'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(function (done) {
|
|
|
|
templateMaps.addTemplate(username, template, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function (done) {
|
|
|
|
templateMaps.delTemplate(username, templateName, done);
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getStaticMap (name, options, callback) {
|
2015-09-29 18:21:11 +08:00
|
|
|
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') {
|
2019-10-22 02:05:51 +08:00
|
|
|
img = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary'));
|
2015-09-29 18:21:11 +08:00
|
|
|
}
|
|
|
|
return callback(err, res, img);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should return 200 if properly authorized', function (done) {
|
|
|
|
getStaticMap(templateName, { params: { auth_token: 'valid1' } }, function (err, res, img) {
|
2015-09-29 18:21:11 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(img.width(), 640);
|
|
|
|
assert.strictEqual(img.height(), 480);
|
2015-09-29 18:21:11 +08:00
|
|
|
|
2019-11-14 03:08:04 +08:00
|
|
|
testHelper.checkSurrogateKey(res, new NamedMapsCacheEntry(username, templateName).key());
|
2015-09-29 18:21:11 +08:00
|
|
|
var redisKeysToDelete = { 'user:localhost:mapviews:global': 5 };
|
|
|
|
redisKeysToDelete['user:localhost:mapviews:stat_tag:' + statTag] = 5;
|
2019-11-14 03:08:04 +08:00
|
|
|
testHelper.deleteRedisKeys(redisKeysToDelete, done);
|
2015-09-29 18:21:11 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|