2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 16:44:45 +08:00
|
|
|
require('../support/test-helper');
|
2015-09-23 22:45:20 +08:00
|
|
|
|
2019-10-07 16:44:45 +08:00
|
|
|
const helper = require('../support/test-helper');
|
2015-09-23 22:45:20 +08:00
|
|
|
var assert = require('../support/assert');
|
|
|
|
var mapnik = require('windshaft').mapnik;
|
2019-10-07 15:40:50 +08:00
|
|
|
var CartodbWindshaft = require('../../lib/server');
|
2019-10-07 16:10:51 +08:00
|
|
|
var serverOptions = require('../../lib/server-options');
|
2015-09-23 22:45:20 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('named maps provider cache', function () {
|
2018-04-16 22:16:23 +08:00
|
|
|
var server;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
server = new CartodbWindshaft(serverOptions);
|
|
|
|
});
|
2018-04-16 20:09:24 +08:00
|
|
|
|
2015-09-23 22:45:20 +08:00
|
|
|
var username = 'localhost';
|
2019-09-13 02:32:52 +08:00
|
|
|
const apikey = 1234;
|
2015-09-23 22:45:20 +08:00
|
|
|
var templateName = 'template_with_color';
|
|
|
|
|
|
|
|
var IMAGE_TOLERANCE = 20;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function createTemplate (color) {
|
2015-09-23 22:45:20 +08:00
|
|
|
return {
|
|
|
|
version: '0.0.1',
|
2019-09-13 02:32:52 +08:00
|
|
|
name: `${templateName}_${color}`,
|
2015-09-23 22:45:20 +08:00
|
|
|
auth: {
|
|
|
|
method: 'open'
|
|
|
|
},
|
|
|
|
placeholders: {
|
|
|
|
color: {
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'css_color',
|
2015-09-23 22:45:20 +08:00
|
|
|
default: color
|
|
|
|
}
|
|
|
|
},
|
|
|
|
layergroup: {
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
type: 'mapnik',
|
|
|
|
options: {
|
|
|
|
sql: 'select * from populated_places_simple_reduced',
|
|
|
|
cartocss: '#layer { marker-fill: <%= color %>; marker-line-color: <%= color %>; }',
|
|
|
|
cartocss_version: '2.3.0'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getNamedTile (templateId, options, callback) {
|
2015-09-23 22:45:20 +08:00
|
|
|
if (!callback) {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var url = '/api/v1/map/named/' + templateId + '/all/' + [0, 0, 0].join('/') + '.png';
|
2015-09-23 22:45:20 +08:00
|
|
|
|
|
|
|
var requestOptions = {
|
|
|
|
url: url,
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
host: username
|
|
|
|
},
|
|
|
|
encoding: 'binary'
|
|
|
|
};
|
|
|
|
|
|
|
|
var statusCode = options.statusCode || 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;
|
2019-09-13 02:32:52 +08:00
|
|
|
if (res.statusCode === 200) {
|
|
|
|
img = mapnik.Image.fromBytes(new Buffer.from(res.body, 'binary'));
|
2015-09-23 22:45:20 +08:00
|
|
|
}
|
|
|
|
return callback(err, res, img);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-13 02:32:52 +08:00
|
|
|
function addTemplate (template, callback) {
|
|
|
|
const createTemplateRequest = {
|
|
|
|
url: `/api/v1/map/named?api_key=${apikey}`,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
host: username,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(template)
|
|
|
|
};
|
|
|
|
|
|
|
|
const expectedResponse = {
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json; charset=utf-8'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.response(server, createTemplateRequest, expectedResponse, (res, err) => {
|
|
|
|
let template;
|
|
|
|
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
template = JSON.parse(res.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(err, res, template);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteTemplate (templateId, callback) {
|
|
|
|
const deleteTemplateRequest = {
|
|
|
|
url: `/api/v1/map/named/${templateId}?api_key=${apikey}`,
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
2019-10-22 01:07:24 +08:00
|
|
|
host: 'localhost'
|
2019-09-13 02:32:52 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const expectedResponse = {
|
2019-10-22 01:07:24 +08:00
|
|
|
status: 204
|
2019-09-13 02:32:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
assert.response(server, deleteTemplateRequest, expectedResponse, (res, err) => {
|
|
|
|
return callback(err, res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function previewFixture (color) {
|
2015-09-23 22:45:20 +08:00
|
|
|
return './test/fixtures/provider/populated_places_simple_reduced-' + color + '.png';
|
|
|
|
}
|
|
|
|
|
2019-09-13 02:32:52 +08:00
|
|
|
var colors = ['black', 'red', 'green', 'blue'];
|
2019-10-22 01:07:24 +08:00
|
|
|
colors.forEach(function (color) {
|
2015-09-23 22:45:20 +08:00
|
|
|
it('should return an image estimating its bounds based on dataset', function (done) {
|
2019-09-13 02:32:52 +08:00
|
|
|
addTemplate(createTemplate(color), function (err, res, template) {
|
2015-09-23 22:45:20 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2019-09-13 02:32:52 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
getNamedTile(template.template_id, function (err, res, img) {
|
2015-09-23 22:45:20 +08:00
|
|
|
assert.ok(!err);
|
2019-09-13 02:32:52 +08:00
|
|
|
assert.imageIsSimilarToFile(img, previewFixture(color), IMAGE_TOLERANCE, (err) => {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
const keysToDelete = {};
|
|
|
|
keysToDelete['map_tpl|localhost'] = 0;
|
|
|
|
helper.deleteRedisKeys(keysToDelete, done);
|
|
|
|
});
|
2015-09-23 22:45:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail to use template from named map provider after template deletion', function (done) {
|
2019-09-13 02:32:52 +08:00
|
|
|
const color = 'black';
|
|
|
|
const templateId = `${templateName}_${color}`;
|
|
|
|
|
|
|
|
addTemplate(createTemplate(color), function (err) {
|
|
|
|
assert.ifError(err);
|
2015-09-23 22:45:20 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
getNamedTile(templateId, function (err, res, img) {
|
2019-09-13 02:32:52 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
assert.imageIsSimilarToFile(img, previewFixture(color), IMAGE_TOLERANCE, function (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
deleteTemplate(templateId, function (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
getNamedTile(templateId, { statusCode: 404 }, function (err, res) {
|
2019-09-13 02:32:52 +08:00
|
|
|
assert.ifError(err);
|
2015-09-23 22:45:20 +08:00
|
|
|
|
2019-10-22 01:52:51 +08:00
|
|
|
assert.deepStrictEqual(
|
2016-06-13 22:14:01 +08:00
|
|
|
JSON.parse(res.body).errors,
|
2019-09-13 02:32:52 +08:00
|
|
|
["Template 'template_with_color_black' of user 'localhost' not found"]
|
2015-09-23 22:45:20 +08:00
|
|
|
);
|
|
|
|
|
2019-09-13 02:32:52 +08:00
|
|
|
done();
|
2015-09-23 22:45:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|