2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2013-01-11 23:15:24 +08:00
|
|
|
require('../../helper');
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var server = require('../../../lib/server')();
|
2015-05-13 17:53:14 +08:00
|
|
|
var assert = require('../../support/assert');
|
|
|
|
var querystring = require('querystring');
|
2015-05-26 19:45:41 +08:00
|
|
|
var shapefile = require('shapefile');
|
2015-05-13 17:53:14 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
var zipfile = require('zipfile');
|
2019-12-24 01:19:08 +08:00
|
|
|
var fs = require('fs');
|
2013-01-11 23:15:24 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('export.shapefile', function () {
|
2013-01-11 23:15:24 +08:00
|
|
|
// SHP tests
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('SHP format, unauthenticated', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'SHP is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=cartodb-query.zip/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
var tmpfile = '/tmp/myshape.zip';
|
|
|
|
var writeErr = fs.writeFileSync(tmpfile, res.body, 'binary');
|
|
|
|
if (writeErr) {
|
|
|
|
return done(writeErr);
|
|
|
|
}
|
|
|
|
var zf = new zipfile.ZipFile(tmpfile);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.dbf'), 'SHP zipfile does not contain .dbf: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.prj'), 'SHP zipfile does not contain .prj: ' + zf.names);
|
|
|
|
// TODO: check DBF contents
|
|
|
|
fs.unlinkSync(tmpfile);
|
|
|
|
done();
|
|
|
|
});
|
2013-01-11 23:15:24 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('SHP format, unauthenticated, POST', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql',
|
|
|
|
data: 'q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'POST'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'SHP is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=cartodb-query.zip/gi.test(cd), 'Unexpected SHP filename: ' + cd);
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-01-11 23:15:24 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('SHP format, big size, POST', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql',
|
|
|
|
data: querystring.stringify({
|
|
|
|
q: 'SELECT 0 as fname, st_makepoint(i,i) FROM generate_series(0,81920) i',
|
|
|
|
format: 'shp'
|
|
|
|
}),
|
|
|
|
headers: { host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'POST'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'SHP is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=cartodb-query.zip/gi.test(cd), 'Unexpected SHP filename: ' + cd);
|
2019-12-24 01:19:08 +08:00
|
|
|
assert.ok(res.body.length > 81920, 'SHP smaller than expected: ' + res.body.length);
|
|
|
|
done();
|
|
|
|
});
|
2013-01-11 23:15:24 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('SHP format, unauthenticated, with custom filename', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp&filename=myshape',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'SHP is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=myshape.zip/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
var tmpfile = '/tmp/myshape.zip';
|
|
|
|
var writeErr = fs.writeFileSync(tmpfile, res.body, 'binary');
|
|
|
|
if (writeErr) {
|
|
|
|
return done(writeErr);
|
|
|
|
}
|
|
|
|
var zf = new zipfile.ZipFile(tmpfile);
|
|
|
|
assert.ok(_.contains(zf.names, 'myshape.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'myshape.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'myshape.dbf'), 'SHP zipfile does not contain .dbf: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'myshape.prj'), 'SHP zipfile does not contain .prj: ' + zf.names);
|
|
|
|
fs.unlinkSync(tmpfile);
|
|
|
|
done();
|
|
|
|
});
|
2013-01-11 23:15:24 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('SHP format, unauthenticated, with custom, dangerous filename', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp&filename=b;"%20()[]a',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var fname = 'b_______a';
|
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'SHP is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=b_______a.zip/gi.test(cd), 'Unexpected SHP filename: ' + cd);
|
2019-12-24 01:19:08 +08:00
|
|
|
var tmpfile = '/tmp/myshape.zip';
|
|
|
|
var writeErr = fs.writeFileSync(tmpfile, res.body, 'binary');
|
|
|
|
if (writeErr) {
|
|
|
|
return done(writeErr);
|
|
|
|
}
|
|
|
|
var zf = new zipfile.ZipFile(tmpfile);
|
|
|
|
assert.ok(_.contains(zf.names, fname + '.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, fname + '.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, fname + '.dbf'), 'SHP zipfile does not contain .dbf: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, fname + '.prj'), 'SHP zipfile does not contain .prj: ' + zf.names);
|
|
|
|
fs.unlinkSync(tmpfile);
|
|
|
|
done();
|
|
|
|
});
|
2013-01-11 23:15:24 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('SHP format, authenticated', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp&api_key=1234',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /filename=cartodb-query.zip/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
var tmpfile = '/tmp/myshape.zip';
|
|
|
|
var writeErr = fs.writeFileSync(tmpfile, res.body, 'binary');
|
|
|
|
if (writeErr) {
|
|
|
|
return done(writeErr);
|
|
|
|
}
|
|
|
|
var zf = new zipfile.ZipFile(tmpfile);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.dbf'), 'SHP zipfile does not contain .dbf: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.prj'), 'SHP zipfile does not contain .prj: ' + zf.names);
|
|
|
|
// TODO: check contents of the DBF
|
|
|
|
fs.unlinkSync(tmpfile);
|
|
|
|
done();
|
|
|
|
});
|
2013-01-11 23:15:24 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/66
|
|
|
|
it('SHP format, unauthenticated, with utf8 data', function (done) {
|
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT '♥♦♣♠' as f, st_makepoint(0,0,4326) as the_geom",
|
|
|
|
format: 'shp',
|
|
|
|
filename: 'myshape'
|
|
|
|
});
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var tmpfile = '/tmp/myshape.zip';
|
|
|
|
var writeErr = fs.writeFileSync(tmpfile, res.body, 'binary');
|
|
|
|
if (writeErr) {
|
|
|
|
return done(writeErr);
|
|
|
|
}
|
|
|
|
var zf = new zipfile.ZipFile(tmpfile);
|
|
|
|
var buffer = zf.readFileSync('myshape.dbf');
|
|
|
|
fs.unlinkSync(tmpfile);
|
|
|
|
var strings = buffer.toString();
|
|
|
|
assert.ok(/♥♦♣♠/.exec(strings), "Cannot find '♥♦♣♠' in here:\n" + strings);
|
|
|
|
done();
|
|
|
|
});
|
2013-01-11 23:15:24 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/66
|
|
|
|
it('mixed type geometry', function (done) {
|
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT 'POINT(0 0)'::geometry as g UNION ALL SELECT 'LINESTRING(0 0, 1 0)'::geometry",
|
|
|
|
format: 'shp'
|
|
|
|
});
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepStrictEqual(res.headers['content-disposition'], 'inline');
|
|
|
|
assert.strictEqual(res.statusCode, 400, res.statusCode + ': ' + res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
var error = parsedBody.error[0];
|
|
|
|
var expectedError = /Attempt to write non-point \(LINESTRING\) geometry to point shapefile/g;
|
|
|
|
assert.ok(expectedError.test(error), error);
|
|
|
|
done();
|
|
|
|
});
|
2013-02-26 01:32:28 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/87
|
|
|
|
it('errors are not confused with warnings', function (done) {
|
|
|
|
var query = querystring.stringify({
|
|
|
|
q: [
|
|
|
|
"SELECT 'POINT(0 0)'::geometry as g, 1 as a_very_very_very_long_field_name",
|
|
|
|
"SELECT 'LINESTRING(0 0, 1 0)'::geometry, 2"
|
|
|
|
].join(' UNION ALL '),
|
|
|
|
format: 'shp'
|
|
|
|
});
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepStrictEqual(res.headers['content-disposition'], 'inline');
|
|
|
|
assert.strictEqual(res.statusCode, 400, res.statusCode + ': ' + res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
var error = parsedBody.error[0];
|
|
|
|
var expectedError = /Attempt to write non-point \(LINESTRING\) geometry to point shapefile/g;
|
|
|
|
assert.ok(expectedError.test(error), error);
|
|
|
|
done();
|
|
|
|
});
|
2013-01-12 00:09:22 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('skipfields controls fields included in SHP output', function (done) {
|
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT 111 as skipme, 222 as keepme, 'POINT(0 0)'::geometry as g",
|
|
|
|
format: 'shp',
|
|
|
|
skipfields: 'skipme',
|
|
|
|
filename: 'myshape'
|
|
|
|
});
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:43:12 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var tmpfile = '/tmp/myshape.zip';
|
|
|
|
var writeErr = fs.writeFileSync(tmpfile, res.body, 'binary');
|
|
|
|
if (writeErr) {
|
|
|
|
return done(writeErr);
|
|
|
|
}
|
|
|
|
var zf = new zipfile.ZipFile(tmpfile);
|
|
|
|
var buffer = zf.readFileSync('myshape.dbf');
|
|
|
|
fs.unlinkSync(tmpfile);
|
|
|
|
var strings = buffer.toString();
|
|
|
|
assert.ok(!/skipme/.exec(strings), "Could not skip 'skipme' field:\n" + strings);
|
|
|
|
done();
|
|
|
|
});
|
2013-02-13 20:02:37 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('SHP format, concurrently', function (done) {
|
|
|
|
var concurrency = 1;
|
|
|
|
var waiting = concurrency;
|
|
|
|
function validate (err, res) {
|
2019-12-26 23:43:12 +08:00
|
|
|
assert.ifError(err);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'SHP is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=cartodb-query.zip/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
var tmpfile = '/tmp/myshape.zip';
|
|
|
|
var writeErr = fs.writeFileSync(tmpfile, res.body, 'binary');
|
|
|
|
if (writeErr) {
|
|
|
|
return done(writeErr);
|
|
|
|
}
|
|
|
|
var zf = new zipfile.ZipFile(tmpfile);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.dbf'), 'SHP zipfile does not contain .dbf: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.prj'), 'SHP zipfile does not contain .prj: ' + zf.names);
|
|
|
|
// TODO: check DBF contents
|
|
|
|
fs.unlinkSync(tmpfile);
|
|
|
|
if (!--waiting) {
|
|
|
|
done();
|
|
|
|
}
|
2015-05-13 17:53:14 +08:00
|
|
|
}
|
2019-12-24 01:19:08 +08:00
|
|
|
for (var i = 0; i < concurrency; ++i) {
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
validate
|
|
|
|
);
|
2015-05-13 17:53:14 +08:00
|
|
|
}
|
2019-12-24 01:19:08 +08:00
|
|
|
});
|
2013-01-11 23:15:24 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/111
|
|
|
|
it('point with null first', function (done) {
|
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT null::geometry as g UNION ALL SELECT 'SRID=4326;POINT(0 0)'::geometry",
|
|
|
|
format: 'shp'
|
|
|
|
});
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /filename=cartodb-query.zip/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
var tmpfile = '/tmp/myshape.zip';
|
|
|
|
var writeErr = fs.writeFileSync(tmpfile, res.body, 'binary');
|
|
|
|
if (writeErr) {
|
|
|
|
return done(writeErr);
|
|
|
|
}
|
|
|
|
var zf = new zipfile.ZipFile(tmpfile);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.dbf'), 'SHP zipfile does not contain .dbf: ' + zf.names);
|
|
|
|
assert.ok(_.contains(zf.names, 'cartodb-query.prj'), 'SHP zipfile does not contain .prj: ' + zf.names);
|
|
|
|
// TODO: check contents of the DBF
|
|
|
|
fs.unlinkSync(tmpfile);
|
|
|
|
done();
|
|
|
|
});
|
2013-10-02 18:12:09 +08:00
|
|
|
});
|
|
|
|
|
2015-05-26 19:45:41 +08:00
|
|
|
var limit = 1200;
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('expects ' + limit + ' rows in public table', function (done) {
|
2015-05-26 19:45:41 +08:00
|
|
|
var filename = 'test_1200';
|
|
|
|
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: 'SELECT * from populated_places_simple_reduced limit ' + limit,
|
|
|
|
format: 'shp',
|
|
|
|
filename: filename
|
|
|
|
}),
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmpShpPath = '/tmp/' + filename + '.zip';
|
|
|
|
err = fs.writeFileSync(tmpShpPath, res.body, 'binary');
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var zf = new zipfile.ZipFile(tmpShpPath);
|
|
|
|
zf.names.forEach(function (name) {
|
|
|
|
var buffer = zf.readFileSync(name);
|
|
|
|
var tmpDbfPath = '/tmp/' + name;
|
|
|
|
err = fs.writeFileSync(tmpDbfPath, buffer);
|
2015-05-26 19:45:41 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2019-12-24 01:19:08 +08:00
|
|
|
});
|
|
|
|
shapefile.read('/tmp/' + filename, function (err, collection) {
|
2015-05-26 19:45:41 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(collection.features.length, limit);
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2015-05-26 19:45:41 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('SHP zip, wrong path for zip command should return error', function (done) {
|
2017-04-17 19:31:49 +08:00
|
|
|
global.settings.zipCommand = '/wrong/path';
|
|
|
|
var query = querystring.stringify({
|
2019-12-24 01:19:08 +08:00
|
|
|
q: 'SELECT st_makepoint(0,0,4326) as the_geom',
|
2017-04-17 19:31:49 +08:00
|
|
|
format: 'shp',
|
|
|
|
filename: 'myshape'
|
|
|
|
});
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + query,
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2017-04-17 19:31:49 +08:00
|
|
|
encoding: 'binary',
|
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 400, res.body);
|
2017-04-17 19:31:49 +08:00
|
|
|
var parsedBody = JSON.parse(res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var respBodyPattern = new RegExp('Error executing zip command, {2}Error: spawn(.*)ENOENT', 'i');
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(respBodyPattern.test(parsedBody.error[0]), true);
|
2017-04-17 19:31:49 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-01-11 23:15:24 +08:00
|
|
|
});
|