test_settings_api.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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