Windshaft-cartodb/test/acceptance/named-layers-visibility-test.js

217 lines
6.0 KiB
JavaScript
Raw Normal View History

'use strict';
var step = require('step');
var test_helper = require('../support/test-helper');
2016-08-26 02:04:23 +08:00
var assert = require('../support/assert');
2019-10-07 16:55:26 +08:00
var CartodbWindshaft = require('../../lib/server');
var serverOptions = require('../../lib/server-options');
2016-08-26 02:04:23 +08:00
var RedisPool = require('redis-mpool');
2019-10-07 16:50:14 +08:00
var TemplateMaps = require('../../lib/backends/template-maps');
var mapnik = require('windshaft').mapnik;
2016-08-26 02:04:23 +08:00
var IMAGE_TOLERANCE = 20;
2016-08-26 02:04:23 +08:00
2019-10-22 01:07:24 +08:00
describe('layers visibility for previews', function () {
var server;
before(function () {
server = new CartodbWindshaft(serverOptions);
});
2016-08-26 02:04:23 +08:00
// configure redis pool instance to use in tests
var redisPool = new RedisPool(global.environment.redis);
var templateMaps = new TemplateMaps(redisPool, {
max_user_templates: global.environment.maxUserTemplates
});
var username = 'localhost';
function createLayer (color, layerId) {
var mod;
if (color === 'red') {
mod = 0;
} else if (color === 'orange') {
mod = 1;
} else if (color === 'blue') {
mod = 2;
} else {
mod = 0;
}
return {
type: 'mapnik',
id: layerId,
options: {
sql: 'select * from populated_places_simple_reduced where cartodb_id % 3 = ' + mod,
cartocss: '#layer { marker-fill: ' + color + '; }',
cartocss_version: '2.3.0'
2016-08-26 02:04:23 +08:00
}
};
}
2016-08-26 02:04:23 +08:00
2019-10-22 01:07:24 +08:00
function createTemplate (scenario) {
return {
2016-08-26 02:04:23 +08:00
version: '0.0.1',
name: scenario.name,
2016-08-26 02:04:23 +08:00
auth: {
method: 'open'
},
view: {
bounds: {
west: 0,
south: 0,
east: 45,
north: 45
},
zoom: 4,
center: {
lng: 40,
lat: 20
},
preview_layers: scenario.layerPerview
2016-08-26 02:04:23 +08:00
},
layergroup: {
layers: scenario.layers
}
};
}
2016-08-26 02:04:23 +08:00
afterEach(function (done) {
test_helper.deleteRedisKeys({
'user:localhost:mapviews:global': 5
}, done);
});
2016-08-26 02:04:23 +08:00
2019-10-22 01:07:24 +08:00
function previewFixture (version) {
return './test/fixtures/previews/populated_places_simple_reduced-' + version + '.png';
}
var threeLayerPointDistintColor = [
createLayer('red'),
createLayer('orange'),
createLayer('blue', 'layer2')
];
var scenarios = [{
name: 'preview_layers_red',
layerPerview: {
2019-10-22 01:07:24 +08:00
0: true,
1: false,
layer2: false
},
layers: threeLayerPointDistintColor
2016-09-01 02:41:07 +08:00
}, {
name: 'preview_layers_orange',
layerPerview: {
2019-10-22 01:07:24 +08:00
0: false,
1: true,
layer2: false
2016-09-01 02:41:07 +08:00
},
layers: threeLayerPointDistintColor
}, {
name: 'preview_layers_blue',
layerPerview: {
2019-10-22 01:07:24 +08:00
0: false,
1: false,
layer2: true
},
layers: threeLayerPointDistintColor
}, {
name: 'preview_layers_red_orange',
layerPerview: {
2019-10-22 01:07:24 +08:00
0: true,
1: true,
layer2: false
},
layers: threeLayerPointDistintColor
}, {
name: 'preview_layers_red_blue',
layerPerview: {
2019-10-22 01:07:24 +08:00
0: true,
1: false,
layer2: true
},
layers: threeLayerPointDistintColor
}, {
2016-09-01 02:41:07 +08:00
name: 'preview_layers_orange_blue',
layerPerview: {
2019-10-22 01:07:24 +08:00
0: false,
1: true,
layer2: true
},
layers: threeLayerPointDistintColor
}, {
name: 'preview_layers_red_orange_blue',
layerPerview: {
2019-10-22 01:07:24 +08:00
0: true,
1: true,
layer2: true
},
layers: threeLayerPointDistintColor
}, {
name: 'preview_layers_all',
layerPerview: {},
layers: threeLayerPointDistintColor
2019-10-22 01:07:24 +08:00
}, {
name: 'preview_layers_undefined',
layerPerview: undefined,
layers: threeLayerPointDistintColor
}];
scenarios.forEach(function (scenario) {
it('should filter layers for template: ' + scenario.name, function (done) {
step(
function addTemplate () {
var next = this;
var template = createTemplate(scenario);
2016-08-26 02:04:23 +08:00
templateMaps.addTemplate(username, template, next);
},
function requestPreview (err) {
assert.ifError(err);
2016-08-26 02:04:23 +08:00
var next = this;
2016-08-26 02:04:23 +08:00
assert.response(server, {
url: '/api/v1/map/static/named/' + scenario.name + '/640/480.png',
2016-08-26 02:04:23 +08:00
method: 'GET',
headers: {
host: 'localhost'
},
encoding: 'binary'
}, {
2016-08-26 02:04:23 +08:00
status: 200,
headers: {
'content-type': 'image/png'
}
}, function (res, err) {
2016-08-26 02:04:23 +08:00
next(err, res);
});
},
function checkPreview (err, res) {
assert.ifError(err);
var next = this;
var img = mapnik.Image.fromBytes(Buffer.from(res.body, 'binary'));
var previewFixturePath = previewFixture(scenario.name);
assert.imageIsSimilarToFile(img, previewFixturePath, IMAGE_TOLERANCE, next);
},
2019-10-22 01:07:24 +08:00
function deleteTemplate (err) {
assert.ifError(err);
var next = this;
templateMaps.delTemplate(username, scenario.name, next);
},
function finish (err) {
done(err);
2016-08-26 02:04:23 +08:00
}
);
});
2016-08-26 02:04:23 +08:00
});
});