49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
"""Tests for routes."""
|
|
|
|
import pytest
|
|
|
|
from app import create_app
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
"""Create a test client for the Flask application."""
|
|
app = create_app()
|
|
app.config["TESTING"] = True
|
|
with app.test_client() as client:
|
|
yield client
|
|
|
|
|
|
class TestMainRoutes:
|
|
"""Test cases for main routes."""
|
|
|
|
def test_index_route(self, client) -> None:
|
|
"""Test the index route."""
|
|
response = client.get("/api/")
|
|
assert response.status_code == 200
|
|
assert response.get_json() == {"message": "Hello from backend!"}
|
|
|
|
def test_hello_route_without_name(self, client) -> None:
|
|
"""Test hello route without name parameter."""
|
|
response = client.get("/api/hello")
|
|
assert response.status_code == 200
|
|
assert response.get_json() == {"message": "Hello from backend!"}
|
|
|
|
def test_hello_route_with_name(self, client) -> None:
|
|
"""Test hello route with name parameter."""
|
|
response = client.get("/api/hello/Alice")
|
|
assert response.status_code == 200
|
|
assert response.get_json() == {"message": "Hello, Alice!"}
|
|
|
|
def test_health_route(self, client) -> None:
|
|
"""Test health check route."""
|
|
response = client.get("/api/health")
|
|
assert response.status_code == 200
|
|
assert response.get_json() == {"status": "ok"}
|
|
|
|
def test_protected_route_without_auth(self, client) -> None:
|
|
"""Test protected route without authentication."""
|
|
response = client.get("/api/protected")
|
|
assert response.status_code == 401
|
|
data = response.get_json()
|
|
assert data["error"] == "Authentication required" |