88 lines
4.2 KiB
Python
88 lines
4.2 KiB
Python
import base64
|
|
|
|
import httpx
|
|
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from config.settings import settings
|
|
from router.router import app
|
|
from services.external import toss_login
|
|
|
|
|
|
def client():
|
|
return AsyncClient(transport=ASGITransport(app=app), base_url="http://test")
|
|
|
|
|
|
@pytest.fixture
|
|
def toss(monkeypatch):
|
|
"""토스 서버 대신 코드별로 정해 둔 userKey 를 돌려준다."""
|
|
keys = {"code-a": 1234567, "code-b": 7654321}
|
|
|
|
async def fake_user_key(code: str, referrer: str) -> int:
|
|
if code not in keys:
|
|
raise toss_login.TossLoginError("generate-token failed: invalid_grant")
|
|
return keys[code]
|
|
|
|
monkeypatch.setattr(toss_login, "user_key", fake_user_key)
|
|
monkeypatch.setattr(settings, "toss_unlink_auth", "toss:unlink-secret")
|
|
|
|
|
|
def _basic(value: str) -> dict:
|
|
return {"Authorization": "Basic " + base64.b64encode(value.encode()).decode()}
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_toss_login_creates_once_and_reuses(toss):
|
|
async with client() as c:
|
|
first = (await c.post("/v1/auth/toss", json={"authorization_code": "code-a", "referrer": "SANDBOX"})).json()
|
|
again = (await c.post("/v1/auth/toss", json={"authorization_code": "code-a", "referrer": "DEFAULT"})).json()
|
|
other = (await c.post("/v1/auth/toss", json={"authorization_code": "code-b", "referrer": "DEFAULT"})).json()
|
|
me = (await c.get("/v1/auth/me", headers={"Authorization": f"Bearer {first['access_token']}"})).json()
|
|
assert first["result"]["success"] is True and first["me"]["provider"] == 3
|
|
assert first["me"]["login_id"] == again["me"]["login_id"] == "toss_1234567"
|
|
assert other["me"]["login_id"] == "toss_7654321"
|
|
assert me["me"]["login_id"] == "toss_1234567"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_toss_login_rejects_bad_code_and_referrer(toss):
|
|
async with client() as c:
|
|
bad_code = (await c.post("/v1/auth/toss", json={"authorization_code": "nope", "referrer": "DEFAULT"})).json()
|
|
bad_ref = (await c.post("/v1/auth/toss", json={"authorization_code": "code-a", "referrer": "WEB"})).json()
|
|
assert bad_code["result"]["desc"] == "AUTH_FAILED"
|
|
assert bad_ref["result"]["desc"] == "INVALID_INPUT"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_toss_unlink_needs_basic_auth_and_kills_tokens(toss):
|
|
async with client() as c:
|
|
login = (await c.post("/v1/auth/toss", json={"authorization_code": "code-a", "referrer": "DEFAULT"})).json()
|
|
body = {"userKey": 1234567, "referrer": "UNLINK"}
|
|
assert (await c.post("/v1/auth/toss/unlink", json=body)).status_code == 401
|
|
assert (await c.post("/v1/auth/toss/unlink", json=body, headers=_basic("toss:wrong"))).status_code == 401
|
|
ok = await c.post("/v1/auth/toss/unlink", json=body, headers=_basic("toss:unlink-secret"))
|
|
unknown = await c.post("/v1/auth/toss/unlink", json={"userKey": 1, "referrer": "WITHDRAWAL_TOSS"}, headers=_basic("toss:unlink-secret"))
|
|
me = await c.get("/v1/auth/me", headers={"Authorization": f"Bearer {login['access_token']}"})
|
|
assert ok.status_code == 200 and ok.json()["result"]["success"] is True
|
|
assert unknown.status_code == 200
|
|
assert me.status_code == 401
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_toss_unlink_rejects_all_when_not_configured(toss, monkeypatch):
|
|
monkeypatch.setattr(settings, "toss_unlink_auth", "")
|
|
async with client() as c:
|
|
r = await c.post("/v1/auth/toss/unlink", json={"userKey": 1, "referrer": "UNLINK"}, headers=_basic(":"))
|
|
assert r.status_code == 401
|
|
|
|
|
|
def test_success_reads_toss_wrapper():
|
|
ok = httpx.Response(200, json={"resultType": "SUCCESS", "success": {"userKey": 1}}, request=httpx.Request("GET", "https://x"))
|
|
fail = httpx.Response(200, json={"resultType": "FAIL", "error": {"errorCode": "4050", "reason": "x"}}, request=httpx.Request("GET", "https://x"))
|
|
grant = httpx.Response(200, json={"error": "invalid_grant"}, request=httpx.Request("GET", "https://x"))
|
|
assert toss_login._success(ok, "login-me") == {"userKey": 1}
|
|
with pytest.raises(toss_login.TossLoginError, match="4050"):
|
|
toss_login._success(fail, "login-me")
|
|
with pytest.raises(toss_login.TossLoginError, match="invalid_grant"):
|
|
toss_login._success(grant, "generate-token")
|