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');
|
2014-08-15 01:54:45 +08:00
|
|
|
var LZMA = require('lzma').LZMA;
|
|
|
|
|
|
|
|
var lzmaWorker = new LZMA();
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2015-09-17 21:10:23 +08:00
|
|
|
var redis = require('redis');
|
|
|
|
var nock = require('nock');
|
2015-09-18 22:59:45 +08:00
|
|
|
var log4js = require('log4js');
|
2015-09-17 21:10:23 +08:00
|
|
|
|
2011-09-05 07:00:41 +08:00
|
|
|
// set environment specific variables
|
2012-07-18 17:00:24 +08:00
|
|
|
global.environment = require(__dirname + '/../../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
|
|
|
|
|
|
|
|
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
|
|
|
|
function lzma_compress_to_base64(payload, mode, callback) {
|
2014-08-15 01:54:45 +08:00
|
|
|
lzmaWorker.compress(payload, mode,
|
2013-04-24 21:10:58 +08:00
|
|
|
function(ints) {
|
2015-07-05 02:41:22 +08:00
|
|
|
ints = ints.map(function(c) { return String.fromCharCode(c + 128); }).join('');
|
2013-04-24 21:10:58 +08:00
|
|
|
var base64 = new Buffer(ints, 'binary').toString('base64');
|
|
|
|
callback(null, base64);
|
|
|
|
},
|
2015-07-05 02:41:22 +08:00
|
|
|
function(/*percent*/) {
|
2013-04-24 21:10:58 +08:00
|
|
|
//console.log("Compressing: " + percent + "%");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-03-04 17:46:15 +08:00
|
|
|
// Check that the response headers do not request caching
|
|
|
|
// Throws on failure
|
|
|
|
function checkNoCache(res) {
|
|
|
|
assert.ok(!res.headers.hasOwnProperty('x-cache-channel'));
|
2015-07-13 22:54:08 +08:00
|
|
|
assert.ok(!res.headers.hasOwnProperty('surrogate-key'));
|
2014-03-04 17:46:15 +08:00
|
|
|
assert.ok(!res.headers.hasOwnProperty('cache-control')); // is this correct ?
|
|
|
|
assert.ok(!res.headers.hasOwnProperty('last-modified')); // is this correct ?
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 19:13:00 +08:00
|
|
|
/**
|
|
|
|
* Check that the response headers do not request caching
|
|
|
|
* @see checkNoCache
|
|
|
|
* @param res
|
|
|
|
*/
|
|
|
|
function checkCache(res) {
|
|
|
|
assert.ok(res.headers.hasOwnProperty('x-cache-channel'));
|
|
|
|
assert.ok(res.headers.hasOwnProperty('cache-control'));
|
|
|
|
assert.ok(res.headers.hasOwnProperty('last-modified'));
|
|
|
|
}
|
|
|
|
|
2015-01-24 00:02:13 +08:00
|
|
|
function checkSurrogateKey(res, expectedKey) {
|
|
|
|
assert.ok(res.headers.hasOwnProperty('surrogate-key'));
|
|
|
|
assert.equal(res.headers['surrogate-key'], expectedKey);
|
|
|
|
}
|
|
|
|
|
2016-02-23 00:51:53 +08:00
|
|
|
var redisClient;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
if (!redisClient) {
|
|
|
|
redisClient = redis.createClient(global.environment.redis.port);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-26 01:23:33 +08:00
|
|
|
//global afterEach to capture test suites that leave keys in redis
|
|
|
|
afterEach(function(done) {
|
2015-09-17 21:10:23 +08:00
|
|
|
|
|
|
|
// restoring nock globally after each suite
|
2015-09-17 23:14:32 +08:00
|
|
|
nock.cleanAll();
|
|
|
|
nock.enableNetConnect();
|
2015-09-17 21:10:23 +08:00
|
|
|
|
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,
|
|
|
|
'rails:users:localhost': true
|
|
|
|
};
|
|
|
|
var databasesTasks = { 0: 'users', 5: 'meta'};
|
|
|
|
|
|
|
|
var keysFound = [];
|
|
|
|
function taskDone(err, db, keys) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete databasesTasks[db];
|
|
|
|
keys.forEach(function(k) {
|
|
|
|
if (!expectedKeys[k]) {
|
2015-09-25 20:21:04 +08:00
|
|
|
keysFound.push('[db='+db+']'+k);
|
2015-09-17 21:07:54 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Object.keys(databasesTasks).length === 0) {
|
2015-09-26 01:23:33 +08:00
|
|
|
assert.equal(keysFound.length, 0, 'Unexpected keys found in redis: ' + keysFound.join(', '));
|
2015-09-17 21:07:54 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(databasesTasks).forEach(function(db) {
|
|
|
|
redisClient.select(db, function() {
|
|
|
|
// Check that we start with an empty redis db
|
|
|
|
redisClient.keys("*", function(err, keys) {
|
|
|
|
return taskDone(err, db, keys);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function deleteRedisKeys(keysToDelete, callback) {
|
|
|
|
|
2015-09-25 20:09:14 +08:00
|
|
|
if (Object.keys(keysToDelete).length === 0) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
2015-09-17 21:07:54 +08:00
|
|
|
function taskDone(k) {
|
|
|
|
delete keysToDelete[k];
|
|
|
|
if (Object.keys(keysToDelete).length === 0) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(keysToDelete).forEach(function(k) {
|
|
|
|
var redisClient = redis.createClient(global.environment.redis.port);
|
|
|
|
redisClient.select(keysToDelete[k], function() {
|
2015-09-26 01:23:33 +08:00
|
|
|
redisClient.del(k, function(err, deletedKeysCount) {
|
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
|
|
|
|
2015-09-26 01:56:28 +08:00
|
|
|
function rmdirRecursiveSync(dirname) {
|
|
|
|
var files = fs.readdirSync(dirname);
|
|
|
|
for (var i=0; i<files.length; ++i) {
|
|
|
|
var f = dirname + "/" + files[i];
|
|
|
|
var s = fs.lstatSync(f);
|
|
|
|
if ( s.isFile() ) {
|
|
|
|
fs.unlinkSync(f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rmdirRecursiveSync(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-24 19:13:00 +08:00
|
|
|
|
2013-04-24 21:10:58 +08:00
|
|
|
module.exports = {
|
2015-09-17 21:07:54 +08:00
|
|
|
deleteRedisKeys: deleteRedisKeys,
|
2014-03-04 17:46:15 +08:00
|
|
|
lzma_compress_to_base64: lzma_compress_to_base64,
|
2014-06-24 19:13:00 +08:00
|
|
|
checkNoCache: checkNoCache,
|
2015-01-24 00:02:13 +08:00
|
|
|
checkSurrogateKey: checkSurrogateKey,
|
2015-09-26 01:56:28 +08:00
|
|
|
checkCache: checkCache,
|
|
|
|
rmdirRecursiveSync: rmdirRecursiveSync
|
2014-06-24 19:13:00 +08:00
|
|
|
};
|
2011-09-05 07:00:41 +08:00
|
|
|
|