2 changed files with 86 additions and 0 deletions
@ -0,0 +1,27 @@ |
|||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from docs_src.handling_errors.tutorial007 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
|
||||
|
def test_unauthenticated(): |
||||
|
response = client.get("/secrets") |
||||
|
assert response.status_code == 401, response.text |
||||
|
assert response.json() == { |
||||
|
"detail": "Access denied. Check your credentials or permissions." |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_unauthorized(): |
||||
|
response = client.get("/secrets", params={"auth_user_id": 1}) |
||||
|
assert response.status_code == 403, response.text |
||||
|
assert response.json() == { |
||||
|
"detail": "Access denied. Check your credentials or permissions." |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_success(): |
||||
|
response = client.get("/secrets", params={"auth_user_id": 0}) |
||||
|
assert response.status_code == 200, response.text |
||||
|
assert response.json() == {"data": "Secret information"} |
@ -0,0 +1,59 @@ |
|||||
|
from pathlib import Path |
||||
|
from unittest.mock import patch |
||||
|
|
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from docs_src.handling_errors import tutorial008 |
||||
|
from docs_src.handling_errors.tutorial008 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
|
||||
|
def test_unsupported_file_type(tmp_path: Path): |
||||
|
file = tmp_path / "test.txt" |
||||
|
file.write_text("<file content>") |
||||
|
with open(file, "+rb") as fp: |
||||
|
response = client.post( |
||||
|
"/upload", |
||||
|
files={"file": ("test.txt", fp, "text/plain")}, |
||||
|
) |
||||
|
assert response.status_code == 415, response.text |
||||
|
assert response.json() == { |
||||
|
"error": "Unsupported file type", |
||||
|
"hint": "Need help? Contact support@example.com", |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_file_too_large(tmp_path: Path): |
||||
|
file = tmp_path / "test.pdf" |
||||
|
file.write_text("<file content>" * 100) # ~1.37 kB |
||||
|
with patch.object( |
||||
|
tutorial008, |
||||
|
"MAX_FILE_SIZE_MB", |
||||
|
new=0.001, # MAX_FILE_SIZE_MB = 1 kB |
||||
|
): |
||||
|
with open(file, "+rb") as fp: |
||||
|
response = client.post( |
||||
|
"/upload", |
||||
|
files={"file": ("test.pdf", fp, "application/pdf")}, |
||||
|
) |
||||
|
assert response.status_code == 413, response.text |
||||
|
assert response.json() == { |
||||
|
"error": "The uploaded file is too large.", |
||||
|
"hint": "Need help? Contact support@example.com", |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_success(tmp_path: Path): |
||||
|
file = tmp_path / "test.pdf" |
||||
|
file.write_text("<file content>") |
||||
|
with open(file, "+rb") as fp: |
||||
|
response = client.post( |
||||
|
"/upload", |
||||
|
files={"file": ("test.pdf", fp, "application/pdf")}, |
||||
|
) |
||||
|
assert response.status_code == 200, response.text |
||||
|
assert response.json() == { |
||||
|
"filename": "test.pdf", |
||||
|
"message": "File uploaded successfully!", |
||||
|
} |
Loading…
Reference in new issue