2019-02-27 17:09:08 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
require('../helper');
|
|
|
|
|
|
|
|
const server = require('../../app/server')();
|
|
|
|
const assert = require('../support/assert');
|
|
|
|
const qs = require('querystring');
|
|
|
|
const BatchTestClient = require('../support/batch-test-client');
|
2019-02-28 18:49:05 +08:00
|
|
|
const { TYPES } = require('../../app/middlewares/log');
|
2019-02-27 17:09:08 +08:00
|
|
|
|
|
|
|
const QUERY = `SELECT 14 as foo`;
|
|
|
|
const API_KEY = 1234;
|
|
|
|
|
2019-02-27 19:50:23 +08:00
|
|
|
const logQueries = global.settings.logQueries;
|
|
|
|
|
2019-02-27 17:09:08 +08:00
|
|
|
describe('Log middleware', function() {
|
2019-02-27 19:50:23 +08:00
|
|
|
before(function() {
|
|
|
|
global.settings.logQueries = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
global.settings.logQueries = logQueries;
|
|
|
|
});
|
|
|
|
|
2019-02-27 17:09:08 +08:00
|
|
|
describe('regular queries endpoint', function() {
|
|
|
|
['GET', 'POST'].forEach(method => {
|
|
|
|
it(`${method} without query fails`, function(done) {
|
|
|
|
assert.response(server,
|
|
|
|
{
|
|
|
|
method,
|
|
|
|
url: '/api/v1/sql?' + qs.stringify({
|
|
|
|
api_key: API_KEY
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ statusCode: 400 },
|
|
|
|
function(err, res) {
|
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
assert.ok(res.headers['x-sqlapi-log']);
|
|
|
|
const log = JSON.parse(res.headers['x-sqlapi-log']);
|
|
|
|
assert.deepEqual(log, {
|
|
|
|
request: {
|
|
|
|
sql: null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`${method} query`, function(done) {
|
|
|
|
assert.response(server,
|
|
|
|
{
|
|
|
|
method,
|
|
|
|
url: '/api/v1/sql?' + qs.stringify({
|
|
|
|
q: QUERY,
|
|
|
|
api_key: API_KEY
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ statusCode: 200 },
|
|
|
|
function(err, res) {
|
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
assert.ok(res.headers['x-sqlapi-log']);
|
|
|
|
const log = JSON.parse(res.headers['x-sqlapi-log']);
|
|
|
|
assert.deepEqual(log, {
|
|
|
|
request: {
|
|
|
|
sql: {
|
2019-02-28 18:49:05 +08:00
|
|
|
type: TYPES.QUERY,
|
|
|
|
sql: QUERY
|
2019-02-27 17:09:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('batch api queries', function() {
|
|
|
|
before(function() {
|
|
|
|
this.batchTestClient = new BatchTestClient();
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function(done) {
|
|
|
|
this.batchTestClient.drain(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('one query', function (done) {
|
2019-02-28 23:02:34 +08:00
|
|
|
const payload = { query: QUERY };
|
2019-02-27 17:09:08 +08:00
|
|
|
this.batchTestClient.createJob(payload, function(err, jobResult, res) {
|
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
assert.ok(res.headers['x-sqlapi-log']);
|
|
|
|
const log = JSON.parse(res.headers['x-sqlapi-log']);
|
|
|
|
assert.deepEqual(log, {
|
|
|
|
request: {
|
|
|
|
sql: {
|
2019-02-28 18:49:05 +08:00
|
|
|
type: TYPES.JOB,
|
|
|
|
sql: QUERY
|
2019-02-27 17:09:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('multiquery job with two queries', function (done) {
|
2019-02-28 23:02:34 +08:00
|
|
|
const payload = { query: [QUERY, QUERY] };
|
2019-02-27 17:09:08 +08:00
|
|
|
this.batchTestClient.createJob(payload, function(err, jobResult, res) {
|
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
assert.ok(res.headers['x-sqlapi-log']);
|
|
|
|
const log = JSON.parse(res.headers['x-sqlapi-log']);
|
|
|
|
assert.deepEqual(log, {
|
|
|
|
request: {
|
|
|
|
sql: {
|
2019-02-28 18:49:05 +08:00
|
|
|
type: TYPES.JOB,
|
|
|
|
sql: [QUERY, QUERY]
|
2019-02-27 17:09:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
});
|
2019-02-28 23:02:34 +08:00
|
|
|
|
|
|
|
it('queries with fallbacks', function (done) {
|
|
|
|
const FALLBACK_QUERY = {
|
|
|
|
query: [{
|
|
|
|
query: QUERY,
|
|
|
|
onsuccess: QUERY,
|
|
|
|
onerror: QUERY
|
|
|
|
}],
|
|
|
|
onsuccess: QUERY,
|
|
|
|
onerror: QUERY
|
|
|
|
};
|
|
|
|
const payload = { query: FALLBACK_QUERY };
|
|
|
|
|
|
|
|
this.batchTestClient.createJob(payload, function(err, jobResult, res) {
|
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
assert.ok(res.headers['x-sqlapi-log']);
|
|
|
|
const log = JSON.parse(res.headers['x-sqlapi-log']);
|
|
|
|
assert.deepEqual(log, {
|
|
|
|
request: {
|
|
|
|
sql: {
|
|
|
|
type: TYPES.JOB,
|
|
|
|
sql: FALLBACK_QUERY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
});
|
2019-02-27 17:09:08 +08:00
|
|
|
});
|
2019-02-27 19:50:23 +08:00
|
|
|
|
|
|
|
describe('disable queries log', function() {
|
|
|
|
before(function() {
|
|
|
|
global.settings.logQueries = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
global.settings.logQueries = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`GET query`, function(done) {
|
|
|
|
assert.response(server,
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url: '/api/v1/sql?' + qs.stringify({
|
|
|
|
q: QUERY,
|
|
|
|
api_key: API_KEY
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ statusCode: 200 },
|
|
|
|
function(err, res) {
|
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
assert.ok(res.headers['x-sqlapi-log']);
|
|
|
|
const log = JSON.parse(res.headers['x-sqlapi-log']);
|
|
|
|
assert.deepEqual(log, {
|
|
|
|
request: {
|
|
|
|
sql: null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2019-02-27 17:09:08 +08:00
|
|
|
});
|