File size: 3,348 Bytes
e4e1bf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# Set env vars for testing before importing anything
os.environ["DATABASE_URL"] = "sqlite:///./test_lexai.db"
os.environ["NVIDIA_API_KEY"] = "" # run in simulation mode

# Add current directory to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from main import app
from database.connection import Base, get_db

# Create test database engine
test_engine = create_engine("sqlite:///./test_lexai.db", connect_args={"check_same_thread": False})
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=test_engine)

# Clean and rebuild test tables
Base.metadata.drop_all(bind=test_engine)
Base.metadata.create_all(bind=test_engine)

# Database override dependency
def override_get_db():
    try:
        db = TestingSessionLocal()
        yield db
    finally:
        db.close()

app.dependency_overrides[get_db] = override_get_db

client = TestClient(app)

def test_health_check():
    response = client.get("/")
    assert response.status_code == 200
    assert response.json()["status"] == "online"

def test_auth_flow():
    # 1. Register User
    reg_response = client.post(
        "/auth/register",
        json={"email": "teststudent@lexai.com", "password": "securepassword123"}
    )
    assert reg_response.status_code == 201
    assert reg_response.json()["email"] == "teststudent@lexai.com"
    
    # 2. Duplicate registration should fail
    dup_response = client.post(
        "/auth/register",
        json={"email": "teststudent@lexai.com", "password": "differentpwd"}
    )
    assert dup_response.status_code == 400

    # 3. Get JWT Token
    login_response = client.post(
        "/auth/token",
        data={"username": "teststudent@lexai.com", "password": "securepassword123"}
    )
    assert login_response.status_code == 200
    assert "access_token" in login_response.json()
    token = login_response.json()["access_token"]

    # 4. Access protected route
    headers = {"Authorization": f"Bearer {token}"}
    me_response = client.get("/auth/me", headers=headers)
    assert me_response.status_code == 200
    assert me_response.json()["email"] == "teststudent@lexai.com"

def test_ai_solver_simulation():
    # Login to get token
    login_response = client.post(
        "/auth/token",
        data={"username": "teststudent@lexai.com", "password": "securepassword123"}
    )
    token = login_response.json()["access_token"]
    headers = {"Authorization": f"Bearer {token}"}

    # Ask a doubt solver question (simulation mode since NVIDIA_API_KEY is empty)
    chat_response = client.post(
        "/ai/chat",
        json={"message": "Explain the concept of Consideration under Indian Contract Act.", "use_rag": True},
        headers=headers
    )
    assert chat_response.status_code == 200
    assert "Simulation Mode" in chat_response.json()["response"]
    assert "sources" in chat_response.json()

if __name__ == "__main__":
    # If run directly, execute pytest on itself
    import subprocess
    print("Installing test runner dependencies...")
    subprocess.run([sys.executable, "-m", "pip", "install", "pytest", "httpx"])
    
    import pytest
    print("\nRunning test suite...")
    sys.exit(pytest.main([__file__, "-v"]))