Skip to content

Commit 28b3c36

Browse files
authored
tests: remove real MongoDB dependency (#422)
OIDC integration tests now use mongomock instead of launching a full mongodb server, which may not be available in a development environment.
1 parent c629dd5 commit 28b3c36

File tree

4 files changed

+22
-97
lines changed

4 files changed

+22
-97
lines changed

tests/conftest.py

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -361,88 +361,3 @@ def consent_module_config(signing_key_path):
361361
}
362362
}
363363
return consent_config
364-
365-
366-
import atexit
367-
import random
368-
import shutil
369-
import subprocess
370-
import tempfile
371-
import time
372-
373-
import pymongo
374-
import pytest
375-
376-
377-
class MongoTemporaryInstance(object):
378-
"""Singleton to manage a temporary MongoDB instance
379-
380-
Use this for testing purpose only. The instance is automatically destroyed
381-
at the end of the program.
382-
383-
"""
384-
_instance = None
385-
386-
@classmethod
387-
def get_instance(cls):
388-
if cls._instance is None:
389-
cls._instance = cls()
390-
atexit.register(cls._instance.shutdown)
391-
return cls._instance
392-
393-
def __init__(self):
394-
self._tmpdir = tempfile.mkdtemp()
395-
self._port = 27017
396-
self._process = subprocess.Popen(['mongod', '--bind_ip', 'localhost',
397-
'--port', str(self._port),
398-
'--dbpath', self._tmpdir,
399-
'--nojournal',
400-
'--noauth',
401-
'--syncdelay', '0'],
402-
stdout=open('/tmp/mongo-temp.log', 'wb'),
403-
stderr=subprocess.STDOUT)
404-
405-
# XXX: wait for the instance to be ready
406-
# Mongo is ready in a glance, we just wait to be able to open a
407-
# Connection.
408-
for i in range(10):
409-
time.sleep(0.2)
410-
try:
411-
self._conn = pymongo.MongoClient('localhost', self._port)
412-
except pymongo.errors.ConnectionFailure:
413-
continue
414-
else:
415-
break
416-
else:
417-
self.shutdown()
418-
assert False, 'Cannot connect to the mongodb test instance'
419-
420-
@property
421-
def conn(self):
422-
return self._conn
423-
424-
@property
425-
def port(self):
426-
return self._port
427-
428-
def shutdown(self):
429-
if self._process:
430-
self._process.terminate()
431-
self._process.wait()
432-
self._process = None
433-
shutil.rmtree(self._tmpdir, ignore_errors=True)
434-
435-
def get_uri(self):
436-
"""
437-
Convenience function to get a mongodb URI to the temporary database.
438-
439-
:return: URI
440-
"""
441-
return 'mongodb://localhost:{port!s}'.format(port=self.port)
442-
443-
444-
@pytest.fixture
445-
def mongodb_instance():
446-
tmp_db = MongoTemporaryInstance()
447-
yield tmp_db
448-
tmp_db.shutdown()

tests/flows/test_oidc-saml.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import base64
44
from urllib.parse import urlparse, urlencode, parse_qsl
55

6+
import mongomock
67
import pytest
78
from jwkest.jwk import rsa_load, RSAKey
89
from jwkest.jws import JWS
910
from oic.oic.message import ClaimsRequest, Claims
10-
from pyop.storage import MongoWrapper
11+
from pyop.storage import StorageBase
1112
from saml2 import BINDING_HTTP_REDIRECT
1213
from saml2.config import IdPConfig
1314
from werkzeug.test import Client
@@ -25,6 +26,7 @@
2526
CLIENT_SECRET = "secret"
2627
CLIENT_REDIRECT_URI = "https://client.example.com/cb"
2728
REDIRECT_URI = "https://client.example.com/cb"
29+
DB_URI = "mongodb://localhost/satosa"
2830

2931
@pytest.fixture(scope="session")
3032
def client_db_path(tmpdir_factory):
@@ -45,26 +47,19 @@ def client_db_path(tmpdir_factory):
4547
return path
4648

4749
@pytest.fixture
48-
def oidc_frontend_config(signing_key_path, mongodb_instance):
50+
def oidc_frontend_config(signing_key_path):
4951
data = {
5052
"module": "satosa.frontends.openid_connect.OpenIDConnectFrontend",
5153
"name": "OIDCFrontend",
5254
"config": {
5355
"issuer": "https://proxy-op.example.com",
5456
"signing_key_path": signing_key_path,
5557
"provider": {"response_types_supported": ["id_token"]},
56-
"client_db_uri": mongodb_instance.get_uri(), # use mongodb for integration testing
57-
"db_uri": mongodb_instance.get_uri() # use mongodb for integration testing
58+
"client_db_uri": DB_URI, # use mongodb for integration testing
59+
"db_uri": DB_URI # use mongodb for integration testing
5860
}
5961
}
6062

61-
# insert client in mongodb
62-
cdb = MongoWrapper(mongodb_instance.get_uri(), "satosa", "clients")
63-
cdb[CLIENT_ID] = {
64-
"redirect_uris": [REDIRECT_URI],
65-
"response_types": ["id_token"]
66-
}
67-
6863
return data
6964

7065

@@ -87,8 +82,20 @@ def oidc_stateless_frontend_config(signing_key_path, client_db_path):
8782
return data
8883

8984

85+
@mongomock.patch(servers=(('localhost', 27017),))
9086
class TestOIDCToSAML:
87+
def _client_setup(self):
88+
"""Insert client in mongodb."""
89+
self._cdb = StorageBase.from_uri(
90+
DB_URI, db_name="satosa", collection="clients", ttl=None
91+
)
92+
self._cdb[CLIENT_ID] = {
93+
"redirect_uris": [REDIRECT_URI],
94+
"response_types": ["id_token"]
95+
}
96+
9197
def test_full_flow(self, satosa_config_dict, oidc_frontend_config, saml_backend_config, idp_conf):
98+
self._client_setup()
9299
subject_id = "testuser1"
93100

94101
# proxy config

tests/satosa/scripts/test_satosa_saml_metadata.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import glob
22
import os
33

4+
import mongomock
45
import pytest
56
from saml2.config import Config
67
from saml2.mdstore import MetaDataFile
@@ -10,7 +11,7 @@
1011

1112

1213
@pytest.fixture
13-
def oidc_frontend_config(signing_key_path, mongodb_instance):
14+
def oidc_frontend_config(signing_key_path):
1415
data = {
1516
"module": "satosa.frontends.openid_connect.OpenIDConnectFrontend",
1617
"name": "OIDCFrontend",
@@ -23,6 +24,7 @@ def oidc_frontend_config(signing_key_path, mongodb_instance):
2324
return data
2425

2526

27+
@mongomock.patch(servers=(('localhost', 27017),))
2628
class TestConstructSAMLMetadata:
2729
def test_saml_saml(self, tmpdir, cert_and_key, satosa_config_dict, saml_frontend_config,
2830
saml_backend_config):

tests/test_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pytest
22
responses
33
beautifulsoup4
44
ldap3
5+
mongomock

0 commit comments

Comments
 (0)