File size: 2,826 Bytes
dc4e6da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Functional tests β€” Health endpoints
======================================
Tests for:
  GET /      β†’ HealthResponse schema
  GET /health β†’ HealthResponse schema
"""
import pytest
import requests
from tests.conftest import BASE_URL, TIMEOUT


class TestRootEndpoint:
    """GET / β€” root health check."""

    def test_root_returns_200(self, http, base_url):
        r = http.get(f"{base_url}/", timeout=TIMEOUT)
        assert r.status_code == 200, f"Expected 200, got {r.status_code}: {r.text}"

    def test_root_content_type_is_json(self, http, base_url):
        r = http.get(f"{base_url}/", timeout=TIMEOUT)
        assert "application/json" in r.headers.get("Content-Type", "")

    def test_root_returns_healthy(self, root_response):
        data = root_response.json()
        assert data.get("status") == "healthy", f"Unexpected status: {data}"

    def test_root_response_has_version(self, root_response):
        data = root_response.json()
        assert "version" in data, "Response missing 'version' field"
        assert isinstance(data["version"], str)
        assert len(data["version"]) > 0

    def test_root_schema_contract(self, root_response):
        """Response must exactly match HealthResponse schema: {status, version}."""
        data = root_response.json()
        assert set(data.keys()) >= {"status", "version"}, (
            f"Missing required fields. Got: {set(data.keys())}"
        )


class TestHealthEndpoint:
    """GET /health β€” dedicated health check."""

    def test_health_returns_200(self, http, base_url):
        r = http.get(f"{base_url}/health", timeout=TIMEOUT)
        assert r.status_code == 200, f"Expected 200, got {r.status_code}: {r.text}"

    def test_health_content_type_is_json(self, http, base_url):
        r = http.get(f"{base_url}/health", timeout=TIMEOUT)
        assert "application/json" in r.headers.get("Content-Type", "")

    def test_health_returns_healthy_status(self, health_response):
        data = health_response.json()
        assert data.get("status") == "healthy", f"Unexpected status: {data}"

    def test_health_response_has_version(self, health_response):
        data = health_response.json()
        assert "version" in data
        assert isinstance(data["version"], str)

    def test_health_schema_contract(self, health_response):
        data = health_response.json()
        assert "status" in data
        assert "version" in data

    def test_health_and_root_agree(self, http, base_url):
        """Both endpoints must report the same status and version."""
        r_root   = http.get(f"{base_url}/",       timeout=TIMEOUT).json()
        r_health = http.get(f"{base_url}/health",  timeout=TIMEOUT).json()
        assert r_root["status"]  == r_health["status"]
        assert r_root["version"] == r_health["version"]