test_user_notifications_api.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Integration tests for User Notifications API endpoints.
  2. Tests the full request/response cycle for /api/v1/user-notifications/ endpoints.
  3. """
  4. import pytest
  5. from httpx import AsyncClient
  6. class TestUserNotificationsAPI:
  7. """Integration tests for /api/v1/user-notifications/ endpoints."""
  8. # ========================================================================
  9. # GET /preferences — no auth
  10. # ========================================================================
  11. @pytest.mark.asyncio
  12. @pytest.mark.integration
  13. async def test_get_preferences_returns_defaults_when_no_auth(self, async_client: AsyncClient):
  14. """Without auth, GET should return all-enabled defaults."""
  15. response = await async_client.get("/api/v1/user-notifications/preferences")
  16. assert response.status_code == 200
  17. data = response.json()
  18. assert data["notify_print_start"] is True
  19. assert data["notify_print_complete"] is True
  20. assert data["notify_print_failed"] is True
  21. assert data["notify_print_stopped"] is True
  22. # ========================================================================
  23. # PUT /preferences — no auth
  24. # ========================================================================
  25. @pytest.mark.asyncio
  26. @pytest.mark.integration
  27. async def test_update_preferences_fails_without_auth(self, async_client: AsyncClient):
  28. """Without auth enabled, PUT should return 400 (no user context)."""
  29. data = {
  30. "notify_print_start": False,
  31. "notify_print_complete": True,
  32. "notify_print_failed": True,
  33. "notify_print_stopped": False,
  34. }
  35. response = await async_client.put("/api/v1/user-notifications/preferences", json=data)
  36. assert response.status_code == 400
  37. assert "Authentication must be enabled" in response.json()["detail"]
  38. # ========================================================================
  39. # Schema validation
  40. # ========================================================================
  41. @pytest.mark.asyncio
  42. @pytest.mark.integration
  43. async def test_update_preferences_rejects_missing_fields(self, async_client: AsyncClient):
  44. """PUT should reject requests missing required boolean fields."""
  45. data = {
  46. "notify_print_start": True,
  47. # missing other fields
  48. }
  49. response = await async_client.put("/api/v1/user-notifications/preferences", json=data)
  50. assert response.status_code == 422 # Pydantic validation error
  51. @pytest.mark.asyncio
  52. @pytest.mark.integration
  53. async def test_update_preferences_rejects_invalid_type(self, async_client: AsyncClient):
  54. """PUT should reject values that cannot be coerced to boolean."""
  55. data = {
  56. "notify_print_start": [1, 2, 3],
  57. "notify_print_complete": True,
  58. "notify_print_failed": True,
  59. "notify_print_stopped": True,
  60. }
  61. response = await async_client.put("/api/v1/user-notifications/preferences", json=data)
  62. assert response.status_code == 422