File size: 5,264 Bytes
7c6ffa6 6515ef9 7c6ffa6 6515ef9 7c6ffa6 6515ef9 7c6ffa6 6515ef9 7c6ffa6 6515ef9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | from __future__ import annotations
import os
from urllib.parse import parse_qs, urlparse
def _set_google_env() -> None:
os.environ["GOOGLE_CLIENT_ID"] = "test-google-client"
os.environ["GOOGLE_CLIENT_SECRET"] = "test-google-secret"
os.environ["FRONTEND_BASE_URL"] = "http://127.0.0.1:3000"
os.environ["GOOGLE_OAUTH_REDIRECT_URI"] = "http://testserver/auth/google/callback"
from app.core.config import get_settings
get_settings.cache_clear()
def _clear_google_env() -> None:
for key in (
"GOOGLE_CLIENT_ID",
"GOOGLE_CLIENT_SECRET",
"FRONTEND_BASE_URL",
"GOOGLE_OAUTH_REDIRECT_URI",
):
os.environ.pop(key, None)
from app.core.config import get_settings
get_settings.cache_clear()
def _fragment_params(location: str) -> dict[str, str]:
fragment = urlparse(location).fragment
parsed = parse_qs(fragment)
return {key: values[0] for key, values in parsed.items()}
def test_google_start_redirects_to_frontend_error_without_oauth_credentials(auth_client):
os.environ["GOOGLE_CLIENT_ID"] = ""
os.environ["GOOGLE_CLIENT_SECRET"] = ""
from app.core.config import get_settings
get_settings.cache_clear()
try:
response = auth_client.get("/auth/google/start", follow_redirects=False)
finally:
_clear_google_env()
assert response.status_code == 302
params = _fragment_params(response.headers["location"])
assert "not ready" in params["error"].lower()
def test_google_callback_creates_user_and_returns_frontend_session(auth_client, monkeypatch):
_set_google_env()
from app.routes import auth as auth_routes
monkeypatch.setattr(
auth_routes,
"_post_google_token",
lambda payload: {"access_token": "google-access-token"},
)
monkeypatch.setattr(
auth_routes,
"_fetch_google_profile",
lambda access_token: {
"email": "google.student@example.com",
"email_verified": True,
"name": "Google Student",
},
)
try:
nonce = "test-google-state-nonce"
state = auth_routes._create_google_state(
next_path="/dashboard", invite_code=None, nonce=nonce
)
response = auth_client.get(
f"/auth/google/callback?code=test-code&state={state}",
cookies={auth_routes.GOOGLE_STATE_COOKIE: nonce},
follow_redirects=False,
)
assert response.status_code == 302
location = response.headers["location"]
assert location.startswith("http://127.0.0.1:3000/auth/google/callback#")
params = _fragment_params(location)
assert params["access_token"]
assert params["token_type"] == "bearer"
assert params["next"] == "/dashboard"
assert "google.student@example.com" in params["user"]
finally:
_clear_google_env()
def test_google_callback_respects_beta_invite_gate(auth_client, monkeypatch):
_set_google_env()
os.environ["BETA_ACCESS_ENABLED"] = "true"
os.environ["BETA_INVITE_CODE"] = "DOCDOE-BETA-2026"
from app.core.config import get_settings
from app.routes import auth as auth_routes
get_settings.cache_clear()
monkeypatch.setattr(
auth_routes,
"_post_google_token",
lambda payload: {"access_token": "google-access-token"},
)
monkeypatch.setattr(
auth_routes,
"_fetch_google_profile",
lambda access_token: {
"email": "invite-needed@example.com",
"email_verified": True,
"name": "Invite Needed",
},
)
try:
nonce = "test-google-state-nonce"
state = auth_routes._create_google_state(
next_path="/onboarding", invite_code=None, nonce=nonce
)
response = auth_client.get(
f"/auth/google/callback?code=test-code&state={state}",
cookies={auth_routes.GOOGLE_STATE_COOKIE: nonce},
follow_redirects=False,
)
assert response.status_code == 302
params = _fragment_params(response.headers["location"])
assert "invite" in params["error"].lower()
assert params["next"] == "/onboarding"
finally:
os.environ["BETA_ACCESS_ENABLED"] = "false"
os.environ.pop("BETA_INVITE_CODE", None)
_clear_google_env()
def test_google_callback_rejects_state_from_another_browser(auth_client, monkeypatch):
_set_google_env()
from app.routes import auth as auth_routes
monkeypatch.setattr(
auth_routes,
"_post_google_token",
lambda payload: (_ for _ in ()).throw(AssertionError("token exchange must not run")),
)
try:
state = auth_routes._create_google_state(
next_path="/dashboard", invite_code=None, nonce="browser-a"
)
response = auth_client.get(
f"/auth/google/callback?code=test-code&state={state}",
cookies={auth_routes.GOOGLE_STATE_COOKIE: "browser-b"},
follow_redirects=False,
)
assert response.status_code == 302
params = _fragment_params(response.headers["location"])
assert "expired" in params["error"].lower()
finally:
_clear_google_env()
|