diff --git a/pymongo/database.py b/pymongo/database.py index 8aae097f8..f92040759 100644 --- a/pymongo/database.py +++ b/pymongo/database.py @@ -368,7 +368,6 @@ class Database(common.BaseObject): '_uuid_subtype': uuid_subtype } - extra_opts['read_preference'] = kwargs.pop( 'read_preference', self.read_preference) @@ -496,11 +495,10 @@ class Database(common.BaseObject): idle operations in the result """ if include_all: - return self['$cmd.sys.inprog'].find_one({"$all":True}) + return self['$cmd.sys.inprog'].find_one({"$all": True}) else: return self['$cmd.sys.inprog'].find_one() - def profiling_level(self): """Get the database's current profiling level. @@ -602,12 +600,20 @@ class Database(common.BaseObject): .. versionadded:: 1.4 """ - pwd = helpers._password_digest(name, password) - self.system.users.update({"user": name}, - {"user": name, - "pwd": pwd, - "readOnly": read_only}, - upsert=True, safe=True) + + user = self.system.users.find_one({"user": name}) or {"user": name} + user["pwd"] = helpers._password_digest(name, password) + user["readOnly"] = common.validate_boolean('read_only', read_only) + + try: + self.system.users.save(user, safe=True) + except OperationFailure, e: + # First admin user add fails gle in MongoDB >= 2.1.2 + # See SERVER-4225 for more information. + if 'login' in str(e): + pass + else: + raise def remove_user(self, name): """Remove user `name` from this :class:`Database`. diff --git a/pymongo/helpers.py b/pymongo/helpers.py index d4804b3d7..7add9e66e 100644 --- a/pymongo/helpers.py +++ b/pymongo/helpers.py @@ -150,6 +150,8 @@ def _password_digest(username, password): if not isinstance(password, basestring): raise TypeError("password must be an instance " "of %s" % (basestring.__name__,)) + if len(password) == 0: + raise TypeError("password can't be empty") if not isinstance(username, basestring): raise TypeError("username must be an instance " "of %s" % (basestring.__name__,)) diff --git a/test/high_availability/test_ha.py b/test/high_availability/test_ha.py index 762ef8cb5..3d9bc5e92 100644 --- a/test/high_availability/test_ha.py +++ b/test/high_availability/test_ha.py @@ -621,11 +621,7 @@ class TestReplicaSetAuth(unittest.TestCase): use_greenlets=use_greenlets) # Add an admin user to enable auth - try: - self.c.admin.add_user('admin', 'adminpass') - except: - # SERVER-4225 - pass + self.c.admin.add_user('admin', 'adminpass') self.c.admin.authenticate('admin', 'adminpass') self.db = self.c.pymongo_ha_auth diff --git a/test/test_connection.py b/test/test_connection.py index 6ce6eb5fb..151cfe5b4 100644 --- a/test/test_connection.py +++ b/test/test_connection.py @@ -239,13 +239,7 @@ class TestConnection(unittest.TestCase): c.admin.system.users.remove({}) c.pymongo_test.system.users.remove({}) - - try: - # First admin user add fails gle in MongoDB >= 2.1.2 - # See SERVER-4225 for more information. - c.admin.add_user("admin", "pass") - except OperationFailure: - pass + c.admin.add_user("admin", "pass") c.admin.authenticate("admin", "pass") c.pymongo_test.add_user("user", "pass") @@ -505,7 +499,7 @@ with get_connection() as connection: def assertRequestSocket(self, pool): self.assertTrue(isinstance(pool._get_request_state(), SocketInfo)) - + def test_with_start_request(self): conn = get_connection(auto_start_request=False) pool = conn._Connection__pool @@ -544,7 +538,7 @@ with conn.start_request() as request: # Request has ended self.assertNoRequest(pool) self.assertDifferentSock(pool) - + def test_auto_start_request(self): for bad_horrible_value in (None, 5, 'hi!'): self.assertRaises( diff --git a/test/test_database.py b/test/test_database.py index 35223491a..7630a3df6 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -34,6 +34,7 @@ from pymongo import (ALL, from pymongo.collection import Collection from pymongo.database import Database from pymongo.errors import (CollectionInvalid, + ConfigurationError, InvalidName, OperationFailure) from pymongo.son_manipulator import (AutoReference, @@ -289,6 +290,13 @@ class TestDatabase(unittest.TestCase): db = self.connection.pymongo_test db.system.users.remove({}) db.remove_user("mike") + + self.assertRaises(TypeError, db.add_user, "user", None) + self.assertRaises(TypeError, db.add_user, "user", '') + self.assertRaises(TypeError, db.add_user, "user", 'password', None) + self.assertRaises(ConfigurationError, db.add_user, + "user", 'password', 'True') + db.add_user("mike", "password") self.assertRaises(TypeError, db.authenticate, 5, "password") diff --git a/test/test_threads.py b/test/test_threads.py index 950472283..d61d44ec4 100644 --- a/test/test_threads.py +++ b/test/test_threads.py @@ -385,12 +385,7 @@ class BaseTestThreadsAuth(object): raise SkipTest("Authentication is not enabled on server") self.conn = conn self.conn.admin.system.users.remove({}) - try: - # First admin user add fails gle in MongoDB >= 2.1.2 - # See SERVER-4225 for more information. - self.conn.admin.add_user('admin-user', 'password') - except OperationFailure: - pass + self.conn.admin.add_user('admin-user', 'password') self.conn.admin.authenticate("admin-user", "password") self.conn.auth_test.system.users.remove({}) self.conn.auth_test.add_user("test-user", "password")