Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Directly return from authentication

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...
commit 249ad88fe494d258e250bcba909ef9ff6bc20ce1 1 parent 7103d18
@bittersweet bittersweet authored
Showing with 24 additions and 17 deletions.
  1. +2 −4 lib/em-synchrony/em-mongo.rb
  2. +22 −13 spec/em-mongo_spec.rb
View
6 lib/em-synchrony/em-mongo.rb
@@ -9,7 +9,6 @@ module Mongo
class Database
def authenticate(username, password)
- response = RequestResponse.new
auth_result = self.collection(SYSTEM_COMMAND_COLLECTION).first({'getnonce' => 1})
auth = BSON::OrderedHash.new
@@ -20,11 +19,10 @@ def authenticate(username, password)
auth_result2 = self.collection(SYSTEM_COMMAND_COLLECTION).first(auth)
if EM::Mongo::Support.ok?(auth_result2)
- response.succeed true
+ true
else
- response.fail auth_result2
+ raise AuthenticationError, auth_result2["errmsg"]
end
- response
end
end
View
35 spec/em-mongo_spec.rb
@@ -231,20 +231,29 @@
end
end
- it "authenticates" do
- # For this to actually assert anything we will need to add the test user to
- # the database
- #
- # From the Mongo shell:
- # > use db
- # > db.addUser('test', 'test')
- EventMachine.synchrony do
- database = EM::Mongo::Connection.new.db('db')
- database.add_user('test', 'test')
- auth = database.authenticate('test', 'test').callback do |cb|
- cb.should == true
+ context "authentication" do
+ # these specs only get asserted if you run mongod with the --auth flag
+ it "successfully authenticates" do
+ # For this spec you will need to add this user to the database
+ #
+ # From the Mongo shell:
+ # > use db
+ # > db.addUser('test', 'test')
+ EventMachine.synchrony do
+ database = EM::Mongo::Connection.new.db('db')
+ database.authenticate('test', 'test').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
- EventMachine.stop
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.