django-notifications/notifications/tests/tests.py

575 lines
28 KiB
Python
Raw Normal View History

2023-06-24 00:10:23 +00:00
"""
2015-03-29 06:37:27 +00:00
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
2023-06-24 00:10:23 +00:00
"""
2018-05-31 04:05:53 +00:00
# -*- coding: utf-8 -*-
# pylint: disable=too-many-lines,missing-docstring
import json
2023-06-24 00:10:23 +00:00
from datetime import datetime, timezone
2018-05-31 04:05:53 +00:00
2023-06-24 00:10:23 +00:00
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
2018-05-31 04:05:53 +00:00
from django.core.exceptions import ImproperlyConfigured
from django.db import connection
2023-06-24 00:10:23 +00:00
from django.shortcuts import render
from django.template import Context, Template
2023-06-24 00:10:23 +00:00
from django.test import override_settings # noqa
from django.test import RequestFactory, TestCase
from django.test.utils import CaptureQueriesContext
from django.urls import reverse
2023-06-24 00:10:23 +00:00
from django.utils.timezone import localtime
from swapper import load_model
2023-07-07 00:54:47 +00:00
from notifications.signals import notify, notify_handler
2018-05-31 04:05:53 +00:00
2023-06-24 00:10:23 +00:00
Notification = load_model("notifications", "Notification")
User = get_user_model()
2015-03-29 06:37:27 +00:00
2023-05-22 23:26:41 +00:00
MALICIOUS_NEXT_URLS = [
"http://bla.com",
"http://www.bla.com",
"https://bla.com",
"https://www.bla.com",
"ftp://www.bla.com/file.exe",
]
2023-06-24 00:10:23 +00:00
2015-03-29 06:37:27 +00:00
class NotificationTest(TestCase):
2023-06-24 00:10:23 +00:00
"""Django notifications automated tests"""
2015-03-29 06:37:27 +00:00
@override_settings(USE_TZ=True)
2023-06-24 00:10:23 +00:00
@override_settings(TIME_ZONE="Asia/Shanghai")
2015-03-29 06:37:27 +00:00
def test_use_timezone(self):
from_user = User.objects.create(username="from", password="pwd", email="example@example.com")
to_user = User.objects.create(username="to", password="pwd", email="example@example.com")
2023-06-24 00:10:23 +00:00
notify.send(from_user, recipient=to_user, verb="commented", action_object=from_user)
2015-03-29 06:37:27 +00:00
notification = Notification.objects.get(recipient=to_user)
delta = datetime.now(tz=timezone.utc) - localtime(notification.timestamp)
2015-03-29 06:37:27 +00:00
self.assertTrue(delta.seconds < 60)
# The delta between the two events will still be less than a second despite the different timezones
2016-01-01 02:59:51 +00:00
# The call to now and the immediate call afterwards will be within a short period of time, not 8 hours as the
# test above was originally.
2015-03-29 06:37:27 +00:00
@override_settings(USE_TZ=False)
2023-06-24 00:10:23 +00:00
@override_settings(TIME_ZONE="Asia/Shanghai")
2015-03-29 06:37:27 +00:00
def test_disable_timezone(self):
from_user = User.objects.create(username="from2", password="pwd", email="example@example.com")
to_user = User.objects.create(username="to2", password="pwd", email="example@example.com")
2023-06-24 00:10:23 +00:00
notify.send(from_user, recipient=to_user, verb="commented", action_object=from_user)
2015-03-29 06:37:27 +00:00
notification = Notification.objects.get(recipient=to_user)
2023-06-24 00:10:23 +00:00
delta = datetime.now() - notification.timestamp
2015-03-29 06:37:27 +00:00
self.assertTrue(delta.seconds < 60)
def test_humanize_naturalday_timestamp(self):
from_user = User.objects.create(username="from2", password="pwd", email="example@example.com")
to_user = User.objects.create(username="to2", password="pwd", email="example@example.com")
2023-06-24 00:10:23 +00:00
notify.send(from_user, recipient=to_user, verb="commented", action_object=from_user)
notification = Notification.objects.get(recipient=to_user)
2023-06-24 00:10:23 +00:00
self.assertEqual(notification.naturalday(), "today")
def test_humanize_naturaltime_timestamp(self):
from_user = User.objects.create(username="from2", password="pwd", email="example@example.com")
to_user = User.objects.create(username="to2", password="pwd", email="example@example.com")
2023-06-24 00:10:23 +00:00
notify.send(from_user, recipient=to_user, verb="commented", action_object=from_user)
notification = Notification.objects.get(recipient=to_user)
2023-06-24 00:10:23 +00:00
self.assertEqual(notification.naturaltime(), "now")
2015-03-29 06:37:27 +00:00
class NotificationManagersTest(TestCase):
2023-06-24 00:10:23 +00:00
"""Django notifications Manager automated tests"""
def setUp(self):
self.message_count = 10
self.other_user = User.objects.create(username="other1", password="pwd", email="example@example.com")
2015-03-29 06:37:27 +00:00
self.from_user = User.objects.create(username="from2", password="pwd", email="example@example.com")
self.to_user = User.objects.create(username="to2", password="pwd", email="example@example.com")
self.to_group = Group.objects.create(name="to2_g")
2017-06-02 03:12:33 +00:00
self.to_user_list = User.objects.all()
self.to_group.user_set.add(self.to_user)
self.to_group.user_set.add(self.other_user)
2018-05-31 04:05:53 +00:00
for _ in range(self.message_count):
2023-06-24 00:10:23 +00:00
notify.send(self.from_user, recipient=self.to_user, verb="commented", action_object=self.from_user)
# Send notification to group
2023-06-24 00:10:23 +00:00
notify.send(self.from_user, recipient=self.to_group, verb="commented", action_object=self.from_user)
self.message_count += self.to_group.user_set.count()
2017-06-02 03:12:33 +00:00
# Send notification to user list
2023-06-24 00:10:23 +00:00
notify.send(self.from_user, recipient=self.to_user_list, verb="commented", action_object=self.from_user)
2017-06-02 03:18:58 +00:00
self.message_count += len(self.to_user_list)
def test_notify_send_return_val(self):
2023-06-24 00:10:23 +00:00
results = notify.send(self.from_user, recipient=self.to_user, verb="commented", action_object=self.from_user)
2018-05-31 04:05:53 +00:00
for result in results:
if result[0] is notify_handler:
self.assertEqual(len(result[1]), 1)
# only check types for now
2018-05-31 04:05:53 +00:00
self.assertEqual(type(result[1][0]), Notification)
2018-05-31 04:05:53 +00:00
def test_notify_send_return_val_group(self): # pylint: disable=invalid-name
2023-06-24 00:10:23 +00:00
results = notify.send(self.from_user, recipient=self.to_group, verb="commented", action_object=self.from_user)
2018-05-31 04:05:53 +00:00
for result in results:
if result[0] is notify_handler:
self.assertEqual(len(result[1]), self.to_group.user_set.count())
for notification in result[1]:
# only check types for now
2018-05-31 04:05:53 +00:00
self.assertEqual(type(notification), Notification)
2015-03-29 06:37:27 +00:00
def test_unread_manager(self):
self.assertEqual(Notification.objects.unread().count(), self.message_count)
2018-05-31 04:05:53 +00:00
notification = Notification.objects.filter(recipient=self.to_user).first()
notification.mark_as_read()
2023-06-24 00:10:23 +00:00
self.assertEqual(Notification.objects.unread().count(), self.message_count - 1)
2018-05-31 04:05:53 +00:00
for notification in Notification.objects.unread():
self.assertTrue(notification.unread)
2015-03-29 06:37:27 +00:00
def test_read_manager(self):
self.assertEqual(Notification.objects.unread().count(), self.message_count)
2018-05-31 04:05:53 +00:00
notification = Notification.objects.filter(recipient=self.to_user).first()
notification.mark_as_read()
2015-12-11 13:32:20 +00:00
self.assertEqual(Notification.objects.read().count(), 1)
2018-05-31 04:05:53 +00:00
for notification in Notification.objects.read():
self.assertFalse(notification.unread)
2015-03-29 06:37:27 +00:00
def test_mark_all_as_read_manager(self):
self.assertEqual(Notification.objects.unread().count(), self.message_count)
2015-03-29 06:37:27 +00:00
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
self.assertEqual(self.to_user.notifications.unread().count(), 0)
2015-03-29 06:37:27 +00:00
2023-06-24 00:10:23 +00:00
@override_settings(DJANGO_NOTIFICATIONS_CONFIG={"SOFT_DELETE": True}) # pylint: disable=invalid-name
def test_mark_all_as_read_manager_with_soft_delete(self):
# even soft-deleted notifications should be marked as read
# refer: https://github.com/django-notifications/django-notifications/issues/126
2023-06-24 00:10:23 +00:00
to_delete = Notification.objects.filter(recipient=self.to_user).order_by("id")[0]
to_delete.deleted = True
to_delete.save()
2023-06-24 00:10:23 +00:00
self.assertTrue(Notification.objects.filter(recipient=self.to_user).order_by("id")[0].unread)
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
2023-06-24 00:10:23 +00:00
self.assertFalse(Notification.objects.filter(recipient=self.to_user).order_by("id")[0].unread)
2015-03-29 06:37:27 +00:00
def test_mark_all_as_unread_manager(self):
self.assertEqual(Notification.objects.unread().count(), self.message_count)
2015-03-29 06:37:27 +00:00
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
self.assertEqual(self.to_user.notifications.unread().count(), 0)
2015-03-29 06:37:27 +00:00
Notification.objects.filter(recipient=self.to_user).mark_all_as_unread()
self.assertEqual(Notification.objects.unread().count(), self.message_count)
2018-05-31 04:05:53 +00:00
def test_mark_all_deleted_manager_without_soft_delete(self): # pylint: disable=invalid-name
self.assertRaises(ImproperlyConfigured, Notification.objects.active)
self.assertRaises(ImproperlyConfigured, Notification.objects.active)
self.assertRaises(ImproperlyConfigured, Notification.objects.mark_all_as_deleted)
self.assertRaises(ImproperlyConfigured, Notification.objects.mark_all_as_active)
2023-06-24 00:10:23 +00:00
@override_settings(DJANGO_NOTIFICATIONS_CONFIG={"SOFT_DELETE": True})
def test_mark_all_deleted_manager(self):
2018-05-31 04:05:53 +00:00
notification = Notification.objects.filter(recipient=self.to_user).first()
notification.mark_as_read()
self.assertEqual(Notification.objects.read().count(), 1)
2023-06-24 00:10:23 +00:00
self.assertEqual(Notification.objects.unread().count(), self.message_count - 1)
self.assertEqual(Notification.objects.active().count(), self.message_count)
self.assertEqual(Notification.objects.deleted().count(), 0)
Notification.objects.mark_all_as_deleted()
self.assertEqual(Notification.objects.read().count(), 0)
self.assertEqual(Notification.objects.unread().count(), 0)
self.assertEqual(Notification.objects.active().count(), 0)
self.assertEqual(Notification.objects.deleted().count(), self.message_count)
Notification.objects.mark_all_as_active()
self.assertEqual(Notification.objects.read().count(), 1)
2023-06-24 00:10:23 +00:00
self.assertEqual(Notification.objects.unread().count(), self.message_count - 1)
self.assertEqual(Notification.objects.active().count(), self.message_count)
self.assertEqual(Notification.objects.deleted().count(), 0)
class NotificationTestPages(TestCase):
2023-06-24 00:10:23 +00:00
"""Django notifications automated page tests"""
def setUp(self):
self.message_count = 10
self.from_user = User.objects.create_user(username="from", password="pwd", email="example@example.com")
self.to_user = User.objects.create_user(username="to", password="pwd", email="example@example.com")
self.to_user.is_staff = True
self.to_user.save()
2018-05-31 04:05:53 +00:00
for _ in range(self.message_count):
2023-06-24 00:10:23 +00:00
notify.send(self.from_user, recipient=self.to_user, verb="commented", action_object=self.from_user)
def logout(self):
2023-06-24 00:10:23 +00:00
self.client.post(reverse("admin:logout") + "?next=/", {})
2015-12-11 13:32:20 +00:00
def login(self, username, password):
self.logout()
2023-07-06 20:54:22 +00:00
response = self.client.post(reverse("admin:login"), {"username": username, "password": password})
2015-12-11 13:32:20 +00:00
self.assertEqual(response.status_code, 302)
return response
def test_all_messages_page(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
response = self.client.get(reverse("notifications:all"))
2015-12-11 13:32:20 +00:00
self.assertEqual(response.status_code, 200)
2023-06-24 00:10:23 +00:00
self.assertEqual(len(response.context["notifications"]), len(self.to_user.notifications.all()))
def test_unread_messages_pages(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
response = self.client.get(reverse("notifications:unread"))
2015-12-11 13:32:20 +00:00
self.assertEqual(response.status_code, 200)
2023-06-24 00:10:23 +00:00
self.assertEqual(len(response.context["notifications"]), len(self.to_user.notifications.unread()))
self.assertEqual(len(response.context["notifications"]), self.message_count)
2018-05-31 04:05:53 +00:00
for index, notification in enumerate(self.to_user.notifications.all()):
if index % 3 == 0:
2023-06-27 00:06:44 +00:00
response = self.client.get(reverse("notifications:mark_as_read", args=[notification.id]))
2015-12-11 13:32:20 +00:00
self.assertEqual(response.status_code, 302)
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:unread"))
2015-12-11 13:32:20 +00:00
self.assertEqual(response.status_code, 200)
2023-06-24 00:10:23 +00:00
self.assertEqual(len(response.context["notifications"]), len(self.to_user.notifications.unread()))
self.assertTrue(len(response.context["notifications"]) < self.message_count)
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:mark_all_as_read"))
self.assertRedirects(response, reverse("notifications:unread"))
response = self.client.get(reverse("notifications:unread"))
self.assertEqual(len(response.context["notifications"]), len(self.to_user.notifications.unread()))
self.assertEqual(len(response.context["notifications"]), 0)
def test_next_pages(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
query_parameters = "?var1=hello&var2=world"
2023-06-24 00:10:23 +00:00
response = self.client.get(
reverse("notifications:mark_all_as_read"),
data={
"next": reverse("notifications:unread") + query_parameters,
},
)
self.assertRedirects(response, reverse("notifications:unread") + query_parameters)
2023-06-27 00:06:44 +00:00
slug = self.to_user.notifications.first().id
2023-06-24 00:10:23 +00:00
response = self.client.get(
reverse("notifications:mark_as_read", args=[slug]),
data={
"next": reverse("notifications:unread") + query_parameters,
},
)
self.assertRedirects(response, reverse("notifications:unread") + query_parameters)
2023-06-27 00:06:44 +00:00
slug = self.to_user.notifications.first().id
2023-06-24 00:10:23 +00:00
response = self.client.get(
reverse("notifications:mark_as_unread", args=[slug]),
{
"next": reverse("notifications:unread") + query_parameters,
},
)
self.assertRedirects(response, reverse("notifications:unread") + query_parameters)
2023-05-22 23:26:41 +00:00
@override_settings(ALLOWED_HOSTS=["www.notifications.com"])
def test_malicious_next_pages(self):
self.client.force_login(self.to_user)
2023-06-24 00:10:23 +00:00
query_parameters = "?var1=hello&var2=world"
2023-05-22 23:26:41 +00:00
for next_url in MALICIOUS_NEXT_URLS:
2023-06-24 00:10:23 +00:00
response = self.client.get(
reverse("notifications:mark_all_as_read"),
data={
"next": next_url + query_parameters,
},
)
self.assertRedirects(response, reverse("notifications:unread"))
2023-05-22 23:26:41 +00:00
def test_delete_messages_pages(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
2023-06-27 00:06:44 +00:00
slug = self.to_user.notifications.first().id
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:delete", args=[slug]))
self.assertRedirects(response, reverse("notifications:all"))
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:all"))
self.assertEqual(response.status_code, 200)
2023-06-24 00:10:23 +00:00
self.assertEqual(len(response.context["notifications"]), len(self.to_user.notifications.all()))
self.assertEqual(len(response.context["notifications"]), self.message_count - 1)
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:unread"))
self.assertEqual(response.status_code, 200)
2023-06-24 00:10:23 +00:00
self.assertEqual(len(response.context["notifications"]), len(self.to_user.notifications.unread()))
self.assertEqual(len(response.context["notifications"]), self.message_count - 1)
2023-06-24 00:10:23 +00:00
@override_settings(DJANGO_NOTIFICATIONS_CONFIG={"SOFT_DELETE": True}) # pylint: disable=invalid-name
def test_soft_delete_messages_manager(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
2023-06-27 00:06:44 +00:00
slug = self.to_user.notifications.first().id
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:delete", args=[slug]))
self.assertRedirects(response, reverse("notifications:all"))
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:all"))
self.assertEqual(response.status_code, 200)
2023-06-24 00:10:23 +00:00
self.assertEqual(len(response.context["notifications"]), len(self.to_user.notifications.active()))
self.assertEqual(len(response.context["notifications"]), self.message_count - 1)
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:unread"))
self.assertEqual(response.status_code, 200)
2023-06-24 00:10:23 +00:00
self.assertEqual(len(response.context["notifications"]), len(self.to_user.notifications.unread()))
self.assertEqual(len(response.context["notifications"]), self.message_count - 1)
2015-07-20 09:51:00 +00:00
def test_unread_count_api(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
2015-07-20 09:51:00 +00:00
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:live_unread_notification_count"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(list(data.keys()), ["unread_count"])
self.assertEqual(data["unread_count"], self.message_count)
2015-07-20 09:51:00 +00:00
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:live_unread_notification_count"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(list(data.keys()), ["unread_count"])
self.assertEqual(data["unread_count"], 0)
2015-07-20 09:51:00 +00:00
2023-06-24 00:10:23 +00:00
notify.send(self.from_user, recipient=self.to_user, verb="commented", action_object=self.from_user)
response = self.client.get(reverse("notifications:live_unread_notification_count"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(list(data.keys()), ["unread_count"])
self.assertEqual(data["unread_count"], 1)
2015-07-20 09:51:00 +00:00
def test_all_count_api(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:live_all_notification_count"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(list(data.keys()), ["all_count"])
self.assertEqual(data["all_count"], self.message_count)
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:live_all_notification_count"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(list(data.keys()), ["all_count"])
self.assertEqual(data["all_count"], self.message_count)
2023-06-24 00:10:23 +00:00
notify.send(self.from_user, recipient=self.to_user, verb="commented", action_object=self.from_user)
response = self.client.get(reverse("notifications:live_all_notification_count"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(list(data.keys()), ["all_count"])
self.assertEqual(data["all_count"], self.message_count + 1)
2015-07-20 09:51:00 +00:00
def test_unread_list_api(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
2015-07-20 09:51:00 +00:00
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:live_unread_notification_list"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(sorted(list(data.keys())), ["unread_count", "unread_list"])
self.assertEqual(data["unread_count"], self.message_count)
self.assertEqual(len(data["unread_list"]), self.message_count)
2015-07-20 09:51:00 +00:00
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:live_unread_notification_list"), data={"max": 5})
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(sorted(list(data.keys())), ["unread_count", "unread_list"])
self.assertEqual(data["unread_count"], self.message_count)
self.assertEqual(len(data["unread_list"]), 5)
2015-07-20 09:51:00 +00:00
# Test with a bad 'max' value
2023-06-24 00:10:23 +00:00
response = self.client.get(
reverse("notifications:live_unread_notification_list"),
data={
"max": "this_is_wrong",
},
)
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(sorted(list(data.keys())), ["unread_count", "unread_list"])
self.assertEqual(data["unread_count"], self.message_count)
self.assertEqual(len(data["unread_list"]), self.message_count)
2015-07-20 09:51:00 +00:00
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:live_unread_notification_list"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(sorted(list(data.keys())), ["unread_count", "unread_list"])
self.assertEqual(data["unread_count"], 0)
self.assertEqual(len(data["unread_list"]), 0)
notify.send(self.from_user, recipient=self.to_user, verb="commented", action_object=self.from_user)
response = self.client.get(reverse("notifications:live_unread_notification_list"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(sorted(list(data.keys())), ["unread_count", "unread_list"])
self.assertEqual(data["unread_count"], 1)
self.assertEqual(len(data["unread_list"]), 1)
self.assertEqual(data["unread_list"][0]["verb"], "commented")
2023-06-27 00:06:44 +00:00
self.assertEqual(data["unread_list"][0]["slug"], data["unread_list"][0]["id"])
2015-07-20 09:51:00 +00:00
def test_all_list_api(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:live_all_notification_list"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(sorted(list(data.keys())), ["all_count", "all_list"])
self.assertEqual(data["all_count"], self.message_count)
self.assertEqual(len(data["all_list"]), self.message_count)
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:live_all_notification_list"), data={"max": 5})
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(sorted(list(data.keys())), ["all_count", "all_list"])
self.assertEqual(data["all_count"], self.message_count)
self.assertEqual(len(data["all_list"]), 5)
# Test with a bad 'max' value
2023-06-24 00:10:23 +00:00
response = self.client.get(
reverse("notifications:live_all_notification_list"),
data={
"max": "this_is_wrong",
},
)
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(sorted(list(data.keys())), ["all_count", "all_list"])
self.assertEqual(data["all_count"], self.message_count)
self.assertEqual(len(data["all_list"]), self.message_count)
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse("notifications:live_all_notification_list"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(sorted(list(data.keys())), ["all_count", "all_list"])
self.assertEqual(data["all_count"], self.message_count)
self.assertEqual(len(data["all_list"]), self.message_count)
notify.send(self.from_user, recipient=self.to_user, verb="commented", action_object=self.from_user)
response = self.client.get(reverse("notifications:live_all_notification_list"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(sorted(list(data.keys())), ["all_count", "all_list"])
self.assertEqual(data["all_count"], self.message_count + 1)
self.assertEqual(len(data["all_list"]), self.message_count)
self.assertEqual(data["all_list"][0]["verb"], "commented")
2023-06-27 00:06:44 +00:00
self.assertEqual(data["all_list"][0]["slug"], data["all_list"][0]["id"])
2018-05-31 04:05:53 +00:00
def test_unread_list_api_mark_as_read(self): # pylint: disable=invalid-name
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
2016-09-25 12:06:45 +00:00
num_requested = 3
2016-09-25 12:43:56 +00:00
response = self.client.get(
2023-06-24 00:10:23 +00:00
reverse("notifications:live_unread_notification_list"), data={"max": num_requested, "mark_as_read": 1}
2016-09-25 12:43:56 +00:00
)
2023-06-24 00:10:23 +00:00
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(data["unread_count"], self.message_count - num_requested)
self.assertEqual(len(data["unread_list"]), num_requested)
2016-09-25 12:43:56 +00:00
response = self.client.get(
2023-06-24 00:10:23 +00:00
reverse("notifications:live_unread_notification_list"), data={"max": num_requested, "mark_as_read": 1}
2016-09-25 12:43:56 +00:00
)
2023-06-24 00:10:23 +00:00
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(data["unread_count"], self.message_count - 2 * num_requested)
self.assertEqual(len(data["unread_list"]), num_requested)
2016-09-25 12:06:45 +00:00
2015-07-20 09:51:00 +00:00
def test_live_update_tags(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
2018-05-31 04:05:53 +00:00
factory = RequestFactory()
2015-07-20 09:51:00 +00:00
2023-06-24 00:10:23 +00:00
request = factory.get("/notification/live_updater")
2015-07-20 09:51:00 +00:00
request.user = self.to_user
2023-06-24 00:10:23 +00:00
render(request, "notifications/test_tags.html", {"request": request, "nonce": "nonce-T5esDNXMnDe5lKMQ6ZzTUw=="})
2015-12-11 13:32:20 +00:00
2016-01-01 02:59:51 +00:00
# TODO: Add more tests to check what is being output.
def test_anon_user_gets_nothing(self):
2023-06-24 00:10:23 +00:00
response = self.client.post(reverse("notifications:live_unread_notification_count"))
self.assertEqual(response.status_code, 200)
2023-06-24 00:10:23 +00:00
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(data["unread_count"], 0)
2023-06-24 00:10:23 +00:00
response = self.client.post(reverse("notifications:live_unread_notification_list"))
self.assertEqual(response.status_code, 200)
2023-06-24 00:10:23 +00:00
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(data["unread_count"], 0)
self.assertEqual(data["unread_list"], [])
2017-06-27 01:34:27 +00:00
class NotificationTestExtraData(TestCase):
2023-06-24 00:10:23 +00:00
"""Django notifications automated extra data tests"""
2017-06-27 01:34:27 +00:00
def setUp(self):
self.message_count = 1
self.from_user = User.objects.create_user(username="from", password="pwd", email="example@example.com")
self.to_user = User.objects.create_user(username="to", password="pwd", email="example@example.com")
self.to_user.is_staff = True
self.to_user.save()
2018-05-31 04:05:53 +00:00
for _ in range(self.message_count):
notify.send(
self.from_user,
recipient=self.to_user,
2023-06-24 00:10:23 +00:00
verb="commented",
2018-05-31 04:05:53 +00:00
action_object=self.from_user,
url="/learn/ask-a-pro/q/test-question-9/299/",
2023-06-24 00:10:23 +00:00
other_content="Hello my 'world'",
2018-05-31 04:05:53 +00:00
)
2017-06-27 01:34:27 +00:00
def logout(self):
2023-06-24 00:10:23 +00:00
self.client.post(reverse("admin:logout") + "?next=/", {})
2017-06-27 01:34:27 +00:00
def login(self, username, password):
self.logout()
2023-07-06 20:54:22 +00:00
response = self.client.post(reverse("admin:login"), {"username": username, "password": password})
2017-06-27 01:34:27 +00:00
self.assertEqual(response.status_code, 302)
return response
def test_extra_data(self):
2023-06-24 00:10:23 +00:00
self.login("to", "pwd")
response = self.client.post(reverse("notifications:live_unread_notification_list"))
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(data["unread_list"][0]["data"]["url"], "/learn/ask-a-pro/q/test-question-9/299/")
self.assertEqual(data["unread_list"][0]["data"]["other_content"], "Hello my 'world'")
class TagTest(TestCase):
2023-06-24 00:10:23 +00:00
"""Django notifications automated tags tests"""
def setUp(self):
self.message_count = 1
self.from_user = User.objects.create_user(username="from", password="pwd", email="example@example.com")
self.to_user = User.objects.create_user(username="to", password="pwd", email="example@example.com")
self.to_user.is_staff = True
self.to_user.save()
for _ in range(self.message_count):
notify.send(
self.from_user,
recipient=self.to_user,
2023-06-24 00:10:23 +00:00
verb="commented",
action_object=self.from_user,
url="/learn/ask-a-pro/q/test-question-9/299/",
2023-06-24 00:10:23 +00:00
other_content="Hello my 'world'",
)
def tag_test(self, template, context, output):
2023-06-24 00:10:23 +00:00
template = Template("{% load notifications_tags %}" + template)
context = Context(context)
self.assertEqual(template.render(context), output)
def test_has_notification(self):
template = "{{ user|has_notification }}"
2023-06-24 00:10:23 +00:00
context = {"user": self.to_user}
output = "True"
self.tag_test(template, context, output)
class AdminTest(TestCase):
2020-04-08 21:40:02 +00:00
app_name = "notifications"
2023-06-24 00:10:23 +00:00
def setUp(self):
self.message_count = 10
self.from_user = User.objects.create_user(username="from", password="pwd", email="example@example.com")
self.to_user = User.objects.create_user(username="to", password="pwd", email="example@example.com")
self.to_user.is_staff = True
self.to_user.is_superuser = True
self.to_user.save()
for _ in range(self.message_count):
notify.send(
self.from_user,
recipient=self.to_user,
2023-06-24 00:10:23 +00:00
verb="commented",
action_object=self.from_user,
)
def test_list(self):
2023-06-24 00:10:23 +00:00
self.client.login(username="to", password="pwd")
with CaptureQueriesContext(connection=connection) as context:
2023-06-24 00:10:23 +00:00
response = self.client.get(reverse(f"admin:{self.app_name}_notification_changelist"))
self.assertLessEqual(len(context), 6)
self.assertEqual(response.status_code, 200, response.content)