2016-02-01 23:53:28 +08:00
|
|
|
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) {
|
2016-02-02 00:22:51 +08:00
|
|
|
var getAffectedTablesCalled = false;
|
2016-02-01 23:53:28 +08:00
|
|
|
var skippedCache = null;
|
2016-02-02 00:22:51 +08:00
|
|
|
var getAffectedTablesFn = QueryTablesApi.prototype.getAffectedTablesAndLastUpdatedTime;
|
2016-02-01 23:53:28 +08:00
|
|
|
QueryTablesApi.prototype.getAffectedTablesAndLastUpdatedTime =
|
|
|
|
function(connectionParams, sql, skipCache, callback) {
|
2016-02-02 00:22:51 +08:00
|
|
|
getAffectedTablesCalled = true;
|
2016-02-01 23:53:28 +08:00
|
|
|
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) {
|
2016-02-02 00:22:51 +08:00
|
|
|
QueryTablesApi.prototype.getAffectedTablesAndLastUpdatedTime = getAffectedTablesFn;
|
2016-02-01 23:53:28 +08:00
|
|
|
|
|
|
|
assert.ok(!err, err);
|
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
|
|
|
|
assert.equal(skippedCache, scenario.shouldSkipCache, 'skip cache expected as true');
|
2016-02-02 00:22:51 +08:00
|
|
|
assert.ok(getAffectedTablesCalled, 'getAffectedTablesAndLastUpdatedTime NOT called');
|
2016-02-01 23:53:28 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|