File size: 10,728 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from contextlib import contextmanager

from src.config.constants import OPENAI_API_ENDPOINTS, OPENAI_PAGE_TYPES
from src.core.register import RegistrationEngine
from src.core.registration_post_create import (
    POST_CREATE_REENTERED_LOGIN_SOURCE,
    POST_CREATE_RESUME_SOURCE,
)
from src.database.models import Account
from tests.test_registration_engine import (
    DummyResponse,
    FakeEmailService,
    FakeOpenAIClient,
    FakeOAuthManagerWithWorkspace,
    FakeOutlookEmailService,
    QueueSession,
    _make_engine_session_manager,
    _response_with_did,
    _workspace_cookie,
)


def test_run_prefers_locked_post_create_continue_over_cached_about_you():
    def create_account_add_phone(session):
        return DummyResponse(
            payload={
                "page": {"type": "add_phone"},
                "continue_url": "https://auth.example.test/add-phone",
            },
            url=OPENAI_API_ENDPOINTS["create_account"],
        )

    def consent_page_with_workspace(session):
        session.cookies["oai-client-auth-session"] = _workspace_cookie("ws-post-create")
        session.cookies["__Secure-next-auth.session-token"] = "session-post-create"
        return DummyResponse(
            status_code=200,
            url="https://auth.openai.com/sign-in-with-chatgpt/codex/consent",
            text="<html><body>consent</body></html>",
        )

    session = QueueSession([
        ("GET", "https://auth.example.test/flow/1", _response_with_did("did-1")),
        (
            "POST",
            OPENAI_API_ENDPOINTS["signup"],
            DummyResponse(payload={"page": {"type": OPENAI_PAGE_TYPES["PASSWORD_REGISTRATION"]}}),
        ),
        ("POST", OPENAI_API_ENDPOINTS["register"], DummyResponse(payload={})),
        ("GET", OPENAI_API_ENDPOINTS["send_otp"], DummyResponse(payload={})),
        (
            "POST",
            OPENAI_API_ENDPOINTS["validate_otp"],
            DummyResponse(
                payload={
                    "page": {"type": OPENAI_PAGE_TYPES["EMAIL_OTP_VERIFICATION"]},
                    "method": "GET",
                    "continue_url": "https://auth.example.test/about-you",
                }
            ),
        ),
        ("POST", OPENAI_API_ENDPOINTS["create_account"], create_account_add_phone),
        (
            "GET",
            "https://auth.example.test/add-phone",
            DummyResponse(
                status_code=200,
                url="https://auth.example.test/add-phone",
                text=(
                    "<html><body><script>"
                    'window.__STATE__={"continue_url":"https://auth.openai.com/sign-in-with-chatgpt/codex/consent"};'
                    "</script></body></html>"
                ),
            ),
        ),
        ("GET", "https://auth.openai.com/sign-in-with-chatgpt/codex/consent", consent_page_with_workspace),
        ("GET", "https://auth.openai.com/sign-in-with-chatgpt/codex/consent", consent_page_with_workspace),
        (
            "POST",
            OPENAI_API_ENDPOINTS["select_workspace"],
            DummyResponse(payload={"continue_url": "https://auth.example.test/post-create-continue"}),
        ),
        (
            "GET",
            "https://auth.example.test/post-create-continue",
            DummyResponse(
                status_code=302,
                headers={"Location": "http://localhost:1455/auth/callback?code=code-post-create&state=state-1"},
            ),
        ),
    ])

    email_service = FakeEmailService(["123456"])
    engine = RegistrationEngine(email_service)
    engine.http_client = FakeOpenAIClient([session], ["sentinel-1"])
    engine.oauth_manager = FakeOAuthManagerWithWorkspace("ws-post-create")

    result = engine.run()

    assert result.success is True
    assert result.workspace_id == "ws-post-create"
    assert result.session_token == "session-post-create"
    assert result.metadata["token_acquired_via_relogin"] is False
    assert result.metadata["last_oauth_resume_source"] == POST_CREATE_RESUME_SOURCE
    assert any("post_create_continue_overrode_cached_resume" in log for log in engine.logs)
    assert any("post_create_continue_followed" in log for log in engine.logs)
    assert any(call["url"] == "https://auth.example.test/add-phone" for call in session.calls)
    assert len([call for call in session.calls if call["url"] == OPENAI_API_ENDPOINTS["validate_otp"]]) == 1


