Fixed MD5 stuff so it works with Ruby 1.9. Bumped gem patch version.

This commit is contained in:
Jim Menard 2009-01-21 11:52:43 -05:00
parent a913823960
commit 3e33811425
3 changed files with 30 additions and 38 deletions

View File

@ -15,7 +15,7 @@
# ++ # ++
require 'socket' require 'socket'
require 'md5' require 'digest/md5'
require 'mutex_m' require 'mutex_m'
require 'mongo/collection' require 'mongo/collection'
require 'mongo/message' require 'mongo/message'
@ -144,10 +144,16 @@ module XGen
auth['authenticate'] = 1 auth['authenticate'] = 1
auth['user'] = username auth['user'] = username
auth['nonce'] = nonce auth['nonce'] = nonce
auth['key'] = MD5.md5("#{nonce}#{username}#{hash_password(password)}").to_s auth['key'] = Digest::MD5.hexdigest("#{nonce}#{username}#{hash_password(password)}")
ok?(db_command(auth)) ok?(db_command(auth))
end end
# Deauthorizes use for this database for this connection.
def logout
doc = db_command(:logout => 1)
raise "error logging out: #{doc.inspect}" unless ok?(doc)
end
# Returns an array of collection names. Each name is of the form # Returns an array of collection names. Each name is of the form
# "database_name.collection_name". # "database_name.collection_name".
def collection_names def collection_names
@ -425,7 +431,7 @@ module XGen
private private
def hash_password(plaintext) def hash_password(plaintext)
MD5.new("mongo#{plaintext}").to_s Digest::MD5.hexdigest("mongo#{plaintext}")
end end
end end

View File

@ -1,6 +1,6 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = 'mongo' s.name = 'mongo'
s.version = '0.2.0' s.version = '0.2.1'
s.platform = Gem::Platform::RUBY s.platform = Gem::Platform::RUBY
s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB' s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB'
s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.' s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'

View File

@ -1,5 +1,5 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'md5' require 'digest/md5'
require 'mongo' require 'mongo'
require 'test/unit' require 'test/unit'
@ -19,12 +19,17 @@ class DBTest < Test::Unit::TestCase
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' @host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT @port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
@db = Mongo.new(@host, @port).db('ruby-mongo-test') @db = Mongo.new(@host, @port).db('ruby-mongo-test')
@spongebob = 'spongebob' @spongebob = 'spongebob'
@spongebob_password = 'squarepants' @spongebob_password = 'squarepants'
@users = @db.collection('system.users')
@users.clear
@db.add_user(@spongebob, @spongebob_password)
end end
def teardown def teardown
if @db.connected? if @db.connected?
@users.clear if @users
@db.close @db.close
end end
end end
@ -54,6 +59,7 @@ class DBTest < Test::Unit::TestCase
def test_array def test_array
@db.close @db.close
@users = nil
@db = Mongo.new([["nosuch.example.com"], [@host, @port]]).db('ruby-mongo-test') @db = Mongo.new([["nosuch.example.com"], [@host, @port]]).db('ruby-mongo-test')
assert @db.connected? assert @db.connected?
end end
@ -86,45 +92,25 @@ class DBTest < Test::Unit::TestCase
end end
def test_add_user def test_add_user
coll = @db.collection('system.users') assert_equal 1, @users.count
coll.clear doc = @users.find({}, :limit => 1).next_object
begin
assert_equal 0, coll.count
@db.add_user(@spongebob, @spongebob_password)
assert_equal 1, coll.count
doc = coll.find({}, :limit => 1).next_object
assert_equal @spongebob, doc['user'] assert_equal @spongebob, doc['user']
assert_equal MD5.new("mongo#{@spongebob_password}").to_s, doc['pwd'] assert_equal Digest::MD5.hexdigest("mongo#{@spongebob_password}"), doc['pwd']
ensure
coll.clear
end
end end
def test_delete_user def test_delete_user
coll = @db.collection('system.users')
coll.clear
begin
assert_equal 0, coll.count
@db.add_user(@spongebob, @spongebob_password)
assert_equal 1, coll.count
@db.delete_user(@spongebob) @db.delete_user(@spongebob)
assert_equal 0, coll.count assert_equal 0, @users.count
ensure
coll.clear
end
end end
def test_authenticate def test_authenticate
coll = @db.collection('system.users')
coll.clear
begin
@db.add_user(@spongebob, @spongebob_password)
assert !@db.authenticate('nobody', 'nopassword') assert !@db.authenticate('nobody', 'nopassword')
assert !@db.authenticate(@spongebob, 'squareliederhosen') assert !@db.authenticate(@spongebob, 'squareliederhosen')
assert @db.authenticate(@spongebob, @spongebob_password) assert @db.authenticate(@spongebob, @spongebob_password)
ensure
coll.clear
end end
def test_logout
@db.logout # only testing that we don't throw exception
end end
end end