test_pushover_priority.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """Tests for Pushover emergency-priority (2) retry/expire handling (#2586).
  2. Pushover rejects a priority-2 (Emergency) message unless it also carries
  3. ``retry`` and ``expire``. These tests pin that we send those params at
  4. priority 2 (clamped to Pushover's legal 30..10800 range) and omit them at
  5. every other priority.
  6. """
  7. import httpx
  8. import pytest
  9. from backend.app.services.notification_service import NotificationService
  10. class _CaptureClient:
  11. """Minimal stand-in for httpx.AsyncClient that records the posted data."""
  12. def __init__(self):
  13. self.is_closed = False
  14. self.last_data: dict | None = None
  15. async def post(self, url, data=None, files=None):
  16. self.last_data = data
  17. return httpx.Response(200, json={"status": 1})
  18. @pytest.fixture
  19. def service_with_capture():
  20. service = NotificationService()
  21. client = _CaptureClient()
  22. service._http_client = client # bypass real HTTP
  23. return service, client
  24. BASE_CONFIG = {"user_key": "u" * 30, "app_token": "a" * 30}
  25. @pytest.mark.asyncio
  26. async def test_priority_2_includes_retry_and_expire(service_with_capture):
  27. service, client = service_with_capture
  28. ok, _ = await service._send_pushover({**BASE_CONFIG, "priority": 2, "retry": 90, "expire": 7200}, "T", "M")
  29. assert ok
  30. assert client.last_data["priority"] == 2
  31. assert client.last_data["retry"] == 90
  32. assert client.last_data["expire"] == 7200
  33. @pytest.mark.asyncio
  34. async def test_priority_2_uses_defaults_when_unset(service_with_capture):
  35. service, client = service_with_capture
  36. ok, _ = await service._send_pushover({**BASE_CONFIG, "priority": 2}, "T", "M")
  37. assert ok
  38. assert client.last_data["retry"] == 60
  39. assert client.last_data["expire"] == 3600
  40. @pytest.mark.asyncio
  41. async def test_priority_2_clamps_to_pushover_limits(service_with_capture):
  42. service, client = service_with_capture
  43. ok, _ = await service._send_pushover({**BASE_CONFIG, "priority": 2, "retry": 5, "expire": 999999}, "T", "M")
  44. assert ok
  45. assert client.last_data["retry"] == 30 # min 30
  46. assert client.last_data["expire"] == 10800 # max 10800
  47. @pytest.mark.asyncio
  48. async def test_priority_2_tolerates_string_values(service_with_capture):
  49. service, client = service_with_capture
  50. ok, _ = await service._send_pushover({**BASE_CONFIG, "priority": "2", "retry": "120", "expire": "1800"}, "T", "M")
  51. assert ok
  52. assert client.last_data["priority"] == 2
  53. assert client.last_data["retry"] == 120
  54. assert client.last_data["expire"] == 1800
  55. @pytest.mark.asyncio
  56. @pytest.mark.parametrize("priority", [-2, -1, 0, 1])
  57. async def test_non_emergency_priority_omits_retry_and_expire(service_with_capture, priority):
  58. service, client = service_with_capture
  59. ok, _ = await service._send_pushover({**BASE_CONFIG, "priority": priority, "retry": 90, "expire": 7200}, "T", "M")
  60. assert ok
  61. assert "retry" not in client.last_data
  62. assert "expire" not in client.last_data