007a16eb9c
- app/store.py login_redirect(): 로그인 전 화면 주소를 /login?next= 로 넘긴다(GET 만 — 폼 POST 주소로 돌아가면 405). 경비·쿠팡·휴가·말레이시아·배송·카페24·실적 11곳이 next 없이 /login 으로 보내던 것과 프로젝트·입출고·안내문 발송의 복사본 4벌을 이 함수로. - UserStore._read: JSONDecodeError 를 빈 목록으로 삼키지 않는다 — 다음 로그인이 전체 사용자·권한을 그 한 명으로 덮어쓰던 것을 막는다. - tests/test_login_redirect.py Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
2.4 KiB
Python
66 lines
2.4 KiB
Python
"""로그인 전 화면 주소 → /login?next=(GET 만), 깨진 users.json 은 덮어쓰지 않는다."""
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
|
|
class LoginRedirectTests(unittest.TestCase):
|
|
def test_next_and_broken_users_json(self):
|
|
env = dict(os.environ)
|
|
for module in (
|
|
"ASSET", "CAFE24", "CUPANG", "VACATION", "PROJECT", "DISPATCH",
|
|
"PERFORMANCE", "MALAYSIA_STOCK", "ITEMCODE", "EXPENSE", "INOUT",
|
|
):
|
|
env[module + "_DB_URL"] = ""
|
|
env.update(
|
|
SESSION_SECRET_KEY="login-redirect-test-only", SESSION_COOKIE_SECURE="false",
|
|
PERFORMANCE_AUTO_SYNC="false", PROJECT_REMINDERS="false", PYTHONIOENCODING="utf-8",
|
|
)
|
|
code = '''
|
|
import json
|
|
from fastapi.testclient import TestClient
|
|
import app.main as main
|
|
|
|
client = TestClient(main.app, follow_redirects=False)
|
|
for path, nxt in [
|
|
("/cafe24/?selected=123", "%2Fcafe24%2F%3Fselected%3D123"),
|
|
("/expense/", "%2Fexpense%2F"),
|
|
("/vacation/", "%2Fvacation%2F"),
|
|
("/performance/", "%2Fperformance%2F"),
|
|
]:
|
|
r = client.get(path)
|
|
assert r.status_code == 303, (path, r.status_code)
|
|
assert r.headers["location"] == "/login?next=" + nxt, (path, r.headers["location"])
|
|
|
|
# 폼 POST 는 next 없이 — 로그인 뒤 같은 주소를 GET 으로 열면 405 가 난다.
|
|
r = client.post("/cafe24/schedules/1/cancel")
|
|
assert r.status_code == 303 and r.headers["location"] == "/login", r.headers.get("location")
|
|
|
|
# 깨진 users.json — 빈 목록으로 읽고 덮어쓰지 않고 오류를 낸다.
|
|
path = main.user_store._path
|
|
path.write_text('{"users": {"a@x.test": {},}}', encoding="utf-8")
|
|
broken = path.read_text(encoding="utf-8")
|
|
try:
|
|
main.user_store.upsert_login(email="b@x.test", name="B", picture="")
|
|
except json.JSONDecodeError:
|
|
pass
|
|
else:
|
|
raise AssertionError("깨진 users.json 을 덮어썼다")
|
|
assert path.read_text(encoding="utf-8") == broken
|
|
print("login redirect OK")
|
|
'''
|
|
with tempfile.TemporaryDirectory(prefix="login-redirect-") as directory:
|
|
env["DATA_DIR"] = directory
|
|
result = subprocess.run(
|
|
[sys.executable, "-c", code], cwd=Path(__file__).resolve().parents[1],
|
|
env=env, capture_output=True, text=True, encoding="utf-8", timeout=60,
|
|
)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|