def test_run_can_complete_post_create_direct_consent_without_relogin():
    def create_account_direct_consent(session):
        return DummyResponse(
            payload={
                "page": {"type": "consent"},
                "continue_url": "https://auth.openai.com/sign-in-with-chatgpt/codex/consent",
            },
            url=OPENAI_API_ENDPOINTS["create_account"],
        )

    def consent_page_with_workspace(session):
        session.cookies["oai-client-auth-session"] = _workspace_cookie("ws-post-create-consent")
        session.cookies["__Secure-next-auth.session-token"] = "session-post-create-consent"
        return DummyResponse(
            status_code=200,
            url="https://auth.openai.com/sign-in-with-chatgpt/codex/consent",
            text="<html><body>consent</body></html>",
        )

    session = QueueSession([
        ("GET", "https://auth.example.test/flow/1", _response_with_did("did-1")),
        (
            "POST",
            OPENAI_API_ENDPOINTS["signup"],
            DummyResponse(payload={"page": {"type": OPENAI_PAGE_TYPES["PASSWORD_REGISTRATION"]}}),
        ),
        ("POST", OPENAI_API_ENDPOINTS["register"], DummyResponse(payload={})),
        ("GET", OPENAI_API_ENDPOINTS["send_otp"], DummyResponse(payload={})),
        ("POST", OPENAI_API_ENDPOINTS["validate_otp"], DummyResponse(payload={})),
        ("POST", OPENAI_API_ENDPOINTS["create_account"], create_account_direct_consent),
        ("GET", "https://auth.openai.com/sign-in-with-chatgpt/codex/consent", consent_page_with_workspace),
        (
            "POST",
            OPENAI_API_ENDPOINTS["select_workspace"],
            DummyResponse(payload={"continue_url": "https://auth.example.test/post-create-consent-continue"}),
        ),
        (
            "GET",
            "https://auth.example.test/post-create-consent-continue",
            DummyResponse(
                status_code=302,
                headers={"Location": "http://localhost:1455/auth/callback?code=code-post-create-consent&state=state-1"},
            ),
        ),
    ])

    email_service = FakeEmailService(["123456"])
    engine = RegistrationEngine(email_service)
    engine.http_client = FakeOpenAIClient([session], ["sentinel-1"])
    engine.oauth_manager = FakeOAuthManagerWithWorkspace("ws-post-create-consent")

    result = engine.run()

    assert result.success is True
    assert result.workspace_id == "ws-post-create-consent"
    assert result.metadata["token_acquired_via_relogin"] is False
    assert result.metadata["last_oauth_resume_source"] == POST_CREATE_RESUME_SOURCE
    assert len([call for call in session.calls if call["url"] == OPENAI_API_ENDPOINTS["validate_otp"]]) == 1


def test_run_fails_post_create_reentry_without_relogin_and_persists_recovery(monkeypatch):
    manager = _make_engine_session_manager("post-create-reentered-login.db")

    @contextmanager
    def fake_get_db():
        session = manager.SessionLocal()
        try:
            yield session
        finally:
            session.close()

    monkeypatch.setattr("src.core.register.get_db", fake_get_db)

    def create_account_add_phone(session):
        return DummyResponse(
            payload={
                "page": {"type": "add_phone"},
                "continue_url": "https://auth.example.test/add-phone",
            },
            url=OPENAI_API_ENDPOINTS["create_account"],
        )

    session = QueueSession([
        ("GET", "https://auth.example.test/flow/1", _response_with_did("did-1")),
        (
            "POST",
            OPENAI_API_ENDPOINTS["signup"],
            DummyResponse(payload={"page": {"type": OPENAI_PAGE_TYPES["PASSWORD_REGISTRATION"]}}),
        ),
        ("POST", OPENAI_API_ENDPOINTS["register"], DummyResponse(payload={})),
        ("GET", OPENAI_API_ENDPOINTS["send_otp"], DummyResponse(payload={})),
        (
            "POST",
            OPENAI_API_ENDPOINTS["validate_otp"],
            DummyResponse(
                payload={
                    "page": {"type": OPENAI_PAGE_TYPES["EMAIL_OTP_VERIFICATION"]},
                    "method": "GET",
                    "continue_url": "https://auth.example.test/about-you",
                }
            ),
        ),
        ("POST", OPENAI_API_ENDPOINTS["create_account"], create_account_add_phone),
        (
            "GET",
            "https://auth.example.test/add-phone",
            DummyResponse(
                status_code=302,
                headers={"Location": "https://auth.example.test/log-in"},
            ),
        ),
        (
            "GET",
            "https://auth.example.test/log-in",
            DummyResponse(
                status_code=200,
                url="https://auth.example.test/log-in",
                text="<html>login</html>",
            ),
        ),
        (
            "GET",
            "https://auth.example.test/log-in",
            DummyResponse(
                status_code=200,
                url="https://auth.example.test/log-in",
                text="<html>login diagnostic</html>",
            ),
        ),
    ])

    email_service = FakeOutlookEmailService(["123456"])
    engine = RegistrationEngine(email_service)
    engine.http_client = FakeOpenAIClient([session], ["sentinel-1"])
    engine.oauth_manager = FakeOAuthManagerWithWorkspace("ws-unused")

    result = engine.run()

    assert result.success is False
    assert result.error_message == "账号已创建,但 post-create 续跑重新进入登录页"
    assert len([call for call in session.calls if call["url"] == OPENAI_API_ENDPOINTS["validate_otp"]]) == 1
    assert len([call for call in session.calls if call["url"] == "https://auth.example.test/flow/2"]) == 0
    assert any("post_create_continue_reentered_login" in log for log in engine.logs)

    with manager.session_scope() as session_db:
        account = session_db.query(Account).filter(Account.email == "tester@example.com").first()
        assert account is not None
        assert account.status == "failed"
        assert account.extra_data["last_recovery_error"] == result.error_message
        assert account.extra_data["last_oauth_resume_source"] == POST_CREATE_REENTERED_LOGIN_SOURCE
        assert account.extra_data["last_workspace_resolution_source"] == POST_CREATE_RESUME_SOURCE