test_settings_api.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. """Integration tests for Settings API endpoints.
  2. Tests the full request/response cycle for /api/v1/settings/ endpoints.
  3. """
  4. import pytest
  5. from httpx import AsyncClient
  6. class TestSettingsAPI:
  7. """Integration tests for /api/v1/settings/ endpoints."""
  8. # ========================================================================
  9. # Get settings
  10. # ========================================================================
  11. @pytest.mark.asyncio
  12. @pytest.mark.integration
  13. async def test_get_settings(self, async_client: AsyncClient):
  14. """Verify settings can be retrieved."""
  15. response = await async_client.get("/api/v1/settings/")
  16. assert response.status_code == 200
  17. result = response.json()
  18. # Check for actual settings fields
  19. assert "auto_archive" in result
  20. assert "currency" in result
  21. assert "date_format" in result
  22. @pytest.mark.asyncio
  23. @pytest.mark.integration
  24. async def test_get_settings_has_defaults(self, async_client: AsyncClient):
  25. """Verify default settings values are returned."""
  26. response = await async_client.get("/api/v1/settings/")
  27. assert response.status_code == 200
  28. result = response.json()
  29. # Verify some default values
  30. assert isinstance(result["auto_archive"], bool)
  31. assert isinstance(result["currency"], str)
  32. # ========================================================================
  33. # Update settings
  34. # ========================================================================
  35. @pytest.mark.asyncio
  36. @pytest.mark.integration
  37. async def test_update_auto_archive(self, async_client: AsyncClient):
  38. """Verify auto_archive can be updated."""
  39. # First get current value
  40. response = await async_client.get("/api/v1/settings/")
  41. original = response.json()["auto_archive"]
  42. # Update to opposite value
  43. new_value = not original
  44. response = await async_client.put("/api/v1/settings/", json={"auto_archive": new_value})
  45. assert response.status_code == 200
  46. assert response.json()["auto_archive"] == new_value
  47. @pytest.mark.asyncio
  48. @pytest.mark.integration
  49. async def test_update_currency(self, async_client: AsyncClient):
  50. """Verify currency can be updated."""
  51. response = await async_client.put("/api/v1/settings/", json={"currency": "EUR"})
  52. assert response.status_code == 200
  53. assert response.json()["currency"] == "EUR"
  54. @pytest.mark.asyncio
  55. @pytest.mark.integration
  56. async def test_update_date_format(self, async_client: AsyncClient):
  57. """Verify date format can be updated."""
  58. response = await async_client.put("/api/v1/settings/", json={"date_format": "eu"})
  59. assert response.status_code == 200
  60. assert response.json()["date_format"] == "eu"
  61. @pytest.mark.asyncio
  62. @pytest.mark.integration
  63. async def test_update_time_format(self, async_client: AsyncClient):
  64. """Verify time format can be updated."""
  65. response = await async_client.put("/api/v1/settings/", json={"time_format": "24h"})
  66. assert response.status_code == 200
  67. assert response.json()["time_format"] == "24h"
  68. @pytest.mark.asyncio
  69. @pytest.mark.integration
  70. async def test_update_filament_cost(self, async_client: AsyncClient):
  71. """Verify default filament cost can be updated."""
  72. response = await async_client.put("/api/v1/settings/", json={"default_filament_cost": 30.0})
  73. assert response.status_code == 200
  74. assert response.json()["default_filament_cost"] == 30.0
  75. @pytest.mark.asyncio
  76. @pytest.mark.integration
  77. async def test_update_energy_cost(self, async_client: AsyncClient):
  78. """Verify energy cost can be updated."""
  79. response = await async_client.put("/api/v1/settings/", json={"energy_cost_per_kwh": 0.20})
  80. assert response.status_code == 200
  81. assert response.json()["energy_cost_per_kwh"] == 0.20
  82. @pytest.mark.asyncio
  83. @pytest.mark.integration
  84. async def test_update_multiple_settings(self, async_client: AsyncClient):
  85. """Verify multiple settings can be updated at once."""
  86. response = await async_client.put(
  87. "/api/v1/settings/",
  88. json={
  89. "currency": "GBP",
  90. "date_format": "iso",
  91. "time_format": "12h",
  92. "save_thumbnails": False,
  93. },
  94. )
  95. assert response.status_code == 200
  96. result = response.json()
  97. assert result["currency"] == "GBP"
  98. assert result["date_format"] == "iso"
  99. assert result["time_format"] == "12h"
  100. assert result["save_thumbnails"] is False
  101. @pytest.mark.asyncio
  102. @pytest.mark.integration
  103. async def test_update_spoolman_settings(self, async_client: AsyncClient):
  104. """Verify Spoolman settings can be updated."""
  105. response = await async_client.put(
  106. "/api/v1/settings/",
  107. json={
  108. "spoolman_enabled": True,
  109. "spoolman_url": "http://localhost:7912",
  110. "spoolman_sync_mode": "manual",
  111. },
  112. )
  113. assert response.status_code == 200
  114. result = response.json()
  115. assert result["spoolman_enabled"] is True
  116. assert result["spoolman_url"] == "http://localhost:7912"
  117. assert result["spoolman_sync_mode"] == "manual"
  118. @pytest.mark.asyncio
  119. @pytest.mark.integration
  120. async def test_update_ams_thresholds(self, async_client: AsyncClient):
  121. """Verify AMS threshold settings can be updated."""
  122. response = await async_client.put(
  123. "/api/v1/settings/",
  124. json={
  125. "ams_humidity_good": 35,
  126. "ams_humidity_fair": 55,
  127. "ams_temp_good": 25.0,
  128. "ams_temp_fair": 32.0,
  129. },
  130. )
  131. assert response.status_code == 200
  132. result = response.json()
  133. assert result["ams_humidity_good"] == 35
  134. assert result["ams_humidity_fair"] == 55
  135. assert result["ams_temp_good"] == 25.0
  136. assert result["ams_temp_fair"] == 32.0
  137. @pytest.mark.asyncio
  138. @pytest.mark.integration
  139. async def test_update_notification_language(self, async_client: AsyncClient):
  140. """Verify notification language can be updated."""
  141. response = await async_client.put("/api/v1/settings/", json={"notification_language": "de"})
  142. assert response.status_code == 200
  143. assert response.json()["notification_language"] == "de"
  144. # ========================================================================
  145. # Settings persistence tests
  146. # ========================================================================
  147. @pytest.mark.asyncio
  148. @pytest.mark.integration
  149. async def test_update_theme_settings(self, async_client: AsyncClient):
  150. """Verify theme settings can be updated."""
  151. response = await async_client.put(
  152. "/api/v1/settings/",
  153. json={
  154. "dark_style": "glow",
  155. "dark_background": "forest",
  156. "dark_accent": "teal",
  157. "light_style": "vibrant",
  158. "light_background": "warm",
  159. "light_accent": "blue",
  160. },
  161. )
  162. assert response.status_code == 200
  163. result = response.json()
  164. assert result["dark_style"] == "glow"
  165. assert result["dark_background"] == "forest"
  166. assert result["dark_accent"] == "teal"
  167. assert result["light_style"] == "vibrant"
  168. assert result["light_background"] == "warm"
  169. assert result["light_accent"] == "blue"
  170. @pytest.mark.asyncio
  171. @pytest.mark.integration
  172. async def test_settings_persist_after_update(self, async_client: AsyncClient):
  173. """CRITICAL: Verify settings changes persist across requests."""
  174. # Update settings
  175. await async_client.put("/api/v1/settings/", json={"currency": "JPY", "check_updates": False})
  176. # Verify persistence in new request
  177. response = await async_client.get("/api/v1/settings/")
  178. result = response.json()
  179. assert result["currency"] == "JPY"
  180. assert result["check_updates"] is False
  181. # ========================================================================
  182. # MQTT settings tests
  183. # ========================================================================
  184. @pytest.mark.asyncio
  185. @pytest.mark.integration
  186. async def test_update_mqtt_settings(self, async_client: AsyncClient):
  187. """Verify MQTT settings can be updated."""
  188. response = await async_client.put(
  189. "/api/v1/settings/",
  190. json={
  191. "mqtt_enabled": True,
  192. "mqtt_broker": "mqtt.example.com",
  193. "mqtt_port": 8883,
  194. "mqtt_username": "testuser",
  195. "mqtt_password": "testpass",
  196. "mqtt_topic_prefix": "myprefix",
  197. "mqtt_use_tls": True,
  198. },
  199. )
  200. assert response.status_code == 200
  201. result = response.json()
  202. assert result["mqtt_enabled"] is True
  203. assert result["mqtt_broker"] == "mqtt.example.com"
  204. assert result["mqtt_port"] == 8883
  205. assert result["mqtt_username"] == "testuser"
  206. assert result["mqtt_password"] == "testpass"
  207. assert result["mqtt_topic_prefix"] == "myprefix"
  208. assert result["mqtt_use_tls"] is True
  209. @pytest.mark.asyncio
  210. @pytest.mark.integration
  211. async def test_mqtt_status_endpoint(self, async_client: AsyncClient):
  212. """Verify MQTT status endpoint returns expected fields."""
  213. response = await async_client.get("/api/v1/settings/mqtt/status")
  214. assert response.status_code == 200
  215. result = response.json()
  216. assert "enabled" in result
  217. assert "connected" in result
  218. assert "broker" in result
  219. assert "port" in result
  220. assert "topic_prefix" in result
  221. @pytest.mark.asyncio
  222. @pytest.mark.integration
  223. async def test_mqtt_defaults(self, async_client: AsyncClient):
  224. """Verify MQTT has correct default values."""
  225. # Reset MQTT settings to defaults
  226. await async_client.put(
  227. "/api/v1/settings/",
  228. json={
  229. "mqtt_enabled": False,
  230. "mqtt_broker": "",
  231. "mqtt_port": 1883,
  232. "mqtt_username": "",
  233. "mqtt_password": "",
  234. "mqtt_topic_prefix": "bambuddy",
  235. "mqtt_use_tls": False,
  236. },
  237. )
  238. response = await async_client.get("/api/v1/settings/")
  239. result = response.json()
  240. assert result["mqtt_enabled"] is False
  241. assert result["mqtt_port"] == 1883
  242. assert result["mqtt_topic_prefix"] == "bambuddy"
  243. assert result["mqtt_use_tls"] is False
  244. # ========================================================================
  245. # Camera settings tests
  246. # ========================================================================
  247. @pytest.mark.asyncio
  248. @pytest.mark.integration
  249. async def test_update_camera_view_mode(self, async_client: AsyncClient):
  250. """Verify camera view mode can be updated."""
  251. response = await async_client.put("/api/v1/settings/", json={"camera_view_mode": "embedded"})
  252. assert response.status_code == 200
  253. assert response.json()["camera_view_mode"] == "embedded"
  254. @pytest.mark.asyncio
  255. @pytest.mark.integration
  256. async def test_camera_view_mode_persists(self, async_client: AsyncClient):
  257. """CRITICAL: Verify camera view mode persists after update."""
  258. # Update to embedded
  259. await async_client.put("/api/v1/settings/", json={"camera_view_mode": "embedded"})
  260. # Verify persistence in new request
  261. response = await async_client.get("/api/v1/settings/")
  262. assert response.json()["camera_view_mode"] == "embedded"
  263. # Update back to window
  264. await async_client.put("/api/v1/settings/", json={"camera_view_mode": "window"})
  265. # Verify persistence
  266. response = await async_client.get("/api/v1/settings/")
  267. assert response.json()["camera_view_mode"] == "window"
  268. @pytest.mark.asyncio
  269. @pytest.mark.integration
  270. async def test_camera_view_mode_default(self, async_client: AsyncClient):
  271. """Verify camera view mode has correct default value."""
  272. # Reset by requesting settings (default should be 'window')
  273. response = await async_client.get("/api/v1/settings/")
  274. result = response.json()
  275. assert "camera_view_mode" in result
  276. # Default is 'window' as defined in schema
  277. assert result["camera_view_mode"] in ["window", "embedded"]
  278. # ========================================================================
  279. # Per-printer mapping settings tests
  280. # ========================================================================
  281. @pytest.mark.asyncio
  282. @pytest.mark.integration
  283. async def test_update_per_printer_mapping_expanded(self, async_client: AsyncClient):
  284. """Verify per_printer_mapping_expanded can be updated."""
  285. response = await async_client.put("/api/v1/settings/", json={"per_printer_mapping_expanded": True})
  286. assert response.status_code == 200
  287. assert response.json()["per_printer_mapping_expanded"] is True
  288. @pytest.mark.asyncio
  289. @pytest.mark.integration
  290. async def test_per_printer_mapping_expanded_persists(self, async_client: AsyncClient):
  291. """CRITICAL: Verify per_printer_mapping_expanded persists after update."""
  292. # Update to True
  293. await async_client.put("/api/v1/settings/", json={"per_printer_mapping_expanded": True})
  294. # Verify persistence in new request
  295. response = await async_client.get("/api/v1/settings/")
  296. assert response.json()["per_printer_mapping_expanded"] is True
  297. # Update back to False
  298. await async_client.put("/api/v1/settings/", json={"per_printer_mapping_expanded": False})
  299. # Verify persistence
  300. response = await async_client.get("/api/v1/settings/")
  301. assert response.json()["per_printer_mapping_expanded"] is False
  302. @pytest.mark.asyncio
  303. @pytest.mark.integration
  304. async def test_per_printer_mapping_expanded_default(self, async_client: AsyncClient):
  305. """Verify per_printer_mapping_expanded has correct default value."""
  306. response = await async_client.get("/api/v1/settings/")
  307. result = response.json()
  308. assert "per_printer_mapping_expanded" in result
  309. # Default is False as defined in schema
  310. assert isinstance(result["per_printer_mapping_expanded"], bool)
  311. # ========================================================================
  312. # Backup/Restore tests
  313. # ========================================================================
  314. @pytest.mark.asyncio
  315. @pytest.mark.integration
  316. async def test_backup_includes_external_camera_settings(self, async_client: AsyncClient, printer_factory):
  317. """Verify backup includes external camera settings for printers."""
  318. # Create a printer with external camera settings
  319. _printer = await printer_factory(
  320. name="Camera Test Printer",
  321. external_camera_url="/dev/video0",
  322. external_camera_type="usb",
  323. external_camera_enabled=True,
  324. )
  325. # Request backup with printers
  326. response = await async_client.get("/api/v1/settings/backup?include_printers=true")
  327. assert response.status_code == 200
  328. backup = response.json()
  329. # Find the printer in the backup
  330. assert "printers" in backup
  331. printer_data = next((p for p in backup["printers"] if p["name"] == "Camera Test Printer"), None)
  332. assert printer_data is not None
  333. # Verify external camera fields are included
  334. assert "external_camera_url" in printer_data
  335. assert "external_camera_type" in printer_data
  336. assert "external_camera_enabled" in printer_data
  337. assert printer_data["external_camera_url"] == "/dev/video0"
  338. assert printer_data["external_camera_type"] == "usb"
  339. assert printer_data["external_camera_enabled"] is True
  340. @pytest.mark.asyncio
  341. @pytest.mark.integration
  342. async def test_restore_external_camera_settings_overwrite(self, async_client: AsyncClient, printer_factory):
  343. """Verify restore with overwrite updates external camera settings."""
  344. import io
  345. # Create a printer without camera settings
  346. printer = await printer_factory(
  347. name="Restore Test",
  348. external_camera_url=None,
  349. external_camera_type=None,
  350. external_camera_enabled=False,
  351. )
  352. # Create backup data with camera settings
  353. backup_data = {
  354. "version": "1.0",
  355. "included": ["printers"],
  356. "printers": [
  357. {
  358. "name": "Restore Test",
  359. "serial_number": printer.serial_number,
  360. "ip_address": printer.ip_address,
  361. "external_camera_url": "/dev/video1",
  362. "external_camera_type": "usb",
  363. "external_camera_enabled": True,
  364. }
  365. ],
  366. }
  367. # Restore with overwrite
  368. import json
  369. files = {"file": ("backup.json", io.BytesIO(json.dumps(backup_data).encode()), "application/json")}
  370. response = await async_client.post("/api/v1/settings/restore?overwrite=true", files=files)
  371. assert response.status_code == 200
  372. result = response.json()
  373. assert result["success"] is True
  374. # Verify the printer was updated
  375. response = await async_client.get(f"/api/v1/printers/{printer.id}")
  376. assert response.status_code == 200
  377. updated_printer = response.json()
  378. assert updated_printer["external_camera_url"] == "/dev/video1"
  379. assert updated_printer["external_camera_type"] == "usb"
  380. assert updated_printer["external_camera_enabled"] is True