mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-01 21:37:33 +08:00
Refactor system API (#13958)
### What problem does this PR solve? - ping - token - log level ### Type of change - [x] Refactoring <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * System endpoints consolidated under /api/v1/system: ping, health check, and token management moved to the centralized API surface. * Token management unified at /api/v1/system/tokens with list/create/delete behavior. * **Documentation** * API reference updated to reflect the new /api/v1/system paths. * **Tests** * Client fixtures and test utilities updated to use /api/v1/system/tokens; one unit test for health/oceanbase status removed. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
@@ -94,17 +94,17 @@ def api_stats(auth, params=None, *, headers=HEADERS):
|
||||
|
||||
# SYSTEM APP
|
||||
def system_new_token(auth, payload=None, *, headers=HEADERS, data=None):
|
||||
res = requests.post(url=f"{HOST_ADDRESS}{SYSTEM_APP_URL}/new_token", headers=headers, auth=auth, json=payload, data=data)
|
||||
res = requests.post(url=f"{HOST_ADDRESS}{SYSTEM_API_URL}/tokens", headers=headers, auth=auth, json=payload, data=data)
|
||||
return res.json()
|
||||
|
||||
|
||||
def system_token_list(auth, params=None, *, headers=HEADERS):
|
||||
res = requests.get(url=f"{HOST_ADDRESS}{SYSTEM_APP_URL}/token_list", headers=headers, auth=auth, params=params)
|
||||
res = requests.get(url=f"{HOST_ADDRESS}{SYSTEM_API_URL}/tokens", headers=headers, auth=auth, params=params)
|
||||
return res.json()
|
||||
|
||||
|
||||
def system_delete_token(auth, token, *, headers=HEADERS):
|
||||
res = requests.delete(url=f"{HOST_ADDRESS}{SYSTEM_APP_URL}/token/{token}", headers=headers, auth=auth)
|
||||
res = requests.delete(url=f"{HOST_ADDRESS}{SYSTEM_API_URL}/tokens/{token}", headers=headers, auth=auth)
|
||||
return res.json()
|
||||
|
||||
|
||||
|
||||
@@ -214,106 +214,6 @@ def test_status_branch_matrix_unit(monkeypatch):
|
||||
assert "Lost connection!" in res["data"]["redis"]["error"]
|
||||
assert res["data"]["task_executor_heartbeats"] == {}
|
||||
|
||||
|
||||
@pytest.mark.p2
|
||||
def test_healthz_and_oceanbase_status_matrix_unit(monkeypatch):
|
||||
module = _load_system_module(monkeypatch)
|
||||
|
||||
monkeypatch.setattr(module, "run_health_checks", lambda: ({"status": "ok"}, True))
|
||||
payload, status = module.healthz()
|
||||
assert status == 200
|
||||
assert payload["status"] == "ok"
|
||||
|
||||
monkeypatch.setattr(module, "run_health_checks", lambda: ({"status": "degraded"}, False))
|
||||
payload, status = module.healthz()
|
||||
assert status == 500
|
||||
assert payload["status"] == "degraded"
|
||||
|
||||
monkeypatch.setattr(module, "get_oceanbase_status", lambda: {"status": "alive", "latency_ms": 8})
|
||||
res = module.oceanbase_status()
|
||||
assert res["code"] == 0
|
||||
assert res["data"]["status"] == "alive"
|
||||
|
||||
monkeypatch.setattr(module, "get_oceanbase_status", lambda: (_ for _ in ()).throw(RuntimeError("ocean boom")))
|
||||
res = module.oceanbase_status()
|
||||
assert res["code"] == 500
|
||||
assert res["data"]["status"] == "error"
|
||||
assert "ocean boom" in res["data"]["message"]
|
||||
|
||||
|
||||
@pytest.mark.p2
|
||||
def test_system_token_routes_matrix_unit(monkeypatch):
|
||||
module = _load_system_module(monkeypatch)
|
||||
|
||||
monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [])
|
||||
res = module.new_token()
|
||||
assert res["message"] == "Tenant not found!"
|
||||
|
||||
monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(role="owner", tenant_id="tenant-1")])
|
||||
monkeypatch.setattr(module.APITokenService, "save", lambda **_kwargs: False)
|
||||
res = module.new_token()
|
||||
assert res["message"] == "Fail to new a dialog!"
|
||||
|
||||
monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: (_ for _ in ()).throw(RuntimeError("tenant query boom")))
|
||||
res = module.new_token()
|
||||
assert res["code"] == 100
|
||||
assert "tenant query boom" in res["message"]
|
||||
|
||||
monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [])
|
||||
res = module.token_list()
|
||||
assert res["message"] == "Tenant not found!"
|
||||
|
||||
class _Token:
|
||||
def __init__(self, token, beta):
|
||||
self.token = token
|
||||
self.beta = beta
|
||||
|
||||
def to_dict(self):
|
||||
return {"token": self.token, "beta": self.beta}
|
||||
|
||||
filter_updates = []
|
||||
monkeypatch.setattr(module, "generate_confirmation_token", lambda: "ragflow-abcdefghijklmnopqrstuvwxyz0123456789")
|
||||
monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(role="owner", tenant_id="tenant-9")])
|
||||
monkeypatch.setattr(module.APITokenService, "query", lambda **_kwargs: [_Token("tok-1", ""), _Token("tok-2", "beta-2")])
|
||||
monkeypatch.setattr(module.APITokenService, "filter_update", lambda conds, payload: filter_updates.append((conds, payload)))
|
||||
res = module.token_list()
|
||||
assert res["code"] == 0
|
||||
assert len(res["data"]) == 2
|
||||
assert len(res["data"][0]["beta"]) == 32
|
||||
assert res["data"][1]["beta"] == "beta-2"
|
||||
assert len(filter_updates) == 1
|
||||
|
||||
monkeypatch.setattr(
|
||||
module.APITokenService,
|
||||
"query",
|
||||
lambda **_kwargs: (_ for _ in ()).throw(RuntimeError("token list boom")),
|
||||
)
|
||||
res = module.token_list()
|
||||
assert res["code"] == 100
|
||||
assert "token list boom" in res["message"]
|
||||
|
||||
monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [])
|
||||
res = module.rm("tok-1")
|
||||
assert res["message"] == "Tenant not found!"
|
||||
|
||||
deleted = []
|
||||
monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(role="owner", tenant_id="tenant-3")])
|
||||
monkeypatch.setattr(module.APITokenService, "filter_delete", lambda conds: deleted.append(conds))
|
||||
res = module.rm("tok-1")
|
||||
assert res["code"] == 0
|
||||
assert res["data"] is True
|
||||
assert deleted
|
||||
|
||||
monkeypatch.setattr(
|
||||
module.APITokenService,
|
||||
"filter_delete",
|
||||
lambda _conds: (_ for _ in ()).throw(RuntimeError("delete boom")),
|
||||
)
|
||||
res = module.rm("tok-1")
|
||||
assert res["code"] == 100
|
||||
assert "delete boom" in res["message"]
|
||||
|
||||
|
||||
@pytest.mark.p2
|
||||
def test_get_config_returns_register_enabled_unit(monkeypatch):
|
||||
module = _load_system_module(monkeypatch)
|
||||
|
||||
Reference in New Issue
Block a user