File size: 7,707 Bytes
8397f09
 
 
 
 
 
 
 
 
 
 
 
 
718633d
8397f09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import pytest
import json
import os
import sys
import tempfile
import shutil
from fpdf import FPDF
import uuid

# Add the parent directory to Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from app import create_app
from backend.app.database import init_db

@pytest.fixture
def app():
    app = create_app({
        'TESTING': True,
        'JWT_SECRET_KEY': 'test-secret-key',
        'DATABASE': ':memory:'
    })
    with app.app_context():
        init_db()
    return app

@pytest.fixture
def client(app):
    return app.test_client()

@pytest.fixture
def auth_headers(client):
    # Register a test user with unique username
    unique_username = f"testuser_{uuid.uuid4().hex[:8]}"
    register_response = client.post('/register', json={
        'username': unique_username,
        'password': 'testpass'
    })
    assert register_response.status_code == 201, "User registration failed"

    # Login to get token
    login_response = client.post('/login', json={
        'username': unique_username,
        'password': 'testpass'
    })
    assert login_response.status_code == 200, "Login failed"
    assert 'access_token' in login_response.json, "No access token in response"
    
    token = login_response.json['access_token']
    return {'Authorization': f'Bearer {token}'}

def create_test_pdf():
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)
    
    # Add more content to make it a realistic document
    pdf.cell(200, 10, txt="Legal Document Analysis", ln=1, align="C")
    pdf.cell(200, 10, txt="This is a test document for legal processing.", ln=1, align="C")
    pdf.cell(200, 10, txt="Section 1: Introduction", ln=1, align="L")
    pdf.cell(200, 10, txt="This document contains various legal clauses and provisions.", ln=1, align="L")
    pdf.cell(200, 10, txt="Section 2: Main Provisions", ln=1, align="L")
    pdf.cell(200, 10, txt="The main provisions of this agreement include confidentiality clauses,", ln=1, align="L")
    pdf.cell(200, 10, txt="intellectual property rights, and dispute resolution mechanisms.", ln=1, align="L")
    pdf.cell(200, 10, txt="Section 3: Conclusion", ln=1, align="L")
    pdf.cell(200, 10, txt="This document serves as a comprehensive legal agreement.", ln=1, align="L")
    
    pdf.output("test.pdf")
    return "test.pdf"

# Authentication Tests
def test_register_success(client):
    unique_username = f"newuser_{uuid.uuid4().hex[:8]}"
    response = client.post('/register', json={
        'username': unique_username,
        'password': 'newpass'
    })
    assert response.status_code == 201
    assert response.json['message'] == "User registered successfully"

def test_register_duplicate_username(client):
    # First registration
    username = f"duplicate_{uuid.uuid4().hex[:8]}"
    client.post('/register', json={
        'username': username,
        'password': 'pass1'
    })
    # Second registration with same username
    response = client.post('/register', json={
        'username': username,
        'password': 'pass2'
    })
    assert response.status_code == 409
    assert 'error' in response.json

def test_login_success(client):
    # Register first
    username = f"loginuser_{uuid.uuid4().hex[:8]}"
    client.post('/register', json={
        'username': username,
        'password': 'loginpass'
    })
    # Then login
    response = client.post('/login', json={
        'username': username,
        'password': 'loginpass'
    })
    assert response.status_code == 200
    assert 'access_token' in response.json

def test_login_invalid_credentials(client):
    response = client.post('/login', json={
        'username': 'nonexistent',
        'password': 'wrongpass'
    })
    assert response.status_code == 401
    assert 'error' in response.json

# Document Upload Tests
def test_upload_success(client, auth_headers):
    pdf_path = create_test_pdf()
    try:
        with open(pdf_path, 'rb') as f:
            response = client.post('/upload',
                data={'file': (f, 'test.pdf')},
                headers=auth_headers,
                content_type='multipart/form-data'
            )
        assert response.status_code == 200
        assert response.json['success'] == True
        assert 'document_id' in response.json
    finally:
        os.unlink(pdf_path)

def test_upload_no_file(client, auth_headers):
    response = client.post('/upload', headers=auth_headers)
    assert response.status_code == 400
    assert 'error' in response.json

def test_upload_unauthorized(client):
    response = client.post('/upload')
    assert response.status_code == 401

# Document Retrieval Tests
def test_list_documents_success(client, auth_headers):
    response = client.get('/documents', headers=auth_headers)
    assert response.status_code == 200
    assert isinstance(response.json, list)

def test_list_documents_unauthorized(client):
    response = client.get('/documents')
    assert response.status_code == 401

def test_get_document_success(client, auth_headers):
    # First upload a document
    pdf_path = create_test_pdf()
    try:
        with open(pdf_path, 'rb') as f:
            upload_response = client.post('/upload',
                data={'file': (f, 'test.pdf')},
                headers=auth_headers,
                content_type='multipart/form-data'
            )
        doc_id = upload_response.json['document_id']
        
        # Then retrieve it
        response = client.get(f'/get_document/{doc_id}', headers=auth_headers)
        assert response.status_code == 200
        assert response.json['id'] == doc_id
    finally:
        os.unlink(pdf_path)

def test_get_document_not_found(client, auth_headers):
    response = client.get('/get_document/99999', headers=auth_headers)
    assert response.status_code == 404

# Search Tests
def test_search_success(client, auth_headers):
    response = client.get('/search_documents?q=test', headers=auth_headers)
    assert response.status_code == 200
    assert 'results' in response.json

def test_search_no_query(client, auth_headers):
    response = client.get('/search_documents', headers=auth_headers)
    assert response.status_code == 400

# QA Tests
def test_qa_success(client, auth_headers):
    # First upload a document
    pdf_path = create_test_pdf()
    try:
        with open(pdf_path, 'rb') as f:
            upload_response = client.post('/upload',
                data={'file': (f, 'test.pdf')},
                headers=auth_headers,
                content_type='multipart/form-data'
            )
        doc_id = upload_response.json['document_id']
        
        # Then ask a question
        response = client.post('/qa',
            json={
                'document_id': doc_id,
                'question': 'What is this document about?'
            },
            headers=auth_headers
        )
        assert response.status_code == 200
        assert 'answer' in response.json
    finally:
        os.unlink(pdf_path)

def test_qa_missing_fields(client, auth_headers):
    response = client.post('/qa',
        json={'document_id': 1},
        headers=auth_headers
    )
    assert response.status_code == 400

# Document Processing Tests
def test_process_document_success(client):
    response = client.post('/process_document',
        json={'text': 'Test legal document content'}
    )
    assert response.status_code == 200
    assert 'processed' in response.json
    assert 'features' in response.json
    assert 'context_analysis' in response.json

def test_process_document_empty_text(client):
    response = client.post('/process_document',
        json={'text': ''}
    )
    assert response.status_code == 400