Skip to content

Commit 2225032

Browse files
committed
Better MONGODB-X509 tests PYTHON-535
1 parent 9307c1d commit 2225032

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

test/test_ssl.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
from pymongo import MongoClient, MongoReplicaSetClient
2828
from pymongo.common import HAS_SSL
29-
from pymongo.errors import ConfigurationError, ConnectionFailure
29+
from pymongo.errors import (ConfigurationError,
30+
ConnectionFailure,
31+
OperationFailure)
3032
from test import host, port, pair, version
3133
from test.utils import get_command_line
3234

@@ -383,11 +385,12 @@ def test_cert_ssl_validation_hostname_fail(self):
383385
def test_mongodb_x509_auth(self):
384386
# Expects the server to be running with the the server.pem, ca.pem
385387
# and crl.pem provided in mongodb and the server tests as well as
386-
# --clusterAuthMode x509 eg:
388+
# --auth
387389
#
388390
# --sslPEMKeyFile=jstests/libs/server.pem
389391
# --sslCAFile=jstests/libs/ca.pem
390392
# --sslCRLFile=jstests/libs/crl.pem
393+
# --auth
391394
if not MONGODB_X509_USERNAME:
392395
raise SkipTest("MONGODB_X509_USERNAME "
393396
"must be set to test MONGODB-X509")
@@ -397,15 +400,28 @@ def test_mongodb_x509_auth(self):
397400
if not version.at_least(client, (2, 5, 1)):
398401
raise SkipTest("MONGODB-X509 requires MongoDB 2.5.1 or newer")
399402
argv = get_command_line(client)
400-
if '--clusterAuthMode' not in argv or 'x509' not in argv:
403+
if '--auth' not in argv:
401404
raise SkipTest("Mongo must be started with "
402-
"'--clusterAuthMode x509' to test MONGODB-X509")
403-
self.assertTrue(client.test.authenticate(MONGODB_X509_USERNAME,
404-
mechanism='MONGODB-X509'))
405+
"--auth to test MONGODB-X509")
406+
# Give admin all necessary priviledges.
407+
client.admin.add_user(MONGODB_X509_USERNAME,
408+
userSource='$external',
409+
roles=['readWriteAnyDatabase',
410+
'userAdminAnyDatabase',
411+
'dbAdminAnyDatabase'])
412+
client = MongoClient(host, port, ssl=True, ssl_certfile=CLIENT_PEM)
413+
coll = client.pymongo_test.test
414+
self.assertRaises(OperationFailure, coll.count)
415+
self.assertTrue(client.admin.authenticate(MONGODB_X509_USERNAME,
416+
mechanism='MONGODB-X509'))
417+
self.assertEqual(0, coll.count())
405418
uri = ('mongodb://%s@%s:%d/?authMechanism='
406419
'MONGODB-X509' % (quote_plus(MONGODB_X509_USERNAME), host, port))
407420
# SSL options aren't supported in the URI...
408421
self.assertTrue(MongoClient(uri, ssl=True, ssl_certfile=CLIENT_PEM))
422+
# Cleanup
423+
client.admin.system.users.remove()
424+
client['$external'].logout()
409425

410426
if __name__ == "__main__":
411427
unittest.main()

0 commit comments

Comments
 (0)