|
| 1 | +"""Tests for Search V2 API route.""" |
| 2 | +import pytest |
| 3 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 4 | + |
| 5 | + |
| 6 | +class TestSearchV2Route: |
| 7 | + """Tests for /api/v1/search/v2 endpoint.""" |
| 8 | + |
| 9 | + @pytest.fixture |
| 10 | + def mock_auth(self): |
| 11 | + with patch("routes.search_v2.require_auth") as mock: |
| 12 | + mock.return_value = MagicMock(user_id="test-user") |
| 13 | + yield mock |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def mock_indexer(self): |
| 17 | + with patch("routes.search_v2.indexer") as mock: |
| 18 | + mock.search_v2 = AsyncMock(return_value=[ |
| 19 | + { |
| 20 | + "name": "authenticate", |
| 21 | + "qualified_name": "auth.authenticate", |
| 22 | + "file_path": "src/auth.py", |
| 23 | + "code": "def authenticate(): pass", |
| 24 | + "signature": "def authenticate() -> bool", |
| 25 | + "language": "python", |
| 26 | + "score": 0.95, |
| 27 | + "line_start": 10, |
| 28 | + "line_end": 20, |
| 29 | + "summary": "Authenticates user", |
| 30 | + "class_name": None, |
| 31 | + "match_reason": None, |
| 32 | + } |
| 33 | + ]) |
| 34 | + yield mock |
| 35 | + |
| 36 | + @pytest.fixture |
| 37 | + def mock_cache(self): |
| 38 | + with patch("routes.search_v2.cache") as mock: |
| 39 | + mock.get_search_results = MagicMock(return_value=None) |
| 40 | + mock.set_search_results = MagicMock() |
| 41 | + yield mock |
| 42 | + |
| 43 | + @pytest.fixture |
| 44 | + def mock_verify_access(self): |
| 45 | + with patch("routes.search_v2.verify_repo_access") as mock: |
| 46 | + yield mock |
| 47 | + |
| 48 | + @pytest.fixture |
| 49 | + def mock_metrics(self): |
| 50 | + with patch("routes.search_v2.metrics") as mock: |
| 51 | + mock.record_search = MagicMock() |
| 52 | + yield mock |
| 53 | + |
| 54 | + @pytest.mark.asyncio |
| 55 | + async def test_search_v2_returns_results( |
| 56 | + self, mock_auth, mock_indexer, mock_cache, mock_verify_access, mock_metrics |
| 57 | + ): |
| 58 | + from routes.search_v2 import search_v2, SearchV2Request |
| 59 | + from middleware.auth import AuthContext |
| 60 | + |
| 61 | + request = SearchV2Request( |
| 62 | + query="authentication", |
| 63 | + repo_id="test-repo", |
| 64 | + top_k=10, |
| 65 | + ) |
| 66 | + auth = AuthContext(user_id="test-user", email="test@test.com") |
| 67 | + |
| 68 | + response = await search_v2(request, auth) |
| 69 | + |
| 70 | + assert response.total == 1 |
| 71 | + assert response.search_version == "v2" |
| 72 | + assert response.cached is False |
| 73 | + assert response.results[0].name == "authenticate" |
| 74 | + |
| 75 | + @pytest.mark.asyncio |
| 76 | + async def test_search_v2_uses_cache( |
| 77 | + self, mock_auth, mock_indexer, mock_cache, mock_verify_access, mock_metrics |
| 78 | + ): |
| 79 | + from routes.search_v2 import search_v2, SearchV2Request |
| 80 | + from middleware.auth import AuthContext |
| 81 | + |
| 82 | + mock_cache.get_search_results.return_value = [ |
| 83 | + {"name": "cached_result", "qualified_name": "cached", "file_path": "x.py", |
| 84 | + "code": "", "signature": "", "language": "python", "score": 0.9, |
| 85 | + "line_start": 1, "line_end": 2} |
| 86 | + ] |
| 87 | + |
| 88 | + request = SearchV2Request(query="test", repo_id="repo", top_k=5) |
| 89 | + auth = AuthContext(user_id="test-user", email="test@test.com") |
| 90 | + |
| 91 | + response = await search_v2(request, auth) |
| 92 | + |
| 93 | + assert response.cached is True |
| 94 | + mock_indexer.search_v2.assert_not_called() |
| 95 | + |
| 96 | + @pytest.mark.asyncio |
| 97 | + async def test_search_v2_rejects_sql_injection(self, mock_auth, mock_verify_access): |
| 98 | + from routes.search_v2 import search_v2, SearchV2Request |
| 99 | + from middleware.auth import AuthContext |
| 100 | + from fastapi import HTTPException |
| 101 | + |
| 102 | + request = SearchV2Request(query="DROP TABLE users;--", repo_id="repo", top_k=10) |
| 103 | + auth = AuthContext(user_id="test-user", email="test@test.com") |
| 104 | + |
| 105 | + with pytest.raises(HTTPException) as exc: |
| 106 | + await search_v2(request, auth) |
| 107 | + |
| 108 | + assert exc.value.status_code == 400 |
| 109 | + |
| 110 | + @pytest.mark.asyncio |
| 111 | + async def test_search_v2_respects_top_k( |
| 112 | + self, mock_auth, mock_indexer, mock_cache, mock_verify_access, mock_metrics |
| 113 | + ): |
| 114 | + from routes.search_v2 import search_v2, SearchV2Request |
| 115 | + from middleware.auth import AuthContext |
| 116 | + |
| 117 | + request = SearchV2Request(query="test query", repo_id="repo", top_k=25) |
| 118 | + auth = AuthContext(user_id="test-user", email="test@test.com") |
| 119 | + |
| 120 | + await search_v2(request, auth) |
| 121 | + |
| 122 | + mock_indexer.search_v2.assert_called_once() |
| 123 | + call_args = mock_indexer.search_v2.call_args |
| 124 | + assert call_args.kwargs["top_k"] == 25 |
0 commit comments