""" This example shows how you can create your own fixtures using the utility functions in `jwtauth.testing.utils.py`. In this example, we create fixtures with `module` scope and a custom secrets manager class. """ from typing import Any import boto3 import pytest import requests from jwtauth.testing import ( SecretLookupInfo, get_bearer_token_identity_uuid, login_from_secrets_manager, ) # Include this line in the `root` test module or `conftest.py` # to discover the jwtauth plugin. pytest_plugins = ["jwtauth.testing.pytest_plugin"] # Create your own secrets manager class. class MySecretsManager: def get_secret(self, secret_name: str) -> Any: """ Feel free to implement your own secretsmanager, if `JwtAuthSecretsManager` doesn't work for you. """ client = boto3.session.Session().client("secretsmanager") get_secret_value_response = client.get_secret_value(SecretId=secret_name) return get_secret_value_response.get("SecretString", "") # Set to `module` scope... for fun. @pytest.fixture(scope="module") def my_secrets_manager() -> MySecretsManager: """Secrets Manager fixture with `module` scope.""" return MySecretsManager() # Create another `module` scope fixture that uses `my_secrets_manager`. @pytest.fixture(scope="module") def pdp_test_bearer_token(my_secrets_manager: MySecretsManager) -> str: """Example that uses the plugin fixtures.""" return login_from_secrets_manager( get_user_creds_args=SecretLookupInfo( environment="qa", service_name="pdp-integration-test", secret_name="PDP_TEST_USER_CREDENTIALS", ), get_auth0_creds_args=SecretLookupInfo( environment="qa", service_name="pdp-integration-test", secret_name="PDP_TEST_APP_AUTH0_CREDENTIALS", ), secrets_manager=my_secrets_manager, ) def test_generate_bearer_token(pdp_test_bearer_token: str) -> None: """Verify that the generated JWT has an `orchardIdentityId`.""" identity_uuid = get_bearer_token_identity_uuid( bearer_token=pdp_test_bearer_token, environment="qa", ) assert identity_uuid == "4d5f24f5-83f9-4989-9f82-0924a5feaf88" def test_responses(pdp_test_bearer_token: str) -> None: """Send example requests to qa-ows-pdp with and without a valid auth header.""" authorized_response = requests.get( "https://qa-ows-pdp.theorchard.io/", headers={"Authorization": f"Bearer {pdp_test_bearer_token}"}, ) assert authorized_response.status_code == 200 authorized_response = requests.get( "https://qa-ows-pdp.theorchard.io/", headers={"Authorization": "Bearer TOKEN"}, ) assert authorized_response.status_code == 401