test_settings_api.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. # Stagger settings tests
  349. # ========================================================================
  350. @pytest.mark.asyncio
  351. @pytest.mark.integration
  352. async def test_stagger_settings_defaults(self, async_client: AsyncClient):
  353. """Verify stagger settings have correct defaults."""
  354. response = await async_client.get("/api/v1/settings/")
  355. result = response.json()
  356. assert result["stagger_group_size"] == 2
  357. assert result["stagger_interval_minutes"] == 5
  358. @pytest.mark.asyncio
  359. @pytest.mark.integration
  360. async def test_update_stagger_settings(self, async_client: AsyncClient):
  361. """Verify stagger settings can be updated."""
  362. response = await async_client.put(
  363. "/api/v1/settings/",
  364. json={"stagger_group_size": 3, "stagger_interval_minutes": 10},
  365. )
  366. assert response.status_code == 200
  367. result = response.json()
  368. assert result["stagger_group_size"] == 3
  369. assert result["stagger_interval_minutes"] == 10
  370. @pytest.mark.asyncio
  371. @pytest.mark.integration
  372. async def test_stagger_settings_persist(self, async_client: AsyncClient):
  373. """Verify stagger settings persist after update."""
  374. await async_client.put(
  375. "/api/v1/settings/",
  376. json={"stagger_group_size": 4, "stagger_interval_minutes": 15},
  377. )
  378. response = await async_client.get("/api/v1/settings/")
  379. result = response.json()
  380. assert result["stagger_group_size"] == 4
  381. assert result["stagger_interval_minutes"] == 15
  382. @pytest.mark.asyncio
  383. @pytest.mark.integration
  384. async def test_stagger_settings_validation(self, async_client: AsyncClient):
  385. """Verify stagger settings reject out-of-range values."""
  386. response = await async_client.put("/api/v1/settings/", json={"stagger_group_size": 0})
  387. assert response.status_code == 422
  388. response = await async_client.put("/api/v1/settings/", json={"stagger_group_size": 51})
  389. assert response.status_code == 422
  390. response = await async_client.put("/api/v1/settings/", json={"stagger_interval_minutes": 0})
  391. assert response.status_code == 422
  392. response = await async_client.put("/api/v1/settings/", json={"stagger_interval_minutes": 61})
  393. assert response.status_code == 422
  394. # ========================================================================
  395. # Default print options tests
  396. # ========================================================================
  397. @pytest.mark.asyncio
  398. @pytest.mark.integration
  399. async def test_default_print_options_defaults(self, async_client: AsyncClient):
  400. """Verify default print options have correct defaults."""
  401. response = await async_client.get("/api/v1/settings/")
  402. result = response.json()
  403. assert result["default_bed_levelling"] is True
  404. assert result["default_flow_cali"] is False
  405. assert result["default_vibration_cali"] is True
  406. assert result["default_layer_inspect"] is False
  407. assert result["default_timelapse"] is False
  408. @pytest.mark.asyncio
  409. @pytest.mark.integration
  410. async def test_update_default_print_options(self, async_client: AsyncClient):
  411. """Verify default print options can be updated."""
  412. response = await async_client.put(
  413. "/api/v1/settings/",
  414. json={
  415. "default_bed_levelling": False,
  416. "default_flow_cali": True,
  417. "default_vibration_cali": False,
  418. "default_layer_inspect": True,
  419. "default_timelapse": True,
  420. },
  421. )
  422. assert response.status_code == 200
  423. result = response.json()
  424. assert result["default_bed_levelling"] is False
  425. assert result["default_flow_cali"] is True
  426. assert result["default_vibration_cali"] is False
  427. assert result["default_layer_inspect"] is True
  428. assert result["default_timelapse"] is True
  429. @pytest.mark.asyncio
  430. @pytest.mark.integration
  431. async def test_default_print_options_persist(self, async_client: AsyncClient):
  432. """CRITICAL: Verify default print options persist after update."""
  433. await async_client.put(
  434. "/api/v1/settings/",
  435. json={
  436. "default_bed_levelling": False,
  437. "default_timelapse": True,
  438. },
  439. )
  440. response = await async_client.get("/api/v1/settings/")
  441. result = response.json()
  442. assert result["default_bed_levelling"] is False
  443. assert result["default_timelapse"] is True
  444. @pytest.mark.asyncio
  445. @pytest.mark.integration
  446. async def test_default_print_options_partial_update(self, async_client: AsyncClient):
  447. """Verify partial updates don't affect other default print options."""
  448. # Set all to non-default
  449. await async_client.put(
  450. "/api/v1/settings/",
  451. json={
  452. "default_bed_levelling": False,
  453. "default_flow_cali": True,
  454. },
  455. )
  456. # Update only one
  457. response = await async_client.put(
  458. "/api/v1/settings/",
  459. json={"default_bed_levelling": True},
  460. )
  461. assert response.status_code == 200
  462. result = response.json()
  463. assert result["default_bed_levelling"] is True
  464. assert result["default_flow_cali"] is True # Should remain from previous update
  465. # ========================================================================
  466. # Home Assistant environment variable tests
  467. # ========================================================================
  468. @pytest.mark.asyncio
  469. @pytest.mark.integration
  470. async def test_ha_settings_default_no_env_vars(self, async_client: AsyncClient):
  471. """Verify HA settings work without environment variables (default behavior)."""
  472. # Ensure no env vars are set
  473. os.environ.pop("HA_URL", None)
  474. os.environ.pop("HA_TOKEN", None)
  475. response = await async_client.get("/api/v1/settings/")
  476. result = response.json()
  477. assert response.status_code == 200
  478. assert "ha_enabled" in result
  479. assert "ha_url" in result
  480. assert "ha_token" in result
  481. assert "ha_url_from_env" in result
  482. assert "ha_token_from_env" in result
  483. assert "ha_env_managed" in result
  484. # Default values without env vars
  485. assert result["ha_url_from_env"] is False
  486. assert result["ha_token_from_env"] is False
  487. assert result["ha_env_managed"] is False
  488. @pytest.mark.asyncio
  489. @pytest.mark.integration
  490. async def test_ha_settings_with_both_env_vars(self, async_client: AsyncClient):
  491. """Verify HA settings are overridden when both env vars are set."""
  492. # Set environment variables
  493. os.environ["HA_URL"] = "http://supervisor/core"
  494. os.environ["HA_TOKEN"] = "test-token-12345"
  495. try:
  496. response = await async_client.get("/api/v1/settings/")
  497. result = response.json()
  498. assert response.status_code == 200
  499. # Verify env var values are used
  500. assert result["ha_url"] == "http://supervisor/core"
  501. assert result["ha_token"] == "test-token-12345"
  502. # Verify metadata fields
  503. assert result["ha_url_from_env"] is True
  504. assert result["ha_token_from_env"] is True
  505. assert result["ha_env_managed"] is True
  506. # Verify auto-enable behavior
  507. assert result["ha_enabled"] is True
  508. finally:
  509. # Clean up
  510. os.environ.pop("HA_URL", None)
  511. os.environ.pop("HA_TOKEN", None)
  512. @pytest.mark.asyncio
  513. @pytest.mark.integration
  514. async def test_ha_settings_with_only_url_env_var(self, async_client: AsyncClient):
  515. """Verify partial configuration when only HA_URL is set."""
  516. # Set only URL env var
  517. os.environ["HA_URL"] = "http://supervisor/core"
  518. os.environ.pop("HA_TOKEN", None)
  519. try:
  520. response = await async_client.get("/api/v1/settings/")
  521. result = response.json()
  522. assert response.status_code == 200
  523. # Verify URL is from env, token is from database
  524. assert result["ha_url"] == "http://supervisor/core"
  525. assert result["ha_url_from_env"] is True
  526. assert result["ha_token_from_env"] is False
  527. assert result["ha_env_managed"] is False
  528. # No auto-enable with partial config
  529. assert result["ha_enabled"] is False # Database default
  530. finally:
  531. os.environ.pop("HA_URL", None)
  532. @pytest.mark.asyncio
  533. @pytest.mark.integration
  534. async def test_ha_settings_with_only_token_env_var(self, async_client: AsyncClient):
  535. """Verify partial configuration when only HA_TOKEN is set."""
  536. # Set only token env var
  537. os.environ.pop("HA_URL", None)
  538. os.environ["HA_TOKEN"] = "test-token-12345"
  539. try:
  540. response = await async_client.get("/api/v1/settings/")
  541. result = response.json()
  542. assert response.status_code == 200
  543. # Verify token is from env, URL is from database
  544. assert result["ha_token"] == "test-token-12345"
  545. assert result["ha_url_from_env"] is False
  546. assert result["ha_token_from_env"] is True
  547. assert result["ha_env_managed"] is False
  548. # No auto-enable with partial config
  549. assert result["ha_enabled"] is False # Database default
  550. finally:
  551. os.environ.pop("HA_TOKEN", None)
  552. @pytest.mark.asyncio
  553. @pytest.mark.integration
  554. async def test_ha_settings_env_vars_override_database(self, async_client: AsyncClient):
  555. """Verify environment variables take precedence over database values."""
  556. # First, set database values
  557. await async_client.put(
  558. "/api/v1/settings/",
  559. json={
  560. "ha_enabled": True,
  561. "ha_url": "http://database-url:8123",
  562. "ha_token": "database-token",
  563. },
  564. )
  565. # Verify database values are set
  566. response = await async_client.get("/api/v1/settings/")
  567. result = response.json()
  568. assert result["ha_url"] == "http://database-url:8123"
  569. assert result["ha_token"] == "database-token"
  570. # Now set environment variables
  571. os.environ["HA_URL"] = "http://env-url/core"
  572. os.environ["HA_TOKEN"] = "env-token-xyz"
  573. try:
  574. response = await async_client.get("/api/v1/settings/")
  575. result = response.json()
  576. # Verify env vars override database
  577. assert result["ha_url"] == "http://env-url/core"
  578. assert result["ha_token"] == "env-token-xyz"
  579. assert result["ha_url_from_env"] is True
  580. assert result["ha_token_from_env"] is True
  581. assert result["ha_env_managed"] is True
  582. assert result["ha_enabled"] is True
  583. finally:
  584. os.environ.pop("HA_URL", None)
  585. os.environ.pop("HA_TOKEN", None)
  586. # Verify database values are still there after removing env vars
  587. response = await async_client.get("/api/v1/settings/")
  588. result = response.json()
  589. assert result["ha_url"] == "http://database-url:8123"
  590. assert result["ha_token"] == "database-token"
  591. assert result["ha_url_from_env"] is False
  592. assert result["ha_token_from_env"] is False
  593. @pytest.mark.asyncio
  594. @pytest.mark.integration
  595. async def test_ha_settings_database_updates_accepted_but_ignored(self, async_client: AsyncClient):
  596. """Verify database updates are accepted but have no effect when env vars are set."""
  597. # Set environment variables
  598. os.environ["HA_URL"] = "http://supervisor/core"
  599. os.environ["HA_TOKEN"] = "env-token"
  600. try:
  601. # Attempt to update via API
  602. response = await async_client.put(
  603. "/api/v1/settings/",
  604. json={
  605. "ha_url": "http://different-url:8123",
  606. "ha_token": "different-token",
  607. },
  608. )
  609. # Update should succeed
  610. assert response.status_code == 200
  611. # But values should still be from env vars
  612. result = response.json()
  613. assert result["ha_url"] == "http://supervisor/core"
  614. assert result["ha_token"] == "env-token"
  615. assert result["ha_url_from_env"] is True
  616. assert result["ha_token_from_env"] is True
  617. finally:
  618. os.environ.pop("HA_URL", None)
  619. os.environ.pop("HA_TOKEN", None)
  620. @pytest.mark.asyncio
  621. @pytest.mark.integration
  622. async def test_ha_settings_empty_env_vars_treated_as_not_set(self, async_client: AsyncClient):
  623. """Verify empty environment variables are treated as not set."""
  624. # Set empty env vars
  625. os.environ["HA_URL"] = ""
  626. os.environ["HA_TOKEN"] = ""
  627. try:
  628. response = await async_client.get("/api/v1/settings/")
  629. result = response.json()
  630. # Empty env vars should be treated as not set
  631. assert result["ha_url_from_env"] is False
  632. assert result["ha_token_from_env"] is False
  633. assert result["ha_env_managed"] is False
  634. finally:
  635. os.environ.pop("HA_URL", None)
  636. os.environ.pop("HA_TOKEN", None)
  637. @pytest.mark.asyncio
  638. @pytest.mark.integration
  639. async def test_ha_settings_can_be_updated_normally_without_env_vars(self, async_client: AsyncClient):
  640. """Verify HA settings can be updated normally when env vars are not set."""
  641. # Ensure no env vars
  642. os.environ.pop("HA_URL", None)
  643. os.environ.pop("HA_TOKEN", None)
  644. # Update HA settings
  645. response = await async_client.put(
  646. "/api/v1/settings/",
  647. json={
  648. "ha_enabled": True,
  649. "ha_url": "http://192.168.1.100:8123",
  650. "ha_token": "my-long-lived-token",
  651. },
  652. )
  653. assert response.status_code == 200
  654. result = response.json()
  655. assert result["ha_enabled"] is True
  656. assert result["ha_url"] == "http://192.168.1.100:8123"
  657. assert result["ha_token"] == "my-long-lived-token"
  658. assert result["ha_url_from_env"] is False
  659. assert result["ha_token_from_env"] is False
  660. assert result["ha_env_managed"] is False
  661. # Verify persistence
  662. response = await async_client.get("/api/v1/settings/")
  663. result = response.json()
  664. assert result["ha_enabled"] is True
  665. assert result["ha_url"] == "http://192.168.1.100:8123"
  666. assert result["ha_token"] == "my-long-lived-token"
  667. class TestSimplifiedBackupRestore:
  668. """Integration tests for the simplified backup/restore endpoints (ZIP-based).
  669. Note: Tests that require actual file operations (backup creation) are skipped
  670. because the test suite uses an in-memory database. These tests focus on
  671. validation and error handling which don't require file I/O.
  672. """
  673. @pytest.mark.asyncio
  674. @pytest.mark.integration
  675. async def test_restore_requires_zip_file(self, async_client: AsyncClient):
  676. """Verify restore rejects non-ZIP files."""
  677. files = {"file": ("backup.txt", b"not a zip file", "text/plain")}
  678. response = await async_client.post("/api/v1/settings/restore", files=files)
  679. assert response.status_code == 400
  680. assert "zip" in response.json()["detail"].lower()
  681. @pytest.mark.asyncio
  682. @pytest.mark.integration
  683. async def test_restore_requires_database_in_zip(self, async_client: AsyncClient):
  684. """Verify restore rejects ZIP without database file."""
  685. import io
  686. import zipfile
  687. # Create a ZIP without bambuddy.db
  688. zip_buffer = io.BytesIO()
  689. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  690. zf.writestr("dummy.txt", "dummy content")
  691. zip_buffer.seek(0)
  692. files = {"file": ("backup.zip", zip_buffer.read(), "application/zip")}
  693. response = await async_client.post("/api/v1/settings/restore", files=files)
  694. assert response.status_code == 400
  695. assert "missing bambuddy.db" in response.json()["detail"].lower()
  696. @pytest.mark.asyncio
  697. @pytest.mark.integration
  698. async def test_restore_invalid_zip(self, async_client: AsyncClient):
  699. """Verify restore rejects corrupted ZIP files."""
  700. files = {"file": ("backup.zip", b"not valid zip content", "application/zip")}
  701. response = await async_client.post("/api/v1/settings/restore", files=files)
  702. assert response.status_code == 400
  703. assert "not a valid zip" in response.json()["detail"].lower()