@@ -282,28 +282,46 @@ def test_from_uri(self):
282282 c .pymongo_test .system .users .remove ({})
283283
284284 c .admin .add_user ("admin" , "pass" )
285- c .pymongo_test .add_user ("user" , "pass" )
286-
287- self .assertRaises (ConfigurationError , Connection ,
288- "mongodb://foo:bar@%s:%s" % (self .host , self .port ))
289- self .assertRaises (ConfigurationError , Connection ,
290- "mongodb://admin:bar@%s:%s" % (self .host , self .port ))
291- self .assertRaises (ConfigurationError , Connection ,
292- "mongodb://user:pass@%s:%s" % (self .host , self .port ))
293- Connection ("mongodb://admin:pass@%s:%s" % (self .host , self .port ))
294-
295- self .assertRaises (ConfigurationError , Connection ,
296- "mongodb://admin:pass@%s:%s/pymongo_test" %
297- (self .host , self .port ))
298- self .assertRaises (ConfigurationError , Connection ,
299- "mongodb://user:foo@%s:%s/pymongo_test" %
300- (self .host , self .port ))
301- Connection ("mongodb://user:pass@%s:%s/pymongo_test" %
302- (self .host , self .port ))
303-
304- self .assert_ (Connection ("mongodb://%s:%s" %
305- (self .host , self .port ),
306- slave_okay = True ).slave_okay )
285+ try :
286+ # Not yet logged in
287+ try :
288+ c .admin .system .users .find_one ()
289+ # If we get this far auth must not be enabled in server
290+ raise SkipTest ()
291+ except OperationFailure :
292+ pass
293+
294+ # Now we log in
295+ c .admin .authenticate ("admin" , "pass" )
296+
297+ c .pymongo_test .add_user ("user" , "pass" )
298+
299+ self .assertRaises (ConfigurationError , Connection ,
300+ "mongodb://foo:bar@%s:%s" % (self .host , self .port ))
301+ self .assertRaises (ConfigurationError , Connection ,
302+ "mongodb://admin:bar@%s:%s" % (self .host , self .port ))
303+ self .assertRaises (ConfigurationError , Connection ,
304+ "mongodb://user:pass@%s:%s" % (self .host , self .port ))
305+ Connection ("mongodb://admin:pass@%s:%s" % (self .host , self .port ))
306+
307+ self .assertRaises (ConfigurationError , Connection ,
308+ "mongodb://admin:pass@%s:%s/pymongo_test" %
309+ (self .host , self .port ))
310+ self .assertRaises (ConfigurationError , Connection ,
311+ "mongodb://user:foo@%s:%s/pymongo_test" %
312+ (self .host , self .port ))
313+ Connection ("mongodb://user:pass@%s:%s/pymongo_test" %
314+ (self .host , self .port ))
315+
316+ self .assert_ (Connection ("mongodb://%s:%s" %
317+ (self .host , self .port ),
318+ slave_okay = True ).slave_okay )
319+ finally :
320+ # Remove auth users from databases
321+ c = Connection (self .host , self .port )
322+ c .admin .authenticate ("admin" , "pass" )
323+ c .admin .system .users .remove ({})
324+ c .pymongo_test .system .users .remove ({})
307325
308326 def test_fork (self ):
309327 """Test using a connection before and after a fork.
@@ -432,6 +450,93 @@ def test_tz_aware(self):
432450 self .assertEqual (aware .pymongo_test .test .find_one ()["x" ].replace (tzinfo = None ),
433451 naive .pymongo_test .test .find_one ()["x" ])
434452
453+ def test_auto_db_authentication (self ):
454+ conn = Connection (self .host , self .port )
455+
456+ # Setup admin user
457+ conn .admin .system .users .remove ({})
458+ conn .admin .add_user ("admin-user" , "password" )
459+ conn .admin .authenticate ("admin-user" , "password" )
460+
461+ try : # try/finally to ensure we remove admin user
462+ # Setup test database user
463+ conn .pymongo_test .system .users .remove ({})
464+ conn .pymongo_test .add_user ("test-user" , "password" )
465+
466+ conn .pymongo_test .drop_collection ("test" )
467+
468+ self .assertRaises (TypeError , conn .add_db_auth , "" , "password" )
469+ self .assertRaises (TypeError , conn .add_db_auth , 5 , "password" )
470+ self .assertRaises (TypeError , conn .add_db_auth , "test-user" , "" )
471+ self .assertRaises (TypeError , conn .add_db_auth , "test-user" , 5 )
472+
473+ # Not yet logged in
474+ conn = Connection (self .host , self .port )
475+ try :
476+ conn .admin .system .users .find_one ()
477+ # If we get this far auth must not be enabled in server
478+ raise SkipTest ()
479+ except OperationFailure :
480+ pass
481+
482+ # Not yet logged in
483+ conn = Connection (self .host , self .port )
484+ self .assertRaises (OperationFailure , conn .pymongo_test .test .count )
485+ self .assertFalse (conn .has_db_auth ('admin' ))
486+ self .assertEquals (None , conn .get_db_auth ('admin' ))
487+
488+ # Admin log in via URI
489+ conn = Connection ('admin-user:password@%s' % self .host , self .port )
490+ self .assertTrue (conn .has_db_auth ('admin' ))
491+ self .assertEquals ('admin-user' , conn .get_db_auth ('admin' )[0 ])
492+ conn .admin .system .users .find ()
493+ conn .pymongo_test .test .insert ({'_id' :1 , 'test' :'data' }, safe = True )
494+ self .assertEquals (1 , conn .pymongo_test .test .find ({'_id' :1 }).count ())
495+ conn .pymongo_test .test .remove ({'_id' :1 })
496+
497+ # Clear and reset database authentication for all sockets
498+ conn .clear_db_auths ()
499+ self .assertFalse (conn .has_db_auth ('admin' ))
500+ self .assertRaises (OperationFailure , conn .pymongo_test .test .count )
501+
502+ # Admin log in via add_db_auth
503+ conn = Connection (self .host , self .port )
504+ conn .admin .system .users .find ()
505+ conn .add_db_auth ('admin' , 'admin-user' , 'password' )
506+ conn .pymongo_test .test .insert ({'_id' :2 , 'test' :'data' }, safe = True )
507+ self .assertEquals (1 , conn .pymongo_test .test .find ({'_id' :2 }).count ())
508+ conn .pymongo_test .test .remove ({'_id' :2 })
509+
510+ # Remove database authentication for specific database
511+ self .assertTrue (conn .has_db_auth ('admin' ))
512+ conn .remove_db_auth ('admin' )
513+ self .assertFalse (conn .has_db_auth ('admin' ))
514+ self .assertRaises (OperationFailure , conn .pymongo_test .test .count )
515+
516+ # Incorrect admin credentials
517+ conn = Connection (self .host , self .port )
518+ conn .add_db_auth ('admin' , 'admin-user' , 'wrong-password' )
519+ self .assertRaises (OperationFailure , conn .pymongo_test .test .count )
520+
521+ # Database-specific log in
522+ conn = Connection (self .host , self .port )
523+ conn .add_db_auth ('pymongo_test' , 'test-user' , 'password' )
524+ self .assertRaises (OperationFailure , conn .admin .system .users .find_one )
525+ conn .pymongo_test .test .insert ({'_id' :3 , 'test' :'data' }, safe = True )
526+ self .assertEquals (1 , conn .pymongo_test .test .find ({'_id' :3 }).count ())
527+ conn .pymongo_test .test .remove ({'_id' :3 })
528+
529+ # Incorrect database credentials
530+ conn = Connection (self .host , self .port )
531+ conn .add_db_auth ('pymongo_test' , 'wrong-user' , 'password' )
532+ self .assertRaises (OperationFailure , conn .pymongo_test .test .find_one )
533+ finally :
534+ # Remove auth users from databases
535+ conn = Connection (self .host , self .port )
536+ conn .admin .authenticate ("admin-user" , "password" )
537+ conn .admin .system .users .remove ({})
538+ conn .pymongo_test .system .users .remove ({})
539+
435540
436541if __name__ == "__main__" :
437542 unittest .main ()
0 commit comments