test_settings_api.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. # Home Assistant environment variable tests
  396. # ========================================================================
  397. @pytest.mark.asyncio
  398. @pytest.mark.integration
  399. async def test_ha_settings_default_no_env_vars(self, async_client: AsyncClient):
  400. """Verify HA settings work without environment variables (default behavior)."""
  401. # Ensure no env vars are set
  402. os.environ.pop("HA_URL", None)
  403. os.environ.pop("HA_TOKEN", None)
  404. response = await async_client.get("/api/v1/settings/")
  405. result = response.json()
  406. assert response.status_code == 200
  407. assert "ha_enabled" in result
  408. assert "ha_url" in result
  409. assert "ha_token" in result
  410. assert "ha_url_from_env" in result
  411. assert "ha_token_from_env" in result
  412. assert "ha_env_managed" in result
  413. # Default values without env vars
  414. assert result["ha_url_from_env"] is False
  415. assert result["ha_token_from_env"] is False
  416. assert result["ha_env_managed"] is False
  417. @pytest.mark.asyncio
  418. @pytest.mark.integration
  419. async def test_ha_settings_with_both_env_vars(self, async_client: AsyncClient):
  420. """Verify HA settings are overridden when both env vars are set."""
  421. # Set environment variables
  422. os.environ["HA_URL"] = "http://supervisor/core"
  423. os.environ["HA_TOKEN"] = "test-token-12345"
  424. try:
  425. response = await async_client.get("/api/v1/settings/")
  426. result = response.json()
  427. assert response.status_code == 200
  428. # Verify env var values are used
  429. assert result["ha_url"] == "http://supervisor/core"
  430. assert result["ha_token"] == "test-token-12345"
  431. # Verify metadata fields
  432. assert result["ha_url_from_env"] is True
  433. assert result["ha_token_from_env"] is True
  434. assert result["ha_env_managed"] is True
  435. # Verify auto-enable behavior
  436. assert result["ha_enabled"] is True
  437. finally:
  438. # Clean up
  439. os.environ.pop("HA_URL", None)
  440. os.environ.pop("HA_TOKEN", None)
  441. @pytest.mark.asyncio
  442. @pytest.mark.integration
  443. async def test_ha_settings_with_only_url_env_var(self, async_client: AsyncClient):
  444. """Verify partial configuration when only HA_URL is set."""
  445. # Set only URL env var
  446. os.environ["HA_URL"] = "http://supervisor/core"
  447. os.environ.pop("HA_TOKEN", None)
  448. try:
  449. response = await async_client.get("/api/v1/settings/")
  450. result = response.json()
  451. assert response.status_code == 200
  452. # Verify URL is from env, token is from database
  453. assert result["ha_url"] == "http://supervisor/core"
  454. assert result["ha_url_from_env"] is True
  455. assert result["ha_token_from_env"] is False
  456. assert result["ha_env_managed"] is False
  457. # No auto-enable with partial config
  458. assert result["ha_enabled"] is False # Database default
  459. finally:
  460. os.environ.pop("HA_URL", None)
  461. @pytest.mark.asyncio
  462. @pytest.mark.integration
  463. async def test_ha_settings_with_only_token_env_var(self, async_client: AsyncClient):
  464. """Verify partial configuration when only HA_TOKEN is set."""
  465. # Set only token env var
  466. os.environ.pop("HA_URL", None)
  467. os.environ["HA_TOKEN"] = "test-token-12345"
  468. try:
  469. response = await async_client.get("/api/v1/settings/")
  470. result = response.json()
  471. assert response.status_code == 200
  472. # Verify token is from env, URL is from database
  473. assert result["ha_token"] == "test-token-12345"
  474. assert result["ha_url_from_env"] is False
  475. assert result["ha_token_from_env"] is True
  476. assert result["ha_env_managed"] is False
  477. # No auto-enable with partial config
  478. assert result["ha_enabled"] is False # Database default
  479. finally:
  480. os.environ.pop("HA_TOKEN", None)
  481. @pytest.mark.asyncio
  482. @pytest.mark.integration
  483. async def test_ha_settings_env_vars_override_database(self, async_client: AsyncClient):
  484. """Verify environment variables take precedence over database values."""
  485. # First, set database values
  486. await async_client.put(
  487. "/api/v1/settings/",
  488. json={
  489. "ha_enabled": True,
  490. "ha_url": "http://database-url:8123",
  491. "ha_token": "database-token",
  492. },
  493. )
  494. # Verify database values are set
  495. response = await async_client.get("/api/v1/settings/")
  496. result = response.json()
  497. assert result["ha_url"] == "http://database-url:8123"
  498. assert result["ha_token"] == "database-token"
  499. # Now set environment variables
  500. os.environ["HA_URL"] = "http://env-url/core"
  501. os.environ["HA_TOKEN"] = "env-token-xyz"
  502. try:
  503. response = await async_client.get("/api/v1/settings/")
  504. result = response.json()
  505. # Verify env vars override database
  506. assert result["ha_url"] == "http://env-url/core"
  507. assert result["ha_token"] == "env-token-xyz"
  508. assert result["ha_url_from_env"] is True
  509. assert result["ha_token_from_env"] is True
  510. assert result["ha_env_managed"] is True
  511. assert result["ha_enabled"] is True
  512. finally:
  513. os.environ.pop("HA_URL", None)
  514. os.environ.pop("HA_TOKEN", None)
  515. # Verify database values are still there after removing env vars
  516. response = await async_client.get("/api/v1/settings/")
  517. result = response.json()
  518. assert result["ha_url"] == "http://database-url:8123"
  519. assert result["ha_token"] == "database-token"
  520. assert result["ha_url_from_env"] is False
  521. assert result["ha_token_from_env"] is False
  522. @pytest.mark.asyncio
  523. @pytest.mark.integration
  524. async def test_ha_settings_database_updates_accepted_but_ignored(self, async_client: AsyncClient):
  525. """Verify database updates are accepted but have no effect when env vars are set."""
  526. # Set environment variables
  527. os.environ["HA_URL"] = "http://supervisor/core"
  528. os.environ["HA_TOKEN"] = "env-token"
  529. try:
  530. # Attempt to update via API
  531. response = await async_client.put(
  532. "/api/v1/settings/",
  533. json={
  534. "ha_url": "http://different-url:8123",
  535. "ha_token": "different-token",
  536. },
  537. )
  538. # Update should succeed
  539. assert response.status_code == 200
  540. # But values should still be from env vars
  541. result = response.json()
  542. assert result["ha_url"] == "http://supervisor/core"
  543. assert result["ha_token"] == "env-token"
  544. assert result["ha_url_from_env"] is True
  545. assert result["ha_token_from_env"] is True
  546. finally:
  547. os.environ.pop("HA_URL", None)
  548. os.environ.pop("HA_TOKEN", None)
  549. @pytest.mark.asyncio
  550. @pytest.mark.integration
  551. async def test_ha_settings_empty_env_vars_treated_as_not_set(self, async_client: AsyncClient):
  552. """Verify empty environment variables are treated as not set."""
  553. # Set empty env vars
  554. os.environ["HA_URL"] = ""
  555. os.environ["HA_TOKEN"] = ""
  556. try:
  557. response = await async_client.get("/api/v1/settings/")
  558. result = response.json()
  559. # Empty env vars should be treated as not set
  560. assert result["ha_url_from_env"] is False
  561. assert result["ha_token_from_env"] is False
  562. assert result["ha_env_managed"] is False
  563. finally:
  564. os.environ.pop("HA_URL", None)
  565. os.environ.pop("HA_TOKEN", None)
  566. @pytest.mark.asyncio
  567. @pytest.mark.integration
  568. async def test_ha_settings_can_be_updated_normally_without_env_vars(self, async_client: AsyncClient):
  569. """Verify HA settings can be updated normally when env vars are not set."""
  570. # Ensure no env vars
  571. os.environ.pop("HA_URL", None)
  572. os.environ.pop("HA_TOKEN", None)
  573. # Update HA settings
  574. response = await async_client.put(
  575. "/api/v1/settings/",
  576. json={
  577. "ha_enabled": True,
  578. "ha_url": "http://192.168.1.100:8123",
  579. "ha_token": "my-long-lived-token",
  580. },
  581. )
  582. assert response.status_code == 200
  583. result = response.json()
  584. assert result["ha_enabled"] is True
  585. assert result["ha_url"] == "http://192.168.1.100:8123"
  586. assert result["ha_token"] == "my-long-lived-token"
  587. assert result["ha_url_from_env"] is False
  588. assert result["ha_token_from_env"] is False
  589. assert result["ha_env_managed"] is False
  590. # Verify persistence
  591. response = await async_client.get("/api/v1/settings/")
  592. result = response.json()
  593. assert result["ha_enabled"] is True
  594. assert result["ha_url"] == "http://192.168.1.100:8123"
  595. assert result["ha_token"] == "my-long-lived-token"
  596. class TestSimplifiedBackupRestore:
  597. """Integration tests for the simplified backup/restore endpoints (ZIP-based).
  598. Note: Tests that require actual file operations (backup creation) are skipped
  599. because the test suite uses an in-memory database. These tests focus on
  600. validation and error handling which don't require file I/O.
  601. """
  602. @pytest.mark.asyncio
  603. @pytest.mark.integration
  604. async def test_restore_requires_zip_file(self, async_client: AsyncClient):
  605. """Verify restore rejects non-ZIP files."""
  606. files = {"file": ("backup.txt", b"not a zip file", "text/plain")}
  607. response = await async_client.post("/api/v1/settings/restore", files=files)
  608. assert response.status_code == 400
  609. assert "zip" in response.json()["detail"].lower()
  610. @pytest.mark.asyncio
  611. @pytest.mark.integration
  612. async def test_restore_requires_database_in_zip(self, async_client: AsyncClient):
  613. """Verify restore rejects ZIP without database file."""
  614. import io
  615. import zipfile
  616. # Create a ZIP without bambuddy.db
  617. zip_buffer = io.BytesIO()
  618. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  619. zf.writestr("dummy.txt", "dummy content")
  620. zip_buffer.seek(0)
  621. files = {"file": ("backup.zip", zip_buffer.read(), "application/zip")}
  622. response = await async_client.post("/api/v1/settings/restore", files=files)
  623. assert response.status_code == 400
  624. assert "missing bambuddy.db" in response.json()["detail"].lower()
  625. @pytest.mark.asyncio
  626. @pytest.mark.integration
  627. async def test_restore_invalid_zip(self, async_client: AsyncClient):
  628. """Verify restore rejects corrupted ZIP files."""
  629. files = {"file": ("backup.zip", b"not valid zip content", "application/zip")}
  630. response = await async_client.post("/api/v1/settings/restore", files=files)
  631. assert response.status_code == 400
  632. assert "not a valid zip" in response.json()["detail"].lower()