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');
|
|
|
|
var JobController = require('../../../app/controllers/job_controller');
|
|
|
|
var redisUtils = require('../../support/redis_utils');
|
|
|
|
var server = require('../../../app/server')();
|
|
|
|
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
|
|
|
|
2016-05-24 20:28:00 +08:00
|
|
|
function payload(query) {
|
|
|
|
return JSON.stringify({query: query});
|
|
|
|
}
|
|
|
|
function payloadSize(query) {
|
|
|
|
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
|
|
|
|
|
|
|
describe('job query limit', function() {
|
|
|
|
|
2016-05-24 20:28:00 +08:00
|
|
|
function expectedErrorMessage(query) {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
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',
|
|
|
|
headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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',
|
|
|
|
headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-24 20:28:00 +08:00
|
|
|
it('POST /api/v2/sql/job with a invalid query size should consider multiple queries', function (done){
|
|
|
|
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',
|
|
|
|
headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('POST /api/v2/sql/job with a invalid query size should consider fallback queries/callbacks', function (done){
|
|
|
|
var fallbackQueries = {
|
|
|
|
query: [{
|
|
|
|
query: queryTooLong,
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}, {
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 2",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 3"
|
|
|
|
}]
|
|
|
|
};
|
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',
|
|
|
|
headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
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
|
|
|
});
|