Skip to content

Commit fd5a641

Browse files
committed
Adds support for OIDC, when available
1 parent cc30a99 commit fd5a641

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

tests/conftest.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,79 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23+
import os
24+
import logging
25+
import json
2326
import pytest
2427

28+
import oauthlib.oauth2
29+
import requests_oauthlib
30+
2531
from mrmat_python_api_flask import create_app, db
2632

33+
LOGGER = logging.getLogger(__name__)
34+
35+
36+
@pytest.fixture
37+
def test_config():
38+
if 'FLASK_CONFIG' not in os.environ:
39+
LOGGER.info('Missing test configuration via FLASK_CONFIG environment variable. Tests are limited')
40+
return None
41+
with open(os.path.expanduser(os.environ['FLASK_CONFIG'])) as C:
42+
return json.load(C)
43+
2744

2845
@pytest.fixture
2946
def client():
30-
# Note how we override the configuration here, especially how we set an ephemeral in-memory database per test
47+
"""Start and configure the WSGI app.
48+
49+
Configuration honours the FLASK_CONFIG environment variable but will set reasonable defaults if not present. This
50+
particularly overrides the configuration of an in-memory database rather than the normal persisted database in the
51+
instance directory.
52+
53+
Yields:
54+
A Flask client used for testing
55+
"""
3156
app = create_app({'TESTING': True, 'SQLALCHEMY_DATABASE_URI': 'sqlite://'})
3257
with app.app_context():
3358
db.create_all()
3459
with app.test_client() as client:
3560
yield client
3661

3762

63+
@pytest.fixture
64+
def oidc_token(test_config):
65+
"""Obtain an OIDC token to be used for client testing.
66+
67+
A token can only be obtained if the configuration file pointed to by the FLASK_CONFIG environment variable
68+
contains required entries to set up OIDC for testing. An empty dict is returned if these are not present.
69+
The following are required:
70+
71+
{
72+
"web": { This entry is required to be the very first entry. If you don't like that,
73+
then externalize it into a separate file and point to it via OIDC_CLIENT_SECRETS
74+
"client_id": Server side client_id
75+
"client_secret": Server-side client_secret
76+
...
77+
},
78+
"client": {
79+
"client_id": Test client client_id
80+
"client_secret": Test client secret
81+
"preferred_name": Asserted preferred_name of the client_id
82+
"OIDC_CLIENT_SECRETS": Point this to the same place as FLASK_CONFIG (to reduce the number of config files
83+
84+
Yields:
85+
A dictionary containing the access token or None if configuration is lacking
86+
"""
87+
if test_config is None or 'client' not in test_config:
88+
LOGGER.info('Missing OIDC test client configuration. Tests will be limited')
89+
return None
90+
for key in ['client_id', 'client_secret', 'preferred_name']:
91+
if key not in test_config['client']:
92+
LOGGER.info(f'Missing {key} in test client configuration. Tests will be limited')
93+
return None
94+
client = oauthlib.oauth2.BackendApplicationClient(client_id=test_config['client']['client_id'])
95+
oauth = requests_oauthlib.OAuth2Session(client=client)
96+
return oauth.fetch_token(token_url=test_config['web']['token_uri'],
97+
client_id=test_config['client']['client_id'],
98+
client_secret=test_config['client']['client_secret'])

0 commit comments

Comments
 (0)