Skip to content

Commit 7decdd8

Browse files
author
Luke Lovett
committed
PYTHON-751 Move all auth tests to the test_auth module.
1 parent 33d0c70 commit 7decdd8

25 files changed

+951
-807
lines changed

test/__init__.py

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import warnings
2020

2121
import pymongo
22-
from pymongo.errors import ConnectionFailure
22+
from nose.plugins.skip import SkipTest
23+
from pymongo.errors import OperationFailure
2324

2425
# hostnames retrieved by MongoReplicaSetClient from isMaster will be of unicode
2526
# type in Python 2, so ensure these hostnames are unicodes, too. It makes tests
@@ -34,6 +35,66 @@
3435
host3 = unicode(os.environ.get("DB_IP3", 'localhost'))
3536
port3 = int(os.environ.get("DB_PORT3", 27019))
3637

38+
db_user = unicode(os.environ.get("DB_USER", "administrator"))
39+
db_pwd = unicode(os.environ.get("DB_PWD", "password"))
40+
41+
42+
class AuthContext(object):
43+
44+
def __init__(self):
45+
self.client = pymongo.MongoClient(host, port)
46+
self.auth_enabled = False
47+
self.restricted_localhost = False
48+
try:
49+
command_line = self.client.admin.command('getCmdLineOpts')
50+
if self._server_started_with_auth(command_line):
51+
self.auth_enabled = True
52+
except OperationFailure, e:
53+
if e.code == 13:
54+
self.auth_enabled = True
55+
self.restricted_localhost = True
56+
else:
57+
raise
58+
59+
def _server_started_with_auth(self, command_line):
60+
# MongoDB >= 2.0
61+
if 'parsed' in command_line:
62+
parsed = command_line['parsed']
63+
# MongoDB >= 2.6
64+
if 'security' in parsed:
65+
security = parsed['security']
66+
if 'authorization' in security:
67+
return security['authorization'] == 'enabled'
68+
return security.get('auth', bool(security.get('keyFile')))
69+
return parsed.get('auth', bool(parsed.get('keyFile')))
70+
# Legacy
71+
argv = command_line['argv']
72+
return '--auth' in argv or '--keyFile' in argv
73+
74+
def add_user_and_log_in(self):
75+
self.client.admin.add_user(db_user, db_pwd,
76+
roles=('userAdminAnyDatabase',
77+
'readWriteAnyDatabase',
78+
'dbAdminAnyDatabase',
79+
'clusterAdmin'))
80+
self.client.admin.authenticate(db_user, db_pwd)
81+
82+
def remove_user_and_log_out(self):
83+
self.client.admin.remove_user(db_user)
84+
self.client.admin.logout()
85+
self.client.disconnect()
86+
87+
88+
auth_context = AuthContext()
89+
90+
91+
def skip_restricted_localhost():
92+
"""Skip tests when the localhost exception is restricted (SERVER-12621)."""
93+
if auth_context.restricted_localhost:
94+
raise SkipTest("Cannot test with restricted localhost exception "
95+
"(SERVER-12621).")
96+
97+
3798
# Make sure warnings are always raised, regardless of
3899
# python version.
39100
def setup():
@@ -42,16 +103,16 @@ def setup():
42103

43104

44105
def teardown():
45-
try:
46-
c = pymongo.MongoClient(host, port)
47-
except ConnectionFailure:
48-
# Tests where ssl=True can cause connection failures here.
49-
# Ignore and continue.
50-
return
51-
52-
c.drop_database("pymongo-pooling-tests")
53-
c.drop_database("pymongo_test")
54-
c.drop_database("pymongo_test1")
55-
c.drop_database("pymongo_test2")
56-
c.drop_database("pymongo_test_mike")
57-
c.drop_database("pymongo_test_bernie")
106+
client = auth_context.client
107+
if auth_context.auth_enabled:
108+
auth_context.add_user_and_log_in()
109+
110+
client.drop_database("pymongo-pooling-tests")
111+
client.drop_database("pymongo_test")
112+
client.drop_database("pymongo_test1")
113+
client.drop_database("pymongo_test2")
114+
client.drop_database("pymongo_test_mike")
115+
client.drop_database("pymongo_test_bernie")
116+
117+
if auth_context.auth_enabled:
118+
auth_context.remove_user_and_log_out()

0 commit comments

Comments
 (0)