fix: improve Hotmail IMAP code extraction and OpenAI mail diagnostics
- 合并 PR #177 的核心改动:补齐 Hotmail helper 在 IMAP 场景下的完整正文提码与 OpenAI 邮件日志输出 - 本地补充修复:无 - 影响范围:scripts/hotmail_helper.py、tests/hotmail_helper_logging_test.py
This commit is contained in:
@@ -18,6 +18,107 @@ hotmail_helper = load_hotmail_helper()
|
||||
|
||||
|
||||
class HotmailHelperLoggingTest(unittest.TestCase):
|
||||
def test_select_latest_code_can_use_full_body_when_preview_is_truncated(self):
|
||||
css_prefix = (
|
||||
'Your temporary ChatGPT verification code '
|
||||
'@font-face { font-family: "Söhne"; src: url(https://cdn.openai.com/common/fonts/soehne/soehne-buch.woff2) format("woff2"); } '
|
||||
'.ExternalClass { width: 100%; } '
|
||||
'#bodyTable { width: 560px; } '
|
||||
'body { min-width: 100% !important; } '
|
||||
) * 8
|
||||
full_body = (
|
||||
css_prefix
|
||||
+ 'Enter this temporary verification code to continue: 272964 '
|
||||
+ 'Please ignore this email if this was not you.'
|
||||
)
|
||||
message = {
|
||||
"id": "imap-1",
|
||||
"mailbox": "INBOX",
|
||||
"subject": "Your temporary ChatGPT verification code",
|
||||
"from": {
|
||||
"emailAddress": {
|
||||
"address": "otp@tm1.openai.com",
|
||||
"name": "OpenAI",
|
||||
}
|
||||
},
|
||||
"bodyPreview": full_body[:500],
|
||||
"body": {
|
||||
"content": full_body,
|
||||
},
|
||||
"receivedTimestamp": 200,
|
||||
}
|
||||
|
||||
result = hotmail_helper.select_latest_code(
|
||||
[message],
|
||||
['openai', 'noreply', 'verify', 'auth', 'chatgpt', 'duckduckgo', 'forward'],
|
||||
['verify', 'verification', 'code', '验证码', 'confirm', 'login'],
|
||||
[],
|
||||
0,
|
||||
)
|
||||
|
||||
self.assertEqual(result["code"], "272964")
|
||||
self.assertEqual(result["message"]["id"], "imap-1")
|
||||
|
||||
def test_log_openai_messages_logs_full_body_when_available(self):
|
||||
messages = [{
|
||||
"mailbox": "INBOX",
|
||||
"subject": "Your verification code",
|
||||
"from": {
|
||||
"emailAddress": {
|
||||
"address": "account-security@openai.com",
|
||||
"name": "OpenAI",
|
||||
}
|
||||
},
|
||||
"bodyPreview": "Use 123456 to continue.",
|
||||
"body": {
|
||||
"content": "Hello there\nUse 123456 to continue.",
|
||||
},
|
||||
}]
|
||||
|
||||
output = io.StringIO()
|
||||
with redirect_stdout(output):
|
||||
hotmail_helper.log_openai_messages(messages, transport="imap")
|
||||
|
||||
rendered = output.getvalue()
|
||||
self.assertIn(
|
||||
"[HotmailHelper] openai mail received transport=imap mailbox=INBOX sender=account-security@openai.com senderName=OpenAI subject=Your verification code",
|
||||
rendered,
|
||||
)
|
||||
self.assertIn(
|
||||
"[HotmailHelper] openai mail full body start transport=imap mailbox=INBOX sender=account-security@openai.com senderName=OpenAI subject=Your verification code",
|
||||
rendered,
|
||||
)
|
||||
self.assertIn("Hello there\nUse 123456 to continue.", rendered)
|
||||
self.assertIn("[HotmailHelper] openai mail full body end", rendered)
|
||||
|
||||
def test_log_openai_messages_falls_back_to_preview_without_full_body(self):
|
||||
messages = [{
|
||||
"mailbox": "Junk",
|
||||
"subject": "Verify your sign in",
|
||||
"from": {
|
||||
"emailAddress": {
|
||||
"address": "noreply@tm.openai.com",
|
||||
"name": "ChatGPT",
|
||||
}
|
||||
},
|
||||
"bodyPreview": "Use 654321 to continue.",
|
||||
}]
|
||||
|
||||
output = io.StringIO()
|
||||
with redirect_stdout(output):
|
||||
hotmail_helper.log_openai_messages(messages, transport="graph")
|
||||
|
||||
rendered = output.getvalue()
|
||||
self.assertIn(
|
||||
"[HotmailHelper] openai mail received transport=graph mailbox=Junk sender=noreply@tm.openai.com senderName=ChatGPT subject=Verify your sign in",
|
||||
rendered,
|
||||
)
|
||||
self.assertIn(
|
||||
"[HotmailHelper] openai mail preview transport=graph mailbox=Junk sender=noreply@tm.openai.com senderName=ChatGPT subject=Verify your sign in preview=Use 654321 to continue.",
|
||||
rendered,
|
||||
)
|
||||
self.assertNotIn("openai mail full body start", rendered)
|
||||
|
||||
def test_refresh_access_token_logs_invalid_grant_and_direct_connection_refused_separately(self):
|
||||
failures = [
|
||||
{
|
||||
@@ -38,7 +139,8 @@ class HotmailHelperLoggingTest(unittest.TestCase):
|
||||
},
|
||||
]
|
||||
|
||||
with mock.patch.object(hotmail_helper, "try_refresh_access_token", side_effect=failures):
|
||||
with mock.patch.object(hotmail_helper, "try_refresh_access_token", side_effect=failures), \
|
||||
mock.patch.object(hotmail_helper, "get_proxy_debug_context", return_value="direct"):
|
||||
output = io.StringIO()
|
||||
with redirect_stdout(output):
|
||||
with self.assertRaises(RuntimeError):
|
||||
|
||||
Reference in New Issue
Block a user