File size: 4,467 Bytes
89419ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from src.services.duck_mail import DuckMailService


class FakeResponse:
    def __init__(self, status_code=200, payload=None, text=""):
        self.status_code = status_code
        self._payload = payload
        self.text = text
        self.headers = {}

    def json(self):
        if self._payload is None:
            raise ValueError("no json payload")
        return self._payload


class FakeHTTPClient:
    def __init__(self, responses):
        self.responses = list(responses)
        self.calls = []

    def request(self, method, url, **kwargs):
        self.calls.append({
            "method": method,
            "url": url,
            "kwargs": kwargs,
        })
        if not self.responses:
            raise AssertionError(f"未准备响应: {method} {url}")
        return self.responses.pop(0)


def test_create_email_creates_account_and_fetches_token():
    service = DuckMailService({
        "base_url": "https://api.duckmail.test",
        "default_domain": "duckmail.sbs",
        "api_key": "dk_test_key",
        "password_length": 10,
    })
    fake_client = FakeHTTPClient([
        FakeResponse(
            status_code=201,
            payload={
                "id": "account-1",
                "address": "tester@duckmail.sbs",
                "authType": "email",
            },
        ),
        FakeResponse(
            payload={
                "id": "account-1",
                "token": "token-123",
            }
        ),
    ])
    service.http_client = fake_client

    email_info = service.create_email()

    assert email_info["email"] == "tester@duckmail.sbs"
    assert email_info["service_id"] == "account-1"
    assert email_info["account_id"] == "account-1"
    assert email_info["token"] == "token-123"

    create_call = fake_client.calls[0]
    assert create_call["method"] == "POST"
    assert create_call["url"] == "https://api.duckmail.test/accounts"
    assert create_call["kwargs"]["json"]["address"].endswith("@duckmail.sbs")
    assert len(create_call["kwargs"]["json"]["password"]) == 10
    assert create_call["kwargs"]["headers"]["Authorization"] == "Bearer dk_test_key"

    token_call = fake_client.calls[1]
    assert token_call["method"] == "POST"
    assert token_call["url"] == "https://api.duckmail.test/token"
    assert token_call["kwargs"]["json"] == {
        "address": "tester@duckmail.sbs",
        "password": email_info["password"],
    }


def test_get_verification_code_reads_message_detail_and_extracts_code():
    service = DuckMailService({
        "base_url": "https://api.duckmail.test",
        "default_domain": "duckmail.sbs",
    })
    fake_client = FakeHTTPClient([
        FakeResponse(
            status_code=201,
            payload={
                "id": "account-1",
                "address": "tester@duckmail.sbs",
                "authType": "email",
            },
        ),
        FakeResponse(
            payload={
                "id": "account-1",
                "token": "token-123",
            }
        ),
        FakeResponse(
            payload={
                "hydra:member": [
                    {
                        "id": "msg-1",
                        "from": {
                            "name": "OpenAI",
                            "address": "noreply@openai.com",
                        },
                        "subject": "Your verification code",
                        "createdAt": "2026-03-19T10:00:00Z",
                    }
                ]
            }
        ),
        FakeResponse(
            payload={
                "id": "msg-1",
                "text": "Your OpenAI verification code is 654321",
                "html": [],
            }
        ),
    ])
    service.http_client = fake_client

    email_info = service.create_email()
    code = service.get_verification_code(
        email=email_info["email"],
        email_id=email_info["service_id"],
        timeout=1,
    )

    assert code == "654321"

    messages_call = fake_client.calls[2]
    assert messages_call["method"] == "GET"
    assert messages_call["url"] == "https://api.duckmail.test/messages"
    assert messages_call["kwargs"]["headers"]["Authorization"] == "Bearer token-123"

    detail_call = fake_client.calls[3]
    assert detail_call["method"] == "GET"
    assert detail_call["url"] == "https://api.duckmail.test/messages/msg-1"
    assert detail_call["kwargs"]["headers"]["Authorization"] == "Bearer token-123"