Skip to content

Instantly share code, notes, and snippets.

@mehd-io
Last active May 24, 2022 18:32
Show Gist options
  • Save mehd-io/3df915bfe6f9d5138551fe2ce0460fe6 to your computer and use it in GitHub Desktop.
Save mehd-io/3df915bfe6f9d5138551fe2ce0460fe6 to your computer and use it in GitHub Desktop.
medium-tftest-api-conftest
from pathlib import Path
import pytest
import tftest
from google.auth.transport.requests import AuthorizedSession
from .cloud_run_client import (
get_auth_session,
get_service_account_file_path,
get_token_credentials_from_service_account,
)
TF_VAR_CLOUD_RUN_URL = "cloud_run_api_url"
@pytest.fixture(name="auth_session", scope="session")
def deploy_api() -> AuthorizedSession:
""" Deploy the API through terraform code and return the Cloud Run URL and auth session
"""
file_path = Path(__file__).resolve()
base_dir = file_path.parent.parent.parent.absolute()
tf = tftest.TerraformTest(tfdir="terraform", basedir=base_dir)
tf.setup()
tf.apply(output=True, tf_non_interactive=True)
cloud_run_api_url = tf.output().__getitem__(TF_VAR_CLOUD_RUN_URL)
token_creds = get_token_credentials_from_service_account(
target_audience=cloud_run_api_url,
service_account_file=get_service_account_file_path(),
)
yield get_auth_session(token_creds)
tf.destroy(auto_approve=True, tf_non_interactive=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment