Windshaft-cartodb/test/acceptance/named-maps-cache-test.js

192 lines
5.8 KiB
JavaScript
Raw Normal View History

'use strict';
require('../support/test-helper');
const helper = require('../support/test-helper');
var assert = require('../support/assert');
var mapnik = require('windshaft').mapnik;
2019-10-07 15:40:50 +08:00
var CartodbWindshaft = require('../../lib/server');
var serverOptions = require('../../lib/server-options');
2019-10-22 01:07:24 +08:00
describe('named maps provider cache', function () {
var server;
before(function () {
server = new CartodbWindshaft(serverOptions);
});
var username = 'localhost';
const apikey = 1234;
var templateName = 'template_with_color';
var IMAGE_TOLERANCE = 20;
2019-10-22 01:07:24 +08:00
function createTemplate (color) {
return {
version: '0.0.1',
name: `${templateName}_${color}`,
auth: {
method: 'open'
},
placeholders: {
color: {
2019-10-22 01:07:24 +08:00
type: 'css_color',
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) {
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';
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;
if (res.statusCode === 200) {
2019-11-06 20:56:59 +08:00
img = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary'));
}
return callback(err, res, img);
});
}
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'
}
};
const expectedResponse = {
2019-10-22 01:07:24 +08:00
status: 204
};
assert.response(server, deleteTemplateRequest, expectedResponse, (res, err) => {
return callback(err, res);
});
}
2019-10-22 01:07:24 +08:00
function previewFixture (color) {
return './test/fixtures/provider/populated_places_simple_reduced-' + color + '.png';
}
var colors = ['black', 'red', 'green', 'blue'];
2019-10-22 01:07:24 +08:00
colors.forEach(function (color) {
it('should return an image estimating its bounds based on dataset', function (done) {
addTemplate(createTemplate(color), function (err, res, template) {
if (err) {
return done(err);
}
2019-10-22 01:07:24 +08:00
getNamedTile(template.template_id, function (err, res, img) {
assert.ok(!err);
assert.imageIsSimilarToFile(img, previewFixture(color), IMAGE_TOLERANCE, (err) => {
assert.ifError(err);
const keysToDelete = {};
keysToDelete['map_tpl|localhost'] = 0;
helper.deleteRedisKeys(keysToDelete, done);
});
});
});
});
});
it('should fail to use template from named map provider after template deletion', function (done) {
const color = 'black';
const templateId = `${templateName}_${color}`;
addTemplate(createTemplate(color), function (err) {
assert.ifError(err);
2019-10-22 01:07:24 +08:00
getNamedTile(templateId, function (err, res, img) {
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) {
assert.ifError(err);
assert.deepStrictEqual(
JSON.parse(res.body).errors,
["Template 'template_with_color_black' of user 'localhost' not found"]
);
done();
});
});
});
});
});
});
});