|
| 1 | +from src.services.freemail import FreemailService |
| 2 | +from src.services.temp_mail import TempMailService |
| 3 | + |
| 4 | + |
| 5 | +class FakeResponse: |
| 6 | + def __init__(self, status_code=200, payload=None, text=""): |
| 7 | + self.status_code = status_code |
| 8 | + self._payload = payload |
| 9 | + self.text = text |
| 10 | + self.headers = {} |
| 11 | + |
| 12 | + def json(self): |
| 13 | + if self._payload is None: |
| 14 | + raise ValueError("no json payload") |
| 15 | + return self._payload |
| 16 | + |
| 17 | + |
| 18 | +class FakeHTTPClient: |
| 19 | + def __init__(self, responses): |
| 20 | + self.responses = list(responses) |
| 21 | + self.calls = [] |
| 22 | + |
| 23 | + def request(self, method, url, **kwargs): |
| 24 | + self.calls.append({ |
| 25 | + "method": method, |
| 26 | + "url": url, |
| 27 | + "kwargs": kwargs, |
| 28 | + }) |
| 29 | + if not self.responses: |
| 30 | + raise AssertionError(f"未准备响应: {method} {url}") |
| 31 | + return self.responses.pop(0) |
| 32 | + |
| 33 | + |
| 34 | +def test_temp_mail_ignores_six_digit_domain_when_extracting_code(): |
| 35 | + service = TempMailService({ |
| 36 | + "base_url": "https://mail.example.com", |
| 37 | + "admin_password": "admin-secret", |
| 38 | + "domain": "123456.com", |
| 39 | + }) |
| 40 | + service.http_client = FakeHTTPClient([ |
| 41 | + FakeResponse( |
| 42 | + payload={ |
| 43 | + "results": [ |
| 44 | + { |
| 45 | + "id": "msg-1", |
| 46 | + "source": "OpenAI <noreply@openai.com>", |
| 47 | + "subject": "Your OpenAI verification code", |
| 48 | + "body": ( |
| 49 | + "Email sent to tester@123456.com.\n" |
| 50 | + "Your OpenAI verification code is 654321" |
| 51 | + ), |
| 52 | + } |
| 53 | + ] |
| 54 | + } |
| 55 | + ) |
| 56 | + ]) |
| 57 | + |
| 58 | + code = service.get_verification_code( |
| 59 | + email="tester@123456.com", |
| 60 | + timeout=1, |
| 61 | + ) |
| 62 | + |
| 63 | + assert code == "654321" |
| 64 | + |
| 65 | + |
| 66 | +def test_freemail_prefers_real_code_over_worker_extracted_domain_digits(): |
| 67 | + service = FreemailService({ |
| 68 | + "base_url": "https://mail.example.com", |
| 69 | + "admin_token": "jwt-token", |
| 70 | + }) |
| 71 | + service.http_client = FakeHTTPClient([ |
| 72 | + FakeResponse( |
| 73 | + payload=[ |
| 74 | + { |
| 75 | + "id": "msg-1", |
| 76 | + "sender": "noreply@openai.com", |
| 77 | + "subject": "Your OpenAI verification code", |
| 78 | + "preview": "Verification email sent to tester@123456.com", |
| 79 | + "verification_code": "123456", |
| 80 | + } |
| 81 | + ] |
| 82 | + ), |
| 83 | + FakeResponse( |
| 84 | + payload={ |
| 85 | + "content": ( |
| 86 | + "To: tester@123456.com\n" |
| 87 | + "Your OpenAI verification code is 654321" |
| 88 | + ), |
| 89 | + "html_content": "", |
| 90 | + } |
| 91 | + ), |
| 92 | + ]) |
| 93 | + |
| 94 | + code = service.get_verification_code( |
| 95 | + email="tester@123456.com", |
| 96 | + timeout=1, |
| 97 | + ) |
| 98 | + |
| 99 | + assert code == "654321" |
0 commit comments