1
1
from django .core import mail
2
+ from django .urls import reverse
2
3
from django .utils .html import strip_tags
3
4
from swapper import load_model
4
5
@@ -21,10 +22,12 @@ def setUp(self):
21
22
def _generic_notification_test (
22
23
self , exp_level , exp_type , exp_verb , exp_message , exp_email_subject
23
24
):
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 } '
24
28
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 } .'
26
30
27
- n = Notification .objects .first ()
28
31
email = mail .outbox .pop ()
29
32
html_message , _ = email .alternatives .pop ()
30
33
self .assertEqual (n .type , exp_type )
@@ -42,19 +45,20 @@ def _generic_notification_test(
42
45
self .assertEqual (
43
46
email .body , exp_email_body .format (message = strip_tags (n .message ))
44
47
)
45
- self .assertIn (n .message , html_message )
46
48
self .assertIn (
47
- f'<a href="{ exp_target_link } ">'
49
+ f'<a href="{ exp_email_link } ">'
48
50
'For further information see "device: default.test.device".</a>' ,
49
51
html_message ,
50
52
)
51
53
52
54
def test_connection_working_notification (self ):
55
+ self .assertEqual (Notification .objects .count (), 0 )
53
56
self .dc = DeviceConnection .objects .create (
54
57
credentials = self .creds , device = self .d , is_working = False
55
58
)
56
59
self .dc .is_working = True
57
60
self .dc .save ()
61
+ self .assertEqual (Notification .objects .count (), 1 )
58
62
self ._generic_notification_test (
59
63
exp_level = 'info' ,
60
64
exp_type = 'connection_is_working' ,
@@ -67,8 +71,10 @@ def test_connection_working_notification(self):
67
71
)
68
72
69
73
def test_connection_not_working_notification (self ):
74
+ self .assertEqual (Notification .objects .count (), 0 )
70
75
self .dc .is_working = False
71
76
self .dc .save ()
77
+ self .assertEqual (Notification .objects .count (), 1 )
72
78
self ._generic_notification_test (
73
79
exp_level = 'error' ,
74
80
exp_type = 'connection_is_not_working' ,
@@ -81,9 +87,11 @@ def test_connection_not_working_notification(self):
81
87
)
82
88
83
89
def test_unreachable_after_upgrade_notification (self ):
90
+ self .assertEqual (Notification .objects .count (), 0 )
84
91
self .dc .is_working = False
85
92
self .dc .failure_reason = 'Giving up, device not reachable anymore after upgrade'
86
93
self .dc .save ()
94
+ self .assertEqual (Notification .objects .count (), 1 )
87
95
self ._generic_notification_test (
88
96
exp_level = 'error' ,
89
97
exp_type = 'connection_is_not_working' ,
0 commit comments