|
20 | 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21 | 21 | # SOFTWARE. |
22 | 22 |
|
| 23 | +import os |
| 24 | +import logging |
| 25 | +import json |
23 | 26 | import pytest |
24 | 27 |
|
| 28 | +import oauthlib.oauth2 |
| 29 | +import requests_oauthlib |
| 30 | + |
25 | 31 | from mrmat_python_api_flask import create_app, db |
26 | 32 |
|
| 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 | + |
27 | 44 |
|
28 | 45 | @pytest.fixture |
29 | 46 | 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 | + """ |
31 | 56 | app = create_app({'TESTING': True, 'SQLALCHEMY_DATABASE_URI': 'sqlite://'}) |
32 | 57 | with app.app_context(): |
33 | 58 | db.create_all() |
34 | 59 | with app.test_client() as client: |
35 | 60 | yield client |
36 | 61 |
|
37 | 62 |
|
| 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