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.
|
|
|
|
*
|
2013-07-25 00:43:38 +08:00
|
|
|
* To run this test, ensure that cartodb_test_user_1_db metadata exists
|
|
|
|
* in Redis for the vizzuality.cartodb.com domain
|
2011-08-25 03:47:10 +08:00
|
|
|
*
|
|
|
|
* SELECT 5
|
|
|
|
* HSET rails:users:vizzuality id 1
|
2013-04-11 19:35:11 +08:00
|
|
|
* HSET rails:users:vizzuality database_name cartodb_test_user_1_db
|
2011-08-25 03:47:10 +08:00
|
|
|
*
|
|
|
|
*/
|
2011-06-13 11:23:02 +08:00
|
|
|
require('../helper');
|
2011-09-07 19:05:10 +08:00
|
|
|
|
2015-12-04 01:33:17 +08:00
|
|
|
var app = require(global.settings.app_root + '/app/app')();
|
2015-05-13 17:21:44 +08:00
|
|
|
var assert = require('../support/assert');
|
|
|
|
var querystring = require('querystring');
|
|
|
|
var _ = require('underscore');
|
|
|
|
var step = require('step');
|
2012-11-12 19:37:34 +08:00
|
|
|
|
2011-06-13 11:23:02 +08:00
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
describe('app.test', function() {
|
2012-07-13 04:54:12 +08:00
|
|
|
|
2013-07-22 23:21:38 +08:00
|
|
|
var expected_cache_control = 'no-cache,max-age=31536000,must-revalidate,public';
|
|
|
|
var expected_rw_cache_control = 'no-cache,max-age=0,must-revalidate,public';
|
2012-11-13 02:14:20 +08:00
|
|
|
var expected_cache_control_persist = 'public,max-age=31536000';
|
2014-03-13 20:40:56 +08:00
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/v1/version', function(done){
|
2014-03-13 20:40:56 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/version',
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
var sqlapi_version = require(__dirname + '/../../package.json').version;
|
|
|
|
assert.ok(parsed.hasOwnProperty('cartodb_sql_api'), "No 'cartodb_sql_api' version in " + parsed);
|
|
|
|
assert.equal(parsed.cartodb_sql_api, sqlapi_version);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2011-06-20 21:39:12 +08:00
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('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) {
|
2013-02-13 20:32:34 +08:00
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
2013-05-23 17:49:23 +08:00
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
|
|
|
assert.deepEqual(JSON.parse(res.body), {"error":["You must indicate a sql query"]});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test base_url setting
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/whatever/sql', function(done){
|
2013-05-23 17:49:23 +08:00
|
|
|
assert.response(app, {
|
2013-05-24 19:51:39 +08:00
|
|
|
url: '/api/whatever/sql?q=SELECT%201',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
2013-05-23 17:49:23 +08:00
|
|
|
method: 'GET'
|
|
|
|
},{
|
|
|
|
}, function(res) {
|
2013-05-24 19:51:39 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2012-07-16 23:16:28 +08:00
|
|
|
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
|
|
|
|
2013-05-24 20:21:13 +08:00
|
|
|
// Test CORS headers with GET
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/whatever/sql', function(done){
|
2013-05-24 20:21:13 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/whatever/sql?q=SELECT%201',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{
|
|
|
|
}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.equal(
|
|
|
|
res.headers['access-control-allow-headers'], 'X-Requested-With, X-Prototype-Version, X-CSRF-Token'
|
|
|
|
);
|
2013-05-24 20:21:13 +08:00
|
|
|
assert.equal(res.headers['access-control-allow-origin'], '*');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-04 22:47:59 +08:00
|
|
|
// Test that OPTIONS does not run queries
|
2015-05-13 17:21:44 +08:00
|
|
|
it('OPTIONS /api/x/sql', function(done){
|
2013-05-24 20:21:13 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/x/sql?q=syntax%20error',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'OPTIONS'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
assert.equal(res.body, '');
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.equal(
|
|
|
|
res.headers['access-control-allow-headers'], 'X-Requested-With, X-Prototype-Version, X-CSRF-Token'
|
|
|
|
);
|
2013-05-24 20:21:13 +08:00
|
|
|
assert.equal(res.headers['access-control-allow-origin'], '*');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2011-10-05 23:49:54 +08:00
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('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',
|
2014-07-04 22:47:59 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
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);
|
2012-10-15 19:20:37 +08:00
|
|
|
// Check cache headers
|
2014-06-26 17:22:40 +08:00
|
|
|
assert.equal(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:public.untitle_table_4');
|
2012-10-15 19:20:37 +08:00
|
|
|
assert.equal(res.headers['cache-control'], expected_cache_control);
|
2012-07-13 17:01:32 +08:00
|
|
|
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
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('cache_policy=persist', function(done){
|
2012-11-13 02:14:20 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&database=cartodb_test_user_1_db&cache_policy=persist',
|
2014-07-04 22:47:59 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
2012-11-13 02:14:20 +08:00
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
// Check cache headers
|
2013-07-22 23:21:38 +08:00
|
|
|
assert.ok(res.headers.hasOwnProperty('x-cache-channel'));
|
2013-10-18 19:29:06 +08:00
|
|
|
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/105
|
2014-06-26 17:22:40 +08:00
|
|
|
assert.equal(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:public.untitle_table_4');
|
2012-11-13 02:14:20 +08:00
|
|
|
assert.equal(res.headers['cache-control'], expected_cache_control_persist);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('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
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /user/vizzuality/api/v1/sql with SQL parameter on SELECT only', function(done){
|
2015-01-30 18:37:29 +08:00
|
|
|
assert.response(app, {
|
2015-04-08 22:13:04 +08:00
|
|
|
url: '/user/vizzuality/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4',
|
2015-01-30 18:37:29 +08:00
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-11-18 20:31:11 +08:00
|
|
|
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/121
|
2015-05-13 17:21:44 +08:00
|
|
|
it('SELECT from user-specific database', function(done){
|
2013-11-18 19:21:30 +08:00
|
|
|
var backupDBHost = global.settings.db_host;
|
|
|
|
global.settings.db_host = '6.6.6.6';
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT+2+as+n',
|
|
|
|
headers: {host: 'cartodb250user.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
global.settings.db_host = backupDBHost;
|
|
|
|
var err = null;
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ": " + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.equal(parsed.rows.length, 1);
|
|
|
|
assert.equal(parsed.rows[0].n, 2);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-18 20:31:11 +08:00
|
|
|
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/120
|
2015-05-13 17:21:44 +08:00
|
|
|
it('SELECT with user-specific password', function(done){
|
2013-11-18 20:31:11 +08:00
|
|
|
var backupDBUserPass = global.settings.db_user_pass;
|
|
|
|
global.settings.db_user_pass = '<%= user_password %>';
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT+2+as+n&api_key=1234',
|
|
|
|
headers: {host: 'cartodb250user.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
global.settings.db_user_pass = backupDBUserPass;
|
|
|
|
var err = null;
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ": " + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.equal(parsed.rows.length, 1);
|
|
|
|
assert.equal(parsed.rows[0].n, 2);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/v1/sql with SQL parameter on SELECT only. no database param, just id using headers. Authenticated.',
|
2012-10-15 19:20:37 +08:00
|
|
|
function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20cartodb_id*2%20FROM%20untitle_table_4&api_key=1234',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
// Check cache headers
|
2014-06-26 17:22:40 +08:00
|
|
|
assert.equal(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:public.untitle_table_4');
|
2012-10-15 19:20:37 +08:00
|
|
|
assert.equal(res.headers['cache-control'], expected_cache_control);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-02-18 23:14:15 +08:00
|
|
|
// Test for https://github.com/Vizzuality/CartoDB-SQL-API/issues/85
|
2015-05-13 17:21:44 +08:00
|
|
|
it("paging doesn't break x-cache-channel",
|
2013-02-18 23:14:15 +08:00
|
|
|
function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
2013-05-24 16:22:17 +08:00
|
|
|
// note: select casing intentionally mixed
|
|
|
|
q: 'selECT cartodb_id*3 FROM untitle_table_4',
|
2013-02-18 23:14:15 +08:00
|
|
|
api_key: '1234',
|
|
|
|
rows_per_page: 1,
|
|
|
|
page: 2
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2014-06-26 17:22:40 +08:00
|
|
|
assert.equal(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:public.untitle_table_4');
|
2013-06-06 21:24:57 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.equal(parsed.rows.length, 1);
|
2013-02-18 23:14:15 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-03 23:52:55 +08:00
|
|
|
// Test page and rows_per_page params
|
2015-05-13 17:21:44 +08:00
|
|
|
it("paging", function(done){
|
2014-03-27 18:18:02 +08:00
|
|
|
var sql = 'SELECT * FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(v)';
|
|
|
|
var pr = [ [2,3], [0,4] ]; // page and rows
|
|
|
|
var methods = [ 'GET', 'POST' ];
|
|
|
|
var authorized = 0;
|
|
|
|
var testing = 0;
|
|
|
|
var method = 0;
|
2015-05-13 17:21:44 +08:00
|
|
|
// jshint maxcomplexity:7
|
2014-03-27 18:18:02 +08:00
|
|
|
var testNext = function() {
|
|
|
|
if ( testing >= pr.length ) {
|
|
|
|
if ( method+1 >= methods.length ) {
|
|
|
|
if ( authorized ) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
authorized = 1;
|
|
|
|
method = 0;
|
|
|
|
testing = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
testing = 0;
|
|
|
|
++method;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var prcur = pr[testing++];
|
2015-05-13 17:21:44 +08:00
|
|
|
console.log("Test " + testing + "/" + pr.length + " method " + methods[method] + " " +
|
|
|
|
( authorized ? "authenticated" : "" ) );
|
2014-03-27 18:18:02 +08:00
|
|
|
var page = prcur[0];
|
|
|
|
var nrows = prcur[1];
|
|
|
|
var data_obj = {
|
|
|
|
q: sql,
|
|
|
|
rows_per_page: nrows,
|
|
|
|
page: page
|
|
|
|
};
|
2015-05-13 17:21:44 +08:00
|
|
|
if ( authorized ) {
|
|
|
|
data_obj.api_key = '1234';
|
|
|
|
}
|
2014-03-27 18:18:02 +08:00
|
|
|
var data = querystring.stringify(data_obj);
|
|
|
|
var req = {
|
|
|
|
url: '/api/v1/sql',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'}
|
|
|
|
};
|
2015-05-13 17:21:44 +08:00
|
|
|
if ( methods[method] === 'GET' ) {
|
2014-03-27 18:18:02 +08:00
|
|
|
req.method = 'GET';
|
|
|
|
req.url += '?' + data;
|
|
|
|
} else {
|
|
|
|
req.method = 'POST';
|
|
|
|
req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
|
|
req.data = data;
|
|
|
|
}
|
|
|
|
assert.response(app, req, {}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.equal(parsed.rows.length, nrows);
|
|
|
|
for (var i=0; i<nrows; ++i) {
|
|
|
|
var obt = parsed.rows[i].v;
|
|
|
|
var exp = page * nrows + i + 1;
|
|
|
|
assert.equal(obt, exp, "Value " + i + " in page " + page + " is " + obt + ", expected " + exp);
|
|
|
|
}
|
|
|
|
testNext();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
testNext();
|
2013-12-03 23:52:55 +08:00
|
|
|
});
|
|
|
|
|
2014-03-27 19:47:34 +08:00
|
|
|
// Test paging with WITH queries
|
2015-05-13 17:21:44 +08:00
|
|
|
it("paging starting with comment", function(done){
|
2014-03-27 19:47:34 +08:00
|
|
|
var sql = "-- this is a comment\n" +
|
|
|
|
"SELECT * FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(v)";
|
|
|
|
var nrows = 3;
|
|
|
|
var page = 2;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: sql,
|
|
|
|
rows_per_page: nrows,
|
|
|
|
page: page
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
}, {}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.equal(parsed.rows.length, 3);
|
|
|
|
for (var i=0; i<nrows; ++i) {
|
|
|
|
var obt = parsed.rows[i].v;
|
|
|
|
var exp = page * nrows + i + 1;
|
|
|
|
assert.equal(obt, exp, "Value " + i + " in page " + page + " is " + obt + ", expected " + exp);
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('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
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/v1/sql with INSERT. oAuth not used, so public user - should fail', function(done){
|
2011-08-25 03:47:10 +08:00
|
|
|
assert.response(app, {
|
2015-05-13 17:21:44 +08:00
|
|
|
url: "/api/v1/sql?q=INSERT%20INTO%20untitle_table_4%20(cartodb_id)%20VALUES%20(1e4)" +
|
|
|
|
"&database=cartodb_test_user_1_db",
|
2014-07-04 22:47:59 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
2011-08-25 03:47:10 +08:00
|
|
|
method: 'GET'
|
|
|
|
},{
|
2013-02-13 20:32:34 +08:00
|
|
|
}, function(res) {
|
2014-09-17 05:57:11 +08:00
|
|
|
assert.equal(res.statusCode, 401, res.statusCode + ': ' + res.body);
|
2013-02-13 20:32:34 +08:00
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
2014-07-04 22:47:59 +08:00
|
|
|
assert.deepEqual(JSON.parse(res.body),
|
|
|
|
{"error":["permission denied for relation untitle_table_4"]}
|
2013-02-13 20:32:34 +08:00
|
|
|
);
|
|
|
|
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
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/v1/sql with DROP TABLE. oAuth not used, so public user - should fail', function(done){
|
2011-06-21 00:03:29 +08:00
|
|
|
assert.response(app, {
|
2013-04-11 19:35:11 +08:00
|
|
|
url: "/api/v1/sql?q=DROP%20TABLE%20untitle_table_4&database=cartodb_test_user_1_db",
|
2014-07-04 22:47:59 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
2011-08-25 03:47:10 +08:00
|
|
|
method: 'GET'
|
2011-06-21 00:03:29 +08:00
|
|
|
},{
|
2013-02-13 20:32:34 +08:00
|
|
|
}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
|
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
2014-07-04 22:47:59 +08:00
|
|
|
assert.deepEqual(JSON.parse(res.body),
|
2013-04-11 19:35:11 +08:00
|
|
|
{"error":["must be owner of relation untitle_table_4"]}
|
2013-02-13 20:32:34 +08:00
|
|
|
);
|
|
|
|
done();
|
2011-06-21 00:03:29 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-08-25 03:47:10 +08:00
|
|
|
|
2016-05-20 20:42:53 +08:00
|
|
|
it('GET /api/v1/sql with INSERT. header based db - should fail', function (done) {
|
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'
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
2011-11-22 08:06:14 +08:00
|
|
|
status: 400
|
2016-05-20 20:42:53 +08:00
|
|
|
}, function (res, err) {
|
|
|
|
done(err);
|
2011-06-21 00:22:46 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-08-25 03:47:10 +08:00
|
|
|
|
2014-07-04 22:47:59 +08:00
|
|
|
// Check results from INSERT
|
2012-09-17 22:56:25 +08:00
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/13
|
2015-05-13 17:21:44 +08:00
|
|
|
it('INSERT returns affected rows', function(done){
|
2012-09-17 22:56:25 +08:00
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2015-05-13 17:21:44 +08:00
|
|
|
url: "/api/v1/sql?api_key=1234&" + querystring.stringify({q:
|
2012-09-17 22:56:25 +08:00
|
|
|
"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);
|
2012-10-15 19:20:37 +08:00
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2013-07-22 23:21:38 +08:00
|
|
|
assert.ok(!res.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
|
2012-09-17 22:56:25 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check results from UPDATE
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/13
|
2015-05-13 17:21:44 +08:00
|
|
|
it('UPDATE returns affected rows', function(done){
|
2012-09-17 22:56:25 +08:00
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2015-05-13 17:21:44 +08:00
|
|
|
url: "/api/v1/sql?api_key=1234&" + querystring.stringify({q:
|
2012-09-17 22:56:25 +08:00
|
|
|
"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);
|
2012-10-15 19:20:37 +08:00
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2013-07-22 23:21:38 +08:00
|
|
|
assert.ok(!res.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
|
2012-09-17 22:56:25 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check results from DELETE
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/13
|
2015-05-13 17:21:44 +08:00
|
|
|
it('DELETE returns affected rows', function(done){
|
2012-09-17 22:56:25 +08:00
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2015-05-13 17:21:44 +08:00
|
|
|
url: "/api/v1/sql?api_key=1234&" + querystring.stringify({q:
|
2012-09-17 22:56:25 +08:00
|
|
|
"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);
|
2012-10-15 19:20:37 +08:00
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2013-07-22 23:21:38 +08:00
|
|
|
assert.ok(!res.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
|
2012-09-17 22:56:25 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-09-17 17:50:19 +08:00
|
|
|
// Check results from INSERT .. RETURNING
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/50
|
2015-05-13 17:21:44 +08:00
|
|
|
it('INSERT with RETURNING returns all results', function(done){
|
2012-09-17 17:50:19 +08:00
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2015-05-13 17:21:44 +08:00
|
|
|
url: "/api/v1/sql?api_key=1234&" + querystring.stringify({q:
|
2012-09-17 17:50:19 +08:00
|
|
|
"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
|
2015-05-13 17:21:44 +08:00
|
|
|
it('UPDATE with RETURNING returns all results', function(done){
|
2012-09-17 17:50:19 +08:00
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2015-05-13 17:21:44 +08:00
|
|
|
url: "/api/v1/sql?api_key=1234&" + querystring.stringify({q:
|
2012-09-17 17:50:19 +08:00
|
|
|
"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
|
2015-05-13 17:21:44 +08:00
|
|
|
it('DELETE with RETURNING returns all results', function(done){
|
2012-09-17 22:50:15 +08:00
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2015-05-13 17:21:44 +08:00
|
|
|
url: "/api/v1/sql?api_key=1234&" + querystring.stringify({q:
|
2012-09-17 22:50:15 +08:00
|
|
|
"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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/v1/sql with SQL parameter on DROP TABLE. should fail', 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=DROP%20TABLE%20untitle_table_4",
|
2011-08-25 03:47:10 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2013-02-13 20:32:34 +08:00
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
|
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
2014-07-04 22:47:59 +08:00
|
|
|
assert.deepEqual(JSON.parse(res.body),
|
|
|
|
{"error":["must be owner of relation untitle_table_4"]}
|
2013-02-13 20:32:34 +08:00
|
|
|
);
|
|
|
|
done();
|
2011-08-25 03:47:10 +08:00
|
|
|
});
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
2011-10-05 23:49:54 +08:00
|
|
|
|
2013-07-08 23:36:53 +08:00
|
|
|
// Check X-Cache-Channel when querying "updated_at" fields
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/99
|
2015-05-13 17:21:44 +08:00
|
|
|
it('Field name is not confused with UPDATE operation', function(done){
|
2013-07-08 23:36:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2015-05-13 17:21:44 +08:00
|
|
|
url: "/api/v1/sql?api_key=1234&" + querystring.stringify({q:
|
2013-07-08 23:36:53 +08:00
|
|
|
"SELECT min(updated_at) FROM private_table"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.localhost.lan:8080' },
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
2014-06-26 17:22:40 +08:00
|
|
|
assert.equal(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:public.private_table');
|
2013-07-08 23:36:53 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('CREATE TABLE with GET and auth', function(done){
|
2012-10-15 19:20:37 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
2012-10-15 19:40:04 +08:00
|
|
|
q: 'CREATE TABLE test_table(a int)',
|
2012-10-15 19:20:37 +08:00
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2013-07-22 23:21:38 +08:00
|
|
|
assert.ok(!res.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
|
2012-10-15 19:20:37 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-10 00:24:48 +08:00
|
|
|
// See http://github.com/CartoDB/CartoDB-SQL-API/issues/127
|
2015-05-13 17:21:44 +08:00
|
|
|
it('SELECT INTO with paging ', function(done){
|
2014-01-10 00:24:48 +08:00
|
|
|
var esc_tabname = 'test ""select into""'; // escaped ident
|
2015-05-13 17:21:44 +08:00
|
|
|
step(
|
2014-01-10 00:24:48 +08:00
|
|
|
function select_into() {
|
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: 'SELECT generate_series(1,10) InTO "' + esc_tabname + '"',
|
|
|
|
rows_per_page: 1, page: 1,
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function check_res_test_fake_into_1(err, res) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2014-01-10 00:24:48 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: 'SELECT \' INTO "c"\' FROM "' + esc_tabname + '"',
|
|
|
|
rows_per_page: 1, page: 1,
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function check_res_drop_table(err, res) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2014-01-10 00:24:48 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
var out = JSON.parse(res.body);
|
|
|
|
assert.equal(out.total_rows, 1); // windowing works
|
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: 'DROP TABLE "' + esc_tabname + '"',
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function check_drop(err, res) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2014-01-10 00:24:48 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
2015-05-13 17:21:44 +08:00
|
|
|
done(err);
|
2014-01-10 00:24:48 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2013-01-17 16:59:48 +08:00
|
|
|
// Test effects of COPY
|
|
|
|
// See https://github.com/Vizzuality/cartodb-management/issues/1502
|
2015-05-13 17:21:44 +08:00
|
|
|
it('COPY TABLE with GET and auth', function(done){
|
2013-01-17 16:59:48 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: 'COPY test_table FROM stdin;',
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
// We expect a problem, actually
|
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
|
2013-02-13 20:32:34 +08:00
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
2013-01-17 16:59:48 +08:00
|
|
|
assert.deepEqual(JSON.parse(res.body), {"error":["COPY from stdin failed: No source stream defined"]});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2012-10-15 19:20:37 +08:00
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('COPY TABLE with GET and auth', function(done){
|
2013-01-22 00:39:07 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: "COPY test_table to '/tmp/x';",
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
// We expect a problem, actually
|
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
|
2013-02-13 20:32:34 +08:00
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
2014-11-21 19:59:48 +08:00
|
|
|
assert.deepEqual(JSON.parse(res.body), {
|
|
|
|
error: ["must be superuser to COPY to or from a file"],
|
|
|
|
hint: "Anyone can COPY to stdout or from stdin. psql's \\copy command also works for anyone."
|
|
|
|
});
|
2013-01-22 00:39:07 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('ALTER TABLE with GET and auth', function(done){
|
2012-10-15 19:40:04 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: 'ALTER TABLE test_table ADD b int',
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2013-07-22 23:21:38 +08:00
|
|
|
assert.ok(!res.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
|
2012-10-15 19:40:04 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('multistatement insert, alter, select, begin, commit', function(done){
|
2013-02-19 01:39:09 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
2015-05-13 17:21:44 +08:00
|
|
|
q: 'BEGIN; DELETE FROM test_table; COMMIT; BEGIN; INSERT INTO test_table(b) values (5); COMMIT; ' +
|
|
|
|
'ALTER TABLE test_table ALTER b TYPE float USING b::float/2; SELECT b FROM test_table;',
|
2013-02-19 01:39:09 +08:00
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
assert.equal(parsedBody.total_rows, 1);
|
|
|
|
assert.deepEqual(parsedBody.rows[0], {b:2.5});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('TRUNCATE TABLE with GET and auth', function(done){
|
2013-05-07 00:21:22 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: 'TRUNCATE TABLE test_table',
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
2013-07-22 23:21:38 +08:00
|
|
|
assert.ok(!res.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
|
2013-05-07 00:21:22 +08:00
|
|
|
var pbody = JSON.parse(res.body);
|
|
|
|
assert.equal(pbody.rows.length, 0);
|
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: 'SELECT count(*) FROM test_table',
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
2014-06-26 17:22:40 +08:00
|
|
|
assert.equal(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:public.test_table');
|
2013-05-07 00:21:22 +08:00
|
|
|
assert.equal(res.headers['cache-control'], expected_cache_control);
|
|
|
|
var pbody = JSON.parse(res.body);
|
|
|
|
assert.equal(pbody.total_rows, 1);
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.equal(pbody.rows[0].count, 0);
|
2013-05-07 00:21:22 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('REINDEX TABLE with GET and auth', function(done){
|
2013-07-25 00:43:38 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: ' ReINdEX TABLE test_table',
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
assert.ok(!res.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
|
|
|
|
var pbody = JSON.parse(res.body);
|
|
|
|
assert.equal(pbody.rows.length, 0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('DROP TABLE with GET and auth', function(done){
|
2012-10-15 19:20:37 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
2012-10-15 19:40:04 +08:00
|
|
|
q: 'DROP TABLE test_table',
|
2012-10-15 19:20:37 +08:00
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2013-07-22 23:21:38 +08:00
|
|
|
assert.ok(!res.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
|
2012-10-15 19:20:37 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-22 19:21:44 +08:00
|
|
|
it('CREATE FUNCTION with GET and auth', function(done){
|
2012-10-15 19:20:37 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: 'CREATE FUNCTION create_func_test(a int) RETURNS INT AS \'SELECT 1\' LANGUAGE \'sql\'',
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2013-07-22 23:21:38 +08:00
|
|
|
assert.ok(!res.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
|
2012-10-15 19:20:37 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('DROP FUNCTION with GET and auth', function(done){
|
2012-10-15 19:20:37 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: 'DROP FUNCTION create_func_test(a int)',
|
|
|
|
api_key: 1234
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2013-07-22 23:21:38 +08:00
|
|
|
assert.ok(!res.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
|
2012-10-15 19:20:37 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('sends a 400 when an unsupported format is requested', function(done){
|
2011-10-28 19:11:18 +08:00
|
|
|
assert.response(app, {
|
2012-10-25 19:34:06 +08:00
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=unknown',
|
2012-10-12 17:42:03 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
2012-10-25 19:34:06 +08:00
|
|
|
assert.equal(res.statusCode, 400, res.body);
|
2013-02-13 20:32:34 +08:00
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
2012-10-25 19:34:06 +08:00
|
|
|
assert.deepEqual(JSON.parse(res.body), {"error":[ "Invalid format: unknown" ]});
|
2012-10-12 17:42:03 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/v1/sql with SQL parameter and no format, ensuring content-disposition set to json', function(done){
|
2012-10-12 17:42:03 +08:00
|
|
|
assert.response(app, {
|
2012-10-25 19:34:06 +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);
|
2012-11-13 02:44:16 +08:00
|
|
|
var ct = res.header('Content-Type');
|
|
|
|
assert.ok(/json/.test(ct), 'Default format is not JSON: ' + ct);
|
2011-10-28 19:11:18 +08:00
|
|
|
var cd = res.header('Content-Disposition');
|
2012-11-13 02:44:16 +08:00
|
|
|
assert.equal(true, /^inline/.test(cd), 'Default format is not disposed inline: ' + cd);
|
2012-11-12 19:37:34 +08:00
|
|
|
assert.equal(true, /filename=cartodb-query.json/gi.test(cd), 'Unexpected JSON filename: ' + 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
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('POST /api/v1/sql with SQL parameter and no format, ensuring content-disposition set to json', function(done){
|
2012-11-14 02:26:36 +08:00
|
|
|
assert.response(app, {
|
2014-07-04 22:47:59 +08:00
|
|
|
url: '/api/v1/sql',
|
2012-11-14 02:26:36 +08:00
|
|
|
data: querystring.stringify({q: "SELECT * FROM untitle_table_4" }),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'POST'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var ct = res.header('Content-Type');
|
|
|
|
assert.ok(/json/.test(ct), 'Default format is not JSON: ' + ct);
|
|
|
|
var cd = res.header('Content-Disposition');
|
2012-11-14 23:30:18 +08:00
|
|
|
assert.equal(true, /^inline/.test(cd), 'Default format is not disposed inline: ' + cd);
|
2012-11-14 02:26:36 +08:00
|
|
|
assert.equal(true, /filename=cartodb-query.json/gi.test(cd), 'Unexpected JSON filename: ' + cd);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/v1/sql with SQL parameter and no format, but a filename', function(done){
|
2012-11-13 02:44:16 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&filename=x',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var ct = res.header('Content-Type');
|
|
|
|
assert.ok(/json/.test(ct), 'Default format is not JSON: ' + ct);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /^attachment/.test(cd), 'Format with filename is not disposed as attachment: ' + cd);
|
|
|
|
assert.equal(true, /filename=x.json/gi.test(cd), 'Unexpected JSON filename: ' + cd);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('field named "the_geom_webmercator" is not skipped by default', function(done){
|
2012-11-13 00:10:16 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var row0 = JSON.parse(res.body).rows[0];
|
|
|
|
var checkfields = {'name':1, 'cartodb_id':1, 'the_geom':1, 'the_geom_webmercator':1};
|
|
|
|
for ( var f in checkfields ) {
|
|
|
|
if ( checkfields[f] ) {
|
|
|
|
assert.ok(row0.hasOwnProperty(f), "result does not include '" + f + "'");
|
|
|
|
} else {
|
|
|
|
assert.ok(!row0.hasOwnProperty(f), "result includes '" + f + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('skipfields controls included fields', function(done){
|
2012-11-13 00:10:16 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&skipfields=the_geom_webmercator,cartodb_id,unexistant',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var row0 = JSON.parse(res.body).rows[0];
|
|
|
|
var checkfields = {'name':1, 'cartodb_id':0, 'the_geom':1, 'the_geom_webmercator':0};
|
|
|
|
for ( var f in checkfields ) {
|
|
|
|
if ( checkfields[f] ) {
|
|
|
|
assert.ok(row0.hasOwnProperty(f), "result does not include '" + f + "'");
|
|
|
|
} else {
|
|
|
|
assert.ok(!row0.hasOwnProperty(f), "result includes '" + f + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('multiple skipfields parameter do not kill the backend', function(done){
|
2013-05-06 18:30:32 +08:00
|
|
|
assert.response(app, {
|
2015-05-13 17:21:44 +08:00
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&skipfields=unexistent,the_geom_webmercator' +
|
|
|
|
'&skipfields=cartodb_id,unexistant',
|
2013-05-06 18:30:32 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var row0 = JSON.parse(res.body).rows[0];
|
|
|
|
var checkfields = {'name':1, 'cartodb_id':0, 'the_geom':1, 'the_geom_webmercator':0};
|
|
|
|
for ( var f in checkfields ) {
|
|
|
|
if ( checkfields[f] ) {
|
|
|
|
assert.ok(row0.hasOwnProperty(f), "result does not include '" + f + "'");
|
|
|
|
} else {
|
|
|
|
assert.ok(!row0.hasOwnProperty(f), "result includes '" + f + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/v1/sql ensure cross domain set on errors', function(done){
|
2012-10-12 18:17:35 +08:00
|
|
|
assert.response(app, {
|
2012-10-25 19:34:06 +08:00
|
|
|
url: '/api/v1/sql?q=SELECT%20*gadfgadfg%20FROM%20untitle_table_4',
|
2012-10-12 18:17:35 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2012-10-25 19:34:06 +08:00
|
|
|
},{
|
|
|
|
status: 400
|
|
|
|
}, function(res){
|
|
|
|
var cd = res.header('Access-Control-Allow-Origin');
|
2013-02-13 20:32:34 +08:00
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
2012-10-25 19:34:06 +08:00
|
|
|
assert.equal(cd, '*');
|
2012-10-12 18:17:35 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-08 18:48:29 +08:00
|
|
|
var systemQueriesSuitesToTest = [
|
|
|
|
{
|
|
|
|
desc: 'pg_ queries work with api_key and fail otherwise',
|
|
|
|
queries: [
|
|
|
|
'SELECT * FROM pg_attribute',
|
|
|
|
'SELECT * FROM PG_attribute',
|
|
|
|
'SELECT * FROM "pg_attribute"',
|
2014-09-22 19:23:30 +08:00
|
|
|
'SELECT a.* FROM untitle_table_4 a,pg_attribute',
|
|
|
|
'SELECT * FROM geometry_columns'
|
2014-08-08 18:48:29 +08:00
|
|
|
],
|
|
|
|
api_key_works: true,
|
|
|
|
no_api_key_works: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: 'Possible false positive queries will work with api_key and without it',
|
|
|
|
queries: [
|
|
|
|
"SELECT 'pg_'",
|
|
|
|
'SELECT pg_attribute FROM ( select 1 as pg_attribute ) as f',
|
|
|
|
'SELECT * FROM cpg_test'
|
|
|
|
],
|
|
|
|
api_key_works: true,
|
|
|
|
no_api_key_works: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: 'Set queries will FAIL for both api_key and no_api_key queries',
|
|
|
|
queries: [
|
|
|
|
' SET work_mem TO 80000',
|
|
|
|
' set statement_timeout TO 400'
|
|
|
|
],
|
|
|
|
api_key_works: false,
|
|
|
|
no_api_key_works: false
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
systemQueriesSuitesToTest.forEach(function(suiteToTest) {
|
|
|
|
var apiKeyStatusErrorCode = !!suiteToTest.api_key_works ? 200 : 403;
|
|
|
|
testSystemQueries(suiteToTest.desc + ' with api_key', suiteToTest.queries, apiKeyStatusErrorCode, '1234');
|
|
|
|
var noApiKeyStatusErrorCode = !!suiteToTest.no_api_key_works ? 200 : 403;
|
|
|
|
testSystemQueries(suiteToTest.desc, suiteToTest.queries, noApiKeyStatusErrorCode);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function testSystemQueries(description, queries, statusErrorCode, apiKey) {
|
|
|
|
queries.forEach(function(query) {
|
2015-05-13 17:21:44 +08:00
|
|
|
it('[' + description + '] query: ' + query, function(done) {
|
2014-08-08 18:48:29 +08:00
|
|
|
var queryStringParams = {q: query};
|
|
|
|
if (!!apiKey) {
|
|
|
|
queryStringParams.api_key = apiKey;
|
|
|
|
}
|
|
|
|
var request = {
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET',
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify(queryStringParams)
|
|
|
|
};
|
|
|
|
assert.response(app, request, function(response) {
|
|
|
|
assert.equal(response.statusCode, statusErrorCode);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2012-09-11 18:22:27 +08:00
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET decent error if domain is incorrect', function(done){
|
2012-09-11 18:22:27 +08:00
|
|
|
assert.response(app, {
|
2012-10-25 19:34:06 +08:00
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
|
|
|
|
headers: {host: 'vizzualinot.cartodb.com'},
|
2012-09-11 18:22:27 +08:00
|
|
|
method: 'GET'
|
2013-11-16 01:36:49 +08:00
|
|
|
}, {}, function(res){
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.equal(res.statusCode, 404, res.statusCode + ( res.statusCode !== 200 ? ( ': ' + res.body ) : ''));
|
2013-02-13 20:32:34 +08:00
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
2012-10-25 19:34:06 +08:00
|
|
|
var result = JSON.parse(res.body);
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.equal(
|
|
|
|
result.error[0],
|
|
|
|
"Sorry, we can't find CartoDB user 'vizzualinot'. Please check that you have entered the correct domain."
|
|
|
|
);
|
2012-09-11 18:22:27 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-06-26 17:22:40 +08:00
|
|
|
// this test does not make sense with the current CDB_QueryTables implementation
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET decent error if SQL is broken', function(done){
|
2012-09-11 18:22:27 +08:00
|
|
|
assert.response(app, {
|
2012-10-25 19:34:06 +08:00
|
|
|
url: '/api/v1/sql?' + querystring.stringify({q:
|
|
|
|
'SELECT star FROM this and that'
|
|
|
|
}),
|
2012-09-11 18:22:27 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2012-10-25 19:34:06 +08:00
|
|
|
},{}, function(res){
|
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
|
2013-02-13 20:32:34 +08:00
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
2012-10-25 19:34:06 +08:00
|
|
|
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-09-11 18:22:27 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-03-14 18:41:07 +08:00
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/88
|
2015-05-13 17:21:44 +08:00
|
|
|
it('numeric arrays are rendered as such', function(done){
|
2013-03-14 18:41:07 +08:00
|
|
|
assert.response(app, {
|
2015-05-13 17:21:44 +08:00
|
|
|
url: "/api/v1/sql?" + querystring.stringify({q:
|
2013-03-14 18:41:07 +08:00
|
|
|
"SELECT ARRAY[8.7,4.3]::numeric[] as x"
|
|
|
|
}),
|
|
|
|
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.ok(out.rows[0].hasOwnProperty('x'));
|
|
|
|
assert.equal(out.rows[0].x.length, 2);
|
|
|
|
assert.equal(out.rows[0].x[0], '8.7');
|
|
|
|
assert.equal(out.rows[0].x[1], '4.3');
|
2015-09-08 00:22:24 +08:00
|
|
|
assert.equal(res.headers.hasOwnProperty('x-cache-channel'), false);
|
2013-03-14 18:41:07 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-06-15 00:35:04 +08:00
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/97
|
2015-05-13 17:21:44 +08:00
|
|
|
it('field names and types are exposed', function(done){
|
2013-06-15 00:35:04 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: "SELECT 1::int as a, 2::float8 as b, 3::varchar as c, " +
|
2013-11-06 18:43:56 +08:00
|
|
|
"4::char as d, now() as e, 'a'::text as f" +
|
|
|
|
", 1::bool as g" +
|
|
|
|
", 'POINT(0 0)'::geometry as h" +
|
|
|
|
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/117
|
|
|
|
", now()::date as i" +
|
2014-07-04 22:47:59 +08:00
|
|
|
", '1'::numeric as j" +
|
2013-11-06 18:43:56 +08:00
|
|
|
" LIMIT 0"
|
2013-06-15 00:35:04 +08:00
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
2013-11-09 23:55:40 +08:00
|
|
|
assert.equal(_.keys(parsedBody.fields).length, 10);
|
2013-06-15 00:35:04 +08:00
|
|
|
assert.equal(parsedBody.fields.a.type, 'number');
|
|
|
|
assert.equal(parsedBody.fields.b.type, 'number');
|
|
|
|
assert.equal(parsedBody.fields.c.type, 'string');
|
|
|
|
assert.equal(parsedBody.fields.d.type, 'string');
|
|
|
|
assert.equal(parsedBody.fields.e.type, 'date');
|
2013-07-04 22:03:05 +08:00
|
|
|
assert.equal(parsedBody.fields.f.type, 'string');
|
2013-08-22 19:44:21 +08:00
|
|
|
assert.equal(parsedBody.fields.g.type, 'boolean');
|
2013-10-02 16:22:13 +08:00
|
|
|
assert.equal(parsedBody.fields.h.type, 'geometry');
|
2013-11-06 18:43:56 +08:00
|
|
|
assert.equal(parsedBody.fields.i.type, 'date');
|
2013-11-09 23:55:40 +08:00
|
|
|
assert.equal(parsedBody.fields.j.type, 'number');
|
2013-06-15 00:35:04 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-09-26 19:26:45 +08:00
|
|
|
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/109
|
2015-05-13 17:21:44 +08:00
|
|
|
it('schema response takes skipfields into account', function(done){
|
2013-09-26 19:26:45 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: "SELECT 1 as a, 2 as b, 3 as c ",
|
|
|
|
skipfields: 'b'
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
assert.equal(_.keys(parsedBody.fields).length, 2);
|
|
|
|
assert.ok(parsedBody.fields.hasOwnProperty('a'));
|
|
|
|
assert.ok(!parsedBody.fields.hasOwnProperty('b'));
|
|
|
|
assert.ok(parsedBody.fields.hasOwnProperty('c'));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-10 03:50:28 +08:00
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/100
|
2015-05-13 17:21:44 +08:00
|
|
|
it('numeric fields are rendered as numbers in JSON', function(done){
|
2013-07-10 03:50:28 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: "WITH inp AS ( SELECT 1::int2 as a, 2::int4 as b, " +
|
|
|
|
"3::int8 as c, 4::float4 as d, " +
|
|
|
|
"5::float8 as e, 6::numeric as f" +
|
|
|
|
") SELECT a,b,c,d,e,f," +
|
|
|
|
" ARRAY[a] AS _a, " +
|
|
|
|
" ARRAY[b] AS _b, " +
|
|
|
|
" ARRAY[c] AS _c, " +
|
|
|
|
" ARRAY[d] AS _d, " +
|
|
|
|
" ARRAY[e] AS _e, " +
|
|
|
|
" ARRAY[f] AS _f " +
|
|
|
|
"FROM inp"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
var row = parsedBody.rows[0];
|
|
|
|
assert.equal(typeof(row.a), 'number');
|
|
|
|
assert.equal(typeof(row.b), 'number');
|
|
|
|
assert.equal(typeof(row.c), 'number');
|
|
|
|
assert.equal(typeof(row.d), 'number');
|
|
|
|
assert.equal(typeof(row.e), 'number');
|
|
|
|
assert.equal(typeof(row.f), 'number');
|
|
|
|
assert.equal(typeof(row._a[0]), 'number');
|
|
|
|
assert.equal(typeof(row._b[0]), 'number');
|
|
|
|
assert.equal(typeof(row._c[0]), 'number');
|
|
|
|
assert.equal(typeof(row._d[0]), 'number');
|
|
|
|
assert.equal(typeof(row._e[0]), 'number');
|
|
|
|
assert.equal(typeof(row._f[0]), 'number');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-06-19 18:24:04 +08:00
|
|
|
// Timezone information is retained with JSON output
|
|
|
|
//
|
|
|
|
// NOTE: results of these tests rely on the TZ env variable
|
|
|
|
// being set to 'Europe/Rome'. The env variable cannot
|
2014-07-04 22:47:59 +08:00
|
|
|
// be set within this test in a reliable way, see
|
2013-06-19 18:24:04 +08:00
|
|
|
// https://github.com/joyent/node/issues/3286
|
|
|
|
//
|
|
|
|
// FIXME: we'd like to also test UTC outputs of these
|
|
|
|
// numbers, but it'd currently take running the
|
|
|
|
// test again (new mocha run) with a different TZ
|
|
|
|
//
|
2015-05-13 17:21:44 +08:00
|
|
|
it('timezone info in JSON output', function(done){
|
|
|
|
step(
|
2013-06-19 18:24:04 +08:00
|
|
|
function testEuropeRomeExplicit() {
|
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: "SET timezone TO 'Europe/Rome'; SELECT '2000-01-01T00:00:00+01'::timestamptz as d"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
assert.equal(parsedBody.rows[0].d, '2000-01-01T00:00:00+0100');
|
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function testEuropeRomeImplicit(err) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2013-06-19 18:24:04 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: "SET timezone TO 'Europe/Rome'; SELECT '2000-01-01T00:00:00'::timestamp as d"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
assert.equal(parsedBody.rows[0].d, '2000-01-01T00:00:00+0100');
|
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function testUTCExplicit(err) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2013-06-19 18:24:04 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: "SET timezone TO 'UTC'; SELECT '2000-01-01T00:00:00+00'::timestamptz as d"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
assert.equal(parsedBody.rows[0].d, '2000-01-01T01:00:00+0100');
|
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function testUTCImplicit(err) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2013-06-19 18:24:04 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: "SET timezone TO 'UTC'; SELECT '2000-01-01T00:00:00'::timestamp as d"
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
assert.equal(parsedBody.rows[0].d, '2000-01-01T00:00:00+0100');
|
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2013-11-19 00:01:06 +08:00
|
|
|
// WARNING and NOTICE in JSON output
|
|
|
|
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/104
|
2015-05-13 17:21:44 +08:00
|
|
|
it('notice and warning info in JSON output', function(done){
|
|
|
|
step(
|
2013-11-19 00:01:06 +08:00
|
|
|
function addRaiseFunction() {
|
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
2015-05-13 17:21:44 +08:00
|
|
|
q: "create or replace function raise(lvl text, msg text) returns void as $$ begin if lvl = 'notice' " +
|
|
|
|
"then raise notice '%', msg; elsif lvl = 'warning' then raise warning '%', msg; " +
|
|
|
|
"else raise exception '%', msg; end if; end; $$ language plpgsql;",
|
2013-11-19 00:01:06 +08:00
|
|
|
api_key: '1234'
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
var err = null;
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
} catch (e) { err = e; }
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function raiseNotice(err) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2013-11-19 00:01:06 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
2013-11-19 00:09:27 +08:00
|
|
|
q: "SET client_min_messages TO 'notice'; select raise('notice', 'hello notice')"
|
2013-11-19 00:01:06 +08:00
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
var err = null;
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
assert.ok(parsedBody.hasOwnProperty('notices'), 'Missing notices from result');
|
|
|
|
assert.equal(parsedBody.notices.length, 1);
|
|
|
|
assert.equal(parsedBody.notices[0], 'hello notice');
|
|
|
|
} catch (e) { err = e; }
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function raiseWarning(err) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2013-11-19 00:01:06 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
2013-11-19 00:09:27 +08:00
|
|
|
q: "SET client_min_messages TO 'notice'; select raise('warning', 'hello warning')"
|
2013-11-19 00:01:06 +08:00
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
var err = null;
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
assert.ok(parsedBody.hasOwnProperty('warnings'), 'Missing warnings from result');
|
|
|
|
assert.equal(parsedBody.warnings.length, 1);
|
|
|
|
assert.equal(parsedBody.warnings[0], 'hello warning');
|
|
|
|
} catch (e) { err = e; }
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function raiseBothWarningAndNotice(err) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2013-11-19 00:01:06 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
2015-05-13 17:21:44 +08:00
|
|
|
q: "SET client_min_messages TO 'notice'; select raise('warning', 'hello again warning'), " +
|
|
|
|
"raise('notice', 'hello again notice');"
|
2013-11-19 00:01:06 +08:00
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
var err = null;
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
assert.ok(parsedBody.hasOwnProperty('warnings'), 'Missing warnings from result');
|
|
|
|
assert.equal(parsedBody.warnings.length, 1);
|
|
|
|
assert.equal(parsedBody.warnings[0], 'hello again warning');
|
|
|
|
assert.ok(parsedBody.hasOwnProperty('notices'), 'Missing notices from result');
|
|
|
|
assert.equal(parsedBody.notices.length, 1);
|
|
|
|
assert.equal(parsedBody.notices[0], 'hello again notice');
|
|
|
|
} catch (e) { err = e; }
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function delRaiseFunction(err) {
|
|
|
|
var next = this;
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: "DROP function raise(text, text)",
|
|
|
|
api_key: '1234'
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
try {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2015-05-13 17:21:44 +08:00
|
|
|
JSON.parse(res.body);
|
2013-11-19 00:01:06 +08:00
|
|
|
} catch (e) { err = new Error(err + ',' + e); }
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2013-06-15 00:35:04 +08:00
|
|
|
|
2012-11-01 20:16:46 +08:00
|
|
|
/**
|
|
|
|
* CORS
|
|
|
|
*/
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET /api/v1/sql with SQL parameter on SELECT only should return CORS headers ', function(done){
|
2012-11-01 20:16:46 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&database=cartodb_test_user_1_db',
|
2014-07-04 22:47:59 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
2012-11-01 20:16:46 +08:00
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
// Check cache headers
|
2014-06-26 17:22:40 +08:00
|
|
|
assert.equal(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:public.untitle_table_4');
|
2012-11-01 20:16:46 +08:00
|
|
|
assert.equal(res.headers['cache-control'], expected_cache_control);
|
|
|
|
assert.equal(res.headers['access-control-allow-origin'], '*');
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.equal(
|
|
|
|
res.headers['access-control-allow-headers'],
|
|
|
|
"X-Requested-With, X-Prototype-Version, X-CSRF-Token"
|
|
|
|
);
|
2012-11-01 20:16:46 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET with callback param returns wrapped result set with callback as jsonp', function(done) {
|
2014-07-31 02:01:43 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&callback=foo_jsonp',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
assert.ok(res.body.match(/foo\_jsonp\(.*\)/));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET with callback must return 200 status error even if it is an error', function(done){
|
2014-08-04 21:56:43 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?q=DROP%20TABLE%20untitle_table_4&callback=foo_jsonp",
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(res) {
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
2014-08-05 06:57:09 +08:00
|
|
|
var didRunJsonCallback = false;
|
2015-05-13 17:21:44 +08:00
|
|
|
// jshint ignore:start
|
2014-08-04 21:56:43 +08:00
|
|
|
function foo_jsonp(body) {
|
|
|
|
assert.deepEqual(body, {"error":["must be owner of relation untitle_table_4"]});
|
2014-08-05 06:57:09 +08:00
|
|
|
didRunJsonCallback = true;
|
2014-08-04 21:56:43 +08:00
|
|
|
}
|
|
|
|
eval(res.body);
|
2015-05-13 17:21:44 +08:00
|
|
|
// jshint ignore:end
|
2014-08-05 06:57:09 +08:00
|
|
|
assert.ok(didRunJsonCallback);
|
2014-08-04 21:56:43 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('GET with slow query exceeding statement timeout returns proper error message', function(done){
|
2014-11-11 20:57:15 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: "/api/v1/sql?q=select%20pg_sleep(2.1)%20as%20sleep",
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400
|
|
|
|
},
|
|
|
|
function(res) {
|
|
|
|
assert.ok(res.body.match(/was not able to finish.*try again/i));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
it('too large rows get into error log', function(done){
|
2015-03-02 19:31:11 +08:00
|
|
|
|
|
|
|
var dbMaxRowSize = global.settings.db_max_row_size;
|
|
|
|
global.settings.db_max_row_size = 4;
|
|
|
|
|
|
|
|
var consoleErrorFn = console.error;
|
|
|
|
var hit = false;
|
|
|
|
var consoleError;
|
|
|
|
console.error = function(what) {
|
|
|
|
hit = true;
|
|
|
|
consoleError = what;
|
|
|
|
};
|
|
|
|
assert.response(
|
|
|
|
app,
|
|
|
|
{
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: "SELECT * FROM untitle_table_4"
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
assert.equal(hit, true);
|
2015-03-02 21:34:01 +08:00
|
|
|
var parsedError = JSON.parse(consoleError);
|
|
|
|
assert.ok(parsedError.error.match(/^row too large.*/i), "Expecting row size limit error");
|
|
|
|
assert.equal(parsedError.username, 'vizzuality');
|
|
|
|
assert.equal(parsedError.type, 'row_size_limit_exceeded');
|
2015-03-02 19:31:11 +08:00
|
|
|
|
|
|
|
global.settings.db_max_row_size = dbMaxRowSize;
|
|
|
|
console.error = consoleErrorFn;
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|