about summary refs log tree commit diff
path: root/conftest.py
diff options
context:
space:
mode:
Diffstat (limited to 'conftest.py')
-rw-r--r--conftest.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/conftest.py b/conftest.py
new file mode 100644
index 0000000..5b5eb87
--- /dev/null
+++ b/conftest.py
@@ -0,0 +1,89 @@
+"""
+Shared fixtures for the GeneNetwork integration test suite.
+
+Base URLs default to the CD environment.  Override via environment variables:
+
+    GN2_BASE_URL     e.g. https://genenetwork.org          (default: CD)
+    GN3_BASE_URL     e.g. https://genenetwork.org/api3     (default: CD)
+    GN_AUTH_BASE_URL e.g. https://auth.genenetwork.org     (default: CD)
+
+For auth-flow tests also set:
+
+    GN_TEST_EMAIL    registered test-user e-mail address
+    GN_TEST_PASSWORD password for GN_TEST_EMAIL
+    GN_OAUTH2_CLIENT_ID     OAuth2 client UUID
+    GN_OAUTH2_CLIENT_SECRET OAuth2 client secret
+"""
+
+import os
+import pytest
+import requests
+
+
+_GN2_DEFAULT = "https://cd.genenetwork.org"
+_GN3_DEFAULT = "https://cd.genenetwork.org/api3"
+_GN_AUTH_DEFAULT = "https://auth-cd.genenetwork.org"
+
+
+@pytest.fixture(scope="session")
+def gn2_url() -> str:
+    return os.environ.get("GN2_BASE_URL", _GN2_DEFAULT).rstrip("/")
+
+
+@pytest.fixture(scope="session")
+def gn3_url() -> str:
+    return os.environ.get("GN3_BASE_URL", _GN3_DEFAULT).rstrip("/")
+
+
+@pytest.fixture(scope="session")
+def gn_auth_url() -> str:
+    return os.environ.get("GN_AUTH_BASE_URL", _GN_AUTH_DEFAULT).rstrip("/")
+
+
+@pytest.fixture(scope="session")
+def http() -> requests.Session:
+    """Shared requests.Session; sets a conservative timeout for all calls."""
+    with requests.Session() as session:
+        session.headers.update({"Accept": "application/json"})
+        yield session
+
+
+# ---------------------------------------------------------------------------
+# Auth-flow helpers (Phase 2 tests)
+# ---------------------------------------------------------------------------
+
+@pytest.fixture(scope="session")
+def oauth2_credentials():
+    """Returns (email, password, client_id, client_secret) or skips the test."""
+    email = os.environ.get("GN_TEST_EMAIL")
+    password = os.environ.get("GN_TEST_PASSWORD")
+    client_id = os.environ.get("GN_OAUTH2_CLIENT_ID")
+    client_secret = os.environ.get("GN_OAUTH2_CLIENT_SECRET")
+    if not all([email, password, client_id, client_secret]):
+        pytest.skip(
+            "Set GN_TEST_EMAIL, GN_TEST_PASSWORD, GN_OAUTH2_CLIENT_ID, and "
+            "GN_OAUTH2_CLIENT_SECRET to run auth-flow tests."
+        )
+    return email, password, client_id, client_secret
+
+
+@pytest.fixture(scope="session")
+def access_token(gn_auth_url, oauth2_credentials, http):
+    """Obtains a Bearer token via the password grant and caches it for the session."""
+    email, password, client_id, client_secret = oauth2_credentials
+    resp = http.post(
+        f"{gn_auth_url}/auth/token",
+        json={
+            "grant_type": "password",
+            "username": email,
+            "password": password,
+            "scope": "profile group resource",
+            "client_id": client_id,
+            "client_secret": client_secret,
+        },
+        timeout=30,
+    )
+    assert resp.status_code == 200, f"Token request failed: {resp.text}"
+    data = resp.json()
+    assert "access_token" in data
+    return data["access_token"]