|
37 | 37 | from pymongo.errors import (ConfigurationError, |
38 | 38 | ConnectionFailure, |
39 | 39 | InvalidName, |
40 | | - OperationFailure) |
| 40 | + OperationFailure, |
| 41 | + PyMongoError) |
41 | 42 | from test import version, host, port |
42 | 43 | from test.utils import (assertRaisesExactly, |
43 | 44 | delay, |
@@ -76,6 +77,35 @@ def test_constants(self): |
76 | 77 | MongoClient.PORT = port |
77 | 78 | self.assertTrue(MongoClient()) |
78 | 79 |
|
| 80 | + def test_init_disconnected(self): |
| 81 | + c = MongoClient(host, port, _connect=False) |
| 82 | + |
| 83 | + # No errors |
| 84 | + c.is_primary |
| 85 | + c.is_mongos |
| 86 | + c.max_pool_size |
| 87 | + c.use_greenlets |
| 88 | + c.nodes |
| 89 | + c.auto_start_request |
| 90 | + c.get_document_class() |
| 91 | + c.tz_aware |
| 92 | + c.max_bson_size |
| 93 | + self.assertEqual(None, c.host) |
| 94 | + self.assertEqual(None, c.port) |
| 95 | + |
| 96 | + c.pymongo_test.test.find_one() # Auto-connect. |
| 97 | + self.assertEqual(host, c.host) |
| 98 | + self.assertEqual(port, c.port) |
| 99 | + |
| 100 | + bad_host = "somedomainthatdoesntexist.org" |
| 101 | + c = MongoClient(bad_host, port, connectTimeoutMS=1,_connect=False) |
| 102 | + self.assertRaises(ConnectionFailure, c.pymongo_test.test.find_one) |
| 103 | + |
| 104 | + def test_init_disconnected_with_auth(self): |
| 105 | + uri = "mongodb://user:pass@somedomainthatdoesntexist" |
| 106 | + c = MongoClient(uri, connectTimeoutMS=1, _connect=False) |
| 107 | + self.assertRaises(ConnectionFailure, c.pymongo_test.test.find_one) |
| 108 | + |
79 | 109 | def test_connect(self): |
80 | 110 | # Check that the exception is a ConnectionFailure, not a subclass like |
81 | 111 | # AutoReconnect |
@@ -272,29 +302,49 @@ def test_auth_from_uri(self): |
272 | 302 |
|
273 | 303 | c.admin.system.users.remove({}) |
274 | 304 | c.pymongo_test.system.users.remove({}) |
275 | | - c.admin.add_user("admin", "pass") |
276 | | - c.admin.authenticate("admin", "pass") |
277 | | - c.pymongo_test.add_user("user", "pass") |
278 | | - |
279 | | - self.assertRaises(ConfigurationError, MongoClient, |
280 | | - "mongodb://foo:bar@%s:%d" % (host, port)) |
281 | | - self.assertRaises(ConfigurationError, MongoClient, |
282 | | - "mongodb://admin:bar@%s:%d" % (host, port)) |
283 | | - self.assertRaises(ConfigurationError, MongoClient, |
284 | | - "mongodb://user:pass@%s:%d" % (host, port)) |
285 | | - MongoClient("mongodb://admin:pass@%s:%d" % (host, port)) |
286 | | - |
287 | | - self.assertRaises(ConfigurationError, MongoClient, |
288 | | - "mongodb://admin:pass@%s:%d/pymongo_test" % |
289 | | - (host, port)) |
290 | | - self.assertRaises(ConfigurationError, MongoClient, |
291 | | - "mongodb://user:foo@%s:%d/pymongo_test" % |
292 | | - (host, port)) |
293 | | - MongoClient("mongodb://user:pass@%s:%d/pymongo_test" % |
294 | | - (host, port)) |
295 | 305 |
|
296 | | - c.admin.system.users.remove({}) |
297 | | - c.pymongo_test.system.users.remove({}) |
| 306 | + try: |
| 307 | + c.admin.add_user("admin", "pass") |
| 308 | + c.admin.authenticate("admin", "pass") |
| 309 | + c.pymongo_test.add_user("user", "pass") |
| 310 | + |
| 311 | + self.assertRaises(ConfigurationError, MongoClient, |
| 312 | + "mongodb://foo:bar@%s:%d" % (host, port)) |
| 313 | + self.assertRaises(ConfigurationError, MongoClient, |
| 314 | + "mongodb://admin:bar@%s:%d" % (host, port)) |
| 315 | + self.assertRaises(ConfigurationError, MongoClient, |
| 316 | + "mongodb://user:pass@%s:%d" % (host, port)) |
| 317 | + MongoClient("mongodb://admin:pass@%s:%d" % (host, port)) |
| 318 | + |
| 319 | + self.assertRaises(ConfigurationError, MongoClient, |
| 320 | + "mongodb://admin:pass@%s:%d/pymongo_test" % |
| 321 | + (host, port)) |
| 322 | + self.assertRaises(ConfigurationError, MongoClient, |
| 323 | + "mongodb://user:foo@%s:%d/pymongo_test" % |
| 324 | + (host, port)) |
| 325 | + MongoClient("mongodb://user:pass@%s:%d/pymongo_test" % |
| 326 | + (host, port)) |
| 327 | + |
| 328 | + # Auth with lazy connection. |
| 329 | + MongoClient( |
| 330 | + "mongodb://user:pass@%s:%d/pymongo_test" % (host, port), |
| 331 | + _connect=False).pymongo_test.test.find_one() |
| 332 | + |
| 333 | + # Wrong password. |
| 334 | + bad_client = MongoClient( |
| 335 | + "mongodb://user:wrong@%s:%d/pymongo_test" % (host, port), |
| 336 | + _connect=False) |
| 337 | + |
| 338 | + # If auth fails with lazy connection, MongoClient raises |
| 339 | + # AutoReconnect instead of the more appropriate OperationFailure, |
| 340 | + # PYTHON-517. |
| 341 | + self.assertRaises( |
| 342 | + PyMongoError, bad_client.pymongo_test.test.find_one) |
| 343 | + |
| 344 | + finally: |
| 345 | + # Clean up. |
| 346 | + c.admin.system.users.remove({}) |
| 347 | + c.pymongo_test.system.users.remove({}) |
298 | 348 |
|
299 | 349 | def test_unix_socket(self): |
300 | 350 | if not hasattr(socket, "AF_UNIX"): |
|
0 commit comments