-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a833364
commit bf9d082
Showing
11 changed files
with
57 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,14 @@ | ||
import typing | ||
import urllib.parse | ||
|
||
import pymongo | ||
|
||
|
||
def create_client( | ||
host: str, | ||
port: str, | ||
uri: str, | ||
username: typing.Optional[str] = None, | ||
password: typing.Optional[str] = None, | ||
srv: bool = False, | ||
tls: bool = False, | ||
tlsCAFile: typing.Optional[str] = None, | ||
) -> pymongo.MongoClient: | ||
protocol = "mongodb" | ||
if srv: | ||
protocol = "mongodb+srv" | ||
|
||
endpoint = f"{protocol}://{host}:{port}" | ||
|
||
if username and password: | ||
encoded_username = urllib.parse.quote_plus(username) | ||
encoded_password = urllib.parse.quote_plus(password) | ||
endpoint = f"{protocol}://{encoded_username}:{encoded_password}@{host}:{port}" | ||
|
||
return pymongo.MongoClient(endpoint, journal=True, connect=False, tls=tls, tlsCAFile=tlsCAFile) | ||
return pymongo.MongoClient(host=uri, journal=True, connect=False, username=username, password=password) | ||
else: | ||
return pymongo.MongoClient(host=uri, journal=True, connect=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,44 @@ | ||
import typing | ||
from unittest import mock | ||
|
||
from polytope_server.common import mongo_client_factory | ||
|
||
|
||
@mock.patch("polytope_server.common.mongo_client_factory.pymongo.MongoClient", autospec=True) | ||
def test_create_without_credentials(mock_mongo: mock.Mock): | ||
mongo_client_factory.create_client("host", "123", username=None, password=None, tls=False) | ||
mongo_client_factory.create_client("mongodb://host:123") | ||
|
||
_verify(mock_mongo, "host:123", False, False) | ||
_verify(mock_mongo, "mongodb://host:123", None, None) | ||
|
||
|
||
@mock.patch("polytope_server.common.mongo_client_factory.pymongo.MongoClient", autospec=True) | ||
def test_create_with_srv(mock_mongo: mock.Mock): | ||
mongo_client_factory.create_client("host", "123", username=None, password=None, srv=True, tls=False) | ||
def test_create_without_password_credentials(mock_mongo: mock.Mock): | ||
mongo_client_factory.create_client("mongodb+srv://host:123", username="admin") | ||
|
||
_verify(mock_mongo, "host:123", True, False) | ||
_verify(mock_mongo, "mongodb+srv://host:123", None, None) | ||
|
||
|
||
@mock.patch("polytope_server.common.mongo_client_factory.pymongo.MongoClient", autospec=True) | ||
def test_create_with_credentials(mock_mongo: mock.Mock): | ||
mongo_client_factory.create_client("host", "123", username="admin", password="admin", tls=False) | ||
|
||
_verify(mock_mongo, "admin:admin@host:123", False, False) | ||
|
||
def test_create_without_username_credentials(mock_mongo: mock.Mock): | ||
mongo_client_factory.create_client("host:123", password="password") | ||
|
||
@mock.patch("polytope_server.common.mongo_client_factory.pymongo.MongoClient", autospec=True) | ||
def test_create_without_credentials_tls(mock_mongo: mock.Mock): | ||
mongo_client_factory.create_client("host", "123", username=None, password=None, tls=True) | ||
|
||
_verify(mock_mongo, "host:123", False, True) | ||
_verify(mock_mongo, "host:123", None, None) | ||
|
||
|
||
@mock.patch("polytope_server.common.mongo_client_factory.pymongo.MongoClient", autospec=True) | ||
def test_create_with_credentials_tls(mock_mongo: mock.Mock): | ||
mongo_client_factory.create_client("host", "123", username="admin", password="admin", tls=True) | ||
|
||
_verify(mock_mongo, "admin:admin@host:123", False, True) | ||
|
||
|
||
@mock.patch("polytope_server.common.mongo_client_factory.pymongo.MongoClient", autospec=True) | ||
def test_create_with_tlsCAfile(mock_mongo: mock.Mock): | ||
mongo_client_factory.create_client( | ||
"host", "123", username="admin", password="admin", tls=True, tlsCAFile="/test/ca.pem" | ||
) | ||
def test_create_with_credentials(mock_mongo: mock.Mock): | ||
mongo_client_factory.create_client("mongodb+srv://host", username="admin", password="est123123") | ||
|
||
_verify(mock_mongo, "admin:admin@host:123", False, True, "/test/ca.pem") | ||
_verify(mock_mongo, "mongodb+srv://host", "admin", "est123123") | ||
|
||
|
||
def _verify(mock_mongo: mock.Mock, endpoint: str, srv: bool, tls: bool, tlsCAFile=None): | ||
def _verify( | ||
mock_mongo: mock.Mock, endpoint: str, username: typing.Optional[str] = None, password: typing.Optional[str] = None | ||
): | ||
mock_mongo.assert_called_once() | ||
args, kwargs = mock_mongo.call_args | ||
if srv: | ||
assert args[0] == f"mongodb+srv://{endpoint}" | ||
else: | ||
assert args[0] == f"mongodb://{endpoint}" | ||
|
||
assert kwargs["tls"] == tls | ||
assert kwargs["tlsCAFile"] == tlsCAFile | ||
assert args[0] == endpoint | ||
if username: | ||
assert kwargs["username"] == username | ||
if password: | ||
assert kwargs["password"] == password |