Skip to content

Commit

Permalink
Directly return from authentication
Browse files Browse the repository at this point in the history
Returns either true or an AuthenticationError if 
authentication wasn't successful.

This way you won't have to use callbacks on it so 
it is more in the spirit of Synchrony.
  • Loading branch information
bittersweet committed Sep 18, 2011
1 parent 7103d18 commit 249ad88
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
6 changes: 2 additions & 4 deletions lib/em-synchrony/em-mongo.rb
Expand Up @@ -9,7 +9,6 @@ module Mongo


class Database class Database
def authenticate(username, password) def authenticate(username, password)
response = RequestResponse.new
auth_result = self.collection(SYSTEM_COMMAND_COLLECTION).first({'getnonce' => 1}) auth_result = self.collection(SYSTEM_COMMAND_COLLECTION).first({'getnonce' => 1})


auth = BSON::OrderedHash.new auth = BSON::OrderedHash.new
Expand All @@ -20,11 +19,10 @@ def authenticate(username, password)


auth_result2 = self.collection(SYSTEM_COMMAND_COLLECTION).first(auth) auth_result2 = self.collection(SYSTEM_COMMAND_COLLECTION).first(auth)
if EM::Mongo::Support.ok?(auth_result2) if EM::Mongo::Support.ok?(auth_result2)
response.succeed true true
else else
response.fail auth_result2 raise AuthenticationError, auth_result2["errmsg"]
end end
response
end end
end end


Expand Down
35 changes: 22 additions & 13 deletions spec/em-mongo_spec.rb
Expand Up @@ -231,20 +231,29 @@
end end
end end


it "authenticates" do context "authentication" do
# For this to actually assert anything we will need to add the test user to # these specs only get asserted if you run mongod with the --auth flag
# the database it "successfully authenticates" do
# # For this spec you will need to add this user to the database
# From the Mongo shell: #
# > use db # From the Mongo shell:
# > db.addUser('test', 'test') # > use db
EventMachine.synchrony do # > db.addUser('test', 'test')
database = EM::Mongo::Connection.new.db('db') EventMachine.synchrony do
database.add_user('test', 'test') database = EM::Mongo::Connection.new.db('db')
auth = database.authenticate('test', 'test').callback do |cb| database.authenticate('test', 'test').should == true
cb.should == true EventMachine.stop
end
end

it "raises an AuthenticationError if it cannot authenticate" do
EventMachine.synchrony do
database = EM::Mongo::Connection.new.db('db')
proc {
database.authenticate('test', 'wrong_password')
}.should raise_error(EventMachine::Mongo::AuthenticationError, "auth fails")
EventMachine.stop
end end
EventMachine.stop
end end
end end
end end

0 comments on commit 249ad88

Please sign in to comment.