Skip to content

Commit

Permalink
[tests] Added support for read_redirect view in notification tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier authored and nepython committed Aug 7, 2020
1 parent b29a359 commit 985eaff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
16 changes: 12 additions & 4 deletions openwisp_monitoring/device/tests/test_device_notifications.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core import mail
from django.urls import reverse
from django.utils.html import strip_tags
from swapper import load_model

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

n = Notification.objects.first()
email = mail.outbox.pop()
html_message, _ = email.alternatives.pop()
self.assertEqual(n.type, exp_type)
Expand All @@ -42,19 +45,20 @@ def _generic_notification_test(
self.assertEqual(
email.body, exp_email_body.format(message=strip_tags(n.message))
)
self.assertIn(n.message, html_message)
self.assertIn(
f'<a href="{exp_target_link}">'
f'<a href="{exp_email_link}">'
'For further information see "device: default.test.device".</a>',
html_message,
)

def test_connection_working_notification(self):
self.assertEqual(Notification.objects.count(), 0)
self.dc = DeviceConnection.objects.create(
credentials=self.creds, device=self.d, is_working=False
)
self.dc.is_working = True
self.dc.save()
self.assertEqual(Notification.objects.count(), 1)
self._generic_notification_test(
exp_level='info',
exp_type='connection_is_working',
Expand All @@ -67,8 +71,10 @@ def test_connection_working_notification(self):
)

def test_connection_not_working_notification(self):
self.assertEqual(Notification.objects.count(), 0)
self.dc.is_working = False
self.dc.save()
self.assertEqual(Notification.objects.count(), 1)
self._generic_notification_test(
exp_level='error',
exp_type='connection_is_not_working',
Expand All @@ -81,9 +87,11 @@ def test_connection_not_working_notification(self):
)

def test_unreachable_after_upgrade_notification(self):
self.assertEqual(Notification.objects.count(), 0)
self.dc.is_working = False
self.dc.failure_reason = 'Giving up, device not reachable anymore after upgrade'
self.dc.save()
self.assertEqual(Notification.objects.count(), 1)
self._generic_notification_test(
exp_level='error',
exp_type='connection_is_not_working',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch

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

with self.subTest('Test notification email for metric crossed alert settings'):
m.write(99)
n = notification_queryset.first()
url_path = reverse('notifications:notification_read_redirect', args=[n.pk])
email_link = f'https://example.com{url_path}'
email = mail.outbox.pop()
html_message, content_type = email.alternatives.pop()
self.assertEqual(email.subject, n.email_subject)
self.assertEqual(
email.body, exp_email_body.format(message=strip_tags(n.message))
email.body,
exp_email_body.format(
message=strip_tags(n.message), email_link=email_link
),
)
self.assertIn(n.message, html_message)
self.assertIn(
f'<a href="{exp_target_link}">'
f'<a href="{email_link}">'
'For further information see "device: default.test.device".</a>',
html_message,
)
self.assertIn(exp_target_link, n.message)

with self.subTest('Test notification email for metric returned under threhold'):
m.write(50)
n = notification_queryset.last()
url_path = reverse('notifications:notification_read_redirect', args=[n.pk])
email_link = f'https://example.com{url_path}'
email = mail.outbox.pop()
html_message, content_type = email.alternatives.pop()
self.assertEqual(email.subject, n.email_subject)
self.assertEqual(
email.body, exp_email_body.format(message=strip_tags(n.message))
email.body,
exp_email_body.format(
message=strip_tags(n.message), email_link=email_link
),
)
self.assertIn(n.message, html_message)
self.assertIn(
f'<a href="{exp_target_link}">'
f'<a href="{email_link}">'
'For further information see "device: default.test.device".</a>',
html_message,
)
self.assertIn(exp_target_link, n.message)

def test_notification_types(self):
self._create_admin()
Expand Down

0 comments on commit 985eaff

Please sign in to comment.