fix(휴가): 메일 실패 시 Gmail 연결 안내·다시 보내기 — 휴가 권한 없는 협의자 포함
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -114,6 +114,12 @@ class ConsultMailTests(unittest.TestCase):
|
||||
self.assertTrue(res["ok"])
|
||||
self.assertEqual(res["to"], ["lee@test.local", "hr@test.local"])
|
||||
|
||||
def test_resend_skips_consultants_who_already_decided(self):
|
||||
cs = [dict(self.CONSULTS[0], state="승인"), self.CONSULTS[1]]
|
||||
with patch("app.mail.send_email", return_value=True) as smtp, patch("app.mail.mail_enabled", return_value=True):
|
||||
notify.deliver(FakeStore(["lee@test.local"]), None, event="submitted", req=dict(REQ, consults=cs), actor=self.OWNER, users=USERS, link="x")
|
||||
self.assertEqual([c.kwargs["to"] for c in smtp.call_args_list], [["choi@test.local"], ["lee@test.local"]])
|
||||
|
||||
def test_consulted_goes_to_approver_notifier_owner_and_lists_consults(self):
|
||||
done = [dict(c, state="승인") for c in self.CONSULTS]
|
||||
store = FakeStore(["lee@test.local"], ["hr@test.local"])
|
||||
@@ -467,6 +473,96 @@ def body(c):
|
||||
assert "바깥 사람 전용" in t and "이팀장 전용" in t and "협의 승인" not in t
|
||||
print("OK")
|
||||
run(body)
|
||||
''')
|
||||
self.assertEqual(res.returncode, 0, res.stdout + res.stderr)
|
||||
self.assertIn("OK", res.stdout)
|
||||
|
||||
def test_failed_mail_gmail_connect_and_retry(self):
|
||||
"""협의자가 Gmail 미연결로 메일 실패 → 상세에 안내 → (휴가 권한 없어도) 연결 → 다시 보내기 → 안내 사라짐."""
|
||||
res = _run('''
|
||||
from fastapi.responses import RedirectResponse
|
||||
from app.modules.inout import gmail
|
||||
|
||||
outsider = U("out@test.local"); outsider["modules"] = {}
|
||||
other = U("other@test.local"); other["modules"] = {}
|
||||
users.extend([outsider, other])
|
||||
LOG = []
|
||||
class Store4(Store):
|
||||
def list_consults(self, request_id):
|
||||
return [{"email": "out@test.local", "name": "out", "message": "m", "state": "승인", "comment": "", "decided_at": None}]
|
||||
def get_request(self, request_id):
|
||||
return dict(self.req)
|
||||
def list_mail_log(self, request_id):
|
||||
return list(LOG)
|
||||
def decide_consult(self, request_id, email, approve, reason=""):
|
||||
self.req["status"] = "제출"
|
||||
return dict(self.req), "consulted"
|
||||
|
||||
class Inout:
|
||||
tokens = set()
|
||||
def gmail_token(self, email):
|
||||
return {"refresh_token_enc": "x"} if email in self.tokens else None
|
||||
def save_gmail_token(self, email, refresh_token_enc, scopes):
|
||||
self.tokens.add(email)
|
||||
|
||||
class Client:
|
||||
async def authorize_redirect(self, request, url, **kw):
|
||||
return RedirectResponse("https://accounts.test/", status_code=302)
|
||||
async def authorize_access_token(self, request):
|
||||
return {"userinfo": {"email": "out@test.local"}, "scope": "openid email " + gmail.GMAIL_SCOPE, "refresh_token": "R"}
|
||||
|
||||
st4 = Store4()
|
||||
st4.req["status"] = "협의중"
|
||||
inout = Inout()
|
||||
def fail(event, sender="out@test.local", ok=False):
|
||||
LOG.append({"event": event, "sender": sender, "ok": ok, "via": "smtp", "recipients": "a@test.local", "sent_at": "2026-11-02T15:59:00", "error": "SMTP 미설정"})
|
||||
def body(c):
|
||||
main.app.state.vacation_store = st4
|
||||
main.app.state.inout_store = inout
|
||||
with patch.object(gmail, "oauth_client", return_value=Client()), \\
|
||||
patch.object(gmail, "encrypt_token", side_effect=lambda e, r: "enc:" + r), \\
|
||||
patch.object(gmail, "configured", return_value=True), \\
|
||||
patch("app.mail.mail_enabled", return_value=False):
|
||||
# 미연결 — 처리는 저장되고 메일은 바로 시도(응답 뒤로 미루지 않음), 기록이 실패면 Gmail 연결 안내
|
||||
who["u"] = outsider
|
||||
n = len(sent)
|
||||
assert c.post("/vacation/r1/consult", data={"decision": "approve"}).status_code == 303
|
||||
assert sent[n:] == [("consulted", "out@test.local", "https://erp.test/vacation/r1")], sent
|
||||
fail("consulted")
|
||||
t = c.get("/vacation/r1").text
|
||||
assert "협의 완료 메일을 보내지 못했습니다" in t and '/vacation/gmail/connect?next=/vacation/r1"' in t and "메일 다시 보내기" not in t
|
||||
# 다른 사람(신청자)에게는 안내 없음, 다시 보내기도 안 됨
|
||||
who["u"] = kim
|
||||
assert "보내지 못했습니다" not in c.get("/vacation/r1").text
|
||||
n = len(sent)
|
||||
assert c.post("/vacation/r1/mail-retry").status_code == 303 and len(sent) == n
|
||||
# 휴가 권한 없는 협의자 — 연결 시작 → 콜백 통과 → 그 상세로 복귀
|
||||
who["u"] = outsider
|
||||
assert c.get("/vacation/gmail/connect").status_code == 403 # 협의 신청 없이는 권한 필요
|
||||
r = c.get("/vacation/gmail/connect?next=/vacation/r1")
|
||||
assert r.status_code == 302, r.status_code
|
||||
r = c.get("/inout/gmail/callback")
|
||||
assert r.status_code == 303 and r.headers["location"].startswith("/vacation/r1?msg="), (r.status_code, r.headers.get("location"))
|
||||
assert "out@test.local" in inout.tokens
|
||||
assert "Gmail" in c.get(r.headers["location"]).text
|
||||
# 협의자가 아닌 신청의 next 는 권한 없으면 거절
|
||||
who["u"] = other
|
||||
assert c.get("/vacation/gmail/connect?next=/vacation/r1").status_code == 403
|
||||
who["u"] = outsider
|
||||
# 연결 뒤 — 다시 보내기 버튼, 누르면 응답 전에 보냄, 성공 기록이면 안내 사라짐
|
||||
t = c.get("/vacation/r1").text
|
||||
assert "메일 다시 보내기" in t and "/vacation/gmail/connect" not in t
|
||||
n = len(sent)
|
||||
assert c.post("/vacation/r1/mail-retry").status_code == 303
|
||||
assert sent[n:] == [("consulted", "out@test.local", "https://erp.test/vacation/r1")], sent
|
||||
fail("consulted", ok=True)
|
||||
assert "보내지 못했습니다" not in c.get("/vacation/r1").text
|
||||
# 일이 더 진행되면(승인 메일 성공) 예전 실패는 안내하지 않음 — 스크린샷 사례
|
||||
LOG.clear(); fail("consulted"); fail("approved", sender="lee@test.local", ok=True)
|
||||
st4.req["status"] = "승인"
|
||||
assert "보내지 못했습니다" not in c.get("/vacation/r1").text
|
||||
print("OK")
|
||||
run(body)
|
||||
''')
|
||||
self.assertEqual(res.returncode, 0, res.stdout + res.stderr)
|
||||
self.assertIn("OK", res.stdout)
|
||||
|
||||
Reference in New Issue
Block a user