forked from AntonioMrtz/SpotifyElectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest__health.py
149 lines (123 loc) · 5.31 KB
/
test__health.py
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
from fastapi.testclient import TestClient
from pytest import fixture
from starlette.status import HTTP_200_OK, HTTP_503_SERVICE_UNAVAILABLE
from app.__main__ import app
client = TestClient(app)
@fixture(scope="module", autouse=True)
def set_up(trigger_app_startup):
"""Setup fixture to initialize the app."""
pass
def mock_check_database_connection():
"""Mock database connection check."""
return {"status": "healthy", "message": "Mocked database connection is active"}
def mock_check_song_service():
"""Mock song service check."""
return {"status": "healthy", "message": "Mocked SongService is properly initialized"}
def mock_check_auth_config():
"""Mock auth config check."""
return {
"status": "healthy",
"message": "Mocked Auth configuration is properly initialized",
}
def test_health_check_all_healthy(mocker):
"""Test health check endpoint when all components are healthy."""
# Mock dependencies
mocker.patch(
"app.spotify_electron.health.health_controller.check_database_connection",
return_value=mock_check_database_connection(),
)
mocker.patch(
"app.spotify_electron.health.health_controller.check_song_service",
return_value=mock_check_song_service(),
)
mocker.patch(
"app.spotify_electron.health.health_controller.check_auth_config",
return_value=mock_check_auth_config(),
)
response = client.get("/health/")
assert response.status_code == HTTP_200_OK
response_json = response.json()
assert response_json["status"] == "healthy"
assert response_json["details"]["database"]["status"] == "healthy"
assert response_json["details"]["song_service"]["status"] == "healthy"
assert response_json["details"]["auth_config"]["status"] == "healthy"
def test_health_check_unhealthy_database(mocker):
"""Test health check endpoint when the database connection is unhealthy."""
# Mock dependencies
mocker.patch(
"app.spotify_electron.health.health_controller.check_database_connection",
return_value={"status": "unhealthy", "message": "Mocked database connection failed"},
)
mocker.patch(
"app.spotify_electron.health.health_controller.check_song_service",
return_value=mock_check_song_service(),
)
mocker.patch(
"app.spotify_electron.health.health_controller.check_auth_config",
return_value=mock_check_auth_config(),
)
response = client.get("/health/")
assert response.status_code == HTTP_503_SERVICE_UNAVAILABLE
response_json = response.json()
assert response_json["status"] == "unhealthy"
assert response_json["details"]["database"]["status"] == "unhealthy"
assert (
response_json["details"]["database"]["message"] == "Mocked database connection failed"
)
assert response_json["details"]["song_service"]["status"] == "healthy"
assert response_json["details"]["auth_config"]["status"] == "healthy"
def test_health_check_unhealthy_song_service(mocker):
"""Test health check endpoint when the SongService is unhealthy."""
# Mock dependencies
mocker.patch(
"app.spotify_electron.health.health_controller.check_database_connection",
return_value=mock_check_database_connection(),
)
mocker.patch(
"app.spotify_electron.health.health_controller.check_song_service",
return_value={
"status": "unhealthy",
"message": "Mocked SongService initialization failed",
},
)
mocker.patch(
"app.spotify_electron.health.health_controller.check_auth_config",
return_value=mock_check_auth_config(),
)
response = client.get("/health/")
assert response.status_code == HTTP_503_SERVICE_UNAVAILABLE
response_json = response.json()
assert response_json["status"] == "unhealthy"
assert response_json["details"]["song_service"]["status"] == "unhealthy"
assert (
response_json["details"]["song_service"]["message"]
== "Mocked SongService initialization failed"
)
assert response_json["details"]["database"]["status"] == "healthy"
assert response_json["details"]["auth_config"]["status"] == "healthy"
def test_health_check_unhealthy_auth_config(mocker):
"""Test health check endpoint when the Auth configuration is unhealthy."""
# Mock dependencies
mocker.patch(
"app.spotify_electron.health.health_controller.check_database_connection",
return_value=mock_check_database_connection(),
)
mocker.patch(
"app.spotify_electron.health.health_controller.check_song_service",
return_value=mock_check_song_service(),
)
mocker.patch(
"app.spotify_electron.health.health_controller.check_auth_config",
return_value={"status": "unhealthy", "message": "Mocked Auth configuration failed"},
)
response = client.get("/health/")
assert response.status_code == HTTP_503_SERVICE_UNAVAILABLE
response_json = response.json()
assert response_json["status"] == "unhealthy"
assert response_json["details"]["auth_config"]["status"] == "unhealthy"
assert (
response_json["details"]["auth_config"]["message"]
== "Mocked Auth configuration failed"
)
assert response_json["details"]["database"]["status"] == "healthy"
assert response_json["details"]["song_service"]["status"] == "healthy"