diff --git a/lib/mongoid/config/replset_database.rb b/lib/mongoid/config/replset_database.rb index f6a984752c..d2a527c7f2 100644 --- a/lib/mongoid/config/replset_database.rb +++ b/lib/mongoid/config/replset_database.rb @@ -17,14 +17,14 @@ def configure #yes, construction is weird but the driver wants "A list of host-port pairs ending with a hash containing any options" #mongo likes symbols options = self.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo} - connection = Mongo::ReplSetConnection.new(*(self['hosts'] << options)) + connection = Mongo::ReplSetConnection.new(*(hosts << options)) if authenticating? connection.add_auth(database, username, password) connection.apply_saved_authentication end - [ connection.db(self['database']), nil ] + [ connection.db(database), nil ] end # Do we need to authenticate against the database? @@ -34,7 +34,7 @@ def configure # # @return [ true, false ] True if auth is needed, false if not. # - # @since 2.0.0.rc.7 + # @since 2.0.2 def authenticating? username || password end @@ -46,7 +46,7 @@ def authenticating? # # @return [ Object ] The value in the hash. # - # @since 2.0.0.rc.7 + # @since 2.0.2 def method_missing(name, *args, &block) self[name.to_s] end @@ -71,8 +71,6 @@ def method_missing(name, *args, &block) # # @since 2.0.0.rc.5 def initialize(options = {}) - self[:logger] = Mongoid::Logger.new - merge!(options) end end diff --git a/spec/config/mongoid.replset.yml b/spec/config/mongoid.replset.yml index 9acffc9acd..39fa2f84cf 100644 --- a/spec/config/mongoid.replset.yml +++ b/spec/config/mongoid.replset.yml @@ -18,3 +18,9 @@ test: hosts: [ [localhost, 27017], [localhost, 27017] ] database: multi_db_replset_test reconnect_time: 5 + +authenticated: + <<: *defaults + database: mongoid_config_authenticated_test + username: mongoid + password: test diff --git a/spec/functional/mongoid/config/replset_database_spec.rb b/spec/functional/mongoid/config/replset_database_spec.rb index 6effa48bf3..58b9e7014d 100644 --- a/spec/functional/mongoid/config/replset_database_spec.rb +++ b/spec/functional/mongoid/config/replset_database_spec.rb @@ -27,5 +27,49 @@ it "does not configure specific slaves" do replica_set[1].should be_nil end + + context "without authentication details" do + + let(:replica_set) do + described_class.new(options['test']).configure + end + + let(:repl_set_connection) do + stub.quacks_like(Mongo::ReplSetConnection.allocate) + end + + before do + Mongo::ReplSetConnection.stubs(:new).returns(repl_set_connection) + end + + it "should not add authentication or apply" do + repl_set_connection.expects(:db) + repl_set_connection.expects(:add_auth).never + repl_set_connection.expects(:apply_saved_authentication).never + replica_set + end + end + + context "with authentication details" do + + let(:replica_set) do + described_class.new(options['authenticated']).configure + end + + let(:repl_set_connection) do + stub.quacks_like(Mongo::ReplSetConnection.allocate) + end + + before do + Mongo::ReplSetConnection.stubs(:new).returns(repl_set_connection) + end + + it "should add authentication and apply" do + repl_set_connection.expects(:db) + repl_set_connection.expects(:add_auth).with(options['authenticated']['database'], options['authenticated']['username'], options['authenticated']['password']) + repl_set_connection.expects(:apply_saved_authentication) + replica_set + end + end end end diff --git a/spec/unit/mongoid/config/replset_database_spec.rb b/spec/unit/mongoid/config/replset_database_spec.rb deleted file mode 100644 index be383cb5d7..0000000000 --- a/spec/unit/mongoid/config/replset_database_spec.rb +++ /dev/null @@ -1,64 +0,0 @@ -require "spec_helper" - -describe Mongoid::Config::ReplsetDatabase do - - describe "#configure" do - - subject do - described_class.new(options) - end - - let(:hosts) do - [["localhost", 27010], ["localhost", 27010]] - end - - let(:options) do - { 'database' => 'mongoid_test', 'hosts' => hosts } - end - - let(:replica_set) do - described_class.new(options).configure - end - - let(:repl_set_connection) do - stub.quacks_like(Mongo::ReplSetConnection.allocate) - end - - before do - Mongo::ReplSetConnection.stubs(:new).returns(repl_set_connection) - end - - it "should set a logger" do - subject[:logger].should be_an_instance_of(Mongoid::Logger) - end - - context "when authentication keys are not given" do - - it { should_not be_authenticating } - - it "should not add auth to connection" do - repl_set_connection.expects(:add_auth).never - end - - it "should not apply authentication" do - repl_set_connection.expects(:apply_saved_authentication).never - end - end - - context "when authentication keys are given" do - - let(:options) do - { 'database' => 'mongoid_test', 'username' => 'mongoid', 'password' => 'test', 'hosts' => hosts } - end - - it { should be_authenticating } - - it "should add authentication and apply" do - repl_set_connection.expects(:db) - repl_set_connection.expects(:add_auth).with(options['database'], options['username'], options['password']) - repl_set_connection.expects(:apply_saved_authentication) - replica_set - end - end - end -end