CartoDB-SQL-API/test/acceptance/copy-statements-test.js

79 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
2016-12-09 18:10:08 +08:00
require('../helper');
var server = require('../../lib/server')();
2016-12-09 18:10:08 +08:00
var assert = require('../support/assert');
var querystring = require('querystring');
2019-12-24 01:19:08 +08:00
describe('copy-statements', function () {
2016-12-09 18:10:08 +08:00
var RESPONSE_OK = {
statusCode: 200
};
2019-12-24 01:19:08 +08:00
before(function (done) {
2016-12-09 18:10:08 +08:00
assert.response(server, {
2019-12-24 01:19:08 +08:00
url: '/api/v1/sql?' + querystring.stringify({
2016-12-09 18:10:08 +08:00
q: 'CREATE TABLE copy_test_table(a int)',
api_key: 1234
}),
2019-12-24 01:19:08 +08:00
headers: { host: 'vizzuality.cartodb.com' },
2016-12-09 18:10:08 +08:00
method: 'GET'
}, RESPONSE_OK, done);
});
2019-12-24 01:19:08 +08:00
after(function (done) {
2016-12-09 18:10:08 +08:00
assert.response(server, {
2019-12-24 01:19:08 +08:00
url: '/api/v1/sql?' + querystring.stringify({
2016-12-09 19:05:49 +08:00
q: 'DROP TABLE IF EXISTS copy_test_table',
2016-12-09 18:10:08 +08:00
api_key: 1234
}),
2019-12-24 01:19:08 +08:00
headers: { host: 'vizzuality.cartodb.com' },
2016-12-09 18:10:08 +08:00
method: 'GET'
}, RESPONSE_OK, done);
});
// Test effects of COPY
// See https://github.com/Vizzuality/cartodb-management/issues/1502
2019-12-24 01:19:08 +08:00
it('COPY TABLE with GET and auth', function (done) {
2016-12-09 18:10:08 +08:00
assert.response(server, {
2019-12-24 01:19:08 +08:00
url: '/api/v1/sql?' + querystring.stringify({
2016-12-09 18:10:08 +08:00
q: 'COPY copy_test_table FROM stdin;',
api_key: 1234
}),
2019-12-24 01:19:08 +08:00
headers: { host: 'vizzuality.cartodb.com' },
2016-12-09 18:10:08 +08:00
method: 'GET'
2019-12-24 01:19:08 +08:00
}, {}, function (err, res) {
2019-12-26 23:43:12 +08:00
assert.ifError(err);
// We expect a problem, actually
2019-12-26 21:01:18 +08:00
assert.strictEqual(res.statusCode, 400, res.statusCode + ': ' + res.body);
assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8');
assert.deepStrictEqual(res.headers['content-disposition'], 'inline');
assert.deepStrictEqual(JSON.parse(res.body), { error: ['COPY from stdin failed: No source stream defined'] });
2019-12-24 01:19:08 +08:00
done();
2016-12-09 18:10:08 +08:00
});
});
2019-12-24 01:19:08 +08:00
it('COPY TABLE with GET and auth', function (done) {
2016-12-09 18:10:08 +08:00
assert.response(server, {
2019-12-24 01:19:08 +08:00
url: '/api/v1/sql?' + querystring.stringify({
2016-12-09 18:10:08 +08:00
q: "COPY copy_test_table to '/tmp/x';",
api_key: 1234
}),
2019-12-24 01:19:08 +08:00
headers: { host: 'vizzuality.cartodb.com' },
2016-12-09 18:10:08 +08:00
method: 'GET'
2019-12-24 01:19:08 +08:00
}, {}, function (err, res) {
2019-12-26 23:43:12 +08:00
assert.ifError(err);
// We expect a problem, actually
2019-12-26 21:01:18 +08:00
assert.strictEqual(res.statusCode, 400, res.statusCode + ': ' + res.body);
assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8');
assert.deepStrictEqual(res.headers['content-disposition'], 'inline');
2019-12-26 23:43:12 +08:00
const errorExp = /must be superuser.* to COPY.* a file/;
const hintExp = /Anyone can COPY to stdout or from stdin. psql's \\copy command also works for anyone./;
assert.ok(JSON.parse(res.body).error[0].match(errorExp));
assert.ok(JSON.parse(res.body).hint.match(hintExp));
2019-12-24 01:19:08 +08:00
done();
2016-12-09 18:10:08 +08:00
});
});
});