Skip to content

Commit e6396ca

Browse files
committed
Merge pull request #218 from bkabrda/master
Raise ValueError if Mongo host URI is given and it doesn't contain DB name
2 parents 069081d + aa072b9 commit e6396ca

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

flask_mongoengine/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def _create_connection(conn_settings):
9292
elif "://" in conn.get('host', ''):
9393
uri_dict = uri_parser.parse_uri(conn['host'])
9494
conn['db'] = uri_dict['database']
95+
if conn['db'] is None:
96+
raise ValueError('Mongo host URI must contain database name')
9597

9698
return mongoengine.connect(conn.pop('db', 'test'), **conn)
9799

tests/test_connection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,11 @@ def test_parse_uri_if_testing_not_true(self):
3232

3333
self.assertRaises(InvalidURI, MongoEngine, self.app)
3434

35+
def test_parse_uri_without_database_name(self):
36+
self.app.config['TESTING'] = False
37+
self.app.config['MONGODB_HOST'] = 'mongodb://localhost'
38+
39+
self.assertRaises(ValueError, MongoEngine, self.app)
40+
3541
if __name__ == '__main__':
3642
unittest.main()

0 commit comments

Comments
 (0)