2011-08-25 03:47:10 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Requires the database and tables setup in config/environments/test.js to exist
|
|
|
|
* Ensure the user is present in the pgbouncer auth file too
|
|
|
|
* TODO: Add OAuth tests.
|
|
|
|
*
|
|
|
|
* To run this test, ensure that cartodb_test_user_1_db metadata exists in Redis for the vizziality.cartodb.com domain
|
|
|
|
*
|
|
|
|
* SELECT 5
|
|
|
|
* HSET rails:users:vizzuality id 1
|
|
|
|
* HSET rails:users:vizzuality database_name cartodb_dev_user_1_db
|
|
|
|
*
|
|
|
|
*/
|
2011-06-13 11:23:02 +08:00
|
|
|
require('../helper');
|
2012-07-13 04:54:12 +08:00
|
|
|
require('../support/assert');
|
2011-09-07 19:05:10 +08:00
|
|
|
|
2011-08-25 03:47:10 +08:00
|
|
|
var app = require(global.settings.app_root + '/app/controllers/app')
|
|
|
|
, assert = require('assert')
|
2012-09-17 17:50:19 +08:00
|
|
|
, querystring = require('querystring')
|
|
|
|
, _ = require('underscore');
|
2011-06-13 11:23:02 +08:00
|
|
|
|
2012-03-16 20:50:07 +08:00
|
|
|
// allow lots of emitters to be set to silence warning
|
|
|
|
app.setMaxListeners(0);
|
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
suite('app.test', function() {
|
|
|
|
|
2011-08-25 03:47:10 +08:00
|
|
|
var real_oauth_header = 'OAuth realm="http://vizzuality.testhost.lan/",oauth_consumer_key="fZeNGv5iYayvItgDYHUbot1Ukb5rVyX6QAg8GaY2",oauth_token="l0lPbtP68ao8NfStCiA3V3neqfM03JKhToxhUQTR",oauth_signature_method="HMAC-SHA1", oauth_signature="o4hx4hWP6KtLyFwggnYB4yPK8xI%3D",oauth_timestamp="1313581372",oauth_nonce="W0zUmvyC4eVL8cBd4YwlH1nnPTbxW0QBYcWkXTwe4",oauth_version="1.0"';
|
2011-06-20 21:39:12 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
// use dec_sep for internationalization
|
|
|
|
var checkDecimals = function(x, dec_sep){
|
2012-07-13 17:11:30 +08:00
|
|
|
var tmp='' + x;
|
2012-07-13 04:54:12 +08:00
|
|
|
if (tmp.indexOf(dec_sep)>-1)
|
|
|
|
return tmp.length-tmp.indexOf(dec_sep)-1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2011-10-05 23:49:54 +08:00
|
|
|
|
2012-07-16 23:16:28 +08:00
|
|
|
test('GET /api/v1/sql', function(done){
|
2011-08-25 03:47:10 +08:00
|
|
|
assert.response(app, {
|
2011-09-07 19:05:10 +08:00
|
|
|
url: '/api/v1/sql',
|
2011-08-25 03:47:10 +08:00
|
|
|
method: 'GET'
|
|
|
|
},{
|
2011-11-22 08:06:14 +08:00
|
|
|
status: 400
|
2012-07-16 23:16:28 +08:00
|
|
|
}, function(res) {
|
|
|
|
assert.deepEqual(JSON.parse(res.body), {"error":["You must indicate a sql query"]});
|
|
|
|
done();
|
2011-08-25 03:47:10 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-06-10 00:34:02 +08:00
|
|
|
|
2011-10-05 23:49:54 +08:00
|
|
|
|
2012-07-13 17:01:32 +08:00
|
|
|
test('GET /api/v1/sql with SQL parameter on SELECT only. No oAuth included ', function(done){
|
2011-08-25 03:47:10 +08:00
|
|
|
assert.response(app, {
|
2011-10-05 23:49:54 +08:00
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&database=cartodb_test_user_1_db',
|
2011-08-25 03:47:10 +08:00
|
|
|
method: 'GET'
|
2012-07-13 17:01:32 +08:00
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
done();
|
2011-08-25 03:47:10 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-08-18 00:27:45 +08:00
|
|
|
|
2011-10-05 23:49:54 +08:00
|
|
|
|
2012-07-13 17:01:32 +08:00
|
|
|
test('GET /api/v1/sql with SQL parameter on SELECT only. no database param, just id using headers', function(done){
|
2011-08-25 03:47:10 +08:00
|
|
|
assert.response(app, {
|
2011-09-07 19:05:10 +08:00
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4',
|
2011-08-25 03:47:10 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2012-07-13 17:01:32 +08:00
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
done();
|
2011-08-25 03:47:10 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-08-25 03:47:10 +08:00
|
|
|
|
2011-10-05 23:49:54 +08:00
|
|
|
|
2012-07-13 17:01:32 +08:00
|
|
|
test('POST /api/v1/sql with SQL parameter on SELECT only. no database param, just id using headers', function(done){
|
2011-09-07 19:05:10 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql',
|
|
|
|
data: querystring.stringify({q: "SELECT * FROM untitle_table_4"}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'POST'
|
2012-07-13 17:01:32 +08:00
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
done();
|
2011-09-07 19:05:10 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-09-07 19:05:10 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql with SQL parameter on INSERT only. oAuth not used, so public user - should fail', function(){
|
2011-08-25 03:47:10 +08:00
|
|
|
assert.response(app, {
|
2011-09-07 19:05:10 +08:00
|
|
|
url: "/api/v1/sql?q=INSERT%20INTO%20untitle_table_4%20(id)%20VALUES%20(1)&database=cartodb_dev_user_1_db",
|
2011-08-25 03:47:10 +08:00
|
|
|
method: 'GET'
|
|
|
|
},{
|
2011-11-22 08:06:14 +08:00
|
|
|
status: 400
|
2011-08-25 03:47:10 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-08-18 00:27:45 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql with SQL parameter on DROP DATABASE only. oAuth not used, so public user - should fail', function(){
|
2011-06-21 00:03:29 +08:00
|
|
|
assert.response(app, {
|
2011-09-07 19:05:10 +08:00
|
|
|
url: "/api/v1/sql?q=DROP%20TABLE%20untitle_table_4&database=cartodb_dev_user_1_db",
|
2011-08-25 03:47:10 +08:00
|
|
|
method: 'GET'
|
2011-06-21 00:03:29 +08:00
|
|
|
},{
|
2011-11-22 08:06:14 +08:00
|
|
|
status: 400
|
2011-06-21 00:03:29 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-08-25 03:47:10 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql with SQL parameter on INSERT only. header based db - should fail', function(){
|
2011-06-21 00:22:46 +08:00
|
|
|
assert.response(app, {
|
2011-09-07 19:05:10 +08:00
|
|
|
url: "/api/v1/sql?q=INSERT%20INTO%20untitle_table_4%20(id)%20VALUES%20(1)",
|
2011-08-25 03:47:10 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2011-06-21 00:22:46 +08:00
|
|
|
},{
|
2011-11-22 08:06:14 +08:00
|
|
|
status: 400
|
2011-06-21 00:22:46 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-08-25 03:47:10 +08:00
|
|
|
|
2012-09-17 22:56:25 +08:00
|
|
|
// Check results from INSERT
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/13
|
|
|
|
test('INSERT returns affected rows', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
|
|
|
url: "/api/v1/sql?api_key=1234&"
|
|
|
|
+ querystring.stringify({q:
|
|
|
|
"INSERT INTO private_table(name) VALUES('noret1') UNION VALUES('noret2')"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.localhost.lan:8080' },
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
var out = JSON.parse(res.body);
|
|
|
|
assert.ok(out.hasOwnProperty('time'));
|
|
|
|
assert.equal(out.total_rows, 2);
|
|
|
|
assert.equal(out.rows.length, 0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check results from UPDATE
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/13
|
|
|
|
test('UPDATE returns affected rows', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
|
|
|
url: "/api/v1/sql?api_key=1234&"
|
|
|
|
+ querystring.stringify({q:
|
|
|
|
"UPDATE private_table SET name = upper(name) WHERE name in ('noret1', 'noret2')"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.localhost.lan:8080' },
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
var out = JSON.parse(res.body);
|
|
|
|
assert.ok(out.hasOwnProperty('time'));
|
|
|
|
assert.equal(out.total_rows, 2);
|
|
|
|
assert.equal(out.rows.length, 0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check results from DELETE
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/13
|
|
|
|
test('DELETE returns affected rows', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
|
|
|
url: "/api/v1/sql?api_key=1234&"
|
|
|
|
+ querystring.stringify({q:
|
|
|
|
"DELETE FROM private_table WHERE name in ('NORET1', 'NORET2')"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.localhost.lan:8080' },
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
var out = JSON.parse(res.body);
|
|
|
|
assert.ok(out.hasOwnProperty('time'));
|
|
|
|
assert.equal(out.total_rows, 2);
|
|
|
|
assert.equal(out.rows.length, 0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-09-17 17:50:19 +08:00
|
|
|
// Check results from INSERT .. RETURNING
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/50
|
|
|
|
test('INSERT with RETURNING returns all results', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
|
|
|
url: "/api/v1/sql?api_key=1234&"
|
|
|
|
+ querystring.stringify({q:
|
|
|
|
"INSERT INTO private_table(name) VALUES('test') RETURNING upper(name), reverse(name)"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.localhost.lan:8080' },
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
var out = JSON.parse(res.body);
|
|
|
|
assert.ok(out.hasOwnProperty('time'));
|
|
|
|
assert.equal(out.total_rows, 1);
|
|
|
|
assert.equal(out.rows.length, 1);
|
|
|
|
assert.equal(_.keys(out.rows[0]).length, 2);
|
|
|
|
assert.equal(out.rows[0].upper, 'TEST');
|
|
|
|
assert.equal(out.rows[0].reverse, 'tset');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check results from UPDATE .. RETURNING
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/50
|
|
|
|
test('UPDATE with RETURNING returns all results', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
|
|
|
url: "/api/v1/sql?api_key=1234&"
|
|
|
|
+ querystring.stringify({q:
|
|
|
|
"UPDATE private_table SET name = 'tost' WHERE name = 'test' RETURNING upper(name), reverse(name)"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.localhost.lan:8080' },
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
var out = JSON.parse(res.body);
|
|
|
|
assert.ok(out.hasOwnProperty('time'));
|
|
|
|
assert.equal(out.total_rows, 1);
|
|
|
|
assert.equal(out.rows.length, 1);
|
|
|
|
assert.equal(_.keys(out.rows[0]).length, 2);
|
|
|
|
assert.equal(out.rows[0].upper, 'TOST');
|
|
|
|
assert.equal(out.rows[0].reverse, 'tsot');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-09-17 22:50:15 +08:00
|
|
|
// Check results from DELETE .. RETURNING
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/50
|
|
|
|
test('DELETE with RETURNING returns all results', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
|
|
|
url: "/api/v1/sql?api_key=1234&"
|
|
|
|
+ querystring.stringify({q:
|
|
|
|
"DELETE FROM private_table WHERE name = 'tost' RETURNING name"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.localhost.lan:8080' },
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
var out = JSON.parse(res.body);
|
|
|
|
assert.ok(out.hasOwnProperty('time'));
|
|
|
|
assert.equal(out.total_rows, 1);
|
|
|
|
assert.equal(out.rows.length, 1);
|
|
|
|
assert.equal(_.keys(out.rows[0]).length, 1);
|
|
|
|
assert.equal(out.rows[0].name, 'tost');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql with SQL parameter on DROP DATABASE only.header based db - should fail', function(){
|
2011-08-25 03:47:10 +08:00
|
|
|
assert.response(app, {
|
2011-09-07 19:05:10 +08:00
|
|
|
url: "/api/v1/sql?q=DROP%20TABLE%20untitle_table_4",
|
2011-08-25 03:47:10 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{
|
2011-11-22 08:06:14 +08:00
|
|
|
status: 400
|
2011-08-25 03:47:10 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-10-05 23:49:54 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql with SQL parameter and geojson format, ensuring content-disposition set to geojson', function(done){
|
2011-10-28 19:11:18 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2012-07-13 17:01:32 +08:00
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2011-10-28 19:11:18 +08:00
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /filename=cartodb-query.geojson/gi.test(cd));
|
2012-07-13 04:54:12 +08:00
|
|
|
done();
|
2011-10-28 19:11:18 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-10-28 19:11:18 +08:00
|
|
|
|
2012-09-11 18:22:27 +08:00
|
|
|
test('GET /api/v1/sql with SVG format', function(done){
|
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT 1 as cartodb_id, ST_MakeLine(ST_MakePoint(10, 10), ST_MakePoint(1034, 778)) AS the_geom ",
|
|
|
|
format: "svg"
|
|
|
|
});
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.ok(/filename=cartodb-query.svg/gi.test(cd), cd);
|
|
|
|
assert.equal(res.header('Content-Type'), 'image/svg+xml; charset=utf-8');
|
|
|
|
assert.ok( res.body.indexOf('<path d="M 0 768 L 1024 0" />') > 0, res.body );
|
|
|
|
// TODO: test viewBox
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('GET /api/v1/sql with SVG format and centered point', function(done){
|
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT 1 as cartodb_id, ST_MakePoint(5000, -54) AS the_geom ",
|
|
|
|
format: "svg"
|
|
|
|
});
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.ok(/filename=cartodb-query.svg/gi.test(cd), cd);
|
|
|
|
assert.equal(res.header('Content-Type'), 'image/svg+xml; charset=utf-8');
|
|
|
|
assert.ok( res.body.indexOf('cx="0" cy="0"') > 0, res.body );
|
|
|
|
// TODO: test viewBox
|
|
|
|
// TODO: test radius
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('GET /api/v1/sql with SVG format and trimmed decimals', function(done){
|
|
|
|
var queryobj = {
|
|
|
|
q: "SELECT 1 as cartodb_id, 'LINESTRING(0 0, 1024 768, 500.123456 600.98765432)'::geometry AS the_geom ",
|
|
|
|
format: "svg",
|
|
|
|
dp: 2
|
|
|
|
};
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify(queryobj),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.ok(/filename=cartodb-query.svg/gi.test(cd), cd);
|
|
|
|
assert.equal(res.header('Content-Type'), 'image/svg+xml; charset=utf-8');
|
|
|
|
assert.ok( res.body.indexOf('<path d="M 0 768 L 1024 0 500.12 167.01" />') > 0, res.body );
|
|
|
|
// TODO: test viewBox
|
|
|
|
|
|
|
|
queryobj.dp = 3;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify(queryobj),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.ok(/filename=cartodb-query.svg/gi.test(cd), cd);
|
|
|
|
assert.equal(res.header('Content-Type'), 'image/svg+xml; charset=utf-8');
|
|
|
|
assert.ok( res.body.indexOf('<path d="M 0 768 L 1024 0 500.123 167.012" />') > 0, res.body );
|
|
|
|
// TODO: test viewBox
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql with SQL parameter and no format, ensuring content-disposition set to json', function(done){
|
2011-10-28 19:11:18 +08:00
|
|
|
assert.response(app, {
|
2011-11-09 07:35:59 +08:00
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4',
|
2011-10-28 19:11:18 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2012-07-13 17:01:32 +08:00
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2011-10-28 19:11:18 +08:00
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /filename=cartodb-query.json/gi.test(cd));
|
2012-07-13 04:54:12 +08:00
|
|
|
done();
|
2011-10-28 19:11:18 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-11-09 07:35:59 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql ensure cross domain set on errors', function(done){
|
2012-04-13 07:30:45 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*gadfgadfg%20FROM%20untitle_table_4',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{
|
|
|
|
status: 400
|
|
|
|
}, function(res){
|
|
|
|
var cd = res.header('Access-Control-Allow-Origin');
|
|
|
|
assert.equal(cd, '*');
|
2012-07-13 04:54:12 +08:00
|
|
|
done();
|
2012-04-13 07:30:45 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2012-04-13 07:30:45 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql as geojson limiting decimal places', function(done){
|
2011-11-09 07:35:59 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson&dp=1',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2012-07-13 17:01:32 +08:00
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2011-11-09 07:35:59 +08:00
|
|
|
var result = JSON.parse(res.body);
|
|
|
|
assert.equal(1, checkDecimals(result.features[0].geometry.coordinates[0], '.'));
|
2012-07-13 04:54:12 +08:00
|
|
|
done();
|
2011-11-09 07:35:59 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-11-09 07:35:59 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql as geojson with default dp as 6', function(done){
|
2012-06-07 01:57:50 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2012-07-13 17:01:32 +08:00
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2012-06-07 01:57:50 +08:00
|
|
|
var result = JSON.parse(res.body);
|
|
|
|
assert.equal(6, checkDecimals(result.features[0].geometry.coordinates[0], '.'));
|
2012-07-13 04:54:12 +08:00
|
|
|
done();
|
2012-06-07 01:57:50 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2012-06-07 01:57:50 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql as csv', function(done){
|
2012-04-13 07:30:45 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20cartodb_id,ST_AsEWKT(the_geom)%20as%20geom%20FROM%20untitle_table_4%20LIMIT%201&format=csv',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2012-07-13 17:01:32 +08:00
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2012-04-13 22:37:09 +08:00
|
|
|
var body = "cartodb_id,geom\r\n1,SRID=4326;POINT(-3.699732 40.423012)";
|
|
|
|
assert.equal(body, res.body);
|
2012-07-13 04:54:12 +08:00
|
|
|
done();
|
2012-04-13 22:37:09 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2012-04-13 22:37:09 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET /api/v1/sql as csv, properly escaped', function(done){
|
2012-04-13 22:37:09 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20cartodb_id,%20address%20FROM%20untitle_table_4%20LIMIT%201&format=csv',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2012-07-13 17:01:32 +08:00
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2012-04-13 22:37:09 +08:00
|
|
|
var body = 'cartodb_id,address\r\n1,"Calle de Pérez Galdós 9, Madrid, Spain"';
|
2012-04-13 07:30:45 +08:00
|
|
|
assert.equal(body, res.body);
|
2012-07-13 04:54:12 +08:00
|
|
|
done();
|
2012-04-13 07:30:45 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2012-04-13 07:30:45 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('cannot GET system tables', function(done){
|
2011-11-22 07:06:38 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20pg_attribute',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{
|
|
|
|
status: 403
|
2012-07-13 04:54:12 +08:00
|
|
|
}, function() { done(); });
|
|
|
|
});
|
2011-11-22 07:06:38 +08:00
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
test('GET decent error if domain is incorrect', function(done){
|
2012-06-07 02:47:21 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
|
|
|
|
headers: {host: 'vizzualinot.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{
|
|
|
|
status: 404
|
|
|
|
}, function(res){
|
|
|
|
var result = JSON.parse(res.body);
|
|
|
|
assert.equal(result.error[0],"Sorry, we can't find this CartoDB. Please check that you have entered the correct domain.");
|
2012-07-13 04:54:12 +08:00
|
|
|
done();
|
2012-06-07 02:47:21 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2012-06-07 02:47:21 +08:00
|
|
|
|
2012-09-17 19:04:16 +08:00
|
|
|
test('GET decent error if SQL is broken', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({q:
|
|
|
|
'SELECT star FROM this and that'
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res){
|
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
|
|
|
|
var result = JSON.parse(res.body);
|
|
|
|
// NOTE: actual error message may be slighly different, possibly worth a regexp here
|
|
|
|
assert.equal(result.error[0], 'syntax error at or near "and"');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-07-24 17:19:06 +08:00
|
|
|
// CSV tests
|
|
|
|
test('CSV format', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=csv',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /filename=cartodb-query.csv/gi.test(cd));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|