-
-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathtest_heartbeat.py
More file actions
359 lines (277 loc) · 13.1 KB
/
Copy pathtest_heartbeat.py
File metadata and controls
359 lines (277 loc) · 13.1 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fastapi import status
from exceptions.fs_exceptions import PlatformAlreadyExistsException
from utils import get_version
def test_heartbeat(client):
response = client.get("/api/heartbeat")
assert response.status_code == status.HTTP_200_OK
heartbeat = response.json()
assert "SYSTEM" in heartbeat
system = heartbeat["SYSTEM"]
assert system["VERSION"] == get_version()
assert isinstance(system["SHOW_SETUP_WIZARD"], bool)
assert "METADATA_SOURCES" in heartbeat
metadata = heartbeat["METADATA_SOURCES"]
assert isinstance(metadata["ANY_SOURCE_ENABLED"], bool)
assert isinstance(metadata["IGDB_API_ENABLED"], bool)
assert isinstance(metadata["MOBY_API_ENABLED"], bool)
assert isinstance(metadata["SS_API_ENABLED"], bool)
assert isinstance(metadata["STEAMGRIDDB_API_ENABLED"], bool)
assert isinstance(metadata["RA_API_ENABLED"], bool)
assert isinstance(metadata["LAUNCHBOX_API_ENABLED"], bool)
assert isinstance(metadata["PLAYMATCH_API_ENABLED"], bool)
assert isinstance(metadata["HASHEOUS_API_ENABLED"], bool)
assert isinstance(metadata["TGDB_API_ENABLED"], bool)
assert isinstance(metadata["FLASHPOINT_API_ENABLED"], bool)
assert "FILESYSTEM" in heartbeat
filesystem = heartbeat["FILESYSTEM"]
assert isinstance(filesystem["FS_PLATFORMS"], list)
assert "EMULATION" in heartbeat
emulation = heartbeat["EMULATION"]
assert isinstance(emulation["DISABLE_EMULATOR_JS"], bool)
assert isinstance(emulation["DISABLE_RUFFLE_RS"], bool)
assert "FRONTEND" in heartbeat
frontend = heartbeat["FRONTEND"]
assert isinstance(frontend["DISABLE_USERPASS_LOGIN"], bool)
assert "OIDC" in heartbeat
oidc = heartbeat["OIDC"]
assert isinstance(oidc["ENABLED"], bool)
assert isinstance(oidc["PROVIDER"], str)
assert isinstance(oidc["RP_INITIATED_LOGOUT"], bool)
@pytest.mark.parametrize("authorization_header", ["Bearer ", "Foo", "a b c"])
def test_heartbeat_with_malformed_authorization_header(client, authorization_header: str):
response = client.get(
"/api/heartbeat", headers={"Authorization": authorization_header}
)
assert response.status_code == status.HTTP_200_OK
def test_heartbeat_metadata(client):
response = client.get("/api/heartbeat/metadata/launchbox")
assert response.status_code == status.HTTP_200_OK
heartbeat = response.json()
assert heartbeat
def test_heartbeat_metadata_unknown_source(client):
response = client.get("/api/heartbeat/metadata/unknown")
assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_get_setup_library_info_structure_a_detected(client, access_token):
"""Test get_setup_library_info with Structure A detected"""
with patch(
"endpoints.heartbeat.fs_platform_handler.detect_library_structure"
) as mock_detect:
mock_detect.return_value = "struct_a"
with patch(
"endpoints.heartbeat.fs_platform_handler.get_platforms"
) as mock_get_platforms:
mock_get_platforms.return_value = ["n64", "psx"]
# Create mock entry objects with .name attribute
def create_mock_entry(name):
entry = MagicMock()
entry.name = name
return entry
async def mock_iterdir_n64():
yield create_mock_entry("game1.z64")
yield create_mock_entry("game2.z64")
async def mock_iterdir_psx():
yield create_mock_entry("game1.iso")
with patch("endpoints.heartbeat.AnyioPath") as mock_anyio_path:
# Create side effects for multiple calls to AnyioPath()
path_instances = []
# First call - n64 roms directory
mock_n64_path = AsyncMock()
mock_n64_path.exists = AsyncMock(return_value=True)
mock_n64_path.iterdir = mock_iterdir_n64
path_instances.append(mock_n64_path)
# Second call - psx roms directory
mock_psx_path = AsyncMock()
mock_psx_path.exists = AsyncMock(return_value=True)
mock_psx_path.iterdir = mock_iterdir_psx
path_instances.append(mock_psx_path)
mock_anyio_path.side_effect = path_instances
response = client.get(
"/api/setup/library",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
data = response.json()
assert data["detected_structure"] == "struct_a"
assert len(data["existing_platforms"]) == 2
assert data["existing_platforms"][0]["fs_slug"] == "n64"
assert data["existing_platforms"][0]["rom_count"] == 2
assert data["existing_platforms"][1]["fs_slug"] == "psx"
assert data["existing_platforms"][1]["rom_count"] == 1
assert "supported_platforms" in data
def test_get_setup_library_info_structure_b_detected(client, admin_user, access_token):
"""Test get_setup_library_info with Structure B detected"""
with patch(
"endpoints.heartbeat.fs_platform_handler.detect_library_structure"
) as mock_detect:
mock_detect.return_value = "B"
with patch(
"endpoints.heartbeat.fs_platform_handler.get_platforms"
) as mock_get_platforms:
mock_get_platforms.return_value = ["gba"]
# Create mock entry objects with .name attribute
def create_mock_entry(name):
entry = MagicMock()
entry.name = name
return entry
async def mock_iterdir_gba():
yield create_mock_entry("game1.gba")
yield create_mock_entry("game2.gba")
yield create_mock_entry("game3.gba")
with patch("endpoints.heartbeat.AnyioPath") as mock_anyio_path:
# Create mock for gba roms directory
mock_gba_path = AsyncMock()
mock_gba_path.exists = AsyncMock(return_value=True)
mock_gba_path.iterdir = mock_iterdir_gba
mock_anyio_path.return_value = mock_gba_path
response = client.get(
"/api/setup/library",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
data = response.json()
assert data["detected_structure"] == "B"
assert len(data["existing_platforms"]) == 1
assert data["existing_platforms"][0]["fs_slug"] == "gba"
assert data["existing_platforms"][0]["rom_count"] == 3
def test_get_setup_library_info_no_structure_detected(client, admin_user, access_token):
"""Test get_setup_library_info when no structure is detected"""
with patch(
"endpoints.heartbeat.fs_platform_handler.detect_library_structure"
) as mock_detect:
mock_detect.return_value = None
with patch(
"endpoints.heartbeat.fs_platform_handler.get_platforms"
) as mock_get_platforms:
mock_get_platforms.return_value = []
response = client.get(
"/api/setup/library",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
data = response.json()
assert data["detected_structure"] is None
assert data["existing_platforms"] == []
assert "supported_platforms" in data
def test_get_setup_library_info_handles_errors(client, admin_user, access_token):
"""Test get_setup_library_info handles filesystem errors gracefully"""
with patch(
"endpoints.heartbeat.fs_platform_handler.detect_library_structure"
) as mock_detect:
mock_detect.return_value = "struct_a"
with patch(
"endpoints.heartbeat.fs_platform_handler.get_platforms"
) as mock_get_platforms:
# Simulate error retrieving platforms
mock_get_platforms.side_effect = Exception("Filesystem error")
response = client.get(
"/api/setup/library",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
data = response.json()
# Should return empty platforms list on error
assert data["existing_platforms"] == []
def test_create_setup_platforms_success(client, admin_user, access_token):
"""Test create_setup_platforms successfully creates platforms"""
platform_slugs = ["n64", "psx", "gba"]
with patch(
"endpoints.heartbeat.fs_platform_handler.detect_library_structure"
) as mock_detect:
mock_detect.return_value = "struct_a"
with patch(
"endpoints.heartbeat.fs_platform_handler.add_platform"
) as mock_add_platform:
mock_add_platform.return_value = None # Successful creation
response = client.post(
"/api/setup/platforms",
json=platform_slugs,
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_201_CREATED
data = response.json()
assert data["success"] is True
assert data["created_count"] == 3
assert "Successfully created 3 platform folder(s)" in data["message"]
assert mock_add_platform.call_count == 3
def test_create_setup_platforms_empty_list(client, admin_user, access_token):
"""Test create_setup_platforms with empty platform list"""
response = client.post(
"/api/setup/platforms",
json=[],
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_201_CREATED
data = response.json()
assert data["success"] is True
assert data["created_count"] == 0
assert data["message"] == "No platforms selected"
def test_create_setup_platforms_creates_structure_a_when_none_exists(
client, admin_user, access_token
):
"""Test create_setup_platforms creates Structure A when no structure detected"""
platform_slugs = ["n64"]
with patch(
"endpoints.heartbeat.fs_platform_handler.detect_library_structure"
) as mock_detect:
mock_detect.return_value = None # No structure detected
with patch("os.makedirs") as mock_makedirs:
with patch("endpoints.heartbeat.fs_platform_handler.add_platform"):
response = client.post(
"/api/setup/platforms",
json=platform_slugs,
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_201_CREATED
# Should create roms folder first
mock_makedirs.assert_called_once()
assert "roms" in str(mock_makedirs.call_args[0][0])
def test_create_setup_platforms_skips_existing_platforms(
client, admin_user, access_token
):
"""Test create_setup_platforms skips platforms that already exist"""
platform_slugs = ["n64", "psx", "gba"]
with patch(
"endpoints.heartbeat.fs_platform_handler.detect_library_structure"
) as mock_detect:
mock_detect.return_value = "struct_a"
with patch(
"endpoints.heartbeat.fs_platform_handler.add_platform"
) as mock_add_platform:
# First platform already exists, second succeeds, third succeeds
mock_add_platform.side_effect = [
PlatformAlreadyExistsException("n64"),
None,
None,
]
response = client.post(
"/api/setup/platforms",
json=platform_slugs,
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_201_CREATED
data = response.json()
assert data["success"] is True
# Should only count 2 created (psx and gba)
assert data["created_count"] == 2
def test_create_setup_platforms_handles_permission_errors(
client, admin_user, access_token
):
"""Test create_setup_platforms handles permission errors"""
platform_slugs = ["n64"]
with patch(
"endpoints.heartbeat.fs_platform_handler.detect_library_structure"
) as mock_detect:
mock_detect.return_value = "struct_a"
with patch(
"endpoints.heartbeat.fs_platform_handler.add_platform"
) as mock_add_platform:
mock_add_platform.side_effect = PermissionError("Permission denied")
response = client.post(
"/api/setup/platforms",
json=platform_slugs,
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
assert "Failed to create some platform folders" in response.json()["detail"]