Skip to content

Commit 944d9b7

Browse files
committed
[tests] Added support for read_redirect view in notification tests
1 parent 5beb089 commit 944d9b7

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

openwisp_monitoring/device/tests/test_device_notifications.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.core import mail
2+
from django.urls import reverse
23
from django.utils.html import strip_tags
34
from swapper import load_model
45

@@ -21,10 +22,12 @@ def setUp(self):
2122
def _generic_notification_test(
2223
self, exp_level, exp_type, exp_verb, exp_message, exp_email_subject
2324
):
25+
n = Notification.objects.first()
26+
url_path = reverse('notifications:notification_read_redirect', args=[n.pk])
27+
exp_email_link = f'https://example.com{url_path}'
2428
exp_target_link = f'https://example.com/admin/config/device/{self.d.id}/change/'
25-
exp_email_body = '{message}' f'\n\nFor more information see {exp_target_link}.'
29+
exp_email_body = '{message}' f'\n\nFor more information see {exp_email_link}.'
2630

27-
n = Notification.objects.first()
2831
email = mail.outbox.pop()
2932
html_message, _ = email.alternatives.pop()
3033
self.assertEqual(n.type, exp_type)
@@ -42,19 +45,20 @@ def _generic_notification_test(
4245
self.assertEqual(
4346
email.body, exp_email_body.format(message=strip_tags(n.message))
4447
)
45-
self.assertIn(n.message, html_message)
4648
self.assertIn(
47-
f'<a href="{exp_target_link}">'
49+
f'<a href="{exp_email_link}">'
4850
'For further information see "device: default.test.device".</a>',
4951
html_message,
5052
)
5153

5254
def test_connection_working_notification(self):
55+
self.assertEqual(Notification.objects.count(), 0)
5356
self.dc = DeviceConnection.objects.create(
5457
credentials=self.creds, device=self.d, is_working=False
5558
)
5659
self.dc.is_working = True
5760
self.dc.save()
61+
self.assertEqual(Notification.objects.count(), 1)
5862
self._generic_notification_test(
5963
exp_level='info',
6064
exp_type='connection_is_working',
@@ -67,8 +71,10 @@ def test_connection_working_notification(self):
6771
)
6872

6973
def test_connection_not_working_notification(self):
74+
self.assertEqual(Notification.objects.count(), 0)
7075
self.dc.is_working = False
7176
self.dc.save()
77+
self.assertEqual(Notification.objects.count(), 1)
7278
self._generic_notification_test(
7379
exp_level='error',
7480
exp_type='connection_is_not_working',
@@ -81,9 +87,11 @@ def test_connection_not_working_notification(self):
8187
)
8288

8389
def test_unreachable_after_upgrade_notification(self):
90+
self.assertEqual(Notification.objects.count(), 0)
8491
self.dc.is_working = False
8592
self.dc.failure_reason = 'Giving up, device not reachable anymore after upgrade'
8693
self.dc.save()
94+
self.assertEqual(Notification.objects.count(), 1)
8795
self._generic_notification_test(
8896
exp_level='error',
8997
exp_type='connection_is_not_working',

openwisp_monitoring/monitoring/tests/test_monitoring_notifications.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from unittest.mock import patch
33

44
from django.core import mail
5+
from django.urls import reverse
56
from django.utils import timezone
67
from django.utils.html import strip_tags
78
from swapper import load_model
@@ -318,39 +319,49 @@ def test_email_notification(self):
318319
m = self._create_general_metric(name='load', content_object=d)
319320
self._create_alert_settings(metric=m, operator='>', value=90, seconds=0)
320321
exp_target_link = f'https://example.com/admin/config/device/{d.id}/change/'
321-
exp_email_body = '{message}' f'\n\nFor more information see {exp_target_link}.'
322+
exp_email_body = '{message}\n\nFor more information see {email_link}.'
322323

323324
with self.subTest('Test notification email for metric crossed alert settings'):
324325
m.write(99)
325326
n = notification_queryset.first()
327+
url_path = reverse('notifications:notification_read_redirect', args=[n.pk])
328+
email_link = f'https://example.com{url_path}'
326329
email = mail.outbox.pop()
327330
html_message, content_type = email.alternatives.pop()
328331
self.assertEqual(email.subject, n.email_subject)
329332
self.assertEqual(
330-
email.body, exp_email_body.format(message=strip_tags(n.message))
333+
email.body,
334+
exp_email_body.format(
335+
message=strip_tags(n.message), email_link=email_link
336+
),
331337
)
332-
self.assertIn(n.message, html_message)
333338
self.assertIn(
334-
f'<a href="{exp_target_link}">'
339+
f'<a href="{email_link}">'
335340
'For further information see "device: default.test.device".</a>',
336341
html_message,
337342
)
343+
self.assertIn(exp_target_link, n.message)
338344

339345
with self.subTest('Test notification email for metric returned under threhold'):
340346
m.write(50)
341347
n = notification_queryset.last()
348+
url_path = reverse('notifications:notification_read_redirect', args=[n.pk])
349+
email_link = f'https://example.com{url_path}'
342350
email = mail.outbox.pop()
343351
html_message, content_type = email.alternatives.pop()
344352
self.assertEqual(email.subject, n.email_subject)
345353
self.assertEqual(
346-
email.body, exp_email_body.format(message=strip_tags(n.message))
354+
email.body,
355+
exp_email_body.format(
356+
message=strip_tags(n.message), email_link=email_link
357+
),
347358
)
348-
self.assertIn(n.message, html_message)
349359
self.assertIn(
350-
f'<a href="{exp_target_link}">'
360+
f'<a href="{email_link}">'
351361
'For further information see "device: default.test.device".</a>',
352362
html_message,
353363
)
364+
self.assertIn(exp_target_link, n.message)
354365

355366
def test_notification_types(self):
356367
self._create_admin()

0 commit comments

Comments
 (0)