""" This module contains pytest fixtures for setting up the test environment. """ import pytest import requests import time import os # Assuming GiteaAPIClient is in tests/lib/common_test_utils.py from tests.lib.common_test_utils import GiteaAPIClient @pytest.fixture(scope="session") def gitea_env(): """ Sets up the Gitea environment with dummy data and provides a GiteaAPIClient instance. """ gitea_url = "http://127.0.0.1:3000" # Read admin token admin_token_path = "./gitea-data/admin.token" # Corrected path admin_token = None try: with open(admin_token_path, "r") as f: admin_token = f.read().strip() except FileNotFoundError: raise Exception(f"Admin token file not found at {admin_token_path}. Ensure it's generated and accessible.") # Headers for authenticated requests auth_headers = {"Authorization": f"token {admin_token}", "Content-Type": "application/json"} # Wait for Gitea to be available print(f"Waiting for Gitea at {gitea_url}...") max_retries = 30 for i in range(max_retries): try: # Check a specific API endpoint that indicates readiness response = requests.get(f"{gitea_url}/api/v1/version", headers=auth_headers, timeout=5) if response.status_code == 200: print("Gitea API is available.") break except requests.exceptions.ConnectionError: pass print(f"Gitea not ready ({response.status_code if 'response' in locals() else 'ConnectionError'}), retrying in 5 seconds... ({i+1}/{max_retries})") time.sleep(5) else: raise Exception("Gitea did not become available within the expected time.") client = GiteaAPIClient(base_url=gitea_url, token=admin_token) # Setup dummy data print("--- Starting Gitea Dummy Data Setup from Pytest Fixture ---") client.create_org("products") client.create_org("pool") client.create_repo("products", "SLFO") client.create_repo("pool", "pkgA") client.create_repo("pool", "pkgB") # The add_submodules method also creates workflow.config and staging.config client.add_submodules("products", "SLFO") client.add_collaborator("products", "SLFO", "autogits_obs_staging_bot", "write") client.add_collaborator("products", "SLFO", "workflow-pr", "write") client.add_collaborator("pool", "pkgA", "workflow-pr", "write") client.add_collaborator("pool", "pkgB", "workflow-pr", "write") client.update_repo_settings("products", "SLFO") client.update_repo_settings("pool", "pkgA") client.update_repo_settings("pool", "pkgB") print("--- Gitea Dummy Data Setup Complete ---") time.sleep(5) # Add a small delay for Gitea to fully process changes yield client # Teardown (optional, depending on test strategy) # For now, we'll leave resources for inspection. If a clean slate is needed for each test, # this fixture's scope would be 'function' and teardown logic would be added here.