-
Notifications
You must be signed in to change notification settings - Fork 394
/
tests.py
404 lines (338 loc) · 18.8 KB
/
tests.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
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.
"""
# -*- coding: utf-8 -*-
import json
from datetime import datetime, timezone
from django.contrib.auth import get_user_model
from django.shortcuts import render
from django.test import override_settings # noqa
from django.test import RequestFactory, TestCase
from django.urls import reverse
from django.utils.timezone import localtime
from swapper import load_model
from notifications.signals import notify
Notification = load_model("notifications", "Notification")
User = get_user_model()
MALICIOUS_NEXT_URLS = [
"http://bla.com",
"http://www.bla.com",
"https://bla.com",
"https://www.bla.com",
"ftp://www.bla.com/file.exe",
]
class NotificationTest(TestCase):
"""Django notifications automated tests"""
@override_settings(USE_TZ=True)
@override_settings(TIME_ZONE="Asia/Shanghai")
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")
notify.send(from_user, recipient=to_user, verb="commented", action_object=from_user)
notification = Notification.objects.get(recipient=to_user)
delta = datetime.now(tz=timezone.utc) - localtime(notification.timestamp)
self.assertTrue(delta.seconds < 60)
# The delta between the two events will still be less than a second despite the different timezones
# 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.
@override_settings(USE_TZ=False)
@override_settings(TIME_ZONE="Asia/Shanghai")
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")
notify.send(from_user, recipient=to_user, verb="commented", action_object=from_user)
notification = Notification.objects.get(recipient=to_user)
delta = datetime.now() - notification.timestamp
self.assertTrue(delta.seconds < 60)
class NotificationTestPages(TestCase):
"""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()
for _ in range(self.message_count):
notify.send(self.from_user, recipient=self.to_user, verb="commented", action_object=self.from_user)
def logout(self):
self.client.post(reverse("admin:logout") + "?next=/", {})
def login(self, username, password):
self.logout()
response = self.client.post(reverse("admin:login"), {"username": username, "password": password})
self.assertEqual(response.status_code, 302)
return response
def test_all_messages_page(self):
self.login("to", "pwd")
response = self.client.get(reverse("notifications:all"))
self.assertEqual(response.status_code, 200)
self.assertEqual(
len(response.context["notifications"]), len(self.to_user.notifications_notification_related.all())
)
def test_unread_messages_pages(self):
self.login("to", "pwd")
response = self.client.get(reverse("notifications:unread"))
self.assertEqual(response.status_code, 200)
self.assertEqual(
len(response.context["notifications"]), len(self.to_user.notifications_notification_related.unread())
)
self.assertEqual(len(response.context["notifications"]), self.message_count)
for index, notification in enumerate(self.to_user.notifications_notification_related.all()):
if index % 3 == 0:
response = self.client.get(reverse("notifications:mark_as_read", args=[notification.id]))
self.assertEqual(response.status_code, 302)
response = self.client.get(reverse("notifications:unread"))
self.assertEqual(response.status_code, 200)
self.assertEqual(
len(response.context["notifications"]), len(self.to_user.notifications_notification_related.unread())
)
self.assertTrue(len(response.context["notifications"]) < self.message_count)
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_notification_related.unread())
)
self.assertEqual(len(response.context["notifications"]), 0)
def test_next_pages(self):
self.login("to", "pwd")
query_parameters = "?var1=hello&var2=world"
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)
slug = self.to_user.notifications_notification_related.first().id
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)
slug = self.to_user.notifications_notification_related.first().id
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)
@override_settings(ALLOWED_HOSTS=["www.notifications_notification_related.com"])
def test_malicious_next_pages(self):
self.client.force_login(self.to_user)
query_parameters = "?var1=hello&var2=world"
for next_url in MALICIOUS_NEXT_URLS:
response = self.client.get(
reverse("notifications:mark_all_as_read"),
data={
"next": next_url + query_parameters,
},
)
self.assertRedirects(response, reverse("notifications:unread"))
def test_delete_messages_pages(self):
self.login("to", "pwd")
slug = self.to_user.notifications_notification_related.first().id
response = self.client.get(reverse("notifications:delete", args=[slug]))
self.assertRedirects(response, reverse("notifications:all"))
response = self.client.get(reverse("notifications:all"))
self.assertEqual(response.status_code, 200)
self.assertEqual(
len(response.context["notifications"]), len(self.to_user.notifications_notification_related.all())
)
self.assertEqual(len(response.context["notifications"]), self.message_count - 1)
response = self.client.get(reverse("notifications:unread"))
self.assertEqual(response.status_code, 200)
self.assertEqual(
len(response.context["notifications"]), len(self.to_user.notifications_notification_related.unread())
)
self.assertEqual(len(response.context["notifications"]), self.message_count - 1)
@override_settings(DJANGO_NOTIFICATIONS_CONFIG={"SOFT_DELETE": True})
def test_soft_delete_messages_manager(self):
self.login("to", "pwd")
slug = self.to_user.notifications_notification_related.first().id
response = self.client.get(reverse("notifications:delete", args=[slug]))
self.assertRedirects(response, reverse("notifications:all"))
response = self.client.get(reverse("notifications:all"))
self.assertEqual(response.status_code, 200)
self.assertEqual(
len(response.context["notifications"]), len(self.to_user.notifications_notification_related.active())
)
self.assertEqual(len(response.context["notifications"]), self.message_count - 1)
response = self.client.get(reverse("notifications:unread"))
self.assertEqual(response.status_code, 200)
self.assertEqual(
len(response.context["notifications"]), len(self.to_user.notifications_notification_related.unread())
)
self.assertEqual(len(response.context["notifications"]), self.message_count - 1)
def test_unread_count_api(self):
self.login("to", "pwd")
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)
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
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)
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)
def test_all_count_api(self):
self.login("to", "pwd")
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()
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)
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)
def test_unread_list_api(self):
self.login("to", "pwd")
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)
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)
# Test with a bad 'max' value
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)
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
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")
self.assertEqual(data["unread_list"][0]["slug"], data["unread_list"][0]["id"])
def test_all_list_api(self):
self.login("to", "pwd")
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)
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
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()
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")
self.assertEqual(data["all_list"][0]["slug"], data["all_list"][0]["id"])
def test_unread_list_api_mark_as_read(self):
self.login("to", "pwd")
num_requested = 3
response = self.client.get(
reverse("notifications:live_unread_notification_list"), data={"max": num_requested, "mark_as_read": 1}
)
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)
response = self.client.get(
reverse("notifications:live_unread_notification_list"), data={"max": num_requested, "mark_as_read": 1}
)
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)
def test_live_update_tags(self):
self.login("to", "pwd")
factory = RequestFactory()
request = factory.get("/notification/live_updater")
request.user = self.to_user
render(request, "notifications/test_tags.html", {"request": request, "nonce": "nonce-T5esDNXMnDe5lKMQ6ZzTUw=="})
# TODO: Add more tests to check what is being output.
def test_anon_user_gets_nothing(self):
response = self.client.post(reverse("notifications:live_unread_notification_count"))
self.assertEqual(response.status_code, 200)
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(data["unread_count"], 0)
response = self.client.post(reverse("notifications:live_unread_notification_list"))
self.assertEqual(response.status_code, 200)
data = json.loads(response.content.decode("utf-8"))
self.assertEqual(data["unread_count"], 0)
self.assertEqual(data["unread_list"], [])
class NotificationTestExtraData(TestCase):
"""Django notifications automated extra data 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,
verb="commented",
action_object=self.from_user,
url="/learn/ask-a-pro/q/test-question-9/299/",
other_content="Hello my 'world'",
)
def logout(self):
self.client.post(reverse("admin:logout") + "?next=/", {})
def login(self, username, password):
self.logout()
response = self.client.post(reverse("admin:login"), {"username": username, "password": password})
self.assertEqual(response.status_code, 302)
return response
def test_extra_data(self):
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'")