Skip to content

Commit 36d58bc

Browse files
author
A. Jesse Jiryu Davis
committed
Raise OperationFailure, not AutoReconnect, if auth fails during lazy connection. PYTHON-517
1 parent 5e94a92 commit 36d58bc

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pymongo/mongo_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,9 @@ def __find_node(self, seeds=None):
711711
raise ConfigurationError("Seed list cannot contain a mix "
712712
"of mongod and mongos instances.")
713713
return node
714+
except OperationFailure:
715+
# The server is available but something failed, probably auth.
716+
raise
714717
except Exception, why:
715718
errors.append(str(why))
716719

test/test_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,22 @@ def test_auth_from_uri(self):
346346
c.admin.system.users.remove({})
347347
c.pymongo_test.system.users.remove({})
348348

349+
def test_lazy_auth_raises_operation_failure(self):
350+
# Check if we have the prerequisites to run this test.
351+
c = MongoClient(host, port)
352+
if not server_started_with_auth(c):
353+
raise SkipTest('Authentication is not enabled on server')
354+
355+
if is_mongos(c) and not version.at_least(c, (2, 0, 0)):
356+
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
357+
358+
lazy_client = MongoClient(
359+
"mongodb://user:wrong@%s:%d/pymongo_test" % (host, port),
360+
_connect=False)
361+
362+
assertRaisesExactly(
363+
OperationFailure, lazy_client.test.collection.find_one)
364+
349365
def test_unix_socket(self):
350366
if not hasattr(socket, "AF_UNIX"):
351367
raise SkipTest("UNIX-sockets are not supported on this system")

0 commit comments

Comments
 (0)