11from django .core import mail
2+ from django .urls import reverse
23from django .utils .html import strip_tags
34from 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 \n For more information see { exp_target_link } .'
29+ exp_email_body = '{message}' f'\n \n For 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' ,
0 commit comments