Skip to content

Commit

Permalink
Merge pull request #138 from IlyaSkriblovsky/15.0.0-uri-authmechanism
Browse files Browse the repository at this point in the history
?authMechanism URI option support backported from 15.1.0 to 15.0.0
  • Loading branch information
psi29a committed Nov 13, 2015
2 parents fbb5a0e + 9ffe3af commit fc83eb3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
11 changes: 11 additions & 0 deletions docs/source/NEWS.rst
@@ -1,6 +1,17 @@
Changelog
=========

Release 15.0.1 (2015-11-13)
---------------------------

This is a maintenance update to outdated 15.0.0 release

Features
^^^^^^^^

- Support for "?authMechanism=..." connection URI option


Release 15.0 (2015-05-04)
-------------------------

Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Expand Up @@ -11,4 +11,5 @@ diff-cover
wheel
setuptools
tox
check-manifest
check-manifest
mock
28 changes: 28 additions & 0 deletions tests/test_auth.py
Expand Up @@ -16,6 +16,7 @@
import os
import shutil
import txmongo
from mock import patch
from bson.son import SON
from pymongo.errors import OperationFailure
from twisted.trial import unittest
Expand Down Expand Up @@ -224,3 +225,30 @@ def test_AuthOnTwoDBsSequential(self):
yield conn[self.db2][self.coll].find_one()
finally:
yield conn.disconnect()


class TestAuthMechanismURIParameter(unittest.TestCase):
mongo_host = "localhost"
mongo_port = 27017

@defer.inlineCallbacks
def _test_uri_auth(self, url_suffix, expected_mechanism):
# Connecting to default no-auth MongoDB, so it will be ok with any
# auth mechanism
with patch("txmongo.connection.ConnectionPool.authenticate") as authfunc:
uri = "mongodb://user:pass@{0}/db{2}".format(self.mongo_host,
self.mongo_port,
url_suffix)
conn = txmongo.connection.ConnectionPool(uri)
authfunc.assert_called_once_with("db", "user", "pass", expected_mechanism)
# Issuing some query to make sure connection is established because
# calling disconnect() may leave Trial's reactor unclean if it
# happen before name resolution is done
yield conn.db.coll.find_one()
yield conn.disconnect()

@defer.inlineCallbacks
def test_AuthMechanismURIParameter(self):
yield self._test_uri_auth("", "DEFAULT")
yield self._test_uri_auth("?authMechanism=MONGODB-CR", "MONGODB-CR")
yield self._test_uri_auth("?authMechanism=SCRAM-SHA-1", "SCRAM-SHA-1")
1 change: 1 addition & 0 deletions tox.ini
Expand Up @@ -7,6 +7,7 @@ envlist =
[testenv]
deps =
coverage
mock
pyopenssl
pyparsing
pycrypto
Expand Down
3 changes: 2 additions & 1 deletion txmongo/connection.py
Expand Up @@ -238,7 +238,8 @@ def __init__(self, uri="mongodb://127.0.0.1:27017", pool_size=1, ssl_context_fac

if self.__uri['database'] and self.__uri['username'] and self.__uri['password']:
self.authenticate(self.__uri['database'], self.__uri['username'],
self.__uri['password'])
self.__uri['password'],
self.__uri['options'].get('authmechanism', 'DEFAULT'))

host, port = self.__uri['nodelist'][0]

Expand Down

0 comments on commit fc83eb3

Please sign in to comment.