1
0
Fork 0

Merge from 'main' into stable_dates

This commit is contained in:
Adeodato Simó 2023-11-19 14:46:32 -03:00
commit 8dbfba17d6
No known key found for this signature in database
GPG key ID: CDF447845F1A986F
15 changed files with 289 additions and 59 deletions

View file

@ -43,7 +43,7 @@ class Notification(TestCase):
def test_notification(self):
"""New notifications are unread"""
notification = models.Notification.objects.create(
user=self.local_user, notification_type=models.Notification.FAVORITE
user=self.local_user, notification_type=models.NotificationType.FAVORITE
)
self.assertFalse(notification.read)
@ -52,7 +52,7 @@ class Notification(TestCase):
models.Notification.notify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
self.assertTrue(models.Notification.objects.exists())
@ -61,7 +61,7 @@ class Notification(TestCase):
models.Notification.notify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
self.assertEqual(models.Notification.objects.count(), 1)
notification = models.Notification.objects.get()
@ -70,7 +70,7 @@ class Notification(TestCase):
models.Notification.notify(
self.local_user,
self.another_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
self.assertEqual(models.Notification.objects.count(), 1)
notification.refresh_from_db()
@ -92,7 +92,7 @@ class Notification(TestCase):
models.Notification.notify(
self.remote_user,
self.local_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
self.assertFalse(models.Notification.objects.exists())
@ -101,7 +101,7 @@ class Notification(TestCase):
models.Notification.notify(
self.local_user,
self.local_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
self.assertFalse(models.Notification.objects.exists())
@ -154,14 +154,14 @@ class Notification(TestCase):
models.Notification.notify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
self.assertTrue(models.Notification.objects.exists())
models.Notification.unnotify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
self.assertFalse(models.Notification.objects.exists())
@ -170,25 +170,112 @@ class Notification(TestCase):
models.Notification.notify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
models.Notification.notify(
self.local_user,
self.another_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
self.assertTrue(models.Notification.objects.exists())
models.Notification.unnotify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
self.assertTrue(models.Notification.objects.exists())
models.Notification.unnotify(
self.local_user,
self.another_user,
notification_type=models.Notification.FAVORITE,
notification_type=models.NotificationType.FAVORITE,
)
self.assertFalse(models.Notification.objects.exists())
class NotifyInviteRequest(TestCase):
"""let admins know of invite requests"""
def setUp(self):
"""ensure there is one admin"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",
"password",
local=True,
localname="mouse",
is_superuser=True,
)
def test_invite_request_triggers_notification(self):
"""requesting an invite notifies the admin"""
admin = models.User.objects.filter(is_superuser=True).first()
request = models.InviteRequest.objects.create(email="user@example.com")
self.assertEqual(models.Notification.objects.count(), 1)
notification = models.Notification.objects.first()
self.assertEqual(notification.user, admin)
self.assertEqual(
notification.notification_type, models.NotificationType.INVITE_REQUEST
)
self.assertEqual(notification.related_invite_requests.count(), 1)
self.assertEqual(notification.related_invite_requests.first(), request)
def test_notify_only_created(self):
"""updating an invite request does not trigger a notification"""
request = models.InviteRequest.objects.create(email="user@example.com")
notification = models.Notification.objects.first()
notification.delete()
self.assertEqual(models.Notification.objects.count(), 0)
request.ignored = True
request.save()
self.assertEqual(models.Notification.objects.count(), 0)
def test_notify_grouping(self):
"""invites group into the same notification, until read"""
requests = [
models.InviteRequest.objects.create(email="user1@example.com"),
models.InviteRequest.objects.create(email="user2@example.com"),
]
self.assertEqual(models.Notification.objects.count(), 1)
notification = models.Notification.objects.first()
self.assertEqual(notification.related_invite_requests.count(), 2)
self.assertCountEqual(notification.related_invite_requests.all(), requests)
notification.read = True
notification.save()
request = models.InviteRequest.objects.create(email="user3@example.com")
_, notification = models.Notification.objects.all()
self.assertEqual(models.Notification.objects.count(), 2)
self.assertEqual(notification.related_invite_requests.count(), 1)
self.assertEqual(notification.related_invite_requests.first(), request)
def test_notify_multiple_admins(self):
"""all admins are notified"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"admin@local.com",
"admin@example.com",
"password",
local=True,
localname="root",
is_superuser=True,
)
models.InviteRequest.objects.create(email="user@example.com")
admins = models.User.objects.filter(is_superuser=True).all()
notifications = models.Notification.objects.all()
self.assertEqual(len(notifications), 2)
self.assertCountEqual([notif.user for notif in notifications], admins)