2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-18 21:30:16 +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 vizzuality.cartodb.com domain
|
|
|
|
*
|
|
|
|
* SELECT 5
|
|
|
|
* HSET rails:users:vizzuality id 1
|
|
|
|
* HSET rails:users:vizzuality database_name cartodb_test_user_1_db
|
|
|
|
*
|
|
|
|
*/
|
2016-10-06 17:53:56 +08:00
|
|
|
require('../../helper');
|
2019-10-04 00:24:39 +08:00
|
|
|
var JobController = require('../../../lib/api/sql/job-controller');
|
|
|
|
var redisUtils = require('../../support/redis-utils');
|
|
|
|
var server = require('../../../lib/server')();
|
2016-10-06 17:53:56 +08:00
|
|
|
var assert = require('../../support/assert');
|
2016-05-24 20:28:00 +08:00
|
|
|
var querystring = require('qs');
|
2016-04-19 00:05:03 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function payload (query) {
|
|
|
|
return JSON.stringify({ query: query });
|
2016-05-24 20:28:00 +08:00
|
|
|
}
|
2019-12-24 01:19:08 +08:00
|
|
|
function payloadSize (query) {
|
2016-05-24 20:28:00 +08:00
|
|
|
return payload(query).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
var minPayloadSize = payloadSize('');
|
|
|
|
var queryMaxSize = new Array(JobController.MAX_LIMIT_QUERY_SIZE_IN_BYTES - minPayloadSize + 1).join('a');
|
2016-04-19 00:05:03 +08:00
|
|
|
var queryTooLong = queryMaxSize.concat('a');
|
2016-04-18 21:30:16 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('job query limit', function () {
|
|
|
|
function expectedErrorMessage (query) {
|
2016-05-24 20:28:00 +08:00
|
|
|
return JobController.getMaxSizeErrorMessage(payload(query));
|
|
|
|
}
|
|
|
|
|
2016-04-18 21:30:16 +08:00
|
|
|
after(function (done) {
|
2016-07-22 23:05:01 +08:00
|
|
|
redisUtils.clean('batch:*', done);
|
2016-04-18 21:30:16 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('POST /api/v2/sql/job with a invalid query size should respond with 400 query too long', function (done) {
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2016-04-18 21:30:16 +08:00
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
2016-04-18 21:30:16 +08:00
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
|
|
|
query: queryTooLong
|
|
|
|
})
|
|
|
|
}, {
|
|
|
|
status: 400
|
2016-09-26 20:37:40 +08:00
|
|
|
}, function (err, res) {
|
2016-04-18 21:30:16 +08:00
|
|
|
var error = JSON.parse(res.body);
|
2016-05-24 20:28:00 +08:00
|
|
|
assert.deepEqual(error, { error: [expectedErrorMessage(queryTooLong)] });
|
2016-04-18 21:30:16 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('POST /api/v2/sql/job with a valid query size should respond with 201 created', function (done) {
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2016-04-18 21:30:16 +08:00
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
2016-04-18 21:30:16 +08:00
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
|
|
|
query: queryMaxSize
|
|
|
|
})
|
|
|
|
}, {
|
|
|
|
status: 201
|
2016-09-26 20:37:40 +08:00
|
|
|
}, function (err, res) {
|
2016-04-18 21:30:16 +08:00
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
assert.ok(job.job_id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('POST /api/v2/sql/job with a invalid query size should consider multiple queries', function (done) {
|
2016-05-24 20:28:00 +08:00
|
|
|
var queries = [queryTooLong, 'select 1'];
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2016-05-24 20:28:00 +08:00
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
2016-05-24 20:28:00 +08:00
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
|
|
|
query: queries
|
|
|
|
})
|
|
|
|
}, {
|
|
|
|
status: 400
|
2016-09-26 20:37:40 +08:00
|
|
|
}, function (err, res) {
|
2016-05-24 20:28:00 +08:00
|
|
|
var error = JSON.parse(res.body);
|
|
|
|
assert.deepEqual(error, { error: [expectedErrorMessage(queries)] });
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('POST /api/v2/sql/job with a invalid query size should consider fallback queries/callbacks', function (done) {
|
2016-05-24 20:28:00 +08:00
|
|
|
var fallbackQueries = {
|
|
|
|
query: [{
|
|
|
|
query: queryTooLong,
|
2019-12-24 01:19:08 +08:00
|
|
|
onsuccess: 'SELECT * FROM untitle_table_4 limit 1'
|
2016-05-24 20:28:00 +08:00
|
|
|
}, {
|
2019-12-24 01:19:08 +08:00
|
|
|
query: 'SELECT * FROM untitle_table_4 limit 2',
|
|
|
|
onsuccess: 'SELECT * FROM untitle_table_4 limit 3'
|
2016-05-24 20:28:00 +08:00
|
|
|
}]
|
|
|
|
};
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2016-05-24 20:28:00 +08:00
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
2016-05-24 20:28:00 +08:00
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
|
|
|
query: fallbackQueries
|
|
|
|
})
|
|
|
|
}, {
|
|
|
|
status: 400
|
2016-09-26 20:37:40 +08:00
|
|
|
}, function (err, res) {
|
2016-05-24 20:28:00 +08:00
|
|
|
var error = JSON.parse(res.body);
|
|
|
|
assert.deepEqual(error, { error: [expectedErrorMessage(fallbackQueries)] });
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-04-18 21:30:16 +08:00
|
|
|
});
|