-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
172 lines (131 loc) · 6.66 KB
/
test.py
File metadata and controls
172 lines (131 loc) · 6.66 KB
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
#######################
## AI-generated test ##
#######################
import os
import base64
from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
from pathlib import Path
import os
import base64
from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
from pathlib import Path
def test_upload_and_retrieve_file(client, create_test_pdf_file):
"""
Test the complete flow: GET a file path -> Get its ID -> Verify we can retrieve it again
"""
test_pdf_path = create_test_pdf_file
auth_header = {}
credentials = base64.b64encode(b"test_user:test_pass").decode("ascii")
auth_header["Authorization"] = f"Basic {credentials}"
# Call GET endpoint with the TEST FILE PATH (not an ID)
response = client.get(f"/pdf/{test_pdf_path}", headers=auth_header)
print(f"GET response status: {response.status_code}")
print(f"GET response headers: {response.headers}")
print(f"Content length: {len(response.content)}")
assert response.status_code == 200, f"GET request failed: {response.text}"
assert response.headers.get("x-pdf-id") is not None, "Missing X-PDF-ID header"
pdf_id = response.headers.get("x-pdf-id")
first_temp_count = response.headers.get("x-temp-count", "0")
print(f"Retrieved PDF ID: {pdf_id}, Temp count: {first_temp_count}")
# Verify we got actual PDF content
assert len(response.content) > 0, "No PDF content returned"
def test_file_not_found(client):
"""Test GET with non-existing file path"""
auth_header = {}
credentials = base64.b64encode(b"test_user:test_pass").decode("ascii")
auth_header["Authorization"] = f"Basic {credentials}"
fake_path = "/this/does/not/exist.pdf"
response = client.get(f"/pdf/{fake_path}", headers=auth_header)
assert response.status_code == 404
assert "file not found" in response.json().get("detail", "")
def test_save_pdf_changes(client, create_test_pdf_file, sample_pdf_content):
"""
Test uploading file replacements via PUT endpoint
GET requires file PATH, but PUT requires PDF ID from the response header
"""
# Create a test file
test_pdf_path = create_test_pdf_file
auth_header = {}
credentials = base64.b64encode(b"test_user:test_pass").decode("ascii")
auth_header["Authorization"] = f"Basic {credentials}"
# FIRST: Access file by PATH to get its ID
get_response = client.get(f"/pdf/{test_pdf_path}", headers=auth_header)
assert get_response.status_code == 200
pdf_id = get_response.headers.get("x-pdf-id")
assert pdf_id is not None
print(f"Original PDF at {test_pdf_path} has ID: {pdf_id}")
# Use the SAME PATH for the test file - it must exist on filesystem
# NOW: Make PUT request with the retrieved ID
modified_content = sample_pdf_content + b'\n% Modified during test\n'
files = {
'file': ('modified.pdf', modified_content, 'application/pdf')
}
put_response = client.put(f"/pdf/{pdf_id}", headers=auth_header, files=files)
print(f"PUT response status: {put_response.status_code}")
print(f"PUT response JSON: {put_response.json()}")
assert put_response.status_code == 200, f"PUT request failed: {put_response.text}"
# Verify response matches expected PDFStatus model format
json_data = put_response.json()
assert 'pdf_id' in json_data
assert 'file_path' in json_data
assert 'last_temp_path' in json_data
assert 'temp_count' in json_data
# The ID should be the same as when we got it initially
assert str(json_data['pdf_id']) == pdf_id
# But file_path should be the original test file we started with
assert json_data['file_path'] == test_pdf_path
def test_put_with_invalid_id(client, sample_pdf_content):
"""Test PUT with non-existent PDF ID"""
auth_header = {}
credentials = base64.b64encode(b"test_user:test_pass").decode("ascii")
auth_header["Authorization"] = f"Basic {credentials}"
files = {
'file': ('test_upload.pdf', sample_pdf_content, 'application/pdf')
}
# Use an ID that definitely doesn't exist
response = client.put("/pdf/999999", headers=auth_header, files=files)
assert response.status_code == 404
assert "PDF ID not found" in response.json().get("detail", "")
def test_basic_auth_protection(client, create_test_pdf_file, sample_pdf_content):
"""Verify endpoints require authentication"""
test_pdf_path = create_test_pdf_file
# Try GET without auth
without_auth_get = client.get(f"/pdf/{test_pdf_path}")
assert without_auth_get.status_code == 401
# Try PUT without auth
files = {'file': ('test.pdf', sample_pdf_content, 'application/pdf')}
without_auth_put = client.put("/pdf/1", files=files)
assert without_auth_put.status_code == 401
def test_end_to_end_workflow(client, create_test_pdf_file, sample_pdf_content):
"""
Test complete workflow: access file path -> upload changes ->
verify the original file path still works and the count increased
"""
test_pdf_path = create_test_pdf_file
auth_header = {}
credentials = base64.b64encode(b"test_user:test_pass").decode("ascii")
auth_header["Authorization"] = f"Basic {credentials}"
# Step 1: Access file to get ID and register it (using PATH, not ID)
get1_response = client.get(f"/pdf/{test_pdf_path}", headers=auth_header)
assert get1_response.status_code == 200
pdf_id = get1_response.headers.get("x-pdf-id")
first_temp_count = get1_response.headers.get("x-temp-count", "0")
print(f"Initial: Path={test_pdf_path}, ID={pdf_id}, Temp Count={int(first_temp_count)}")
# Step 2: Upload a modification using the PDF ID
modified_content = sample_pdf_content + b'\n% First modification\n'
files = {'file': ('mod1.pdf', modified_content, 'application/pdf')}
put_response = client.put(f"/pdf/{pdf_id}", headers=auth_header, files=files)
assert put_response.status_code == 200
# Step 3: Access via the same ORIGINAL FILE PATH again to see incremented count
# This SHOULD work because the PUT updated the original file at test_pdf_path
get2_response = client.get(f"/pdf/{test_pdf_path}", headers=auth_header) # SAME PATH as before
assert get2_response.status_code == 200
second_temp_count = get2_response.headers.get("x-temp-count", "0")
print(f"After mod: Path={test_pdf_path}, ID={get2_response.headers.get('x-pdf-id')}, Temp Count={int(second_temp_count)}")
# Temp count should have incremented due to the change
assert int(second_temp_count) == int(first_temp_count) + 1
print("✓ Workflow completed successfully - temp count increased as expected!")