File size: 3,043 Bytes
243b4bc | 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 | import json
import pytest
def test_api_health_check(api_client):
"""Test that the health check endpoint returns 200 and success."""
response = api_client.get("/api/v1/common/health")
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["message"] == "Server is fit and fine"
def test_api_available_time(api_client):
"""Test that the available_time endpoint returns the list of session durations."""
response = api_client.get("/api/v1/common/available_time")
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert isinstance(data["data"], list)
assert len(data["data"]) > 0
def test_api_login_sets_cookie(api_client):
"""Test that login route sets cookie thread_id."""
response = api_client.get("/api/v1/user/login/2")
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert "thread_id" in response.cookies
assert response.cookies["thread_id"] == data["data"]
def test_api_chat_unauthorized(api_client):
"""Test that chat endpoint rejects requests without session cookie."""
response = api_client.post(
"/api/v1/agent/chat",
data={"body": json.dumps({"thread_id": "test-thread", "message": "hello"})}
)
assert response.status_code == 401
assert response.json()["detail"]["success"] is False
def test_api_chat_authorized(api_client):
"""Test that chat endpoint accepts requests with cookie and streams mock response."""
# First login to get a cookie
login_res = api_client.get("/api/v1/user/login/2")
thread_id = login_res.json()["data"]
# Post chat request with cookie
response = api_client.post(
"/api/v1/agent/chat",
data={"body": json.dumps({"thread_id": thread_id, "message": "Recommend some shoes"})},
cookies={"thread_id": thread_id}
)
assert response.status_code == 200
assert "text/event-stream" in response.headers["content-type"]
# Read streamed chunks
content = response.text
assert "Mocked agent response" in content
def test_api_train_unauthorized(api_client):
"""Test that train endpoint rejects requests without session cookie."""
response = api_client.get("/api/v1/model/train")
assert response.status_code == 401
assert response.json()["detail"]["success"] is False
def test_api_train_authorized(api_client):
"""Test that train endpoint initiates model training when authorized."""
# First login to get a cookie
login_res = api_client.get("/api/v1/user/login/2")
thread_id = login_res.json()["data"]
# Request train with cookie
response = api_client.get(
"/api/v1/model/train",
cookies={"thread_id": thread_id}
)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["message"] == "model Trained Succesfully"
assert data["data"]["model_path"] == "dummy_model.pt"
|