test_sub2api_adapter.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from types import SimpleNamespace
  2. from ops.sub2api_adapter import Sub2ApiAdapter
  3. from ops.sub2api_client import Sub2ApiClient
  4. from ops.common import create_backend_client
  5. class StubSub2ApiClient:
  6. def __init__(self):
  7. self.deleted_ids: list[int] = []
  8. self.created_payloads: list[dict] = []
  9. self.account_by_id = {
  10. 11: {
  11. "id": 11,
  12. "name": "alpha@example.com.json",
  13. "credentials": {"refresh_token": "rt-a", "access_token": "at-a", "email": "alpha@example.com"},
  14. }
  15. }
  16. def health_check(self) -> bool:
  17. return True
  18. def list_accounts(self, platform="openai", page=1, page_size=100):
  19. return {
  20. "items": [
  21. {
  22. "id": 11,
  23. "name": "alpha@example.com.json",
  24. "credentials": {"refresh_token": "rt-a", "access_token": "at-a", "email": "alpha@example.com"},
  25. }
  26. ],
  27. "total": 1,
  28. "page": page,
  29. "page_size": page_size,
  30. "pages": 1,
  31. }
  32. def create_account(self, **payload):
  33. self.created_payloads.append(payload)
  34. return {"id": 12, **payload}
  35. def delete_account(self, account_id: int) -> bool:
  36. self.deleted_ids.append(account_id)
  37. return True
  38. def get_account(self, account_id: int):
  39. return self.account_by_id.get(account_id)
  40. def test_list_auth_files_converts_format():
  41. adapter = Sub2ApiAdapter(StubSub2ApiClient())
  42. files = adapter.list_auth_files()
  43. assert files == [
  44. {
  45. "name": "alpha@example.com.json",
  46. "content": {
  47. "refresh_token": "rt-a",
  48. "access_token": "at-a",
  49. "email": "alpha@example.com",
  50. },
  51. }
  52. ]
  53. def test_upload_auth_file_creates_oauth_account():
  54. client = StubSub2ApiClient()
  55. adapter = Sub2ApiAdapter(client)
  56. ok = adapter.upload_auth_file(
  57. "beta@example.com.json",
  58. {"refresh_token": "rt-b", "access_token": "at-b", "email": "beta@example.com"},
  59. )
  60. assert ok is True
  61. assert client.created_payloads == [
  62. {
  63. "name": "beta@example.com.json",
  64. "platform": "openai",
  65. "type": "oauth",
  66. "credentials": {
  67. "refresh_token": "rt-b",
  68. "access_token": "at-b",
  69. "email": "beta@example.com",
  70. },
  71. }
  72. ]
  73. def test_delete_auth_file_uses_name_to_id_cache():
  74. client = StubSub2ApiClient()
  75. adapter = Sub2ApiAdapter(client)
  76. adapter.list_auth_files()
  77. ok = adapter.delete_auth_file("alpha@example.com.json")
  78. assert ok is True
  79. assert client.deleted_ids == [11]
  80. def test_count_auth_files():
  81. adapter = Sub2ApiAdapter(StubSub2ApiClient())
  82. assert adapter.count_auth_files() == 1
  83. def test_create_backend_client_returns_sub2api_adapter(monkeypatch):
  84. settings = SimpleNamespace(
  85. backend="sub2api",
  86. sub2api_base_url="http://127.0.0.1:8080",
  87. sub2api_admin_email="admin@example.com",
  88. sub2api_admin_password="secret",
  89. sub2api_api_key="",
  90. cpa_management_base_url="http://unused",
  91. )
  92. client = create_backend_client(settings)
  93. assert isinstance(client, Sub2ApiAdapter)
  94. assert isinstance(client.client, Sub2ApiClient)
  95. assert client.client.base_url == "http://127.0.0.1:8080"