test_settings_api.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. """Integration tests for Settings API endpoints.
  2. Tests the full request/response cycle for /api/v1/settings/ endpoints.
  3. """
  4. import os
  5. import pytest
  6. from httpx import AsyncClient
  7. class TestSettingsAPI:
  8. """Integration tests for /api/v1/settings/ endpoints."""
  9. # ========================================================================
  10. # Get settings
  11. # ========================================================================
  12. @pytest.mark.asyncio
  13. @pytest.mark.integration
  14. async def test_get_settings(self, async_client: AsyncClient):
  15. """Verify settings can be retrieved."""
  16. response = await async_client.get("/api/v1/settings/")
  17. assert response.status_code == 200
  18. result = response.json()
  19. # Check for actual settings fields
  20. assert "auto_archive" in result
  21. assert "currency" in result
  22. assert "date_format" in result
  23. @pytest.mark.asyncio
  24. @pytest.mark.integration
  25. async def test_get_settings_has_defaults(self, async_client: AsyncClient):
  26. """Verify default settings values are returned."""
  27. response = await async_client.get("/api/v1/settings/")
  28. assert response.status_code == 200
  29. result = response.json()
  30. # Verify some default values
  31. assert isinstance(result["auto_archive"], bool)
  32. assert isinstance(result["currency"], str)
  33. # ========================================================================
  34. # Update settings
  35. # ========================================================================
  36. @pytest.mark.asyncio
  37. @pytest.mark.integration
  38. async def test_update_auto_archive(self, async_client: AsyncClient):
  39. """Verify auto_archive can be updated."""
  40. # First get current value
  41. response = await async_client.get("/api/v1/settings/")
  42. original = response.json()["auto_archive"]
  43. # Update to opposite value
  44. new_value = not original
  45. response = await async_client.put("/api/v1/settings/", json={"auto_archive": new_value})
  46. assert response.status_code == 200
  47. assert response.json()["auto_archive"] == new_value
  48. @pytest.mark.asyncio
  49. @pytest.mark.integration
  50. async def test_update_currency(self, async_client: AsyncClient):
  51. """Verify currency can be updated."""
  52. response = await async_client.put("/api/v1/settings/", json={"currency": "EUR"})
  53. assert response.status_code == 200
  54. assert response.json()["currency"] == "EUR"
  55. @pytest.mark.asyncio
  56. @pytest.mark.integration
  57. async def test_update_date_format(self, async_client: AsyncClient):
  58. """Verify date format can be updated."""
  59. response = await async_client.put("/api/v1/settings/", json={"date_format": "eu"})
  60. assert response.status_code == 200
  61. assert response.json()["date_format"] == "eu"
  62. @pytest.mark.asyncio
  63. @pytest.mark.integration
  64. async def test_update_time_format(self, async_client: AsyncClient):
  65. """Verify time format can be updated."""
  66. response = await async_client.put("/api/v1/settings/", json={"time_format": "24h"})
  67. assert response.status_code == 200
  68. assert response.json()["time_format"] == "24h"
  69. @pytest.mark.asyncio
  70. @pytest.mark.integration
  71. async def test_update_filament_cost(self, async_client: AsyncClient):
  72. """Verify default filament cost can be updated."""
  73. response = await async_client.put("/api/v1/settings/", json={"default_filament_cost": 30.0})
  74. assert response.status_code == 200
  75. assert response.json()["default_filament_cost"] == 30.0
  76. @pytest.mark.asyncio
  77. @pytest.mark.integration
  78. async def test_update_energy_cost(self, async_client: AsyncClient):
  79. """Verify energy cost can be updated."""
  80. response = await async_client.put("/api/v1/settings/", json={"energy_cost_per_kwh": 0.20})
  81. assert response.status_code == 200
  82. assert response.json()["energy_cost_per_kwh"] == 0.20
  83. @pytest.mark.asyncio
  84. @pytest.mark.integration
  85. async def test_update_multiple_settings(self, async_client: AsyncClient):
  86. """Verify multiple settings can be updated at once."""
  87. response = await async_client.put(
  88. "/api/v1/settings/",
  89. json={
  90. "currency": "GBP",
  91. "date_format": "iso",
  92. "time_format": "12h",
  93. "save_thumbnails": False,
  94. },
  95. )
  96. assert response.status_code == 200
  97. result = response.json()
  98. assert result["currency"] == "GBP"
  99. assert result["date_format"] == "iso"
  100. assert result["time_format"] == "12h"
  101. assert result["save_thumbnails"] is False
  102. @pytest.mark.asyncio
  103. @pytest.mark.integration
  104. async def test_update_spoolman_settings(self, async_client: AsyncClient):
  105. """Verify Spoolman settings can be updated."""
  106. response = await async_client.put(
  107. "/api/v1/settings/",
  108. json={
  109. "spoolman_enabled": True,
  110. "spoolman_url": "http://localhost:7912",
  111. "spoolman_sync_mode": "manual",
  112. },
  113. )
  114. assert response.status_code == 200
  115. result = response.json()
  116. assert result["spoolman_enabled"] is True
  117. assert result["spoolman_url"] == "http://localhost:7912"
  118. assert result["spoolman_sync_mode"] == "manual"
  119. @pytest.mark.asyncio
  120. @pytest.mark.integration
  121. async def test_update_ams_thresholds(self, async_client: AsyncClient):
  122. """Verify AMS threshold settings can be updated."""
  123. response = await async_client.put(
  124. "/api/v1/settings/",
  125. json={
  126. "ams_humidity_good": 35,
  127. "ams_humidity_fair": 55,
  128. "ams_temp_good": 25.0,
  129. "ams_temp_fair": 32.0,
  130. },
  131. )
  132. assert response.status_code == 200
  133. result = response.json()
  134. assert result["ams_humidity_good"] == 35
  135. assert result["ams_humidity_fair"] == 55
  136. assert result["ams_temp_good"] == 25.0
  137. assert result["ams_temp_fair"] == 32.0
  138. @pytest.mark.asyncio
  139. @pytest.mark.integration
  140. async def test_update_low_stock_threshold(self, async_client: AsyncClient):
  141. """Verify low stock threshold setting can be updated."""
  142. # Get default value
  143. response = await async_client.get("/api/v1/settings/")
  144. assert response.status_code == 200
  145. assert response.json()["low_stock_threshold"] == 20.0
  146. # Update to custom value
  147. response = await async_client.put("/api/v1/settings/", json={"low_stock_threshold": 15.5})
  148. assert response.status_code == 200
  149. result = response.json()
  150. assert result["low_stock_threshold"] == 15.5
  151. # Verify persistence
  152. response = await async_client.get("/api/v1/settings/")
  153. assert response.status_code == 200
  154. assert response.json()["low_stock_threshold"] == 15.5
  155. @pytest.mark.asyncio
  156. @pytest.mark.integration
  157. async def test_update_notification_language(self, async_client: AsyncClient):
  158. """Verify notification language can be updated."""
  159. response = await async_client.put("/api/v1/settings/", json={"notification_language": "de"})
  160. assert response.status_code == 200
  161. assert response.json()["notification_language"] == "de"
  162. # ========================================================================
  163. # Settings persistence tests
  164. # ========================================================================
  165. @pytest.mark.asyncio
  166. @pytest.mark.integration
  167. async def test_update_theme_settings(self, async_client: AsyncClient):
  168. """Verify theme settings can be updated."""
  169. response = await async_client.put(
  170. "/api/v1/settings/",
  171. json={
  172. "dark_style": "glow",
  173. "dark_background": "forest",
  174. "dark_accent": "teal",
  175. "light_style": "vibrant",
  176. "light_background": "warm",
  177. "light_accent": "blue",
  178. },
  179. )
  180. assert response.status_code == 200
  181. result = response.json()
  182. assert result["dark_style"] == "glow"
  183. assert result["dark_background"] == "forest"
  184. assert result["dark_accent"] == "teal"
  185. assert result["light_style"] == "vibrant"
  186. assert result["light_background"] == "warm"
  187. assert result["light_accent"] == "blue"
  188. @pytest.mark.asyncio
  189. @pytest.mark.integration
  190. async def test_settings_persist_after_update(self, async_client: AsyncClient):
  191. """CRITICAL: Verify settings changes persist across requests."""
  192. # Update settings
  193. await async_client.put("/api/v1/settings/", json={"currency": "JPY", "check_updates": False})
  194. # Verify persistence in new request
  195. response = await async_client.get("/api/v1/settings/")
  196. result = response.json()
  197. assert result["currency"] == "JPY"
  198. assert result["check_updates"] is False
  199. @pytest.mark.asyncio
  200. @pytest.mark.integration
  201. async def test_update_check_printer_firmware(self, async_client: AsyncClient):
  202. """Verify check_printer_firmware can be updated."""
  203. # Default should be True
  204. response = await async_client.get("/api/v1/settings/")
  205. assert response.json()["check_printer_firmware"] is True
  206. # Update to False
  207. response = await async_client.put("/api/v1/settings/", json={"check_printer_firmware": False})
  208. assert response.status_code == 200
  209. assert response.json()["check_printer_firmware"] is False
  210. # Verify persistence
  211. response = await async_client.get("/api/v1/settings/")
  212. assert response.json()["check_printer_firmware"] is False
  213. # Update back to True
  214. response = await async_client.put("/api/v1/settings/", json={"check_printer_firmware": True})
  215. assert response.status_code == 200
  216. assert response.json()["check_printer_firmware"] is True
  217. # ========================================================================
  218. # MQTT settings tests
  219. # ========================================================================
  220. @pytest.mark.asyncio
  221. @pytest.mark.integration
  222. async def test_update_mqtt_settings(self, async_client: AsyncClient):
  223. """Verify MQTT settings can be updated."""
  224. response = await async_client.put(
  225. "/api/v1/settings/",
  226. json={
  227. "mqtt_enabled": True,
  228. "mqtt_broker": "mqtt.example.com",
  229. "mqtt_port": 8883,
  230. "mqtt_username": "testuser",
  231. "mqtt_password": "testpass",
  232. "mqtt_topic_prefix": "myprefix",
  233. "mqtt_use_tls": True,
  234. },
  235. )
  236. assert response.status_code == 200
  237. result = response.json()
  238. assert result["mqtt_enabled"] is True
  239. assert result["mqtt_broker"] == "mqtt.example.com"
  240. assert result["mqtt_port"] == 8883
  241. assert result["mqtt_username"] == "testuser"
  242. assert result["mqtt_password"] == "testpass"
  243. assert result["mqtt_topic_prefix"] == "myprefix"
  244. assert result["mqtt_use_tls"] is True
  245. @pytest.mark.asyncio
  246. @pytest.mark.integration
  247. async def test_mqtt_status_endpoint(self, async_client: AsyncClient):
  248. """Verify MQTT status endpoint returns expected fields."""
  249. response = await async_client.get("/api/v1/settings/mqtt/status")
  250. assert response.status_code == 200
  251. result = response.json()
  252. assert "enabled" in result
  253. assert "connected" in result
  254. assert "broker" in result
  255. assert "port" in result
  256. assert "topic_prefix" in result
  257. @pytest.mark.asyncio
  258. @pytest.mark.integration
  259. async def test_mqtt_defaults(self, async_client: AsyncClient):
  260. """Verify MQTT has correct default values."""
  261. # Reset MQTT settings to defaults
  262. await async_client.put(
  263. "/api/v1/settings/",
  264. json={
  265. "mqtt_enabled": False,
  266. "mqtt_broker": "",
  267. "mqtt_port": 1883,
  268. "mqtt_username": "",
  269. "mqtt_password": "",
  270. "mqtt_topic_prefix": "bambuddy",
  271. "mqtt_use_tls": False,
  272. },
  273. )
  274. response = await async_client.get("/api/v1/settings/")
  275. result = response.json()
  276. assert result["mqtt_enabled"] is False
  277. assert result["mqtt_port"] == 1883
  278. assert result["mqtt_topic_prefix"] == "bambuddy"
  279. assert result["mqtt_use_tls"] is False
  280. # ========================================================================
  281. # Camera settings tests
  282. # ========================================================================
  283. @pytest.mark.asyncio
  284. @pytest.mark.integration
  285. async def test_update_camera_view_mode(self, async_client: AsyncClient):
  286. """Verify camera view mode can be updated."""
  287. response = await async_client.put("/api/v1/settings/", json={"camera_view_mode": "embedded"})
  288. assert response.status_code == 200
  289. assert response.json()["camera_view_mode"] == "embedded"
  290. @pytest.mark.asyncio
  291. @pytest.mark.integration
  292. async def test_camera_view_mode_persists(self, async_client: AsyncClient):
  293. """CRITICAL: Verify camera view mode persists after update."""
  294. # Update to embedded
  295. await async_client.put("/api/v1/settings/", json={"camera_view_mode": "embedded"})
  296. # Verify persistence in new request
  297. response = await async_client.get("/api/v1/settings/")
  298. assert response.json()["camera_view_mode"] == "embedded"
  299. # Update back to window
  300. await async_client.put("/api/v1/settings/", json={"camera_view_mode": "window"})
  301. # Verify persistence
  302. response = await async_client.get("/api/v1/settings/")
  303. assert response.json()["camera_view_mode"] == "window"
  304. @pytest.mark.asyncio
  305. @pytest.mark.integration
  306. async def test_camera_view_mode_default(self, async_client: AsyncClient):
  307. """Verify camera view mode has correct default value."""
  308. # Reset by requesting settings (default should be 'window')
  309. response = await async_client.get("/api/v1/settings/")
  310. result = response.json()
  311. assert "camera_view_mode" in result
  312. # Default is 'window' as defined in schema
  313. assert result["camera_view_mode"] in ["window", "embedded"]
  314. # ========================================================================
  315. # Per-printer mapping settings tests
  316. # ========================================================================
  317. @pytest.mark.asyncio
  318. @pytest.mark.integration
  319. async def test_update_per_printer_mapping_expanded(self, async_client: AsyncClient):
  320. """Verify per_printer_mapping_expanded can be updated."""
  321. response = await async_client.put("/api/v1/settings/", json={"per_printer_mapping_expanded": True})
  322. assert response.status_code == 200
  323. assert response.json()["per_printer_mapping_expanded"] is True
  324. @pytest.mark.asyncio
  325. @pytest.mark.integration
  326. async def test_per_printer_mapping_expanded_persists(self, async_client: AsyncClient):
  327. """CRITICAL: Verify per_printer_mapping_expanded persists after update."""
  328. # Update to True
  329. await async_client.put("/api/v1/settings/", json={"per_printer_mapping_expanded": True})
  330. # Verify persistence in new request
  331. response = await async_client.get("/api/v1/settings/")
  332. assert response.json()["per_printer_mapping_expanded"] is True
  333. # Update back to False
  334. await async_client.put("/api/v1/settings/", json={"per_printer_mapping_expanded": False})
  335. # Verify persistence
  336. response = await async_client.get("/api/v1/settings/")
  337. assert response.json()["per_printer_mapping_expanded"] is False
  338. @pytest.mark.asyncio
  339. @pytest.mark.integration
  340. async def test_per_printer_mapping_expanded_default(self, async_client: AsyncClient):
  341. """Verify per_printer_mapping_expanded has correct default value."""
  342. response = await async_client.get("/api/v1/settings/")
  343. result = response.json()
  344. assert "per_printer_mapping_expanded" in result
  345. # Default is False as defined in schema
  346. assert isinstance(result["per_printer_mapping_expanded"], bool)
  347. # ========================================================================
  348. # Home Assistant environment variable tests
  349. # ========================================================================
  350. @pytest.mark.asyncio
  351. @pytest.mark.integration
  352. async def test_ha_settings_default_no_env_vars(self, async_client: AsyncClient):
  353. """Verify HA settings work without environment variables (default behavior)."""
  354. # Ensure no env vars are set
  355. os.environ.pop("HA_URL", None)
  356. os.environ.pop("HA_TOKEN", None)
  357. response = await async_client.get("/api/v1/settings/")
  358. result = response.json()
  359. assert response.status_code == 200
  360. assert "ha_enabled" in result
  361. assert "ha_url" in result
  362. assert "ha_token" in result
  363. assert "ha_url_from_env" in result
  364. assert "ha_token_from_env" in result
  365. assert "ha_env_managed" in result
  366. # Default values without env vars
  367. assert result["ha_url_from_env"] is False
  368. assert result["ha_token_from_env"] is False
  369. assert result["ha_env_managed"] is False
  370. @pytest.mark.asyncio
  371. @pytest.mark.integration
  372. async def test_ha_settings_with_both_env_vars(self, async_client: AsyncClient):
  373. """Verify HA settings are overridden when both env vars are set."""
  374. # Set environment variables
  375. os.environ["HA_URL"] = "http://supervisor/core"
  376. os.environ["HA_TOKEN"] = "test-token-12345"
  377. try:
  378. response = await async_client.get("/api/v1/settings/")
  379. result = response.json()
  380. assert response.status_code == 200
  381. # Verify env var values are used
  382. assert result["ha_url"] == "http://supervisor/core"
  383. assert result["ha_token"] == "test-token-12345"
  384. # Verify metadata fields
  385. assert result["ha_url_from_env"] is True
  386. assert result["ha_token_from_env"] is True
  387. assert result["ha_env_managed"] is True
  388. # Verify auto-enable behavior
  389. assert result["ha_enabled"] is True
  390. finally:
  391. # Clean up
  392. os.environ.pop("HA_URL", None)
  393. os.environ.pop("HA_TOKEN", None)
  394. @pytest.mark.asyncio
  395. @pytest.mark.integration
  396. async def test_ha_settings_with_only_url_env_var(self, async_client: AsyncClient):
  397. """Verify partial configuration when only HA_URL is set."""
  398. # Set only URL env var
  399. os.environ["HA_URL"] = "http://supervisor/core"
  400. os.environ.pop("HA_TOKEN", None)
  401. try:
  402. response = await async_client.get("/api/v1/settings/")
  403. result = response.json()
  404. assert response.status_code == 200
  405. # Verify URL is from env, token is from database
  406. assert result["ha_url"] == "http://supervisor/core"
  407. assert result["ha_url_from_env"] is True
  408. assert result["ha_token_from_env"] is False
  409. assert result["ha_env_managed"] is False
  410. # No auto-enable with partial config
  411. assert result["ha_enabled"] is False # Database default
  412. finally:
  413. os.environ.pop("HA_URL", None)
  414. @pytest.mark.asyncio
  415. @pytest.mark.integration
  416. async def test_ha_settings_with_only_token_env_var(self, async_client: AsyncClient):
  417. """Verify partial configuration when only HA_TOKEN is set."""
  418. # Set only token env var
  419. os.environ.pop("HA_URL", None)
  420. os.environ["HA_TOKEN"] = "test-token-12345"
  421. try:
  422. response = await async_client.get("/api/v1/settings/")
  423. result = response.json()
  424. assert response.status_code == 200
  425. # Verify token is from env, URL is from database
  426. assert result["ha_token"] == "test-token-12345"
  427. assert result["ha_url_from_env"] is False
  428. assert result["ha_token_from_env"] is True
  429. assert result["ha_env_managed"] is False
  430. # No auto-enable with partial config
  431. assert result["ha_enabled"] is False # Database default
  432. finally:
  433. os.environ.pop("HA_TOKEN", None)
  434. @pytest.mark.asyncio
  435. @pytest.mark.integration
  436. async def test_ha_settings_env_vars_override_database(self, async_client: AsyncClient):
  437. """Verify environment variables take precedence over database values."""
  438. # First, set database values
  439. await async_client.put(
  440. "/api/v1/settings/",
  441. json={
  442. "ha_enabled": True,
  443. "ha_url": "http://database-url:8123",
  444. "ha_token": "database-token",
  445. },
  446. )
  447. # Verify database values are set
  448. response = await async_client.get("/api/v1/settings/")
  449. result = response.json()
  450. assert result["ha_url"] == "http://database-url:8123"
  451. assert result["ha_token"] == "database-token"
  452. # Now set environment variables
  453. os.environ["HA_URL"] = "http://env-url/core"
  454. os.environ["HA_TOKEN"] = "env-token-xyz"
  455. try:
  456. response = await async_client.get("/api/v1/settings/")
  457. result = response.json()
  458. # Verify env vars override database
  459. assert result["ha_url"] == "http://env-url/core"
  460. assert result["ha_token"] == "env-token-xyz"
  461. assert result["ha_url_from_env"] is True
  462. assert result["ha_token_from_env"] is True
  463. assert result["ha_env_managed"] is True
  464. assert result["ha_enabled"] is True
  465. finally:
  466. os.environ.pop("HA_URL", None)
  467. os.environ.pop("HA_TOKEN", None)
  468. # Verify database values are still there after removing env vars
  469. response = await async_client.get("/api/v1/settings/")
  470. result = response.json()
  471. assert result["ha_url"] == "http://database-url:8123"
  472. assert result["ha_token"] == "database-token"
  473. assert result["ha_url_from_env"] is False
  474. assert result["ha_token_from_env"] is False
  475. @pytest.mark.asyncio
  476. @pytest.mark.integration
  477. async def test_ha_settings_database_updates_accepted_but_ignored(self, async_client: AsyncClient):
  478. """Verify database updates are accepted but have no effect when env vars are set."""
  479. # Set environment variables
  480. os.environ["HA_URL"] = "http://supervisor/core"
  481. os.environ["HA_TOKEN"] = "env-token"
  482. try:
  483. # Attempt to update via API
  484. response = await async_client.put(
  485. "/api/v1/settings/",
  486. json={
  487. "ha_url": "http://different-url:8123",
  488. "ha_token": "different-token",
  489. },
  490. )
  491. # Update should succeed
  492. assert response.status_code == 200
  493. # But values should still be from env vars
  494. result = response.json()
  495. assert result["ha_url"] == "http://supervisor/core"
  496. assert result["ha_token"] == "env-token"
  497. assert result["ha_url_from_env"] is True
  498. assert result["ha_token_from_env"] is True
  499. finally:
  500. os.environ.pop("HA_URL", None)
  501. os.environ.pop("HA_TOKEN", None)
  502. @pytest.mark.asyncio
  503. @pytest.mark.integration
  504. async def test_ha_settings_empty_env_vars_treated_as_not_set(self, async_client: AsyncClient):
  505. """Verify empty environment variables are treated as not set."""
  506. # Set empty env vars
  507. os.environ["HA_URL"] = ""
  508. os.environ["HA_TOKEN"] = ""
  509. try:
  510. response = await async_client.get("/api/v1/settings/")
  511. result = response.json()
  512. # Empty env vars should be treated as not set
  513. assert result["ha_url_from_env"] is False
  514. assert result["ha_token_from_env"] is False
  515. assert result["ha_env_managed"] is False
  516. finally:
  517. os.environ.pop("HA_URL", None)
  518. os.environ.pop("HA_TOKEN", None)
  519. @pytest.mark.asyncio
  520. @pytest.mark.integration
  521. async def test_ha_settings_can_be_updated_normally_without_env_vars(self, async_client: AsyncClient):
  522. """Verify HA settings can be updated normally when env vars are not set."""
  523. # Ensure no env vars
  524. os.environ.pop("HA_URL", None)
  525. os.environ.pop("HA_TOKEN", None)
  526. # Update HA settings
  527. response = await async_client.put(
  528. "/api/v1/settings/",
  529. json={
  530. "ha_enabled": True,
  531. "ha_url": "http://192.168.1.100:8123",
  532. "ha_token": "my-long-lived-token",
  533. },
  534. )
  535. assert response.status_code == 200
  536. result = response.json()
  537. assert result["ha_enabled"] is True
  538. assert result["ha_url"] == "http://192.168.1.100:8123"
  539. assert result["ha_token"] == "my-long-lived-token"
  540. assert result["ha_url_from_env"] is False
  541. assert result["ha_token_from_env"] is False
  542. assert result["ha_env_managed"] is False
  543. # Verify persistence
  544. response = await async_client.get("/api/v1/settings/")
  545. result = response.json()
  546. assert result["ha_enabled"] is True
  547. assert result["ha_url"] == "http://192.168.1.100:8123"
  548. assert result["ha_token"] == "my-long-lived-token"
  549. class TestSimplifiedBackupRestore:
  550. """Integration tests for the simplified backup/restore endpoints (ZIP-based).
  551. Note: Tests that require actual file operations (backup creation) are skipped
  552. because the test suite uses an in-memory database. These tests focus on
  553. validation and error handling which don't require file I/O.
  554. """
  555. @pytest.mark.asyncio
  556. @pytest.mark.integration
  557. async def test_restore_requires_zip_file(self, async_client: AsyncClient):
  558. """Verify restore rejects non-ZIP files."""
  559. files = {"file": ("backup.txt", b"not a zip file", "text/plain")}
  560. response = await async_client.post("/api/v1/settings/restore", files=files)
  561. assert response.status_code == 400
  562. assert "zip" in response.json()["detail"].lower()
  563. @pytest.mark.asyncio
  564. @pytest.mark.integration
  565. async def test_restore_requires_database_in_zip(self, async_client: AsyncClient):
  566. """Verify restore rejects ZIP without database file."""
  567. import io
  568. import zipfile
  569. # Create a ZIP without bambuddy.db
  570. zip_buffer = io.BytesIO()
  571. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  572. zf.writestr("dummy.txt", "dummy content")
  573. zip_buffer.seek(0)
  574. files = {"file": ("backup.zip", zip_buffer.read(), "application/zip")}
  575. response = await async_client.post("/api/v1/settings/restore", files=files)
  576. assert response.status_code == 400
  577. assert "missing bambuddy.db" in response.json()["detail"].lower()
  578. @pytest.mark.asyncio
  579. @pytest.mark.integration
  580. async def test_restore_invalid_zip(self, async_client: AsyncClient):
  581. """Verify restore rejects corrupted ZIP files."""
  582. files = {"file": ("backup.zip", b"not valid zip content", "application/zip")}
  583. response = await async_client.post("/api/v1/settings/restore", files=files)
  584. assert response.status_code == 400
  585. assert "not a valid zip" in response.json()["detail"].lower()