Skip to content

Commit

Permalink
* Merge pull request.
Browse files Browse the repository at this point in the history
  • Loading branch information
kcbanner committed Sep 9, 2012
2 parents e4b8d78 + e75692b commit 3b5e48e
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Expand Up @@ -24,6 +24,7 @@ via npm:
option (optional, default: false).
- `url` Connection url of the form: `mongodb://user:pass@host:port/database/collection`.
If provided, information in the URL takes priority over the other options.
- `mongooseConnection` in the form: `someMongooseDb.connections[0]` to use an existing mongoose connection. (optional)
- `clear_interval` Interval in seconds to clear expired sessions (optional, default: `-1`).
Values <= 0 disable expired session clearing.
- `stringify` If true, connect-mongo will serialize sessions using `JSON.stringify` before
Expand Down Expand Up @@ -86,4 +87,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 25 additions & 12 deletions lib/connect-mongo.js
Expand Up @@ -75,19 +75,32 @@ module.exports = function(connect) {
}
}

if(!options.db) {
throw new Error('Required MongoStore option `db` missing');
if (options.mongooseConnection){
if (options.mongooseConnection.user && options.mongooseConnection.pass) {
options.username = options.mongooseConnection.user;
options.password = options.mongooseConnection.pass;
}

this.db = new mongo.Db(options.mongooseConnection.db.databaseName,
new mongo.Server(options.mongooseConnection.db.serverConfig.host,
options.mongooseConnection.db.serverConfig.port,
options.mongooseConnection.db.serverConfig.options
));

} else {
if(!options.db) {
throw new Error('Required MongoStore option `db` missing');
}

this.db = new mongo.Db(options.db,
new mongo.Server(options.host || defaultOptions.host,
options.port || defaultOptions.port,
{
auto_reconnect: options.auto_reconnect ||
defaultOptions.auto_reconnect
}));
}

this.db = new mongo.Db(options.db,
new mongo.Server(
options.host || defaultOptions.host,
options.port || defaultOptions.port,
{
auto_reconnect: options.auto_reconnect ||
defaultOptions.auto_reconnect
}));

this.db_collection_name = options.collection || defaultOptions.collection;

if (options.hasOwnProperty('stringify') ?
Expand Down Expand Up @@ -260,4 +273,4 @@ module.exports = function(connect) {
};

return MongoStore;
};
};
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -9,7 +9,8 @@
},
"devDependencies": {
"mocha": "1.x",
"connect": ">= 1.x"
"connect": ">= 1.x",
"mongoose": ">= 2.6.x"
},
"scripts" : { "test": "make test" },
"main": "index",
Expand Down
203 changes: 197 additions & 6 deletions test/connect-mongo.test.js
Expand Up @@ -10,15 +10,45 @@ var assert = require('assert');
var options = {db: 'connect-mongo-test'};
var mongo = require('mongodb');

