test_chatgpt_plugin.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import json
  2. from pathlib import Path
  3. from core.base_platform import RegisterConfig
  4. from platforms.chatgpt.plugin import ChatGPTPlatform
  5. from platforms.chatgpt.register import RegistrationResult
  6. class _FakeMailbox:
  7. def get_email(self): # type: ignore[no-untyped-def]
  8. raise AssertionError("mailbox should not be used in this test")
  9. def wait_for_code(self, *args, **kwargs): # type: ignore[no-untyped-def]
  10. raise AssertionError("mailbox should not be used in this test")
  11. def test_run_register_once_persists_password_in_pool_file(monkeypatch, tmp_path: Path) -> None:
  12. class FakeEngine:
  13. def __init__(self, email_service, proxy_url=None, **kwargs): # type: ignore[no-untyped-def]
  14. del kwargs
  15. self.email_service = email_service
  16. self.proxy_url = proxy_url
  17. self.email = None
  18. self.password = None
  19. def run(self): # type: ignore[no-untyped-def]
  20. return RegistrationResult(
  21. success=True,
  22. stage="completed",
  23. email="demo@example.com",
  24. password="pw-secret",
  25. account_id="acct-123",
  26. access_token="access-123",
  27. refresh_token="refresh-123",
  28. id_token="id-123",
  29. metadata={"expired": "2026-03-30T00:00:00Z"},
  30. )
  31. monkeypatch.setattr("platforms.chatgpt.register.RegistrationEngine", FakeEngine)
  32. platform = ChatGPTPlatform(
  33. config=RegisterConfig(proxy="http://127.0.0.1:7899", extra={"mail_provider": "cfmail"}),
  34. mailbox=_FakeMailbox(),
  35. )
  36. payload = platform.run_register_once(write_pool=True, pool_dir=tmp_path)
  37. pool_file = Path(payload["pool_file"])
  38. data = json.loads(pool_file.read_text(encoding="utf-8"))
  39. assert payload["success"] is True
  40. assert payload["written_to_pool"] is True
  41. assert pool_file.exists()
  42. assert data["email"] == "demo@example.com"
  43. assert data["password"] == "pw-secret"
  44. assert data["mail_provider"] == "cfmail"
  45. assert data["mailbox"]["email"] == "demo@example.com"
  46. assert data["mailbox"]["account_id"] == ""
  47. assert data["mailbox"]["extra"] == {}
  48. assert data["access_token"] == "access-123"
  49. assert data["refresh_token"] == "refresh-123"
  50. def test_run_register_once_persists_mailbox_account_context(monkeypatch, tmp_path: Path) -> None:
  51. class FakeMailbox:
  52. pass
  53. mailbox = FakeMailbox()
  54. class FakeEngine:
  55. def __init__(self, email_service, proxy_url=None, **kwargs): # type: ignore[no-untyped-def]
  56. del kwargs
  57. self.email_service = email_service
  58. self.proxy_url = proxy_url
  59. self.email = None
  60. self.password = None
  61. def run(self): # type: ignore[no-untyped-def]
  62. self.email_service._account = type( # type: ignore[attr-defined]
  63. "Account",
  64. (),
  65. {
  66. "email": "mailbox@example.com",
  67. "account_id": "jwt-123",
  68. "extra": {"api_base": "https://email-api.example.test", "config_name": "cfmail-a"},
  69. },
  70. )()
  71. return RegistrationResult(
  72. success=True,
  73. stage="completed",
  74. email="mailbox@example.com",
  75. password="pw-mailbox",
  76. account_id="acct-mailbox",
  77. access_token="access-mailbox",
  78. refresh_token="refresh-mailbox",
  79. id_token="id-mailbox",
  80. metadata={"expired": "2026-03-30T00:00:00Z"},
  81. )
  82. monkeypatch.setattr("platforms.chatgpt.register.RegistrationEngine", FakeEngine)
  83. platform = ChatGPTPlatform(
  84. config=RegisterConfig(proxy="http://127.0.0.1:7899", extra={"mail_provider": "cfmail"}),
  85. mailbox=mailbox,
  86. )
  87. payload = platform.run_register_once(write_pool=True, pool_dir=tmp_path)
  88. data = json.loads(Path(payload["pool_file"]).read_text(encoding="utf-8"))
  89. assert data["mailbox"]["email"] == "mailbox@example.com"
  90. assert data["mailbox"]["account_id"] == "jwt-123"
  91. assert data["mailbox"]["extra"] == {
  92. "api_base": "https://email-api.example.test",
  93. "config_name": "cfmail-a",
  94. }