Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions camply/notifications/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def send_campsites(self, campsites: List[AvailableCampsite], **kwargs) -> None:
----------
campsites: List[AvailableCampsite]
"""
if not campsites:
logger.debug("No campsites to send to webhook.")
return
webhook_body = WebhookBody(campsites=campsites).json().encode("utf-8")
response = self.session.post(url=self.webhook_url, data=webhook_body)
try:
Expand Down
16 changes: 8 additions & 8 deletions camply/search/base_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,14 @@ def _continuous_search_retry(
)
logger.info(f"{len(new_campsites)} New Campsites Found.")
self.campsites_found.update(new_campsites)
logged_campsites = list(new_campsites)
self._handle_notifications(
retryer=retryer,
notifier=self.notifier,
logged_campsites=logged_campsites,
continuous_search_attempts=continuous_search_attempts,
notify_first_try=notify_first_try,
)
if new_campsites:
self._handle_notifications(
retryer=retryer,
notifier=self.notifier,
logged_campsites=list(new_campsites),
continuous_search_attempts=continuous_search_attempts,
notify_first_try=notify_first_try,
)
return list(self.campsites_found)

@classmethod
Expand Down
49 changes: 49 additions & 0 deletions tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
Notification Testing
"""

from unittest.mock import MagicMock

from pytest import MonkeyPatch

from camply import AvailableCampsite
from camply.config.notification_config import WebhookConfig
from camply.notifications import PushoverNotifications
from camply.notifications.webhook import WebhookNotifications
from camply.search.base_search import BaseCampingSearch
from tests.conftest import vcr_cassette


Expand All @@ -23,3 +30,45 @@ def test_pushover_campsite(available_campsite: AvailableCampsite):
"""
pusher = PushoverNotifications()
pusher.send_campsites(campsites=[available_campsite])


def test_no_notifications_when_no_new_campsites():
"""
Verify that notifications are not sent when no new campsites are found
"""
self_mock = MagicMock(spec=BaseCampingSearch)
self_mock._get_polling_minutes.return_value = 10
self_mock._search_matching_campsites_available.return_value = ["campsite1"]
self_mock.campsites_found = {"campsite1"}

BaseCampingSearch._continuous_search_retry(
self=self_mock,
log=False,
verbose=False,
polling_interval=10,
continuous_search_attempts=1,
notification_provider="silent",
notify_first_try=False,
search_once=True,
)

self_mock.assemble_availabilities.assert_called_once_with(
matching_data=[], log=False, verbose=False
)
self_mock._handle_notifications.assert_not_called()


def test_webhook_notifier_empty_campsites(monkeypatch: MonkeyPatch):
"""
Verify WebhookNotifications does not send empty campsite lists
"""
monkeypatch.setattr(WebhookConfig, "WEBHOOK_URL", "http://example.com/webhook")

mock_session = MagicMock()

notifier = WebhookNotifications()
notifier.session = mock_session

notifier.send_campsites([])

mock_session.post.assert_not_called()
Loading