Skip to content

Commit 8daadd2

Browse files
committed
test(auth): HTTPS環境でのログアウトCookie削除属性を検証 (Issue #122)
- FRONTEND_URLがHTTPSの場合、Set-Cookieヘッダーに samesite=none と secure が含まれることをテスト - Cookie削除時は設定時と同じ属性が必要(ブラウザ仕様) - 本番環境で確実にCookie削除されることを担保
1 parent 19c20c5 commit 8daadd2

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

backend/tests/test_api/test_auth.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,51 @@ def test_logout_without_cookie_still_200(client):
338338
assert res.status_code == 200
339339

340340

341+
def test_logout_cookie_samesite_secure_for_https(client, monkeypatch):
342+
"""HTTPS環境でログアウト時、Set-Cookieにsamesite=none; secureが設定される(Issue #122)。
343+
344+
背景: Cookie削除は設定時と同じ属性(SameSite/Secure)が必須。
345+
本番環境(FRONTEND_URL=https://...)で確実に削除されることを担保する。
346+
"""
347+
# FRONTEND_URLをHTTPSに設定
348+
monkeypatch.setattr("app.core.config.settings.FRONTEND_URL", "https://example.com")
349+
350+
# まずCookieをセット(認証済みにする)
351+
state = _valid_state()
352+
client.cookies.set("oauth_state", state)
353+
mock_client = _mock_httpx_client()
354+
with patch("app.api.endpoints.auth.httpx.AsyncClient", return_value=mock_client):
355+
client.get(f"/api/v1/auth/github/callback?code=fake_code&state={state}")
356+
357+
# ログアウト
358+
res = client.post("/api/v1/auth/logout")
359+
assert res.status_code == 200
360+
361+
# Set-Cookieヘッダーを取得(access_token のみ削除される)
362+
set_cookie_headers = res.headers.get_list("set-cookie")
363+
assert len(set_cookie_headers) >= 1, "access_token の Cookie削除が必要"
364+
365+
# access_tokenのSet-Cookieヘッダーを特定
366+
access_token_header = next(
367+
(h for h in set_cookie_headers if "access_token" in h), None
368+
)
369+
assert (
370+
access_token_header is not None
371+
), "access_token の Cookie削除ヘッダーが見つからない"
372+
373+
# HTTPS環境では samesite=none と secure が設定されることを確認
374+
access_token_header_lower = access_token_header.lower()
375+
assert (
376+
"samesite=none" in access_token_header_lower
377+
), "HTTPS環境では samesite=none が必要(Cookie削除時も設定時と同じ属性が必須)"
378+
assert (
379+
"secure" in access_token_header_lower
380+
), "HTTPS環境では secure が必要(Cookie削除時も設定時と同じ属性が必須)"
381+
382+
# Cookieが削除されていることも確認(max-age=0)
383+
assert "max-age=0" in access_token_header_lower, "Cookie削除には max-age=0 が必要"
384+
385+
341386
# ---------------------------------------------------------------------------
342387
# GET /auth/github/callback - ネットワークエラー系 (T-1, T-2)
343388
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)