Permalink
Browse files

Issue 1002 - Bug in MongoDB.defineDb

  • Loading branch information...
1 parent f2522b6 commit 916d2b88bf7c49511fa0472bcdcaea1a3632b6d2 @eltimn eltimn committed May 7, 2011
@@ -99,8 +99,8 @@ object MongoDB {
/*
* Define a Mongo db using a standard Mongo instance.
*/
- def defineDb(name: MongoIdentifier, mongo: Mongo, dbName: String) {
- dbs.put(name, MongoAddress(new MongoHostBase { def mongo = mongo }, dbName))
+ def defineDb(name: MongoIdentifier, mngo: Mongo, dbName: String) {
+ dbs.put(name, MongoAddress(new MongoHostBase { def mongo = mngo }, dbName))
}
/*
@@ -114,6 +114,16 @@ object MongoDB {
}
/*
+ * Define and authenticate a Mongo db using a standard Mongo instance.
+ */
+ def defineDbAuth(name: MongoIdentifier, mngo: Mongo, dbName: String, username: String, password: String) {
+ if (!mngo.getDB(dbName).authenticate(username, password.toCharArray))
+ throw new MongoException("Authorization failed: "+mngo.toString)
+
+ dbs.put(name, MongoAddress(new MongoHostBase { def mongo = mngo }, dbName))
+ }
+
+ /*
* Get a DB reference
*/
def getDb(name: MongoIdentifier): Option[DB] = dbs.get(name) match {
@@ -28,6 +28,10 @@ object MongoSpec extends Specification("Mongo Specification") {
}
def passDefinitionTests(id: MongoIdentifier, ma: MongoAddress): Unit = {
+ // define the db
+ MongoDB.close
+ MongoDB.defineDb(id, ma)
+
// make sure mongo is running
try {
MongoDB.use(id) { db =>
@@ -38,10 +42,6 @@ object MongoSpec extends Specification("Mongo Specification") {
case e: Exception => skip("MongoDB is not running")
}
- // define the db
- MongoDB.close
- MongoDB.defineDb(id, ma)
-
// using an undefined identifier throws an exception
MongoDB.use(DefaultMongoIdentifier) { db =>
db.getLastError.ok must beEqualTo(true)
@@ -60,11 +60,6 @@ object MongoSpec extends Specification("Mongo Specification") {
"Define DB with specified host and port" in {
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoHost("127.0.0.1", 27017), "test_default"))
}
- "Define DB pair with DBAddress" in {
- val dba = new DBAddress("127.0.0.1", 27017, "test_a")
- val dbb = new DBAddress("127.0.0.1", 27018, "test_b")
- passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoPair(dba, dbb), "test_default"))
- }
"Define DB with ServerAddress and MongoOptions" in {
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoHost(new ServerAddress, new MongoOptions), "test_default"))
}
@@ -76,6 +71,12 @@ object MongoSpec extends Specification("Mongo Specification") {
mo.connectionsPerHost = 12
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoHost(options=mo), "test_default"))
}
+ /* These need all of the Mongo instances to be running to be useful.
+ "Define DB pair with DBAddress" in {
+ val dba = new DBAddress("127.0.0.1", 27017, "test_a")
+ val dbb = new DBAddress("127.0.0.1", 27018, "test_b")
+ passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoPair(dba, dbb), "test_default"))
+ }
"Define DB pair with ServerAddress" in {
val dba = new ServerAddress("127.0.0.1", 27017)
val dbb = new ServerAddress("127.0.0.1", 27018)
@@ -87,5 +88,50 @@ object MongoSpec extends Specification("Mongo Specification") {
val dbc = new ServerAddress("127.0.0.1", 27019)
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoSet(List(dba, dbb, dbc)), "test_default"))
}
+ */
+ "Define DB with Mongo instance" in {
+ // define the db
+ MongoDB.close
+ MongoDB.defineDb(TestMongoIdentifier, new Mongo, "test_default")
+
+ // make sure mongo is running
+ try {
+ MongoDB.use(TestMongoIdentifier) { db =>
+ db.getLastError.ok must beEqualTo(true)
+ }
+ }
+ catch {
+ case e: Exception => skip("MongoDB is not running")
+ }
+
+ // using an undefined identifier throws an exception
+ MongoDB.use(DefaultMongoIdentifier) { db =>
+ db.getLastError.ok must beEqualTo(true)
+ } must throwA(new MongoException("Mongo not found: MongoIdentifier(test)"))
+ // remove defined db
+ MongoDB.close
+ }
+ /* Requires a server other than localhost with auth setup.
+ "Define and authenticate DB with Mongo instance" in {
+ MongoDB.close
+
+ // make sure mongo is running
+ try {
+ val pwd = "lift_pwd"
+ val dbUri = new MongoURI("mongodb://")
+ // define the db
+ MongoDB.defineDbAuth(TestMongoIdentifier, new Mongo(dbUri), "lift_auth_test", "lift_user", pwd)
+ // try to use it
+ MongoDB.use(TestMongoIdentifier) { db =>
+ db.getLastError.ok must beEqualTo(true)
+ }
+ }
+ catch {
+ case e: Exception => skip("MongoDB is not running")
+ }
+ // remove defined db
+ MongoDB.close
+ }
+ */
}
}

0 comments on commit 916d2b8

Please sign in to comment.