notify/tests/test_unread_signal.py

156 lines
6.0 KiB
Python

from datetime import timedelta
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.utils import timezone
from notify.models import DeliveryChannel
from notify.services import create_notification, unread_count
from notify.signals import browser_notification_created, unread_count_changed
User = get_user_model()
class UnreadCountSignalTests(TestCase):
def setUp(self):
self.user = User.objects.create_user("alice", "alice@example.com", "pass")
self.client.force_login(self.user)
self.users = []
unread_count_changed.connect(self._record)
def tearDown(self):
unread_count_changed.disconnect(self._record)
def _record(self, sender, user, **kwargs):
self.users.append(user.pk)
def test_browser_create_read_and_delete(self):
with self.captureOnCommitCallbacks(execute=True):
notification = create_notification(
title="Hi",
recipients=[{"user": self.user, "channels": [{"channel": DeliveryChannel.BROWSER}]}],
)
self.assertEqual(self.users, [self.user.pk])
recipient = notification.recipients.get()
with self.captureOnCommitCallbacks(execute=True):
recipient.make_read()
self.assertEqual(self.users, [self.user.pk, self.user.pk])
with self.captureOnCommitCallbacks(execute=True):
recipient.make_unread()
with self.captureOnCommitCallbacks(execute=True):
notification.delete()
self.assertEqual(self.users, [self.user.pk, self.user.pk, self.user.pk, self.user.pk])
def test_email_does_not_send(self):
with patch("notify.tasks.deliver_notification.apply_async"):
with self.captureOnCommitCallbacks(execute=True):
create_notification(
title="Mail",
recipients=[
{
"user": self.user,
"channels": [{"channel": DeliveryChannel.EMAIL, "target": {"email": "alice@example.com"}}],
}
],
)
self.assertEqual(self.users, [])
def test_make_all_read_sends_once(self):
with self.captureOnCommitCallbacks(execute=True):
create_notification(
title="One",
recipients=[{"user": self.user, "channels": [{"channel": DeliveryChannel.BROWSER}]}],
)
create_notification(
title="Two",
recipients=[{"user": self.user, "channels": [{"channel": DeliveryChannel.BROWSER}]}],
)
self.users.clear()
with self.captureOnCommitCallbacks(execute=True):
response = self.client.post("/api/make-all-read/")
self.assertEqual(response.status_code, 200)
self.assertEqual(self.users, [self.user.pk])
class UnreadCountTests(TestCase):
def setUp(self):
self.user = User.objects.create_user("alice", "alice@example.com", "pass")
self.other = User.objects.create_user("bob", "bob@example.com", "pass")
def test_counts_open_unread_browser_notifications(self):
create_notification(
title="Hi",
recipients=[{"user": self.user, "channels": [{"channel": DeliveryChannel.BROWSER}]}],
)
read = create_notification(
title="Read",
recipients=[{"user": self.user, "channels": [{"channel": DeliveryChannel.BROWSER}]}],
)
read.recipients.get().make_read()
create_notification(
title="Old",
expires_at=timezone.now() - timedelta(minutes=1),
recipients=[{"user": self.user, "channels": [{"channel": DeliveryChannel.BROWSER}]}],
)
with patch("notify.tasks.deliver_notification.apply_async"):
create_notification(
title="Mail",
recipients=[
{
"user": self.user,
"channels": [{"channel": DeliveryChannel.EMAIL, "target": {"email": "alice@example.com"}}],
}
],
)
create_notification(
title="Other",
recipients=[{"user": self.other, "channels": [{"channel": DeliveryChannel.BROWSER}]}],
)
self.assertEqual(unread_count(self.user), 1)
self.assertEqual(unread_count(self.other), 1)
self.assertEqual(unread_count(None), 0)
class BrowserNotificationCreatedTests(TestCase):
def setUp(self):
self.user = User.objects.create_user("alice", "alice@example.com", "pass")
self.events = []
browser_notification_created.connect(self._record)
def tearDown(self):
browser_notification_created.disconnect(self._record)
def _record(self, sender, user, notification, **kwargs):
self.events.append((user.pk, notification.pk))
def test_browser_create_sends_user_and_notification(self):
with self.captureOnCommitCallbacks(execute=True):
notification = create_notification(
title="Hi",
recipients=[{"user": self.user, "channels": [{"channel": DeliveryChannel.BROWSER}]}],
)
self.assertEqual(self.events, [(self.user.pk, notification.pk)])
self.events.clear()
with self.captureOnCommitCallbacks(execute=True):
notification.recipients.get().make_read()
self.assertEqual(self.events, [])
def test_email_does_not_send(self):
with patch("notify.tasks.deliver_notification.apply_async"):
with self.captureOnCommitCallbacks(execute=True):
create_notification(
title="Mail",
recipients=[
{
"user": self.user,
"channels": [{"channel": DeliveryChannel.EMAIL, "target": {"email": "alice@example.com"}}],
}
],
)
self.assertEqual(self.events, [])