CartoDB-SQL-API/test/acceptance/batch/job-query-limit-test.js

122 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
/**
*
* 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
*
*/
require('../../helper');
var JobController = require('../../../lib/api/sql/job-controller');
var redisUtils = require('../../support/redis-utils');
var server = require('../../../lib/server')();
var assert = require('../../support/assert');
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 });
}
2019-12-24 01:19:08 +08:00
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');
2019-12-24 01:19:08 +08:00
describe('job query limit', function () {
function expectedErrorMessage (query) {
return JobController.getMaxSizeErrorMessage(payload(query));
}
after(function (done) {
redisUtils.clean('batch:*', done);
});
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) {
assert.response(server, {
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' },
method: 'POST',
data: querystring.stringify({
query: queryTooLong
})
}, {
status: 400
}, function (err, res) {
var error = JSON.parse(res.body);
assert.deepEqual(error, { error: [expectedErrorMessage(queryTooLong)] });
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) {
assert.response(server, {
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' },
method: 'POST',
data: querystring.stringify({
query: queryMaxSize
})
}, {
status: 201
}, function (err, res) {
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) {
var queries = [queryTooLong, 'select 1'];
assert.response(server, {
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' },
method: 'POST',
data: querystring.stringify({
query: queries
})
}, {
status: 400
}, function (err, res) {
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) {
var fallbackQueries = {
query: [{
query: queryTooLong,
2019-12-24 01:19:08 +08:00
onsuccess: 'SELECT * FROM untitle_table_4 limit 1'
}, {
2019-12-24 01:19:08 +08:00
query: 'SELECT * FROM untitle_table_4 limit 2',
onsuccess: 'SELECT * FROM untitle_table_4 limit 3'
}]
};
assert.response(server, {
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' },
method: 'POST',
data: querystring.stringify({
query: fallbackQueries
})
}, {
status: 400
}, function (err, res) {
var error = JSON.parse(res.body);
assert.deepEqual(error, { error: [expectedErrorMessage(fallbackQueries)] });
done();
});
});
});