From 985eaff2c3756dae2e3ffab21e41c9ee98eb1dde Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Thu, 6 Aug 2020 12:57:12 -0500 Subject: [PATCH] [tests] Added support for read_redirect view in notification tests --- .../device/tests/test_device_notifications.py | 16 +++++++++--- .../tests/test_monitoring_notifications.py | 25 +++++++++++++------ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/openwisp_monitoring/device/tests/test_device_notifications.py b/openwisp_monitoring/device/tests/test_device_notifications.py index 17bc8c837..173ac5371 100644 --- a/openwisp_monitoring/device/tests/test_device_notifications.py +++ b/openwisp_monitoring/device/tests/test_device_notifications.py @@ -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 @@ -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) @@ -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'' + f'' 'For further information see "device: default.test.device".', 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', @@ -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', @@ -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', diff --git a/openwisp_monitoring/monitoring/tests/test_monitoring_notifications.py b/openwisp_monitoring/monitoring/tests/test_monitoring_notifications.py index 9cd55959d..fcdbbbe84 100644 --- a/openwisp_monitoring/monitoring/tests/test_monitoring_notifications.py +++ b/openwisp_monitoring/monitoring/tests/test_monitoring_notifications.py @@ -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 @@ -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'' + f'' 'For further information see "device: default.test.device".', 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'' + f'' 'For further information see "device: default.test.device".', html_message, ) + self.assertIn(exp_target_link, n.message) def test_notification_types(self): self._create_admin()