1
+ import importlib
1
2
from unittest import mock
2
3
3
- from django .core .exceptions import ValidationError
4
+ from django .core .exceptions import ImproperlyConfigured , ValidationError
5
+ from django .db .models .signals import post_delete , post_save
4
6
from django .test import TestCase , TransactionTestCase , override_settings
5
7
from django .urls import reverse
6
8
from geoip2 import errors
7
9
from swapper import load_model
8
10
11
+ from ...tests .utils import TestAdminMixin
9
12
from .. import settings as app_settings
10
13
from .handlers import connect_who_is_handlers
11
14
from .utils import CreateWhoIsMixin
18
21
notification_qs = Notification .objects .all ()
19
22
20
23
21
- class TestWhoIsInfoModel (CreateWhoIsMixin , TestCase ):
22
- def setUp (self ):
23
- self .admin = self ._get_admin ()
24
- # connect the signals related to who_is
25
- connect_who_is_handlers ()
24
+ class TestWhoIsFeature (CreateWhoIsMixin , TestAdminMixin , TestCase ):
25
+ @override_settings (
26
+ OPENWISP_CONTROLLER_WHO_IS_ENABLED = False ,
27
+ OPENWISP_CONTROLLER_GEOIP_ACCOUNT_ID = None ,
28
+ OPENWISP_CONTROLLER_GEOIP_LICENSE_KEY = None ,
29
+ )
30
+ def test_who_is_configuration_setting (self ):
31
+ # disconnect previously connected signals on app start, if any
32
+ self ._disconnect_signals ()
33
+ # reload app_settings to apply the overridden settings
34
+ importlib .reload (app_settings )
35
+
36
+ with self .subTest ("Test Signals not connected when WHO_IS_CONFIGURED is False" ):
37
+ # should not connect any handlers since WHO_IS_CONFIGURED is False
38
+ connect_who_is_handlers ()
39
+
40
+ assert not any (
41
+ "device.delete_who_is_info" in str (r [0 ]) for r in post_delete .receivers
42
+ )
43
+ assert not any (
44
+ "invalidate_org_config_cache_on_org_config_save" in str (r [0 ])
45
+ for r in post_save .receivers
46
+ )
47
+ assert not any (
48
+ "invalidate_org_config_cache_on_org_config_delete" in str (r [0 ])
49
+ for r in post_delete .receivers
50
+ )
51
+
52
+ with self .subTest (
53
+ "Test WhoIs field hidden on admin when WHO_IS_CONFIGURED is False"
54
+ ):
55
+ self ._login ()
56
+ url = reverse (
57
+ "admin:openwisp_users_organization_change" ,
58
+ args = [self ._get_org ().pk ],
59
+ )
60
+ response = self .client .get (url )
61
+ self .assertNotContains (response , 'name="config_settings-0-who_is_enabled"' )
26
62
27
- def test_who_is_feature (self ):
63
+ with self .subTest (
64
+ "Test ImproperlyConfigured raised when WHO_IS_CONFIGURED is False"
65
+ ):
66
+ with override_settings (OPENWISP_CONTROLLER_WHO_IS_ENABLED = True ):
67
+ with self .assertRaises (ImproperlyConfigured ):
68
+ # reload app_settings to apply the overridden settings
69
+ importlib .reload (app_settings )
70
+
71
+ with override_settings (
72
+ OPENWISP_CONTROLLER_GEOIP_ACCOUNT_ID = "test_account_id" ,
73
+ OPENWISP_CONTROLLER_GEOIP_LICENSE_KEY = "test_license_key" ,
74
+ ):
75
+ importlib .reload (app_settings )
76
+ with self .subTest (
77
+ "Test WHO_IS_CONFIGURED is True when both settings are set"
78
+ ):
79
+ self .assertTrue (app_settings .WHO_IS_CONFIGURED )
80
+
81
+ with self .subTest (
82
+ "Test WhoIs field visible on admin when WHO_IS_CONFIGURED is True"
83
+ ):
84
+ self ._login ()
85
+ url = reverse (
86
+ "admin:openwisp_users_organization_change" ,
87
+ args = [self ._get_org ().pk ],
88
+ )
89
+ response = self .client .get (url )
90
+ self .assertContains (response , 'name="config_settings-0-who_is_enabled"' )
91
+
92
+ def test_who_is_enabled (self ):
28
93
org = self ._get_org ()
29
94
org_settings_obj = OrganizationConfigSettings (
30
95
organization = org , who_is_enabled = True
31
96
)
32
- with self .assertRaises (ValidationError ):
97
+
98
+ with self .subTest ("Test WhoIs not configured does not allow enabling who_is" ):
99
+ with mock .patch .object (
100
+ app_settings , "WHO_IS_CONFIGURED" , False
101
+ ), self .assertRaises (ValidationError ):
102
+ org_settings_obj .full_clean ()
103
+
104
+ # create org settings object with WHO_IS_CONFIGURED set to True
105
+ with mock .patch .object (app_settings , "WHO_IS_CONFIGURED" , True ):
33
106
org_settings_obj .full_clean ()
34
107
org_settings_obj .save ()
35
108
36
- def test_validate_who_is_fields (self ):
109
+ with self .subTest ("Test setting who_is enabled to True" ):
110
+ org .config_settings .who_is_enabled = True
111
+ org .config_settings .save (update_fields = ["who_is_enabled" ])
112
+ org .config_settings .refresh_from_db (fields = ["who_is_enabled" ])
113
+ self .assertEqual (getattr (org .config_settings , "who_is_enabled" ), True )
114
+
115
+ with self .subTest ("Test setting who_is enabled to False" ):
116
+ org .config_settings .who_is_enabled = False
117
+ org .config_settings .save (update_fields = ["who_is_enabled" ])
118
+ org .config_settings .refresh_from_db (fields = ["who_is_enabled" ])
119
+ self .assertEqual (getattr (org .config_settings , "who_is_enabled" ), False )
120
+
121
+ with self .subTest (
122
+ "Test setting who_is enabled to None fallbacks to global setting"
123
+ ):
124
+ # reload app_settings to ensure latest settings are applied
125
+ importlib .reload (app_settings )
126
+ org .config_settings .who_is_enabled = None
127
+ org .config_settings .save (update_fields = ["who_is_enabled" ])
128
+ org .config_settings .refresh_from_db (fields = ["who_is_enabled" ])
129
+ self .assertEqual (
130
+ getattr (org .config_settings , "who_is_enabled" ),
131
+ app_settings .WHO_IS_ENABLED ,
132
+ )
133
+
134
+
135
+ class TestWhoIsInfoModel (CreateWhoIsMixin , TestCase ):
136
+ def test_who_is_model_fields_validation (self ):
37
137
"""
38
138
Test db_constraints and validators for WhoIsInfo model fields.
39
139
"""
40
140
org = self ._get_org ()
141
+ # using `create` to bypass `clean` method validation
41
142
OrganizationConfigSettings .objects .create (organization = org , who_is_enabled = True )
42
143
43
144
with self .assertRaises (ValidationError ):
@@ -55,27 +156,6 @@ def test_validate_who_is_fields(self):
55
156
with self .assertRaises (ValidationError ):
56
157
self ._create_who_is_info (asn = "InvalidASN" )
57
158
58
- def test_who_is_enabled (self ):
59
- org = self ._get_org ()
60
- OrganizationConfigSettings .objects .create (organization = org )
61
-
62
- with self .subTest ("Test who_is enabled set to True" ):
63
- org .config_settings .who_is_enabled = True
64
- self .assertEqual (getattr (org .config_settings , "who_is_enabled" ), True )
65
-
66
- with self .subTest ("Test who_is enabled set to False" ):
67
- org .config_settings .who_is_enabled = False
68
- self .assertEqual (getattr (org .config_settings , "who_is_enabled" ), False )
69
-
70
- with self .subTest ("Test who_is enabled set to None" ):
71
- org .config_settings .who_is_enabled = None
72
- org .config_settings .save (update_fields = ["who_is_enabled" ])
73
- org .config_settings .refresh_from_db (fields = ["who_is_enabled" ])
74
- self .assertEqual (
75
- getattr (org .config_settings , "who_is_enabled" ),
76
- app_settings .WHO_IS_ENABLED ,
77
- )
78
-
79
159
80
160
class TestWhoIsTransaction (CreateWhoIsMixin , TransactionTestCase ):
81
161
_WHO_IS_GEOIP_CLIENT = (
@@ -89,8 +169,6 @@ class TestWhoIsTransaction(CreateWhoIsMixin, TransactionTestCase):
89
169
90
170
def setUp (self ):
91
171
self .admin = self ._get_admin ()
92
- # connect the signals related to who_is
93
- connect_who_is_handlers ()
94
172
95
173
@mock .patch .object (app_settings , "WHO_IS_CONFIGURED" , True )
96
174
@mock .patch (
@@ -99,6 +177,7 @@ def setUp(self):
99
177
def test_task_called (self , mocked_task ):
100
178
org = self ._get_org ()
101
179
OrganizationConfigSettings .objects .create (organization = org , who_is_enabled = True )
180
+ connect_who_is_handlers ()
102
181
103
182
with self .subTest ("task called when last_ip is public" ):
104
183
device = self ._create_device (last_ip = "172.217.22.14" )
@@ -117,8 +196,9 @@ def test_task_called(self, mocked_task):
117
196
mocked_task .assert_not_called ()
118
197
mocked_task .reset_mock ()
119
198
120
- with self .subTest ("task not called when last_ip is not changed" ):
121
- device .last_ip = "10.0.0.1"
199
+ with self .subTest ("task not called when last_ip has related WhoIsInfo" ):
200
+ device .last_ip = "172.217.22.10"
201
+ self ._create_who_is_info (ip_address = device .last_ip )
122
202
device .save ()
123
203
mocked_task .assert_not_called ()
124
204
mocked_task .reset_mock ()
@@ -127,15 +207,15 @@ def test_task_called(self, mocked_task):
127
207
Device .objects .all ().delete () # Clear existing devices
128
208
org .config_settings .who_is_enabled = False
129
209
# Invalidates old org config settings cache
130
- org .config_settings .save ()
210
+ org .config_settings .save (update_fields = [ "who_is_enabled" ] )
131
211
device = self ._create_device (last_ip = "172.217.22.14" )
132
212
mocked_task .assert_not_called ()
133
213
mocked_task .reset_mock ()
134
214
135
215
with self .subTest ("task called via DeviceChecksumView when who_is is enabled" ):
136
216
org .config_settings .who_is_enabled = True
137
217
# Invalidates old org config settings cache
138
- org .config_settings .save ()
218
+ org .config_settings .save (update_fields = [ "who_is_enabled" ] )
139
219
# config is required for checksum view to work
140
220
self ._create_config (device = device )
141
221
# setting remote address field to a public IP
0 commit comments