Skip to content

Commit

Permalink
tests for #5600
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Apr 16, 2017
1 parent 185b6e5 commit 65a65b1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/database/mongo.js
Expand Up @@ -116,9 +116,7 @@
var meta = require('../meta');
var sessionStore;

var ttlDays = 60 * 60 * 24 * (parseInt(meta.config.loginDays, 10) || 0);
var ttlSeconds = (parseInt(meta.config.loginSeconds, 10) || 0);
var ttl = ttlSeconds || ttlDays || 1209600; // Default to 14 days in seconds
var ttl = meta.getSessionTTLSeconds();

if (nconf.get('redis')) {
sessionStore = require('connect-redis')(session);
Expand Down
6 changes: 1 addition & 5 deletions src/database/redis.js
Expand Up @@ -60,13 +60,9 @@
var meta = require('../meta');
var sessionStore = require('connect-redis')(session);

var ttlDays = 60 * 60 * 24 * (parseInt(meta.config.loginDays, 10) || 0);
var ttlSeconds = (parseInt(meta.config.loginSeconds, 10) || 0);
var ttl = ttlSeconds || ttlDays || 1209600; // Default to 14 days

module.sessionStore = new sessionStore({
client: module.client,
ttl: ttl,
ttl: meta.getSessionTTLSeconds(),
});

if (typeof callback === 'function') {
Expand Down
7 changes: 7 additions & 0 deletions src/meta.js
Expand Up @@ -51,6 +51,13 @@ var utils = require('./utils');
restart();
};

Meta.getSessionTTLSeconds = function () {
var ttlDays = 60 * 60 * 24 * (parseInt(Meta.config.loginDays, 10) || 0);
var ttlSeconds = (parseInt(Meta.config.loginSeconds, 10) || 0);
var ttl = ttlSeconds || ttlDays || 1209600; // Default to 14 days
return ttl;
};

if (nconf.get('isPrimary') === 'true') {
pubsub.on('meta:restart', function (data) {
if (data.hostname !== os.hostname()) {
Expand Down
4 changes: 1 addition & 3 deletions src/webserver.js
Expand Up @@ -166,9 +166,7 @@ function setupFavicon(app) {
}

function setupCookie() {
var ttlDays = 1000 * 60 * 60 * 24 * (parseInt(meta.config.loginDays, 10) || 0);
var ttlSeconds = 1000 * (parseInt(meta.config.loginSeconds, 10) || 0);
var ttl = ttlSeconds || ttlDays || 1209600000; // Default to 14 days
var ttl = meta.getSessionTTLSeconds() * 1000;

var cookie = {
maxAge: ttl,
Expand Down
20 changes: 20 additions & 0 deletions test/meta.js
Expand Up @@ -192,6 +192,26 @@ describe('meta', function () {
});


describe('session TTL', function () {
it('should return 14 days in seconds', function (done) {
assert(meta.getSessionTTLSeconds(), 1209600);
done();
});

it('should return 7 days in seconds', function (done) {
meta.config.loginDays = 7;
assert(meta.getSessionTTLSeconds(), 604800);
done();
});

it('should return 2 days in seconds', function (done) {
meta.config.loginSeconds = 172800;
assert(meta.getSessionTTLSeconds(), 172800);
done();
});
});


after(function (done) {
db.emptydb(done);
});
Expand Down

0 comments on commit 65a65b1

Please sign in to comment.