Skip to content

Commit 4d8520f

Browse files
fsonntagFelix Sonntagtillahoffmann
authored
Fixed MongoDbContainer to actually use username and password (#103)
* Fixed MongoDbContainer to actually use username and password * Fixed line width * Added test for MongoDbContainer to connect without authentication * Fixed line length too long Co-authored-by: Felix Sonntag <[email protected]> Co-authored-by: Till Hoffmann <[email protected]>
1 parent 266aa27 commit 4d8520f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

testcontainers/mongodb.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,28 @@ class MongoDbContainer(DockerContainer):
4949
MONGO_INITDB_ROOT_PASSWORD = os.environ.get("MONGO_INITDB_ROOT_PASSWORD", "test")
5050
MONGO_DB = os.environ.get("MONGO_DB", "test")
5151

52-
def __init__(self, image="mongodb:latest"):
52+
def __init__(self, image="mongo:latest"):
5353
super(MongoDbContainer, self).__init__(image=image)
5454
self.command = "mongo"
5555
self.port_to_expose = 27017
5656
self.with_exposed_ports(self.port_to_expose)
5757

58-
def _configure(self):
58+
def start(self):
59+
self._configure()
60+
super().start()
61+
return self
5962

63+
def _configure(self):
6064
self.with_env("MONGO_INITDB_ROOT_USERNAME", self.MONGO_INITDB_ROOT_USERNAME)
6165
self.with_env("MONGO_INITDB_ROOT_PASSWORD", self.MONGO_INITDB_ROOT_PASSWORD)
6266
self.with_env("MONGO_DB", self.MONGO_DB)
6367

6468
def get_connection_url(self):
6569
port = self.get_exposed_port(self.port_to_expose)
66-
return "mongodb://{}:{}".format(self.get_container_host_ip(), port)
70+
return "mongodb://{}:{}@{}:{}".format(self.MONGO_INITDB_ROOT_USERNAME,
71+
self.MONGO_INITDB_ROOT_PASSWORD,
72+
self.get_container_host_ip(), port)
6773

6874
def get_connection_client(self):
6975
from pymongo import MongoClient
70-
return MongoClient("mongodb://{}:{}".format(self.get_container_host_ip(),
71-
self.get_exposed_port(self.port_to_expose)))
76+
return MongoClient(self.get_connection_url())

tests/test_db_containers.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import pytest
22
import sqlalchemy
33
from pymongo import MongoClient
4+
from pymongo.errors import OperationFailure
45

56
from testcontainers.core.generic import GenericContainer
67
from testcontainers.core.waiting_utils import wait_for
8+
from testcontainers.mongodb import MongoDbContainer
79
from testcontainers.mssql import SqlServerContainer
810
from testcontainers.mysql import MySqlContainer, MariaDbContainer
911
from testcontainers.oracle import OracleDbContainer
1012
from testcontainers.postgres import PostgresContainer
11-
from testcontainers.mongodb import MongoDbContainer
1213

1314

1415
def test_docker_run_mysql():
@@ -73,6 +74,16 @@ def test_docker_run_mongodb():
7374
assert cursor.next()['restaurant_id'] == doc['restaurant_id']
7475

7576

77+
def test_docker_run_mongodb_connect_without_credentials():
78+
mongo_container = MongoDbContainer()
79+
with mongo_container as mongo:
80+
connection_url = "mongodb://{}:{}".format(mongo.get_container_host_ip(),
81+
mongo.get_exposed_port(mongo.port_to_expose))
82+
db = MongoClient(connection_url).test
83+
with pytest.raises(OperationFailure):
84+
db.restaurants.insert_one({})
85+
86+
7687
def test_docker_generic_db():
7788
mongo_container = GenericContainer("mongo:latest")
7889
mongo_container.with_bind_ports(27017, 27017)

0 commit comments

Comments
 (0)