-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_notifications.py
225 lines (180 loc) · 7.6 KB
/
test_notifications.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from datetime import date
import pytest
from django.conf import settings
from django.core import mail
from django.utils.html import escape
from django.urls import reverse
from itsdangerous import URLSafeTimedSerializer
from ..constants import NEWS_APPROVAL_SALT
from ..models import NEWS_MODELS
from ..notifications import (
send_email_news_approved,
send_email_news_needs_moderation,
send_email_news_posted,
generate_magic_approval_link,
)
from users.models import Preferences
@pytest.mark.parametrize("model_class", NEWS_MODELS)
def test_send_email_news_approved(rf, tp, make_entry, model_class):
entry = make_entry(model_class, approved=True, created_at=date(2023, 5, 31))
assert entry.tag in entry.author.preferences.allow_notification_own_news_approved
request = rf.get("")
result = send_email_news_approved(request, entry)
assert result == 1
assert len(mail.outbox) == 1
msg = mail.outbox[0]
assert "news entry approved" in msg.subject.lower()
assert entry.title in msg.body
assert escape(entry.title) not in msg.body
assert "May 31st, 2023" in msg.body
assert request.build_absolute_uri(entry.get_absolute_url()) in msg.body
assert msg.recipients() == [entry.author.email]
@pytest.mark.parametrize("model_class", NEWS_MODELS)
def test_send_email_news_approved_author_email_preferences_do_not_notify_all_types(
rf, tp, make_entry, model_class, make_user
):
entry = make_entry(model_class, approved=True)
entry.author.preferences.notifications[Preferences.OWNS_NEWS_APPROVED] = []
assert (
entry.tag not in entry.author.preferences.allow_notification_own_news_approved
)
request = rf.get("")
result = send_email_news_approved(request, entry)
assert result is False
def test_send_email_news_approved_author_email_preferences_do_not_notify_other_type(
rf, tp, make_entry, make_user
):
entry = make_entry(model_class=NEWS_MODELS[0], approved=True)
entry.author.preferences.notifications[Preferences.OWNS_NEWS_APPROVED] = [
NEWS_MODELS[1].news_type
]
assert (
entry.tag not in entry.author.preferences.allow_notification_own_news_approved
)
request = rf.get("")
result = send_email_news_approved(request, entry)
assert result is False
@pytest.mark.parametrize("model_class", NEWS_MODELS)
def test_send_email_news_needs_moderation(
rf,
tp,
make_entry,
model_class,
make_user,
moderator_user,
superuser,
):
entry = make_entry(model_class, approved=True)
# Other moderator with default email notifications preferences
other_moderator = make_user(groups={"moderator": ["news.*"]}, email="[email protected]")
# Third moderator that does not allow any email notifications
third = make_user(groups={"editors": ["news.*"]}, email="[email protected]")
third.preferences.notifications[Preferences.OTHERS_NEWS_NEEDS_MODERATION] = []
third.preferences.save()
# Forth moderator that do allow email notifications for this news type
forth = make_user(perms=["news.*"], email="[email protected]")
forth.preferences.notifications[Preferences.OTHERS_NEWS_NEEDS_MODERATION] = [
entry.tag
]
forth.preferences.save()
request = rf.get("")
with tp.assertNumQueriesLessThan(2, verbose=True):
result = send_email_news_needs_moderation(request, entry)
assert result == 4
assert len(mail.outbox) == 4
msg = mail.outbox[0]
assert "news entry needs moderation" in msg.subject.lower()
assert entry.title in msg.body
assert escape(entry.title) not in msg.body
assert entry.author.get_display_name in msg.body
assert entry.author.email in msg.body
assert request.build_absolute_uri(entry.get_absolute_url()) in msg.body
assert request.build_absolute_uri(reverse("news-moderate")) in msg.body
recipients = []
for msg in mail.outbox:
recipients.extend(msg.recipients())
assert set(recipients) == {
other_moderator.email,
moderator_user.email,
superuser.email,
forth.email,
}
def test_generate_magic_approval_link(make_entry, make_user):
entry = make_entry()
moderator = make_user(groups={"moderator": ["news.*"]}, email="[email protected]")
url = generate_magic_approval_link(entry.slug, moderator.id)
dummy_token = "dummy-token"
expected_base_url = (
reverse("news-magic-approve", kwargs={"token": dummy_token})
.replace(dummy_token, "")
.rstrip("/")
)
assert url.startswith(expected_base_url)
token = url.split(expected_base_url)[-1].strip("/")
serializer = URLSafeTimedSerializer(settings.SECRET_KEY)
data = serializer.loads(token, salt=NEWS_APPROVAL_SALT)
assert data["entry_slug"] == entry.slug
assert data["moderator_id"] == moderator.id
@pytest.mark.parametrize("model_class", NEWS_MODELS)
def test_send_email_news_needs_moderation_no_moderator_match(
rf, tp, make_entry, model_class, moderator_user
):
moderator_user.preferences.notifications[
Preferences.OTHERS_NEWS_NEEDS_MODERATION
] = []
moderator_user.preferences.save()
entry = make_entry(model_class, approved=True)
request = rf.get("")
with tp.assertNumQueriesLessThan(2, verbose=True):
result = send_email_news_needs_moderation(request, entry)
assert result is False
@pytest.mark.parametrize("model_class", NEWS_MODELS)
def test_send_email_news_posted_no_other_user(rf, make_entry, model_class):
entry = make_entry(model_class, approved=False)
request = rf.get("")
result = send_email_news_posted(request, entry)
assert result is False
@pytest.mark.parametrize("model_class", NEWS_MODELS)
def test_send_email_news_posted_no_other_user_allows(
rf, make_entry, make_user, model_class
):
entry = make_entry(model_class, approved=False)
request = rf.get("")
for _ in range(3):
u = make_user()
u.preferences.allow_notification_others_news_posted = []
u.preferences.save()
result = send_email_news_posted(request, entry)
assert result is False
@pytest.mark.parametrize("model_class", NEWS_MODELS)
def test_send_email_news_posted_many_users(rf, tp, make_entry, make_user, model_class):
entry = make_entry(model_class, approved=False)
request = rf.get("")
recipients = {
# user does not allow notifications
"[email protected]": [],
# allows nofitications for all news type
"[email protected]": [m.news_type for m in NEWS_MODELS],
# allows only for the same type as entry
"[email protected]": [entry.tag],
# allows for any other type except entry's
"[email protected]": [
m.news_type for m in NEWS_MODELS if m.news_type != entry.tag
],
}
for email, notifications in recipients.items():
make_user(email=email, allow_notification_others_news_posted=notifications)
with tp.assertNumQueriesLessThan(2, verbose=True):
result = send_email_news_posted(request, entry)
# We should send two emails - one to u2, one to u3
assert result == len(mail.outbox) == 2
for usr_num, msg in enumerate(mail.outbox, start=2):
assert "news entry posted" in msg.subject.lower()
assert entry.title in msg.body
assert escape(entry.title) not in msg.body
assert entry.author.email not in msg.body # never disclose author email!
assert request.build_absolute_uri(entry.get_absolute_url()) in msg.body
assert request.build_absolute_uri(tp.reverse("profile-account")) in msg.body
# do not share all emails among all recipients
assert msg.to == [f"u{usr_num}@example.com"]
assert msg.recipients() == [f"u{usr_num}@example.com"]