CartoDB-SQL-API/test/support/test-client.js

149 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-10-10 21:37:46 +08:00
'use strict';
require('../helper');
var assert = require('assert');
var appServer = require('../../lib/server');
var redisUtils = require('./redis-utils');
2017-08-09 18:53:59 +08:00
const step = require('step');
const PSQL = require('cartodb-psql');
const _ = require('underscore');
2016-10-10 21:37:46 +08:00
2019-12-24 01:19:08 +08:00
function response (code) {
2016-10-10 21:37:46 +08:00
return {
status: code
};
}
var RESPONSE = {
OK: response(200),
CREATED: response(201)
};
2019-12-24 01:19:08 +08:00
function TestClient (config) {
2016-10-10 21:37:46 +08:00
this.config = config || {};
this.server = appServer();
}
module.exports = TestClient;
2019-12-24 01:19:08 +08:00
TestClient.prototype.getResult = function (query, override, callback) {
2016-10-19 22:56:43 +08:00
if (!callback) {
callback = override;
override = {};
}
2018-02-16 17:46:58 +08:00
2016-10-10 21:37:46 +08:00
assert.response(
this.server,
{
2016-10-19 22:56:43 +08:00
url: this.getUrl(override),
2016-10-10 21:37:46 +08:00
headers: {
2016-10-19 22:56:43 +08:00
host: this.getHost(override),
2018-02-16 17:46:58 +08:00
'Content-Type': this.getContentType(override),
authorization: this.getAuthorization(override)
2016-10-10 21:37:46 +08:00
},
method: 'POST',
data: this.getParser(override)({
q: query,
2017-08-09 18:53:59 +08:00
format: this.getFormat(override),
filename: this.getFilename(override)
2016-10-10 21:37:46 +08:00
})
},
this.getExpectedResponse(override),
2016-10-10 21:37:46 +08:00
function (err, res) {
if (err) {
return callback(err);
}
var result = JSON.parse(res.body);
if (res.statusCode > 299) {
return callback(null, result);
}
return callback(null, result.rows || [], result);
2016-10-10 21:37:46 +08:00
}
);
};
2019-12-24 01:19:08 +08:00
TestClient.prototype.getHost = function (override) {
2016-10-19 22:56:43 +08:00
return override.host || this.config.host || 'vizzuality.cartodb.com';
2016-10-10 21:37:46 +08:00
};
2018-02-16 17:46:58 +08:00
TestClient.prototype.getAuthorization = function (override) {
const auth = override.authorization || this.config.authorization;
if (auth) {
return `Basic ${new Buffer(auth).toString('base64')}`;
}
};
2019-12-24 01:19:08 +08:00
TestClient.prototype.getContentType = function (override) {
return override['Content-Type'] || this.config['Content-Type'] || 'application/json';
};
TestClient.prototype.getParser = function (override) {
2019-12-24 01:19:08 +08:00
return override.parser || this.config.parser || JSON.stringify;
};
2019-12-24 01:19:08 +08:00
TestClient.prototype.getUrl = function (override) {
if (override.anonymous) {
return '/api/v1/sql?';
}
2016-10-19 22:56:43 +08:00
return '/api/v2/sql?api_key=' + (override.apiKey || this.config.apiKey || '1234');
2016-10-10 21:37:46 +08:00
};
TestClient.prototype.getExpectedResponse = function (override) {
return override.response || this.config.response || RESPONSE.OK;
};
TestClient.prototype.getFormat = function (override) {
return override.format || this.config.format || undefined;
};
2017-08-09 18:53:59 +08:00
TestClient.prototype.getFilename = function (override) {
return override.filename || this.config.filename || undefined;
};
TestClient.prototype.setUserRenderTimeoutLimit = function (user, userTimeoutLimit, callback) {
const userTimeoutLimitsKey = `limits:timeout:${user}`;
const params = [
userTimeoutLimitsKey,
'render', userTimeoutLimit,
'render_public', userTimeoutLimit
];
redisUtils.configureUserMetadata('hmset', params, callback);
};
TestClient.prototype.setUserDatabaseTimeoutLimit = function (user, timeoutLimit, callback) {
const dbname = _.template(global.settings.db_base_name, { user_id: 1 });
2019-12-24 01:19:08 +08:00
const dbuser = _.template(global.settings.db_user, { user_id: 1 });
const pass = _.template(global.settings.db_user_pass, { user_id: 1 });
2017-08-09 18:53:59 +08:00
const publicuser = global.settings.db_pubuser;
const psql = new PSQL({
user: 'postgres',
dbname: dbname,
host: global.settings.db_host,
port: global.settings.db_port
});
// we need to guarantee all new connections have the new settings
psql.end();
2017-08-09 18:53:59 +08:00
step(
function configureTimeouts () {
const timeoutSQLs = [
`ALTER ROLE "${publicuser}" SET STATEMENT_TIMEOUT TO ${timeoutLimit}`,
`ALTER ROLE "${dbuser}" SET STATEMENT_TIMEOUT TO ${timeoutLimit}`,
`ALTER DATABASE "${dbname}" SET STATEMENT_TIMEOUT TO ${timeoutLimit}`
];
const group = this.group();
timeoutSQLs.forEach(sql => psql.query(sql, group()));
},
callback
);
};