1313import os
1414import time
1515import typing as t
16+ from urllib .parse import quote
1617
1718import pymongo .errors
1819from pymongo import MongoClient
1920from testcontainers .core .exceptions import ContainerStartException
2021from testcontainers .mongodb import MongoDbContainer
22+ from yarl import URL
2123
2224from cratedb_toolkit .testing .testcontainers .util import DockerSkippingContainer , KeepaliveContainer
2325
@@ -26,7 +28,7 @@ class MongoDbContainerWithKeepalive(DockerSkippingContainer, KeepaliveContainer,
2628 """
2729 A Testcontainer for MongoDB with improved configurability.
2830
29- It honors the `TC_KEEPALIVE` and `MONGODB_VERSION` environment variables.
31+ It honours the `TC_KEEPALIVE` and `MONGODB_VERSION` environment variables.
3032
3133 Defining `TC_KEEPALIVE` will set a signal not to shut down the container
3234 after running the test cases, in order to speed up subsequent invocations.
@@ -87,6 +89,11 @@ def _create_connection_url(
8789 dbname : t .Optional [str ] = None ,
8890 ** kwargs ,
8991 ) -> str :
92+ """
93+ TODO: Why does this method need to be overwritten?
94+ Hint: Probably because of getting rid of credentials forwarding,
95+ which is currently intended to reduce configuration complexity.
96+ """
9097 from testcontainers .core .utils import raise_for_deprecated_parameter
9198
9299 if raise_for_deprecated_parameter (kwargs , "db_name" , "dbname" ):
@@ -96,16 +103,20 @@ def _create_connection_url(
96103 host = host or self .get_container_host_ip ()
97104 assert port is not None # noqa: S101
98105 port = self .get_exposed_port (port )
99- url = f"{ dialect } ://{ host } :{ port } "
106+ url = URL (f"{ dialect } ://{ host } :{ port } " )
107+ if username :
108+ url = url .with_user (username )
109+ if password :
110+ url = url .with_password (quote (password , safe = " +" ))
100111 if dbname :
101- url = f" { url } / { dbname } "
102- return url
112+ url = url . with_path ( dbname )
113+ return str ( url )
103114
104115 def get_connection_url (self ) -> str :
105116 return self ._create_connection_url (
106117 dialect = "mongodb" ,
107- username = "admin" ,
108- password = "" ,
118+ username = None , # ty: ignore[invalid-argument-type]
119+ password = None , # ty: ignore[invalid-argument-type]
109120 port = self .port ,
110121 )
111122
0 commit comments