diff --git a/src/monger/core.clj b/src/monger/core.clj index 7599136..cf86497 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -176,7 +176,7 @@ (defn connect-via-uri! "Connects to MongoDB using a URI, sets up default connection and database. Commonly used for PaaS-based applications, for example, running on Heroku. If username and password are provided, performs authentication." - [{ :keys [uri] }] + [uri] (let [uri (MongoURI. uri) ;; yes, you are not hallucinating. A class named MongoURI has a method called connectDB. ;; I call it "college OOP". Or maybe "don't give a shit" OOP. @@ -192,7 +192,8 @@ (when db (set-db! db)) (when (and user pwd) - (authenticate db user pwd)) + (when-not (authenticate (.getName db) user pwd) + (throw (IllegalArgumentException. "Could not authenticate. Either database name or credentials are invalid.")))) conn)) diff --git a/test/monger/test/core.clj b/test/monger/test/core.clj index a91ead0..6bcae50 100644 --- a/test/monger/test/core.clj +++ b/test/monger/test/core.clj @@ -24,8 +24,20 @@ (is (instance? com.mongodb.Mongo connection)))) (deftest connect-to-mongo-via-uri-without-credentials - (let [connection (monger.core/connect "mongodb://127.0.0.1")] - (is (instance? com.mongodb.Mongo connection)))) + (let [connection (monger.core/connect-via-uri! "mongodb://127.0.0.1/monger-test4")] + (is (= (-> connection .getAddress (.sameHost "127.0.0.1"))))) + ;; reconnect using regular host + (helper/connect!)) + +(deftest connect-to-mongo-via-uri-with-valid-credentials + (let [connection (monger.core/connect-via-uri! "mongodb://clojurewerkz/monger!:monger!@127.0.0.1/monger-test4")] + (is (= (-> connection .getAddress (.sameHost "127.0.0.1"))))) + ;; reconnect using regular host + (helper/connect!)) + +(deftest connect-to-mongo-via-uri-with-invalid-credentials + (is (thrown? IllegalArgumentException + (monger.core/connect-via-uri! "mongodb://clojurewerkz/monger!:ahsidaysd78jahsdi8@127.0.0.1/monger-test4")))) (deftest test-mongo-options-builder