From 19b8e48084a5c4fb94236688d5d4c98d74f62507 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Wed, 11 Apr 2012 01:33:29 +0400 Subject: [PATCH] Make sure we authenticate before calling set-db! (which instantiates a GridFS object) Fixes Heroku/MongoHQ add-on issues --- project.clj | 1 + src/monger/core.clj | 5 +++-- test/monger/test/core.clj | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/project.clj b/project.clj index 44c18b9..c1fc532 100644 --- a/project.clj +++ b/project.clj @@ -8,6 +8,7 @@ :test-selectors {:default (complement :performance) :focus :focus :indexing :indexing + :external :external :performance :performance :all (constantly true)} :codox {:exclude [monger.internal.pagination]} diff --git a/src/monger/core.clj b/src/monger/core.clj index cf86497..be62984 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -189,11 +189,12 @@ ;; ;; First we set connection, then DB, then authentcate (set-connection! conn) - (when db - (set-db! db)) (when (and user pwd) (when-not (authenticate (.getName db) user pwd) (throw (IllegalArgumentException. "Could not authenticate. Either database name or credentials are invalid.")))) + ;; only do this *after* we authenticated because set-db! will try to set up a default GridFS instance. MK. + (when db + (set-db! db)) conn)) diff --git a/test/monger/test/core.clj b/test/monger/test/core.clj index 6bcae50..d0a8b95 100644 --- a/test/monger/test/core.clj +++ b/test/monger/test/core.clj @@ -35,6 +35,14 @@ ;; reconnect using regular host (helper/connect!)) +(if-let [uri (System/getenv "MONGOHQ_URL")] + (deftest ^{:external true} connect-to-mongo-via-uri-with-valid-credentials + (let [connection (monger.core/connect-via-uri! uri)] + (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"))))