feat(avatar): add BOXIM chat takeover
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
"""Tests for the authorization takeover configuration endpoint."""
|
||||
"""Tests for takeover configuration and BOXIM connection status."""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from database import SessionLocal
|
||||
from main import app
|
||||
from models import Authorization
|
||||
from models import Authorization, TakeoverCursor, TakeoverReplyTask, User
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
@@ -123,3 +125,81 @@ def test_takeover_is_limited_to_active_user_authorizations(authorization_context
|
||||
).json()
|
||||
assert response["code"] == 400
|
||||
assert "单聊接管" in response["message"]
|
||||
|
||||
|
||||
def test_takeover_status_reports_disabled_and_requires_owner_login(authorization_context):
|
||||
context = authorization_context
|
||||
endpoint = f"/api/avatar/{context['avatar'].id}/takeover/status"
|
||||
|
||||
disabled = client.get(endpoint, headers=context["owner_headers"])
|
||||
assert disabled.status_code == 200
|
||||
assert disabled.json()["data"]["status"] == "disabled"
|
||||
|
||||
client.put(
|
||||
f"/api/avatar/{context['avatar'].id}/permission-settings",
|
||||
headers=context["owner_headers"],
|
||||
json={"permissions": ["chat", "takeover"]},
|
||||
)
|
||||
needs_login = client.get(endpoint, headers=context["owner_headers"]).json()["data"]
|
||||
assert needs_login["enabled"] is True
|
||||
assert needs_login["status"] == "needs_login"
|
||||
assert "BOXIM" in needs_login["message"]
|
||||
|
||||
assert client.get(endpoint).status_code == 401
|
||||
assert client.get(endpoint, headers=context["other_headers"]).status_code == 403
|
||||
|
||||
|
||||
def test_takeover_status_reports_ready_pending_count_and_errors(authorization_context):
|
||||
context = authorization_context
|
||||
avatar_id = context["avatar"].id
|
||||
endpoint = f"/api/avatar/{avatar_id}/takeover/status"
|
||||
client.put(
|
||||
f"/api/avatar/{avatar_id}/permission-settings",
|
||||
headers=context["owner_headers"],
|
||||
json={"permissions": ["chat", "takeover"]},
|
||||
)
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
owner = db.query(User).filter(User.id == context["owner"].id).one()
|
||||
owner.huihui_token = "production-login-token"
|
||||
cursor = TakeoverCursor(
|
||||
avatar_id=avatar_id,
|
||||
owner_id=owner.huihui_user_id,
|
||||
boxim_owner_id="100",
|
||||
last_message_id="10",
|
||||
initialized=True,
|
||||
last_polled_at=datetime.utcnow(),
|
||||
)
|
||||
task = TakeoverReplyTask(
|
||||
avatar_id=avatar_id,
|
||||
owner_id=owner.huihui_user_id,
|
||||
peer_id="200",
|
||||
trigger_message_id="11",
|
||||
source_message_ids=["11"],
|
||||
prompt="你好",
|
||||
status="pending",
|
||||
scheduled_at=datetime.utcnow(),
|
||||
boxim_local_id="123",
|
||||
)
|
||||
db.add_all([cursor, task])
|
||||
db.commit()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
ready = client.get(endpoint, headers=context["owner_headers"]).json()["data"]
|
||||
assert ready["status"] == "ready"
|
||||
assert ready["pendingCount"] == 1
|
||||
assert ready["lastPolledAt"]
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
cursor = db.query(TakeoverCursor).filter(TakeoverCursor.avatar_id == avatar_id).one()
|
||||
cursor.last_error = "BOXIM 暂时不可用"
|
||||
db.commit()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
failed = client.get(endpoint, headers=context["owner_headers"]).json()["data"]
|
||||
assert failed["status"] == "error"
|
||||
assert failed["message"] == "BOXIM 暂时不可用"
|
||||
|
||||
Reference in New Issue
Block a user