Spaces:
Running
Running
| """Author RAG — Enterprise roadmap unit tests (R-139–R-173).""" | |
| import re | |
| from unittest.mock import AsyncMock, MagicMock, patch | |
| import pytest | |
| class TestQASanitize: | |
| def test_script_tags_stripped_on_import(self): | |
| from app.services.qa_service import _sanitize_qa_text | |
| raw = "Hello <script>alert(1)</script> world" | |
| assert "<script>" not in _sanitize_qa_text(raw) | |
| assert "Hello" in _sanitize_qa_text(raw) | |
| class TestCompetitorGuard: | |
| def test_competitor_mention_blocked(self): | |
| from app.services.guardrails import check_boundary | |
| vtype, _ = check_boundary("Is this better than ChatGPT for books?") | |
| assert vtype == "competitor_mention" | |
| class TestAnalyticsFunnel: | |
| async def test_funnel_counts_session_link_clicks(self, db_session, author_user, book_with_buy_url): | |
| from datetime import datetime, timezone | |
| from app.models.chat_session import ChatSession | |
| from app.models.base import generate_uuid | |
| from app.repositories.analytics_repo import AnalyticsRepository | |
| session = ChatSession( | |
| id=generate_uuid(), | |
| author_id=author_user.id, | |
| visitor_fingerprint="abc123", | |
| link_clicked=True, | |
| created_at=datetime.now(timezone.utc), | |
| ) | |
| db_session.add(session) | |
| await db_session.commit() | |
| repo = AnalyticsRepository(db_session) | |
| since = datetime.now(timezone.utc).replace(year=2000) | |
| counts = await repo.funnel_counts(author_user.id, since) | |
| assert counts["link_clicked"] >= 1 | |
| class TestJWTRefreshBlacklist: | |
| async def test_refresh_blacklists_old_jti(self): | |
| from app.core.access.jwt_blacklist import blacklist_token, is_blacklisted | |
| redis = AsyncMock() | |
| redis.setex = AsyncMock() | |
| redis.exists = AsyncMock(return_value=1) | |
| await blacklist_token(redis, "test-jti-abc", 3600) | |
| redis.setex.assert_called_once() | |
| assert await is_blacklisted(redis, "test-jti-abc") is True | |
| class TestAdminAnalyticsUI: | |
| def test_no_fake_multipliers_in_admin_template(self): | |
| from pathlib import Path | |
| html = Path("app/admin/templates/admin.html").read_text(encoding="utf-8") | |
| assert "* 0.7" not in html | |
| assert "* 0.15" not in html | |
| assert "analytics/funnel" in html | |