Compare commits
1 Commits
no-conflic
...
simpler-in
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea82cb80f6 |
@@ -25,8 +25,7 @@ _dbs = {}
|
|||||||
|
|
||||||
def register_connection(alias, name=None, host=None, port=None,
|
def register_connection(alias, name=None, host=None, port=None,
|
||||||
read_preference=READ_PREFERENCE,
|
read_preference=READ_PREFERENCE,
|
||||||
username=None, password=None,
|
username=None, password=None, authentication_source=None,
|
||||||
authentication_source=None,
|
|
||||||
authentication_mechanism=None,
|
authentication_mechanism=None,
|
||||||
**kwargs):
|
**kwargs):
|
||||||
"""Add a connection.
|
"""Add a connection.
|
||||||
@@ -71,26 +70,20 @@ def register_connection(alias, name=None, host=None, port=None,
|
|||||||
|
|
||||||
resolved_hosts = []
|
resolved_hosts = []
|
||||||
for entity in conn_host:
|
for entity in conn_host:
|
||||||
|
# Handle uri style connections
|
||||||
# Handle Mongomock
|
|
||||||
if entity.startswith('mongomock://'):
|
if entity.startswith('mongomock://'):
|
||||||
conn_settings['is_mock'] = True
|
conn_settings['is_mock'] = True
|
||||||
# `mongomock://` is not a valid url prefix and must be replaced by `mongodb://`
|
# `mongomock://` is not a valid url prefix and must be replaced by `mongodb://`
|
||||||
resolved_hosts.append(entity.replace('mongomock://', 'mongodb://', 1))
|
resolved_hosts.append(entity.replace('mongomock://', 'mongodb://', 1))
|
||||||
|
|
||||||
# Handle URI style connections, only updating connection params which
|
|
||||||
# were explicitly specified in the URI.
|
|
||||||
elif '://' in entity:
|
elif '://' in entity:
|
||||||
uri_dict = uri_parser.parse_uri(entity)
|
uri_dict = uri_parser.parse_uri(entity)
|
||||||
resolved_hosts.append(entity)
|
resolved_hosts.append(entity)
|
||||||
|
conn_settings.update({
|
||||||
if uri_dict.get('database'):
|
'name': uri_dict.get('database') or name,
|
||||||
conn_settings['name'] = uri_dict.get('database')
|
'username': uri_dict.get('username'),
|
||||||
|
'password': uri_dict.get('password'),
|
||||||
for param in ('read_preference', 'username', 'password'):
|
'read_preference': read_preference,
|
||||||
if uri_dict.get(param):
|
})
|
||||||
conn_settings[param] = uri_dict[param]
|
|
||||||
|
|
||||||
uri_options = uri_dict['options']
|
uri_options = uri_dict['options']
|
||||||
if 'replicaset' in uri_options:
|
if 'replicaset' in uri_options:
|
||||||
conn_settings['replicaSet'] = True
|
conn_settings['replicaSet'] = True
|
||||||
|
|||||||
@@ -2,10 +2,8 @@
|
|||||||
import unittest
|
import unittest
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
sys.path[0:0] = [""]
|
|
||||||
|
|
||||||
import pymongo
|
import pymongo
|
||||||
from random import randint
|
|
||||||
|
|
||||||
from nose.plugins.skip import SkipTest
|
from nose.plugins.skip import SkipTest
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
@@ -17,11 +15,9 @@ __all__ = ("IndexesTest", )
|
|||||||
|
|
||||||
|
|
||||||
class IndexesTest(unittest.TestCase):
|
class IndexesTest(unittest.TestCase):
|
||||||
_MAX_RAND = 10 ** 10
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.db_name = 'mongoenginetest_IndexesTest_' + str(randint(0, self._MAX_RAND))
|
self.connection = connect(db='mongoenginetest')
|
||||||
self.connection = connect(db=self.db_name)
|
|
||||||
self.db = get_db()
|
self.db = get_db()
|
||||||
|
|
||||||
class Person(Document):
|
class Person(Document):
|
||||||
|
|||||||
@@ -174,9 +174,19 @@ class ConnectionTest(unittest.TestCase):
|
|||||||
c.mongoenginetest.system.users.remove({})
|
c.mongoenginetest.system.users.remove({})
|
||||||
|
|
||||||
def test_connect_uri_without_db(self):
|
def test_connect_uri_without_db(self):
|
||||||
"""Ensure connect() method works properly if the URI doesn't
|
"""Ensure connect() method works properly with uri's without database_name
|
||||||
include a database name.
|
|
||||||
"""
|
"""
|
||||||
|
c = connect(db='mongoenginetest', alias='admin')
|
||||||
|
c.admin.system.users.remove({})
|
||||||
|
c.mongoenginetest.system.users.remove({})
|
||||||
|
|
||||||
|
c.admin.add_user("admin", "password")
|
||||||
|
c.admin.authenticate("admin", "password")
|
||||||
|
c.mongoenginetest.add_user("username", "password")
|
||||||
|
|
||||||
|
if not IS_PYMONGO_3:
|
||||||
|
self.assertRaises(ConnectionError, connect, "testdb_uri_bad", host='mongodb://test:password@localhost')
|
||||||
|
|
||||||
connect("mongoenginetest", host='mongodb://localhost/')
|
connect("mongoenginetest", host='mongodb://localhost/')
|
||||||
|
|
||||||
conn = get_connection()
|
conn = get_connection()
|
||||||
@@ -186,31 +196,8 @@ class ConnectionTest(unittest.TestCase):
|
|||||||
self.assertTrue(isinstance(db, pymongo.database.Database))
|
self.assertTrue(isinstance(db, pymongo.database.Database))
|
||||||
self.assertEqual(db.name, 'mongoenginetest')
|
self.assertEqual(db.name, 'mongoenginetest')
|
||||||
|
|
||||||
def test_connect_uri_default_db(self):
|
c.admin.system.users.remove({})
|
||||||
"""Ensure connect() defaults to the right database name if
|
c.mongoenginetest.system.users.remove({})
|
||||||
the URI and the database_name don't explicitly specify it.
|
|
||||||
"""
|
|
||||||
connect(host='mongodb://localhost/')
|
|
||||||
|
|
||||||
conn = get_connection()
|
|
||||||
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
|
|
||||||
|
|
||||||
db = get_db()
|
|
||||||
self.assertTrue(isinstance(db, pymongo.database.Database))
|
|
||||||
self.assertEqual(db.name, 'test')
|
|
||||||
|
|
||||||
def test_uri_without_credentials_doesnt_override_conn_settings(self):
|
|
||||||
"""Ensure connect() uses the username & password params if the URI
|
|
||||||
doesn't explicitly specify them.
|
|
||||||
"""
|
|
||||||
c = connect(host='mongodb://localhost/mongoenginetest',
|
|
||||||
username='user',
|
|
||||||
password='pass')
|
|
||||||
|
|
||||||
# OperationFailure means that mongoengine attempted authentication
|
|
||||||
# w/ the provided username/password and failed - that's the desired
|
|
||||||
# behavior. If the MongoDB URI would override the credentials
|
|
||||||
self.assertRaises(OperationFailure, get_db)
|
|
||||||
|
|
||||||
def test_connect_uri_with_authsource(self):
|
def test_connect_uri_with_authsource(self):
|
||||||
"""Ensure that the connect() method works well with
|
"""Ensure that the connect() method works well with
|
||||||
|
|||||||
Reference in New Issue
Block a user