Spaces:
Sleeping
Sleeping
File size: 5,520 Bytes
5eebd59 | 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 | """
Tests for API endpoints.
"""
import pytest
from fastapi.testclient import TestClient
import os
import sys
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
class TestHealthEndpoints:
"""Test cases for health check endpoints."""
@pytest.fixture
def client(self):
"""Create a test client."""
from main import app
return TestClient(app)
def test_health_check(self, client):
"""Test the health check endpoint."""
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "healthy"
assert "timestamp" in data
def test_root_endpoint(self, client):
"""Test the root endpoint returns service info."""
response = client.get("/")
assert response.status_code == 200
data = response.json()
assert "service" in data
assert "version" in data
assert "endpoints" in data
class TestTriageEndpoint:
"""Test cases for the triage endpoint."""
@pytest.fixture
def client(self):
"""Create a test client."""
from main import app
return TestClient(app)
def test_triage_requires_auth(self, client):
"""Test that triage endpoint requires authentication."""
response = client.post(
"/triage",
json={
"title": "Test Issue",
"body": "This is a test issue body",
}
)
# Should require auth
assert response.status_code in [401, 422] # 422 if validation runs first
def test_triage_with_auth(self, client, auth_headers):
"""Test triage endpoint with valid authentication."""
response = client.post(
"/triage",
json={
"title": "Bug: Application crashes on startup",
"body": "The application crashes when I try to start it.",
"authorName": "testuser",
"isPR": False,
},
headers=auth_headers
)
# May fail due to AI service, but should not be 401
assert response.status_code != 401
class TestChatEndpoint:
"""Test cases for the chat endpoint."""
@pytest.fixture
def client(self):
"""Create a test client."""
from main import app
return TestClient(app)
def test_chat_requires_auth(self, client):
"""Test that chat endpoint requires authentication."""
response = client.post(
"/chat",
json={
"message": "Hello, how can you help me?",
}
)
assert response.status_code in [401, 422]
def test_chat_with_auth(self, client, auth_headers):
"""Test chat endpoint with valid authentication."""
response = client.post(
"/chat",
json={
"message": "Hello, how can you help me?",
},
headers=auth_headers
)
# May fail due to AI service, but should not be 401
assert response.status_code != 401
class TestRAGEndpoints:
"""Test cases for RAG chatbot endpoints."""
@pytest.fixture
def client(self):
"""Create a test client."""
from main import app
return TestClient(app)
def test_rag_chat_requires_auth(self, client):
"""Test that RAG chat endpoint requires authentication."""
response = client.post(
"/rag/chat",
json={
"question": "How does the authentication work?",
}
)
assert response.status_code in [401, 422]
def test_rag_index_requires_auth(self, client):
"""Test that RAG index endpoint requires authentication."""
response = client.post(
"/rag/index",
json={
"repo_name": "facebook/react",
}
)
assert response.status_code in [401, 422]
def test_rag_suggestions_public(self, client):
"""Test that RAG suggestions endpoint is publicly accessible."""
response = client.get("/rag/suggestions")
# Should not require auth
assert response.status_code != 401
class TestMentorMatchEndpoint:
"""Test cases for mentor matching endpoint."""
@pytest.fixture
def client(self):
"""Create a test client."""
from main import app
return TestClient(app)
def test_mentor_match_requires_auth(self, client):
"""Test that mentor match endpoint requires authentication."""
response = client.post(
"/mentor-match",
json={
"user_id": "user-123",
"username": "testuser",
}
)
assert response.status_code in [401, 422]
class TestHypeEndpoint:
"""Test cases for hype generator endpoint."""
@pytest.fixture
def client(self):
"""Create a test client."""
from main import app
return TestClient(app)
def test_hype_requires_auth(self, client):
"""Test that hype endpoint requires authentication."""
response = client.post(
"/hype",
json={
"pr_title": "Add new feature",
"additions": 100,
"deletions": 10,
}
)
assert response.status_code in [401, 422]
|