Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for connect 2.0 -- Issue 50 #60

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NODE = node
MOCHA = node_modules/mocha/bin/mocha

test:
@$(NODE) tests/core.js
@$(MOCHA) --ignore-leaks tests/core.js

.PHONY: test
25 changes: 20 additions & 5 deletions lib/connect-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ function _default(callback) {
return callback;
}

/**
* One day in millis.
*/

var oneDay = 86400000;

/**
* Initialize MongoStore with the given `options`.
*
Expand All @@ -28,6 +34,8 @@ var MONGOSTORE = module.exports = function MongoStore(options, callback) {

var db, server_config, url, auth;

this.ttl = options.ttl;

function getCollection(db, callback) {
db.collection(options.collection || _defaults.collection, function (err, col) {
if (err) callback(err);
Expand Down Expand Up @@ -138,12 +146,19 @@ MONGOSTORE.prototype.get = function (sid, cb) {
*/
MONGOSTORE.prototype.set = function (sid, sess, cb) {
cb = _default(cb);
var update = {_id: sid, session: JSON.stringify(sess)};
if (sess && sess.cookie && sess.cookie.expires) {
update.expires = Date.parse(sess.cookie.expires);
}

_collection.update({_id: sid}, update, {upsert: true}, function (err, data) {
var maxAge = sess.cookie.maxAge
, ttl = this.ttl
, session = JSON.stringify(sess);

ttl = ttl || ('number' == typeof maxAge
? maxAge / 1000 | 0
: oneDay);

var update = {_id: sid, session: session};
update.expires = Date.now() + ttl

_collection.update({_id: sid}, update, {upsert: true, safe:true}, function (err, data) {
cb.apply(this, arguments);
});
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Vladimir Dronnikov <[email protected]>",
"contributors": ["Pau Ramon <[email protected]>", "ramv"],
"repository": {"type": "git", "url": "git://github.com/masylum/connect-mongodb.git"},
"dependencies": {"mongodb": "1.x", "connect": ">=1.8.5 <2"},
"devDependencies": {"testosterone": "1.2.0", "gently": "0.9.1", "funk": "1.5.0"},
"dependencies": {"mongodb": "1.x", "connect": "2.4.2"},
"devDependencies": {"mocha": "~1.3.0"},
"main": "./lib/connect-mongodb"
}
70 changes: 55 additions & 15 deletions tests/core.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,67 @@
var testosterone = require('testosterone')({title: 'models/advertiser'})
, assert = testosterone.assert
, gently = global.GENTLY = new (require('gently'))
var assert = require('assert')
, Db = require('mongodb').Db
, Server = require('mongodb').Server
, server_config = new Server('localhost', 27017, {auto_reconnect: true, native_parser: true})
, url = 'mongodb://localhost:27017/test'
, connect_mongodb = require('..')
, db = new Db('test', server_config, {});

testosterone
describe('mongodb connect', function () {
store = undefined;

.add('Should callback an error if no db given', function (done) {
var funk = require('funk')('parallel');
it('should set the session in mongo', function (done) {
store.set('123', {cookie: {maxAge: 3000}, name: 'Sidney Crosby'}, function (err, ok) {
assert.ok(!err, 'error on set');
db.collection('sessions', function (err, col) {
col.find().toArray(function (err, data) {
assert.equal(data.length, 1)
assert.equal(data[0]._id, 123)
assert.equal(data[0]._id, 123)
assert.deepEqual(data[0].session, JSON.stringify({ cookie: { maxAge: 3000 }, name: 'Sidney Crosby' }));
done();
});
});
});
});

db.open(function () {
connect_mongodb(null, funk.add(assert.ok));
connect_mongodb({db: null}, funk.add(assert.ok));
connect_mongodb({db: db, setInterval: -1}, funk.add(assert.ifError));
connect_mongodb({server_config: server_config, setInterval: -1}, funk.add(assert.ifError));
connect_mongodb({url: url, setInterval: -1}, funk.add(assert.ifError));
funk.run(done);
it('should get the session', function (done) {
store.set('123', {cookie: {maxAge: 3000}, name: 'Sidney Crosby'}, function (err, ok) {
store.get('123', function (err, data) {
assert.ok(!err, 'error on get');
assert.deepEqual({ cookie: { maxAge: 3000 }, name: 'Sidney Crosby' }, data);
done();
});
});
});

it('should destroy a session', function (done) {
store.set('123', {cookie: {maxAge: 3000}, name: 'Sidney Crosby'}, function (err, ok) {
store.destroy('123', function (err, data) {
assert.ok(!data, 'should not have this record')
done();
});
});
})
});

.run();
it('should clear expired sessions', function (done) {
store.set('123', {cookie: {}, name: 'Sidney Crosby'}, function (err, ok) {
setTimeout(function () {
store.get('123', function (err, data) {
assert.ok(!data);
done();
});
}, 1000)
});
});

beforeEach(function(done) {
db.open(function () {
store = new connect_mongodb({db: db, reapInterval: 500, ttl: 250});
done();
});
});

afterEach(function(done) {
db.close(done);
});
});