64 lines
2.4 KiB
Python
64 lines
2.4 KiB
Python
"""auth 도메인 e2e 테스트.
|
|
|
|
실행 전제: docker-compose 로 PostgreSQL 이 떠 있어야 한다 (negosium_db 사용).
|
|
docker compose up -d # 또는 로컬 postgres
|
|
cd backend && python -m pytest
|
|
"""
|
|
|
|
|
|
async def test_create_and_login_flow(client):
|
|
# 1) 계정 생성
|
|
r = await client.post("/v1/auth/create", json={"id": "user1", "pw": "pw1234", "nickname": "닉네임"})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["result"]["success"] is True
|
|
assert body["uid"] > 0
|
|
|
|
# 2) 로그인 -> 토큰 발급
|
|
r = await client.post("/v1/auth/login", json={"id": "user1", "pw": "pw1234"})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["result"]["success"] is True
|
|
assert body["access_token"]
|
|
assert body["refresh_token"]
|
|
assert body["nickname"] == "닉네임"
|
|
access_token = body["access_token"]
|
|
|
|
# 3) 보호된 엔드포인트 호출
|
|
r = await client.get("/v1/auth/me", headers={"Authorization": f"Bearer {access_token}"})
|
|
assert r.status_code == 200
|
|
assert r.json()["id"] == "user1"
|
|
|
|
|
|
async def test_login_with_wrong_password(client):
|
|
await client.post("/v1/auth/create", json={"id": "user2", "pw": "correct", "nickname": "n"})
|
|
|
|
r = await client.post("/v1/auth/login", json={"id": "user2", "pw": "wrong"})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["result"]["success"] is False
|
|
# 자격증명 오류는 ACCOUNT_INVALID_INFO(1200)
|
|
assert body["result"]["code"] == 1200
|
|
assert body.get("access_token", "") == "" # 실패 시 토큰은 빈 문자열
|
|
|
|
|
|
async def test_login_nonexistent_account(client):
|
|
r = await client.post("/v1/auth/login", json={"id": "ghost", "pw": "whatever"})
|
|
assert r.json()["result"]["success"] is False
|
|
|
|
|
|
async def test_duplicate_account_create(client):
|
|
r1 = await client.post("/v1/auth/create", json={"id": "dup", "pw": "pw1234", "nickname": "n"})
|
|
assert r1.json()["result"]["success"] is True
|
|
|
|
r2 = await client.post("/v1/auth/create", json={"id": "dup", "pw": "pw5678", "nickname": "n2"})
|
|
body = r2.json()
|
|
assert body["result"]["success"] is False
|
|
# ACCOUNT_ALREADY_EXIST(1201)
|
|
assert body["result"]["code"] == 1201
|
|
|
|
|
|
async def test_me_without_token_is_rejected(client):
|
|
r = await client.get("/v1/auth/me")
|
|
assert r.status_code in (401, 403) # HTTPBearer 가 자격증명 없음을 거부
|