2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2011-09-05 07:00:41 +08:00
|
|
|
/**
|
|
|
|
* User: simon
|
|
|
|
* Date: 30/08/2011
|
|
|
|
* Time: 13:52
|
|
|
|
* Desc: Loads test specific variables
|
|
|
|
*/
|
|
|
|
|
2014-03-04 17:46:15 +08:00
|
|
|
var assert = require('assert');
|
2015-09-26 01:56:28 +08:00
|
|
|
var fs = require('fs');
|
2019-10-22 01:07:24 +08:00
|
|
|
var LZMA = require('lzma').LZMA;
|
2014-08-15 01:54:45 +08:00
|
|
|
|
|
|
|
var lzmaWorker = new LZMA();
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2015-09-17 21:10:23 +08:00
|
|
|
var redis = require('redis');
|
2015-09-18 22:59:45 +08:00
|
|
|
var log4js = require('log4js');
|
2019-10-07 16:04:39 +08:00
|
|
|
const setICUEnvVariable = require('../../lib/utils/icu-data-env-setter');
|
2015-09-17 21:10:23 +08:00
|
|
|
|
2011-09-05 07:00:41 +08:00
|
|
|
// set environment specific variables
|
2019-10-22 01:07:24 +08:00
|
|
|
global.environment = require('../../config/environments/test');
|
2015-07-08 06:12:32 +08:00
|
|
|
global.environment.name = 'test';
|
2014-02-28 23:14:44 +08:00
|
|
|
process.env.NODE_ENV = 'test';
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2018-01-11 04:06:47 +08:00
|
|
|
setICUEnvVariable();
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2015-09-18 22:59:45 +08:00
|
|
|
// don't output logs in test environment to reduce noise
|
|
|
|
log4js.configure({ appenders: [] });
|
|
|
|
global.logger = log4js.getLogger();
|
|
|
|
|
2013-04-24 21:10:58 +08:00
|
|
|
// Utility function to compress & encode LZMA
|
2019-10-22 01:07:24 +08:00
|
|
|
function lzma_compress_to_base64 (payload, mode, callback) {
|
|
|
|
lzmaWorker.compress(payload, mode,
|
|
|
|
function (ints) {
|
|
|
|
ints = ints.map(function (c) { return String.fromCharCode(c + 128); }).join('');
|
2019-10-22 02:05:51 +08:00
|
|
|
var base64 = Buffer.from(ints, 'binary').toString('base64');
|
2019-10-22 01:07:24 +08:00
|
|
|
callback(null, base64);
|
|
|
|
},
|
|
|
|
function (/* percent */) {
|
|
|
|
// console.log("Compressing: " + percent + "%");
|
|
|
|
}
|
|
|
|
);
|
2013-04-24 21:10:58 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 17:46:15 +08:00
|
|
|
// Check that the response headers do not request caching
|
|
|
|
// Throws on failure
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkNoCache (res) {
|
2019-10-22 05:33:27 +08:00
|
|
|
assert.ok(!Object.prototype.hasOwnProperty.call(res.headers, 'x-cache-channel'));
|
|
|
|
assert.ok(!Object.prototype.hasOwnProperty.call(res.headers, 'surrogate-key'));
|
|
|
|
assert.ok(!Object.prototype.hasOwnProperty.call(res.headers, 'cache-control')); // is this correct ?
|
|
|
|
assert.ok(!Object.prototype.hasOwnProperty.call(res.headers, 'last-modified')); // is this correct ?
|
2014-03-04 17:46:15 +08:00
|
|
|
}
|
|
|
|
|
2014-06-24 19:13:00 +08:00
|
|
|
/**
|
|
|
|
* Check that the response headers do not request caching
|
|
|
|
* @see checkNoCache
|
|
|
|
* @param res
|
|
|
|
*/
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkCache (res) {
|
2019-10-22 05:33:27 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(res.headers, 'x-cache-channel'));
|
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(res.headers, 'cache-control'));
|
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(res.headers, 'last-modified'));
|
2014-06-24 19:13:00 +08:00
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function checkSurrogateKey (res, expectedKey) {
|
2019-10-22 05:33:27 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(res.headers, 'surrogate-key'));
|
2016-02-17 19:15:20 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function createSet (keys, key) {
|
2016-02-17 19:15:20 +08:00
|
|
|
keys[key] = true;
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
var keys = res.headers['surrogate-key'].split(' ').reduce(createSet, {});
|
|
|
|
var expectedKeys = expectedKey.split(' ').reduce(createSet, {});
|
|
|
|
|
2019-10-22 01:52:51 +08:00
|
|
|
assert.deepStrictEqual(keys, expectedKeys);
|
2015-01-24 00:02:13 +08:00
|
|
|
}
|
|
|
|
|
2017-12-29 00:37:17 +08:00
|
|
|
var uncaughtExceptions = [];
|
2019-10-22 01:07:24 +08:00
|
|
|
process.on('uncaughtException', function (err) {
|
2017-12-29 00:37:17 +08:00
|
|
|
uncaughtExceptions.push(err);
|
|
|
|
});
|
2019-10-22 01:07:24 +08:00
|
|
|
beforeEach(function () {
|
2017-12-29 00:37:17 +08:00
|
|
|
uncaughtExceptions = [];
|
|
|
|
});
|
2019-10-22 01:07:24 +08:00
|
|
|
// global afterEach to capture uncaught exceptions
|
|
|
|
afterEach(function () {
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(
|
2017-12-29 00:37:17 +08:00
|
|
|
uncaughtExceptions.length,
|
|
|
|
0,
|
|
|
|
'uncaughtException:\n\n' + uncaughtExceptions.map(err => err.stack).join('\n\n'));
|
|
|
|
});
|
|
|
|
|
2016-02-23 00:51:53 +08:00
|
|
|
var redisClient;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
beforeEach(function () {
|
2016-02-23 00:51:53 +08:00
|
|
|
if (!redisClient) {
|
|
|
|
redisClient = redis.createClient(global.environment.redis.port);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
// global afterEach to capture test suites that leave keys in redis
|
|
|
|
afterEach(function (done) {
|
2015-09-17 21:07:54 +08:00
|
|
|
var expectedKeys = {
|
|
|
|
'rails:test_windshaft_cartodb_user_1_db:test_table_private_1': true,
|
|
|
|
'rails:test_windshaft_cartodb_user_1_db:my_table': true,
|
|
|
|
'rails:users:localhost:map_key': true,
|
|
|
|
'rails:users:cartodb250user': true,
|
2018-02-07 18:59:00 +08:00
|
|
|
'rails:users:localhost': true,
|
|
|
|
'api_keys:localhost:1234': true,
|
2018-02-08 18:13:21 +08:00
|
|
|
'api_keys:localhost:default_public': true,
|
|
|
|
'api_keys:cartodb250user:4321': true,
|
2018-02-08 19:34:24 +08:00
|
|
|
'api_keys:cartodb250user:default_public': true,
|
2018-02-08 20:07:25 +08:00
|
|
|
'api_keys:localhost:regular1': true,
|
2019-10-22 01:07:24 +08:00
|
|
|
'api_keys:localhost:regular2': true
|
2015-09-17 21:07:54 +08:00
|
|
|
};
|
2019-10-22 01:07:24 +08:00
|
|
|
var databasesTasks = { 0: 'users', 5: 'meta' };
|
2015-09-17 21:07:54 +08:00
|
|
|
|
|
|
|
var keysFound = [];
|
2019-10-22 01:07:24 +08:00
|
|
|
function taskDone (err, db, keys) {
|
2015-09-17 21:07:54 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete databasesTasks[db];
|
2019-10-22 01:07:24 +08:00
|
|
|
keys.forEach(function (k) {
|
2015-09-17 21:07:54 +08:00
|
|
|
if (!expectedKeys[k]) {
|
2019-10-22 01:07:24 +08:00
|
|
|
keysFound.push('[db=' + db + ']' + k);
|
2015-09-17 21:07:54 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Object.keys(databasesTasks).length === 0) {
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(keysFound.length, 0, 'Unexpected keys found in redis: ' + keysFound.join(', '));
|
2015-09-17 21:07:54 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(databasesTasks).forEach(function (db) {
|
|
|
|
redisClient.select(db, function () {
|
2015-09-17 21:07:54 +08:00
|
|
|
// Check that we start with an empty redis db
|
2019-10-22 01:07:24 +08:00
|
|
|
redisClient.keys('*', function (err, keys) {
|
2015-09-17 21:07:54 +08:00
|
|
|
return taskDone(err, db, keys);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function deleteRedisKeys (keysToDelete, callback) {
|
2015-09-25 20:09:14 +08:00
|
|
|
if (Object.keys(keysToDelete).length === 0) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function taskDone (k) {
|
2015-09-17 21:07:54 +08:00
|
|
|
delete keysToDelete[k];
|
|
|
|
if (Object.keys(keysToDelete).length === 0) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(keysToDelete).forEach(function (k) {
|
2015-09-17 21:07:54 +08:00
|
|
|
var redisClient = redis.createClient(global.environment.redis.port);
|
2019-10-22 01:07:24 +08:00
|
|
|
redisClient.select(keysToDelete[k], function () {
|
|
|
|
redisClient.del(k, function (err, deletedKeysCount) {
|
2019-10-25 00:38:37 +08:00
|
|
|
assert.ifError(err);
|
2016-02-23 00:51:53 +08:00
|
|
|
redisClient.quit();
|
2015-09-26 01:23:33 +08:00
|
|
|
assert.notStrictEqual(deletedKeysCount, 0, 'No KEYS deleted for: [db=' + keysToDelete[k] + ']' + k);
|
2015-09-17 21:07:54 +08:00
|
|
|
taskDone(k);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function rmdirRecursiveSync (dirname) {
|
2015-09-26 01:56:28 +08:00
|
|
|
var files = fs.readdirSync(dirname);
|
2019-10-22 01:07:24 +08:00
|
|
|
for (var i = 0; i < files.length; ++i) {
|
|
|
|
var f = dirname + '/' + files[i];
|
2015-09-26 01:56:28 +08:00
|
|
|
var s = fs.lstatSync(f);
|
2019-10-22 01:07:24 +08:00
|
|
|
if (s.isFile()) {
|
2015-09-26 01:56:28 +08:00
|
|
|
fs.unlinkSync(f);
|
2019-10-22 01:07:24 +08:00
|
|
|
} else {
|
2015-09-26 01:56:28 +08:00
|
|
|
rmdirRecursiveSync(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-24 19:13:00 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function configureMetadata (action, params, callback) {
|
2017-07-19 02:50:31 +08:00
|
|
|
redisClient.SELECT(5, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
redisClient[action](params, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-04-24 21:10:58 +08:00
|
|
|
module.exports = {
|
2019-10-22 01:07:24 +08:00
|
|
|
deleteRedisKeys: deleteRedisKeys,
|
|
|
|
lzma_compress_to_base64: lzma_compress_to_base64,
|
|
|
|
checkNoCache: checkNoCache,
|
|
|
|
checkSurrogateKey: checkSurrogateKey,
|
|
|
|
checkCache: checkCache,
|
|
|
|
rmdirRecursiveSync: rmdirRecursiveSync,
|
|
|
|
configureMetadata
|
2014-06-24 19:13:00 +08:00
|
|
|
};
|