1
+ import os
2
+
1
3
from django .apps .registry import apps
2
4
from django .core import mail
5
+ from django .test import TestCase
3
6
from django .urls import reverse
4
7
from django .utils .html import strip_tags
5
8
from openwisp_notifications .types import unregister_notification_type
12
15
DeviceConnection = load_model ('connection' , 'DeviceConnection' )
13
16
14
17
15
- class TestNotifications (CreateConnectionsMixin ):
18
+ class TestNotifications (CreateConnectionsMixin , TestCase ):
16
19
app_label = 'connection'
17
20
18
21
def setUp (self ):
@@ -21,23 +24,26 @@ def setUp(self):
21
24
self .creds = Credentials .objects .create (
22
25
connector = 'openwisp_controller.connection.connectors.ssh.Ssh'
23
26
)
24
- self .dc = DeviceConnection .objects .create (credentials = self .creds , device = self .d )
25
27
26
28
def _generic_notification_test (
27
29
self , exp_level , exp_type , exp_verb , exp_message , exp_email_subject
28
30
):
29
31
n = Notification .objects .first ()
30
32
url_path = reverse ('notifications:notification_read_redirect' , args = [n .pk ])
31
33
exp_email_link = f'https://example.com{ url_path } '
32
- exp_target_link = f'https://example.com/admin/config/device/{ self .d .id } /change/'
34
+ config_app = (
35
+ 'config' if not os .environ .get ('SAMPLE_APP' , False ) else 'sample_config'
36
+ )
37
+ device_url_path = reverse (f'admin:{ config_app } _device_change' , args = [self .d .id ])
38
+ exp_target_link = f'https://example.com{ device_url_path } '
33
39
exp_email_body = '{message}' f'\n \n For more information see { exp_email_link } .'
34
40
35
41
email = mail .outbox .pop ()
36
42
html_message , _ = email .alternatives .pop ()
37
43
self .assertEqual (n .type , exp_type )
38
44
self .assertEqual (n .level , exp_level )
39
45
self .assertEqual (n .verb , exp_verb )
40
- self .assertEqual (n .actor , self .dc )
46
+ self .assertEqual (n .actor , self .d . deviceconnection_set . first () )
41
47
self .assertEqual (n .target , self .d )
42
48
self .assertEqual (
43
49
n .message , exp_message .format (n = n , target_link = exp_target_link )
@@ -51,17 +57,17 @@ def _generic_notification_test(
51
57
)
52
58
self .assertIn (
53
59
f'<a href="{ exp_email_link } ">'
54
- 'For further information see "device: default.test.device ".</a>' ,
60
+ f 'For further information see "device: { n . target } ".</a>' ,
55
61
html_message ,
56
62
)
57
63
58
64
def test_connection_working_notification (self ):
59
65
self .assertEqual (Notification .objects .count (), 0 )
60
- self . dc = DeviceConnection .objects .create (
66
+ device_connection = DeviceConnection .objects .create (
61
67
credentials = self .creds , device = self .d , is_working = False
62
68
)
63
- self . dc .is_working = True
64
- self . dc .save ()
69
+ device_connection .is_working = True
70
+ device_connection .save ()
65
71
self .assertEqual (Notification .objects .count (), 1 )
66
72
self ._generic_notification_test (
67
73
exp_level = 'info' ,
@@ -74,10 +80,36 @@ def test_connection_working_notification(self):
74
80
exp_email_subject = '[example.com] RECOVERY: Connection to device {n.target}' ,
75
81
)
76
82
83
+ def test_connection_is_working_none (self ):
84
+ self .assertEqual (Notification .objects .count (), 0 )
85
+
86
+ with self .subTest ('no problem notification created when is_working=None' ):
87
+ DeviceConnection .objects .all ().delete ()
88
+ device_connection = DeviceConnection .objects .create (
89
+ credentials = self .creds , device = self .d , is_working = None
90
+ )
91
+ self .assertIsNone (device_connection .is_working )
92
+ device_connection .is_working = False
93
+ device_connection .save ()
94
+ self .assertEqual (Notification .objects .count (), 0 )
95
+
96
+ with self .subTest ('no recovery notification created when is_working=None' ):
97
+ DeviceConnection .objects .all ().delete ()
98
+ device_connection = DeviceConnection .objects .create (
99
+ credentials = self .creds , device = self .d , is_working = None
100
+ )
101
+ self .assertIsNone (device_connection .is_working )
102
+ device_connection .is_working = True
103
+ device_connection .save ()
104
+ self .assertEqual (Notification .objects .count (), 0 )
105
+
77
106
def test_connection_not_working_notification (self ):
107
+ device_connection = DeviceConnection .objects .create (
108
+ credentials = self .creds , device = self .d , is_working = True
109
+ )
78
110
self .assertEqual (Notification .objects .count (), 0 )
79
- self . dc .is_working = False
80
- self . dc .save ()
111
+ device_connection .is_working = False
112
+ device_connection .save ()
81
113
self .assertEqual (Notification .objects .count (), 1 )
82
114
self ._generic_notification_test (
83
115
exp_level = 'error' ,
@@ -91,10 +123,15 @@ def test_connection_not_working_notification(self):
91
123
)
92
124
93
125
def test_unreachable_after_upgrade_notification (self ):
126
+ device_connection = DeviceConnection .objects .create (
127
+ credentials = self .creds , device = self .d , is_working = True
128
+ )
94
129
self .assertEqual (Notification .objects .count (), 0 )
95
- self .dc .is_working = False
96
- self .dc .failure_reason = 'Giving up, device not reachable anymore after upgrade'
97
- self .dc .save ()
130
+ device_connection .is_working = False
131
+ device_connection .failure_reason = (
132
+ 'Giving up, device not reachable anymore after upgrade'
133
+ )
134
+ device_connection .save ()
98
135
self .assertEqual (Notification .objects .count (), 1 )
99
136
self ._generic_notification_test (
100
137
exp_level = 'error' ,
0 commit comments