Merge pull request #268 from CartoDB/skip-query-tables-api-cache

Skip query-tables-api for authenticated requests
This commit is contained in:
Raul Ochoa 2016-02-01 17:51:57 +01:00
commit e02e7e2f88
3 changed files with 80 additions and 3 deletions

View File

@ -133,7 +133,9 @@ QueryController.prototype.handleQuery = function (req, res) {
checkAborted('queryExplain');
self.queryTablesApi.getAffectedTablesAndLastUpdatedTime(authDbParams, sql, this);
var skipCache = !!dbopts.authenticated;
self.queryTablesApi.getAffectedTablesAndLastUpdatedTime(authDbParams, sql, skipCache, this);
},
function setHeaders(err, queryExplainResult) {
assert.ifError(err);

View File

@ -9,11 +9,15 @@ function QueryTablesApi(tableCache) {
module.exports = QueryTablesApi;
QueryTablesApi.prototype.getAffectedTablesAndLastUpdatedTime = function (connectionParams, sql, callback) {
QueryTablesApi.prototype.getAffectedTablesAndLastUpdatedTime = function (connectionParams, sql, skipCache, callback) {
var self = this;
var cacheKey = sqlCacheKey(connectionParams.user, sql);
var queryExplainResult = this.tableCache.get(cacheKey);
var queryExplainResult;
if (!skipCache) {
queryExplainResult = this.tableCache.get(cacheKey);
}
if (queryExplainResult) {
queryExplainResult.hits++;

View File

@ -0,0 +1,71 @@
require('../helper');
var qs = require('querystring');
var app = require(global.settings.app_root + '/app/app')();
var assert = require('../support/assert');
var QueryTablesApi = require('../../app/services/query-tables-api');
describe('query-tables-api', function() {
var scenarios = [
{
apiKey: 1234,
shouldSkipCache: true
},
{
apiKey: null,
shouldSkipCache: false
}
];
scenarios.forEach(function(scenario) {
var shouldOrShouldNot = scenario.shouldSkipCache ? 'should' : 'should NOT';
var desc = 'authenticated=' + JSON.stringify(!!scenario.apiKey) + ' requests' +
' ' + shouldOrShouldNot + ' skip internal query-tables-api cache';
it(desc, function(done) {
var getAffectedTablesCalled = false;
var skippedCache = null;
var getAffectedTablesFn = QueryTablesApi.prototype.getAffectedTablesAndLastUpdatedTime;
QueryTablesApi.prototype.getAffectedTablesAndLastUpdatedTime =
function(connectionParams, sql, skipCache, callback) {
getAffectedTablesCalled = true;
skippedCache = skipCache;
return callback(null, {
affectedTables: [],
lastModified: Date.now(),
mayWrite: false,
hits: 1
});
};
assert.response(
app,
{
url: '/api/v1/sql?' + qs.stringify({
api_key: scenario.apiKey,
q: 'SELECT * FROM untitle_table_4'
}),
headers: {
host: 'vizzuality.cartodb.com'
},
method: 'GET'
},
{
// status: 200 // not using this as we cannot restore getAffectedTablesAndLastUpdatedTime
},
function(res, err) {
QueryTablesApi.prototype.getAffectedTablesAndLastUpdatedTime = getAffectedTablesFn;
assert.ok(!err, err);
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
assert.equal(skippedCache, scenario.shouldSkipCache, 'skip cache expected as true');
assert.ok(getAffectedTablesCalled, 'getAffectedTablesAndLastUpdatedTime NOT called');
done();
}
);
});
});
});