diff --git a/tests/test_tutorial/test_handling_errors/test_tutorial007.py b/tests/test_tutorial/test_handling_errors/test_tutorial007.py new file mode 100644 index 000000000..19d702aab --- /dev/null +++ b/tests/test_tutorial/test_handling_errors/test_tutorial007.py @@ -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"} diff --git a/tests/test_tutorial/test_handling_errors/test_tutorial008.py b/tests/test_tutorial/test_handling_errors/test_tutorial008.py new file mode 100644 index 000000000..d64ad887d --- /dev/null +++ b/tests/test_tutorial/test_handling_errors/test_tutorial008.py @@ -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("") + 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("" * 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("") + 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!", + }