From d2408faea6684205131bf8e48460ca7de8af0f75 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Thu, 2 Feb 2012 09:53:26 +0400 Subject: [PATCH] Introduce monger.core/authenticate --- .travis.yml | 3 ++- bin/ci/before_script.sh | 8 ++++++++ src/monger/core.clj | 8 ++++++++ test/monger/test/authentication.clj | 19 +++++++++++++++++++ test/monger/test/helper.clj | 3 ++- 5 files changed, 39 insertions(+), 2 deletions(-) create mode 100755 bin/ci/before_script.sh create mode 100644 test/monger/test/authentication.clj diff --git a/.travis.yml b/.travis.yml index 4f42080..5abb530 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1 +1,2 @@ -language: clojure \ No newline at end of file +language: clojure +before_script: ./bin/ci/before_script.sh diff --git a/bin/ci/before_script.sh b/bin/ci/before_script.sh new file mode 100755 index 0000000..ca5f541 --- /dev/null +++ b/bin/ci/before_script.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +# MongoDB Java driver won't run authentication twice on the same DB instance, +# so we need to use multiple DBs. +mongo --eval 'db.addUser("clojurewerkz/monger", "monger")' monger-test +mongo --eval 'db.addUser("clojurewerkz/monger", "monger")' monger-test2 +mongo --eval 'db.addUser("clojurewerkz/monger", "monger")' monger-test3 +mongo --eval 'db.addUser("clojurewerkz/monger", "monger")' monger-test4 diff --git a/src/monger/core.clj b/src/monger/core.clj index 151d42a..90fe581 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -66,6 +66,14 @@ (.getDB connection name))) +(defn authenticate + ([^String db ^String username ^chars password] + (authenticate *mongodb-connection* db username password)) + ([^Mongo connection ^String db ^String username ^chars password] + (.authenticate (.getDB connection db) username password))) + + + (defmacro with-connection [conn & body] `(binding [*mongodb-connection* ~conn] diff --git a/test/monger/test/authentication.clj b/test/monger/test/authentication.clj new file mode 100644 index 0000000..002eed7 --- /dev/null +++ b/test/monger/test/authentication.clj @@ -0,0 +1,19 @@ +(ns monger.test.authentication + (:require [monger core util] + [monger.test.helper :as helper]) + (:use [clojure.test])) + +(helper/connect!) + + + +(deftest test-authentication-with-valid-credentials + ;; see ./bin/ci/before_script.sh. MK. + (let [username "clojurewerkz/monger" + pwd "monger"] + (is (monger.core/authenticate "monger-test" username (.toCharArray pwd))))) + +(deftest test-authentication-with-invalid-credentials + (let [username "monger" + ^String pwd (monger.util/random-str 128 32)] + (is (not (monger.core/authenticate "monger-test2" username (.toCharArray pwd)))))) diff --git a/test/monger/test/helper.clj b/test/monger/test/helper.clj index 2635a6b..89006f1 100644 --- a/test/monger/test/helper.clj +++ b/test/monger/test/helper.clj @@ -13,4 +13,5 @@ (do (monger.core/connect!) (monger.core/set-db! (monger.core/get-db "monger-test")) - (monger.core/set-default-write-concern! WriteConcern/SAFE)))) \ No newline at end of file + (monger.core/set-default-write-concern! WriteConcern/SAFE) + (reset! connected true))))