Repository navigation
Expand file tree
/
Copy pathtest_admin_api.py
More file actions
451 lines (399 loc) · 15.8 KB
/
Copy pathtest_admin_api.py
File metadata and controls
451 lines (399 loc) · 15.8 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from fastapi import FastAPI
from fastapi.testclient import TestClient
from codebuddy_proxy import admin_api
from codebuddy_proxy.admin_api import create_admin_router
class FakeManager:
def __init__(self):
self.login_calls = []
self.activated = []
self.deleted = []
self.settings = {
"auto_switch_on_429": False,
"default_cooldown_seconds": 60,
"auto_checkin_enabled": False,
}
self.checkins = FakeCheckins()
def has_account(self, account_id):
return account_id == "account-1"
def snapshot(self):
return {
"accounts": [
{
"id": "account-1",
"backend": "domestic",
"endpoint": "https://copilot.tencent.com",
"uid": "user-1",
"nickname": "Local user",
"active": True,
"model_cooldowns": [
{
"model": "model-a",
"rate_limited": True,
"cooldown_until": 1234.5,
"cooldown_remaining_seconds": 7,
"last_rate_limited_at": 1227.5,
"cooldown_source": "retry_after",
"session": {"accessToken": "private-token-value"},
},
{
"model": "bad\nmodel",
"rate_limited": True,
"cooldown_source": "default",
},
{
"model": "x" * 257,
"rate_limited": True,
"cooldown_source": "default",
},
],
# Previous account-wide cooldown fields must not leak into
# the public response even if a stale caller provides them.
"rate_limited": True,
"cooldown_until": 9999.0,
"checkin": {
"date": "2026-10-24",
"status": "checked",
"checked": True,
"last_checked_at": 1220,
"last_checkin_at": 1210,
"next_checkin_at": 1729766400,
"error": None,
"session": {"accessToken": "private-token-value"},
},
"session": {"auth": {"accessToken": "private-token-value"}},
}
],
"active_account_id": "account-1",
"settings": self.settings,
"server_time": 1227.5,
"raw_session": "private-token-value",
}
def get_settings(self):
return dict(self.settings)
def set_settings(self, auto_switch_on_429=None, *, auto_checkin_enabled=None):
if auto_switch_on_429 is not None:
self.settings["auto_switch_on_429"] = auto_switch_on_429
if auto_checkin_enabled is not None:
self.settings["auto_checkin_enabled"] = auto_checkin_enabled
return dict(self.settings)
def activate(self, account_id):
self.activated.append(account_id)
def delete(self, account_id):
self.deleted.append(account_id)
def start_login(self, backend):
self.login_calls.append(backend)
return {
"id": "job-1",
"backend": backend,
"status": "pending",
"auth_url": "https://login.example.test/auth",
"error": None,
"account_id": None,
"state": "private-state",
"session": {"auth": {"accessToken": "private-token-value"}},
}
def get_login(self, job_id):
return {
"id": job_id,
"backend": "global",
"status": "failed",
"auth_url": None,
"error": "登录失败,请重试",
"account_id": None,
"exception": "private-token-value",
}
def cancel_login(self, job_id):
return {"id": job_id, "backend": "global", "status": "cancelled"}
class FakeCheckins:
def __init__(self):
self.calls = []
self.status_result = {
"date": "2026-10-24",
"status": "unchecked",
"checked": False,
"last_checked_at": 1220.0,
"last_checkin_at": None,
"next_checkin_at": 1729766400.0,
"error": None,
"session": {"accessToken": "private-token-value"},
}
self.checkin_result = {
"date": "2026-10-24",
"status": "checked",
"checked": True,
"last_checked_at": 1230.0,
"last_checkin_at": 1230.0,
"next_checkin_at": 1729766400.0,
"error": None,
"raw_response": "private-token-value",
}
async def check_status(self, account_id, *, reason):
self.calls.append(("status", account_id, reason))
return self.status_result
async def check_in(self, account_id, *, reason):
self.calls.append(("checkin", account_id, reason))
return self.checkin_result
def _app(manager):
app = FastAPI()
app.include_router(create_admin_router(lambda: manager))
return app
def _client(manager):
return TestClient(
_app(manager),
base_url="http://localhost:8123",
client=("127.0.0.1", 50000),
)
def test_account_and_job_responses_are_sanitized_and_uncached():
manager = FakeManager()
with _client(manager) as client:
accounts = client.get("/admin/api/accounts")
assert accounts.status_code == 200
assert accounts.headers["cache-control"] == "no-store"
assert accounts.json() == {
"accounts": [
{
"id": "account-1",
"backend": "domestic",
"endpoint": "https://copilot.tencent.com",
"uid": "user-1",
"nickname": "Local user",
"active": True,
"model_cooldowns": [
{
"model": "model-a",
"rate_limited": True,
"cooldown_until": 1234.5,
"cooldown_remaining_seconds": 7,
"last_rate_limited_at": 1227.5,
"cooldown_source": "retry_after",
}
],
"checkin": {
"date": "2026-10-24",
"status": "checked",
"checked": True,
"last_checked_at": 1220.0,
"last_checkin_at": 1210.0,
"error": None,
"next_checkin_at": 1729766400.0,
},
}
],
"active_account_id": "account-1",
"settings": {
"auto_switch_on_429": False,
"auto_checkin_enabled": False,
"default_cooldown_seconds": 60,
},
"server_time": 1227.5,
}
job = client.get("/admin/api/logins/job-1")
assert job.status_code == 200
assert job.headers["cache-control"] == "no-store"
assert "private-token-value" not in job.text
assert "exception" not in job.json()
def test_write_routes_require_header_and_same_origin_with_port_check():
manager = FakeManager()
with _client(manager) as client:
denied = client.post("/admin/api/logins", json={"backend": "global"})
assert denied.status_code == 403
assert manager.login_calls == []
cross_port = client.post(
"/admin/api/logins",
json={"backend": "global"},
headers={
"X-Account-Manager": "1",
"Origin": "http://localhost:8124",
},
)
assert cross_port.status_code == 403
assert manager.login_calls == []
accepted = client.post(
"/admin/api/logins",
json={"backend": "global"},
headers={
"X-Account-Manager": "1",
"Origin": "http://localhost:8123",
},
)
assert accepted.status_code == 202
assert accepted.headers["cache-control"] == "no-store"
assert accepted.json() == {
"id": "job-1",
"backend": "global",
"status": "pending",
"auth_url": "https://login.example.test/auth",
"error": None,
"account_id": None,
}
assert manager.login_calls == ["global"]
originless = client.post(
"/admin/api/accounts/account-1/activate",
headers={"X-Account-Manager": "1"},
)
assert originless.status_code == 200
assert manager.activated == ["account-1"]
def test_settings_read_write_is_protected_strict_and_no_store():
manager = FakeManager()
with _client(manager) as client:
read = client.get("/admin/api/settings")
assert read.status_code == 200
assert read.headers["cache-control"] == "no-store"
assert read.json() == {
"auto_switch_on_429": False,
"auto_checkin_enabled": False,
"default_cooldown_seconds": 60,
}
denied = client.patch(
"/admin/api/settings",
json={"auto_switch_on_429": True},
)
assert denied.status_code == 403
assert manager.settings["auto_switch_on_429"] is False
cross_port = client.patch(
"/admin/api/settings",
json={"auto_switch_on_429": True},
headers={
"X-Account-Manager": "1",
"Origin": "http://localhost:8124",
},
)
assert cross_port.status_code == 403
assert manager.settings["auto_switch_on_429"] is False
invalid_payloads = (
{"auto_switch_on_429": 1},
{"auto_switch_on_429": True, "unknown": "value"},
{},
{"auto_checkin_enabled": "true"},
)
for payload in invalid_payloads:
response = client.patch(
"/admin/api/settings",
json=payload,
headers={"X-Account-Manager": "1"},
)
assert response.status_code == 400
duplicate_key = client.patch(
"/admin/api/settings",
content='{"auto_switch_on_429":false,"auto_switch_on_429":true}',
headers={"X-Account-Manager": "1", "Content-Type": "application/json"},
)
assert duplicate_key.status_code == 400
assert manager.settings["auto_switch_on_429"] is False
updated = client.patch(
"/admin/api/settings",
json={"auto_switch_on_429": True},
headers={"X-Account-Manager": "1"},
)
assert updated.status_code == 200
assert updated.headers["cache-control"] == "no-store"
assert updated.json() == {
"auto_switch_on_429": True,
"auto_checkin_enabled": False,
"default_cooldown_seconds": 60,
}
assert client.get("/admin/api/settings").json()["auto_switch_on_429"] is True
checkin_enabled = client.patch(
"/admin/api/settings",
json={"auto_checkin_enabled": True},
headers={"X-Account-Manager": "1"},
)
assert checkin_enabled.status_code == 200
assert checkin_enabled.json() == {
"auto_switch_on_429": True,
"auto_checkin_enabled": True,
"default_cooldown_seconds": 60,
}
def test_checkin_routes_keep_shape_sanitize_errors_and_never_change_active():
manager = FakeManager()
with _client(manager) as client:
status = client.get("/admin/api/accounts/account-1/checkin-status")
assert status.status_code == 200
assert status.headers["cache-control"] == "no-store"
assert status.json() == {
"date": "2026-10-24",
"status": "unchecked",
"checked": False,
"last_checked_at": 1220.0,
"last_checkin_at": None,
"error": None,
"next_checkin_at": 1729766400.0,
}
assert manager.checkins.calls == [("status", "account-1", "manual_status")]
denied = client.post("/admin/api/accounts/account-1/checkin")
assert denied.status_code == 403
assert len(manager.checkins.calls) == 1
cross_port = client.post(
"/admin/api/accounts/account-1/checkin",
headers={
"X-Account-Manager": "1",
"Origin": "http://localhost:8124",
},
)
assert cross_port.status_code == 403
assert len(manager.checkins.calls) == 1
checked = client.post(
"/admin/api/accounts/account-1/checkin",
headers={"X-Account-Manager": "1", "Origin": "http://localhost:8123"},
)
assert checked.status_code == 200
assert checked.json()["status"] == "checked"
assert checked.json()["checked"] is True
assert "private-token-value" not in checked.text
assert manager.checkins.calls[-1] == ("checkin", "account-1", "manual")
assert manager.snapshot()["active_account_id"] == "account-1"
manager.checkins.status_result = {
"date": "2026-10-24",
"status": "error",
"error": "upstream token private-token-value",
"exception": "secret",
}
sanitized = client.get("/admin/api/accounts/account-1/checkin-status")
assert sanitized.status_code == 200
assert sanitized.json()["error"] == "签到状态查询失败"
assert "private-token-value" not in sanitized.text
unknown = client.get("/admin/api/accounts/missing/checkin-status")
assert unknown.status_code == 404
assert client.post(
"/admin/api/accounts/missing/checkin",
headers={"X-Account-Manager": "1"},
).status_code == 404
def test_cross_site_origin_host_and_non_loopback_peer_are_rejected():
manager = FakeManager()
with _client(manager) as client:
cross_site = client.get(
"/admin/api/accounts",
headers={"Origin": "http://evil.example"},
)
assert cross_site.status_code == 403
bad_host = client.get("/admin/api/accounts", headers={"Host": "evil.example"})
assert bad_host.status_code == 403
remote = TestClient(
_app(manager),
base_url="http://localhost:8123",
client=("192.0.2.4", 50000),
)
with remote:
response = remote.get("/admin/api/accounts")
assert response.status_code == 403
assert manager.login_calls == []
def test_admin_page_is_loaded_as_a_no_store_package_resource(monkeypatch):
class Resource:
def joinpath(self, _name):
return self
def read_text(self, *, encoding):
assert encoding == "utf-8"
return "<!doctype html><title>Account manager</title>"
monkeypatch.setattr(admin_api.resources, "files", lambda _package: Resource())
with _client(FakeManager()) as client:
for path in ("/admin", "/admin/"):
response = client.get(path)
assert response.status_code == 200
assert response.headers["cache-control"] == "no-store"
assert "Account manager" in response.text
def test_unmatched_admin_path_is_no_store_and_has_generic_error():
with _client(FakeManager()) as client:
response = client.get("/admin/api/not-real")
assert response.status_code == 404
assert response.headers["cache-control"] == "no-store"
assert "private-token-value" not in response.text