feat: add test notif button on admin

This commit is contained in:
2026-07-14 21:30:22 +02:00
parent f783794b86
commit 5fbe70b6db
3 changed files with 162 additions and 1 deletions
+78
View File
@@ -486,3 +486,81 @@ class SendRemindersTests(TrackerTestCase):
webpush.side_effect = None
self.run_command("08:10")
self.assertEqual(ReminderSent.objects.count(), 1)
@override_settings(
VAPID_PUBLIC_KEY="BMn-test-public-key",
VAPID_PRIVATE_KEY="DiXsYTkxHVb68y2-9CGUZheAe7Iah9cLZ_vXc9DbJ5w",
)
class PushSubscriptionAdminTests(TrackerTestCase):
def setUp(self):
super().setUp()
self.client.force_login(self.user)
self.subscription = PushSubscription.objects.create(
user=self.user,
endpoint="https://push.example/phone",
p256dh="key",
auth="secret",
)
self.url = reverse(
"admin:accounts_pushsubscription_change", args=[self.subscription.pk]
)
def messages_from(self, response) -> list[str]:
return [str(m) for m in response.context["messages"]]
def test_the_change_page_offers_the_button(self):
response = self.client.get(self.url)
self.assertContains(response, "_send_test")
self.assertContains(response, "Send test notification")
@patch("tracker.push.webpush")
def test_the_button_pushes_to_that_device(self, webpush):
response = self.client.post(self.url, {"_send_test": ""}, follow=True)
webpush.assert_called_once()
sent_to = webpush.call_args.kwargs["subscription_info"]
self.assertEqual(sent_to["endpoint"], self.subscription.endpoint)
self.assertIn("Test notification sent", " ".join(self.messages_from(response)))
@patch("tracker.push.webpush")
def test_a_dead_device_is_reported_and_removed(self, webpush):
webpush.side_effect = WebPushException(
"gone", response=type("R", (), {"status_code": 410})()
)
response = self.client.post(self.url, {"_send_test": ""}, follow=True)
self.assertFalse(
PushSubscription.objects.filter(pk=self.subscription.pk).exists()
)
self.assertIn("has been removed", " ".join(self.messages_from(response)))
@patch("tracker.push.webpush")
def test_the_bulk_action_pushes_to_each_selected_device(self, webpush):
other = PushSubscription.objects.create(
user=self.user,
endpoint="https://push.example/tablet",
p256dh="key",
auth="secret",
)
response = self.client.post(
reverse("admin:accounts_pushsubscription_changelist"),
{
"action": "send_test_notification",
"_selected_action": [self.subscription.pk, other.pk],
},
follow=True,
)
self.assertEqual(webpush.call_count, 2)
self.assertEqual(len(self.messages_from(response)), 2)
@override_settings(VAPID_PUBLIC_KEY="", VAPID_PRIVATE_KEY="")
@patch("tracker.push.webpush")
def test_it_says_so_when_the_server_has_no_keys(self, webpush):
response = self.client.post(self.url, {"_send_test": ""}, follow=True)
webpush.assert_not_called()
self.assertIn("VAPID keys are not set", " ".join(self.messages_from(response)))