@@ -177,9 +177,10 @@ def test_whois_enabled(self):
177177 )
178178
179179 with self .subTest ("Test WHOIS not configured does not allow enabling WHOIS" ):
180- with mock .patch .object (
181- app_settings , "WHOIS_CONFIGURED" , False
182- ), self .assertRaises (ValidationError ) as context_manager :
180+ with (
181+ mock .patch .object (app_settings , "WHOIS_CONFIGURED" , False ),
182+ self .assertRaises (ValidationError ) as context_manager ,
183+ ):
183184 org_settings_obj .full_clean ()
184185 self .assertEqual (
185186 context_manager .exception .message_dict ["whois_enabled" ][0 ],
@@ -483,9 +484,12 @@ def test_whois_task_called(self, mocked_lookup_task):
483484 org .config_settings .save ()
484485
485486 with self .subTest ("WHOIS lookup task called when last_ip is public" ):
486- with mock .patch (
487- "django.core.cache.cache.get" , return_value = None
488- ) as mocked_get , mock .patch ("django.core.cache.cache.set" ) as mocked_set :
487+ with (
488+ mock .patch (
489+ "django.core.cache.cache.get" , return_value = None
490+ ) as mocked_get ,
491+ mock .patch ("django.core.cache.cache.set" ) as mocked_set ,
492+ ):
489493 device = self ._create_device (last_ip = "172.217.22.14" )
490494 mocked_lookup_task .assert_called ()
491495 mocked_set .assert_called_once_with (
@@ -499,9 +503,10 @@ def test_whois_task_called(self, mocked_lookup_task):
499503 with self .subTest (
500504 "WHOIS lookup task called when last_ip is changed and is public"
501505 ):
502- with mock .patch ("django.core.cache.cache.get" ) as mocked_get , mock .patch (
503- "django.core.cache.cache.set"
504- ) as mocked_set :
506+ with (
507+ mock .patch ("django.core.cache.cache.get" ) as mocked_get ,
508+ mock .patch ("django.core.cache.cache.set" ) as mocked_set ,
509+ ):
505510 device .last_ip = "172.217.22.10"
506511 device .save ()
507512 device .refresh_from_db ()
@@ -823,11 +828,13 @@ def test_device_last_ip_deferred_checks(self):
823828 device ._check_last_ip ()
824829 self .assertTrue (device ._is_deferred ("last_ip" ))
825830
826- with self .subTest (
827- "Test update_whois does not run if last_ip is deferred"
828- ), mock .patch (
829- "openwisp_controller.config.whois.service.WHOISService.update_whois_info"
830- ) as mock_update_whois :
831+ with (
832+ self .subTest ("Test update_whois does not run if last_ip is deferred" ),
833+ mock .patch (
834+ "openwisp_controller.config.whois.service"
835+ ".WHOISService.update_whois_info"
836+ ) as mock_update_whois ,
837+ ):
831838 threshold = app_settings .WHOIS_REFRESH_THRESHOLD_DAYS + 1
832839 new_time = timezone .now () - timedelta (days = threshold )
833840 WHOISInfo .objects .filter (pk = whois_obj .pk ).update (modified = new_time )
@@ -871,9 +878,12 @@ def test_whois_task_failure_notification(self, mock_info, mock_warn, mock_error)
871878 def assert_logging_on_exception (
872879 exception , info_calls = 0 , warn_calls = 0 , error_calls = 1 , notification_count = 1
873880 ):
874- with self .subTest (
875- f"Test notification and logging when { exception .__name__ } is raised"
876- ), mock .patch (self ._WHOIS_GEOIP_CLIENT ) as mock_client :
881+ with (
882+ self .subTest (
883+ f"Test notification and logging when { exception .__name__ } is raised"
884+ ),
885+ mock .patch (self ._WHOIS_GEOIP_CLIENT ) as mock_client ,
886+ ):
877887 mock_client .return_value .city .side_effect = exception ("test" )
878888 Device .objects .all ().delete () # Clear existing devices
879889 device = self ._create_device (last_ip = "172.217.22.14" )
@@ -947,9 +957,10 @@ def trigger_error_and_assert_cached(exc, notification_count=0):
947957 ):
948958 trigger_error_and_assert_cached (subsequent_error , 0 )
949959
950- with self .subTest ("Test cache updated on success" ), mock .patch (
951- self ._WHOIS_GEOIP_CLIENT
952- ) as mock_client :
960+ with (
961+ self .subTest ("Test cache updated on success" ),
962+ mock .patch (self ._WHOIS_GEOIP_CLIENT ) as mock_client ,
963+ ):
953964 Device .objects .all ().delete ()
954965 mocked_response = self ._mocked_client_response ()
955966 mocked_response .location = None
@@ -965,9 +976,12 @@ def trigger_error_and_assert_cached(exc, notification_count=0):
965976 @mock .patch ("openwisp_controller.config.whois.tasks.fetch_whois_details.retry" )
966977 def test_whois_task_retry_mechanism (self , mock_retry ):
967978 def assert_retry_on_exception (exception , should_retry ):
968- with self .subTest (
969- f"Test retry mechanism when { exception .__name__ } is raised"
970- ), mock .patch (self ._WHOIS_GEOIP_CLIENT ) as mock_client :
979+ with (
980+ self .subTest (
981+ f"Test retry mechanism when { exception .__name__ } is raised"
982+ ),
983+ mock .patch (self ._WHOIS_GEOIP_CLIENT ) as mock_client ,
984+ ):
971985 mock_client .return_value .city .side_effect = exception ("test" )
972986 Device .objects .all ().delete ()
973987 mock_retry .reset_mock ()
@@ -995,6 +1009,20 @@ def test_fetch_whois_details_device_not_found(self, mock_warn):
9951009 f"Device { invalid_pk } not found, skipping WHOIS lookup"
9961010 )
9971011
1012+ @mock .patch .object (app_settings , "WHOIS_CONFIGURED" , True )
1013+ @mock .patch (_WHOIS_TASKS_INFO_LOGGER )
1014+ @mock .patch (_WHOIS_GEOIP_CLIENT )
1015+ def test_fetch_whois_details_skips_when_deactivated (self , mock_client , mock_info ):
1016+ whois_obj = self ._create_whois_info (ip_address = "8.8.8.8" )
1017+ device = self ._create_device (last_ip = whois_obj .ip_address )
1018+ mock_client .reset_mock ()
1019+ device .deactivate ()
1020+ fetch_whois_details (device_pk = device .pk , initial_ip_address = "10.0.0.1" )
1021+ mock_info .assert_called_once_with (
1022+ f"Device { device .pk } is deactivated, skipping WHOIS lookup"
1023+ )
1024+ mock_client .assert_not_called ()
1025+
9981026 @mock .patch .object (app_settings , "WHOIS_CONFIGURED" , True )
9991027 @mock .patch (_WHOIS_GEOIP_CLIENT )
10001028 def test_fetch_whois_details_record_already_exists (self , mock_client ):
@@ -1077,6 +1105,67 @@ def test_get_whois_info_when_not_configured(self):
10771105 result = get_whois_info (pk = device .pk )
10781106 self .assertIsNone (result )
10791107
1108+ @mock .patch .object (app_settings , "WHOIS_CONFIGURED" , True )
1109+ @mock .patch ("openwisp_controller.config.whois.service.fetch_whois_details.delay" )
1110+ def test_process_ip_skips_when_deactivated (self , mock_task ):
1111+ # Public IP that would normally trigger a lookup, so the only reason
1112+ # the task is not enqueued is the deactivation guard under test.
1113+ device = self ._create_device ()
1114+ org_settings = device .organization .config_settings
1115+ org_settings .whois_enabled = True
1116+ org_settings .save ()
1117+ device ._initial_last_ip = None
1118+ device .last_ip = "8.8.8.8"
1119+ device .save ()
1120+ mock_task .reset_mock ()
1121+ device .deactivate ()
1122+ service = WHOISService (device )
1123+ service .process_ip_data_and_location ()
1124+ self .assertEqual (mock_task .call_count , 0 )
1125+
1126+ @mock .patch .object (app_settings , "WHOIS_CONFIGURED" , True )
1127+ @mock .patch ("openwisp_controller.config.whois.service.fetch_whois_details.delay" )
1128+ def test_process_ip_runs_when_active (self , mock_task ):
1129+ device = self ._create_device ()
1130+ org_settings = device .organization .config_settings
1131+ org_settings .whois_enabled = True
1132+ org_settings .save ()
1133+ device ._initial_last_ip = None
1134+ device .last_ip = "8.8.8.8"
1135+ device .save ()
1136+ mock_task .reset_mock ()
1137+ service = WHOISService (device )
1138+ service .process_ip_data_and_location ()
1139+ # transaction.on_commit executes immediately in TransactionTestCase,
1140+ # so the task is triggered synchronously here
1141+ # _initial_last_ip is '8.8.8.8' here because _check_last_ip in
1142+ # device.save() set it to device.last_ip after the first save.
1143+ mock_task .assert_called_once_with (
1144+ device_pk = device .pk ,
1145+ initial_ip_address = "8.8.8.8" ,
1146+ )
1147+
1148+ @mock .patch .object (app_settings , "WHOIS_CONFIGURED" , True )
1149+ @mock .patch ("openwisp_controller.config.whois.service.fetch_whois_details.delay" )
1150+ def test_update_whois_skips_when_deactivated (self , mock_task ):
1151+ device = self ._create_device ()
1152+ org_settings = device .organization .config_settings
1153+ org_settings .whois_enabled = True
1154+ org_settings .save ()
1155+ ip = "8.8.8.8"
1156+ device ._initial_last_ip = None
1157+ device .last_ip = ip
1158+ device .save ()
1159+ threshold = app_settings .WHOIS_REFRESH_THRESHOLD_DAYS + 1
1160+ expired_time = timezone .now () - timedelta (days = threshold )
1161+ whois_obj = self ._create_whois_info (ip_address = ip )
1162+ WHOISInfo .objects .filter (pk = whois_obj .pk ).update (modified = expired_time )
1163+ device .deactivate ()
1164+ mock_task .reset_mock ()
1165+ service = WHOISService (device )
1166+ service .update_whois_info ()
1167+ mock_task .assert_not_called ()
1168+
10801169
10811170@tag ("selenium_tests" )
10821171class TestWHOISSelenium (CreateWHOISMixin , SeleniumTestMixin , StaticLiveServerTestCase ):
0 commit comments