var open_db = function(options, callback) {
var store = new MongoStore(options, function() {
var db = new mongo.Db(options.db, new mongo.Server('127.0.0.1', 27017, {}));

db.open(function(err) {
var mongoose = require('mongoose');
var testMongooseDb = mongoose.connect('mongodb://127.0.0.1:27017/connect-mongo-test');
var options_with_mongoose_connection = { mongooseConnection: testMongooseDb.connections[0] };

var auth_or_not = function(store, db, options, callback){
if (options.username && options.password) {
db.authenticate(options.username, options.password, function () {
db.collection('sessions', function(err, collection) {
callback(store, db, collection);
});
});
} else {
db.collection('sessions', function(err, collection) {
callback(store, db, collection);
});
}
};

var open_db = function(options, callback) {
var store = new MongoStore(options, function() {
var db;
if (options.mongooseConnection) {
db = new mongo.Db(options.mongooseConnection.db.databaseName,
new mongo.Server(options.mongooseConnection.db.serverConfig.host,
options.mongooseConnection.db.serverConfig.port,
options.mongooseConnection.db.serverConfig.options
));
} else {
db = new mongo.Db(options.db, new mongo.Server('127.0.0.1', 27017, {}));
}

if (db.openCalled) {
auth_or_not(store, db, options, callback);
} else {
db.open(function(err) {
auth_or_not(store, db, options, callback);
});
}

});
};

Expand Down Expand Up @@ -234,4 +264,165 @@ exports.test_options_url_and_db = function(done){
cleanup_store(store);
done();
});
}
};

/* options.mongooseConnection tests */

exports.test_set_with_raw_db = function(done) {
open_db(options_with_mongoose_connection, function(store, db, collection) {
var sid = 'test_set-sid';
store.set(sid, {foo:'bar'}, function(err, session) {
assert.strictEqual(err, null);

// Verify it was saved
collection.findOne({_id: sid}, function(err, session) {
assert.deepEqual(session,
{
session: JSON.stringify({foo: 'bar'}),
_id: sid
});

cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_set_no_stringify_with_raw_db = function(done) {
open_db({mongooseConnection: options_with_mongoose_connection.mongooseConnection, stringify: false}, function(store, db, collection) {
var sid = 'test_set-sid';
store.set(sid, {foo: 'bar'}, function(err, session) {
assert.strictEqual(err, null);

// Verify it was saved
collection.findOne({_id: sid}, function(err, session) {
assert.deepEqual(session,
{
session: {foo: 'bar'},
_id: sid
});

cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_set_expires_with_raw_db = function(done) {
open_db(options_with_mongoose_connection, function(store, db, collection) {
var sid = 'test_set_expires-sid';
var data = {
foo:'bar',
cookie:
{
_expires: '2011-04-26T03:10:12.890Z'
}
};

store.set(sid, data, function(err, session) {
assert.strictEqual(err, null);

// Verify it was saved
collection.findOne({_id: sid}, function(err, session) {
assert.deepEqual(session.session, JSON.stringify(data));
assert.strictEqual(session._id, sid);
assert.equal(session.expires.toJSON(), new Date(data.cookie._expires).toJSON());

cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_get_with_raw_db = function(done) {
open_db(options_with_mongoose_connection, function(store, db, collection) {
var sid = 'test_get-sid';
collection.insert({_id: sid, session: JSON.stringify({key1: 1, key2: 'two'})}, function(error, ids) {
store.get(sid, function(err, session) {
assert.deepEqual(session, {key1: 1, key2: 'two'});
cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_length_with_raw_db = function(done) {
open_db(options_with_mongoose_connection, function(store, db, collection) {
var sid = 'test_length-sid';
collection.insert({_id: sid, session: JSON.stringify({key1: 1, key2: 'two'})}, function(error, ids) {
store.length(function(err, length) {
assert.strictEqual(err, null);
assert.strictEqual(length, 1);
cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_destroy_ok_with_raw_db = function(done) {
open_db(options_with_mongoose_connection, function(store, db, collection) {
var sid = 'test_destroy_ok-sid';
collection.insert({_id: sid, session: JSON.stringify({key1: 1, key2: 'two'})}, function(error, ids) {
store.destroy(sid, function(err) {
assert.strictEqual(err, undefined);
cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_clear_with_raw_db = function(done) {
open_db(options_with_mongoose_connection, function(store, db, collection) {
var sid = 'test_length-sid';
collection.insert({_id: sid, key1: 1, key2: 'two'}, function(error, ids) {
store.clear(function(err) {
collection.count(function(err, count) {
assert.strictEqual(count, 0);

cleanup(store, db, collection, function() {
done();
});
});
});
});
});
};

exports.test_clear_expired_with_raw_db = function(done) {
open_db({mongooseConnection: options_with_mongoose_connection.mongooseConnection, clear_interval: 0.1}, function(store, db, collection) {
var sid = 'test_clear_expired-sid';
store.set(sid, {foo:'bar', cookie: {_expires: '2011-04-26T03:10:12.890Z'}}, function(err, session) {
setTimeout(function() {
collection.find({_id: sid}).toArray(function(err, results) {
assert.strictEqual(results.length, 0);

cleanup(store, db, collection, function() {
done();
});
});
}, 150);
});
});
};

exports.test_options_bad_db_with_raw_db = function(done) {
assert.throws(
function() {
var store = new MongoStore({mongooseConnection: 'foobar'}, function() {});
},
Error);

done();
};

0 comments on commit 3b5e48e

Please sign in to comment.