Skip to content

Commit 0d6b4f5

Browse files
committed
Ignore unknown config var starting by MONGODB_ instead of crashing
1 parent 6da4eae commit 0d6b4f5

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

flask_mongoengine/connection.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import mongoengine
22
from pymongo import ReadPreference, uri_parser
33

4+
45
__all__ = (
56
'create_connections', 'get_connection_settings', 'InvalidSettingsError',
67
)
78

89

10+
MONGODB_CONF_VARS = ('MONGODB_ALIAS', 'MONGODB_DB', 'MONGODB_HOST', 'MONGODB_IS_MOCK',
11+
'MONGODB_PASSWORD', 'MONGODB_PORT', 'MONGODB_USERNAME')
12+
13+
914
class InvalidSettingsError(Exception):
1015
pass
1116

@@ -82,10 +87,10 @@ def get_connection_settings(config):
8287
else:
8388
return _sanitize_settings(settings)
8489

85-
# If "MONGODB_SETTINGS" doesn't exist, sanitize all the keys starting with
86-
# "MONGODB_" as if they all describe a single connection.
90+
# If "MONGODB_SETTINGS" doesn't exist, sanitize the "MONGODB_" keys
91+
# as if they all describe a single connection.
8792
else:
88-
config = dict((k, v) for k, v in config.items() if k.startswith('MONGODB_')) # ugly dict comprehention in order to support python 2.6
93+
config = dict((k, v) for k, v in config.items() if k in MONGODB_CONF_VARS) # ugly dict comprehention in order to support python 2.6
8994
return _sanitize_settings(config)
9095

9196

tests/test_connection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ def test_connection_with_invalid_uri(self):
156156
self.app.config['MONGODB_HOST'] = 'mongo://localhost'
157157
self.assertRaises(InvalidURI, MongoEngine, self.app)
158158

159+
def test_ingnored_mongodb_prefix_config(self):
160+
"""Config starting by MONGODB_ but not used by flask-mongoengine
161+
should be ignored.
162+
"""
163+
db = MongoEngine()
164+
self.app.config['MONGODB_HOST'] = 'mongodb://localhost:27017/flask_mongoengine_test_db_prod'
165+
# Invalid host, should trigger exception if used
166+
self.app.config['MONGODB_TEST_HOST'] = 'dummy://localhost:27017/test'
167+
self._do_persist(db)
168+
159169
def test_connection_kwargs(self):
160170
"""Make sure additional connection kwargs work."""
161171

0 commit comments

Comments
 (0)