diff --git a/pyproject.toml b/pyproject.toml index d92f837b..0b06c0f1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "devplacepy" -version = "1.1.32" +version = "1.1.33" description = "DevPlace - The Developer Social Network" requires-python = ">=3.12" dependencies = [ diff --git a/tests/unit/push.py b/tests/unit/push.py index 83cc376e..bab81ba9 100644 --- a/tests/unit/push.py +++ b/tests/unit/push.py @@ -136,6 +136,39 @@ def test_apns_prepare_survives_an_empty_payload(): body = json.loads(providers.PROVIDERS["apns"].prepare({})) assert body["aps"]["alert"]["title"] == "DevPlace" assert body["url"] == "/notifications" + assert "badge" not in body["aps"] + + +def test_apns_prepare_maps_unread_to_the_icon_badge(): + import json + from devplacepy.push import providers + + apns = providers.PROVIDERS["apns"] + assert json.loads(apns.prepare({"unread": 7}))["aps"]["badge"] == 7 + assert json.loads(apns.prepare({"unread": 0}))["aps"]["badge"] == 0 + + +def test_apns_prepare_omits_the_badge_for_an_invalid_unread_count(): + import json + from devplacepy.push import providers + + apns = providers.PROVIDERS["apns"] + for unread in (None, -1, True, "3", 2.5): + assert "badge" not in json.loads(apns.prepare({"unread": unread}))["aps"] + + +def test_webpush_prepare_forwards_the_shared_payload_unchanged(): + import json + from devplacepy.push import providers + + payload = { + "title": "DevPlace", + "message": "hi", + "icon": "/static/apple-touch-icon.png", + "url": "/notifications", + "unread": 3, + } + assert json.loads(providers.PROVIDERS["webpush"].prepare(payload)) == payload def _apns_settings(monkeypatch, **values): diff --git a/tests/unit/utils.py b/tests/unit/utils.py index bd42d3b3..133ca0fd 100644 --- a/tests/unit/utils.py +++ b/tests/unit/utils.py @@ -4,6 +4,9 @@ from devplacepy.database import ( get_table, get_primary_admin_uid, invalidate_admins_cache, + set_notification_grouping_enabled, + set_notification_pref, + set_notification_throttle_enabled, ) from devplacepy.utils import ( award_badge, @@ -11,6 +14,7 @@ from devplacepy.utils import ( get_badge, check_milestone_badges, create_mention_notifications, + create_notification, extract_mentions, generate_uid, hash_password, @@ -298,3 +302,51 @@ def test_create_mention_notifications_targets_known_users(local_db): ) assert get_table("notifications").count(user_uid=target_uid, type="mention") == 1 assert get_table("notifications").count(user_uid=actor_uid, type="mention") == 0 + + +def _capture_push_unread(monkeypatch): + sent = [] + monkeypatch.setattr( + "devplacepy.utils.notifications._schedule_push", + lambda user_uid, message, target_url, unread: sent.append(unread), + ) + return sent + + +def test_push_payload_counts_unread_rows_for_single_notifications(local_db, monkeypatch): + uid, _ = _seed_user() + sent = _capture_push_unread(monkeypatch) + create_notification(uid, "badge", "first", uid, "/profile/x") + create_notification(uid, "badge", "second", uid, "/profile/x") + assert sent == [1, 2] + + +def test_push_payload_counts_groups_when_grouping_is_on(local_db, monkeypatch): + uid, _ = _seed_user() + actor_uid, _ = _seed_user() + set_notification_throttle_enabled(uid, False) + sent = _capture_push_unread(monkeypatch) + create_notification(uid, "mention", "one", actor_uid, "/posts/x") + create_notification(uid, "mention", "two", actor_uid, "/posts/x") + assert sent == [1, 1] + + +def test_push_payload_counts_rows_when_grouping_is_off(local_db, monkeypatch): + uid, _ = _seed_user() + actor_uid, _ = _seed_user() + set_notification_throttle_enabled(uid, False) + set_notification_grouping_enabled(uid, False) + sent = _capture_push_unread(monkeypatch) + create_notification(uid, "mention", "one", actor_uid, "/posts/x") + create_notification(uid, "mention", "two", actor_uid, "/posts/x") + assert sent == [1, 2] + + +def test_push_payload_keeps_the_unread_count_when_in_app_is_off(local_db, monkeypatch): + uid, _ = _seed_user() + sent = _capture_push_unread(monkeypatch) + create_notification(uid, "badge", "stored", uid, "/profile/x") + set_notification_pref(uid, "level", "in_app", False) + create_notification(uid, "level", "not stored", uid, "/profile/x") + assert sent == [1, 1] + assert get_table("notifications").count(user_uid=uid, type="level") == 0