-
Notifications
You must be signed in to change notification settings - Fork 394
/
test_template_tags.py
119 lines (89 loc) · 3.96 KB
/
test_template_tags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from unittest.mock import Mock, patch
import pytest
from django.template import Context, Template
from django.urls import reverse_lazy
from freezegun import freeze_time
from swapper import load_model
from notifications.templatetags.notifications_tags import (
live_notify_badge,
user_context,
)
from notifications.tests.factories.notifications import NotificationFullFactory
from notifications.tests.factories.users import RecipientFactory
Notification = load_model("notifications", "Notification")
def render_tag(template, context):
template = Template("{% load notifications_tags %}" + template)
return template.render(Context(context))
@pytest.mark.django_db
def test_notifications_unread():
assert render_tag("{% notifications_unread %}", {}) == ""
assert (
render_tag("{% notifications_unread %}", {"user": Mock(), "request": Mock(user=Mock(is_anonymous=True))}) == ""
)
notification = NotificationFullFactory()
assert (
render_tag(
"{% notifications_unread %}", {"user": notification.recipient, "request": Mock(user=notification.recipient)}
)
== "1"
)
@pytest.mark.django_db
def test_has_notification():
user = RecipientFactory()
assert render_tag("{{ user|has_notification }}", {"user": user}) == "False"
notification = NotificationFullFactory(recipient=user)
assert render_tag("{{ user|has_notification }}", {"user": user}) == "True"
notification.mark_as_read()
assert render_tag("{{ user|has_notification }}", {"user": user}) == "False"
@pytest.mark.parametrize(
"_type,url",
(
("list", reverse_lazy("notifications:live_unread_notification_list")),
("count", reverse_lazy("notifications:live_unread_notification_count")),
),
)
def test_register_notify_callbacks(_type, url):
tag = f"{{% register_notify_callbacks badge_class='badge1' menu_class='menu2' refresh_period=10 callbacks='cb1,cb2' api_name='{_type}' fetch=50 nonce='123' mark_as_read=True %}}" # pylint: disable=line-too-long
render = render_tag(tag, {})
assert "notify_badge_class='badge1'" in render
assert "notify_menu_class='menu2'" in render
assert "notify_refresh_period=10" in render
assert "register_notifier(cb1);" in render
assert "register_notifier(cb2);" in render
assert "notify_fetch_count='50'" in render
assert 'nonce="123"' in render
assert "true" in render
assert str(url) in render
@patch("notifications.templatetags.notifications_tags.user_context")
@patch("notifications.templatetags.notifications_tags.get_cached_notification_unread_count")
@pytest.mark.django_db
def test_live_notify_badge(cache_mock, user_context_mock):
user = RecipientFactory()
user_context_mock.return_value = None
assert live_notify_badge({}) == ""
assert render_tag("{% live_notify_badge %}", {}) == ""
user_context_mock.return_value = user
badge = render_tag("{% live_notify_badge badge_class='blafoo' %}", {})
assert "blafoo" in badge
assert "0" in badge
with freeze_time("2024-01-01 00:00:00"):
NotificationFullFactory(recipient=user)
badge = render_tag("{% live_notify_badge badge_class='blafoo' %}", {})
assert "blafoo" in badge
assert "0" in badge # Because of cache
cache_mock.side_effect = lambda user: user.notifications.unread().count()
badge = render_tag("{% live_notify_badge badge_class='blafoo' %}", {})
assert "blafoo" in badge
assert "1" in badge
def test_live_notify_list():
resp = render_tag("{% live_notify_list list_class='blafoo' %}", {})
assert "<ul" in resp
assert "class='blafoo'" in resp
def test_user_context():
assert user_context({}) is None
with pytest.raises(KeyError):
user_context({"user": Mock()})
user = Mock(is_anonymous=True)
assert user_context({"user": user, "request": Mock(user=user)}) is None
user = Mock(is_anonymous=False)
assert user_context({"user": user, "request": Mock(user=user)}) == user