2121from selenium .webdriver .common .by import By
2222from swapper import load_model
2323
24- from openwisp_controller .config import settings as app_settings
2524from openwisp_controller .config .signals import whois_fetched , whois_lookup_skipped
26- from openwisp_controller .config .whois .service import WHOISService
2725from openwisp_utils .tests import SeleniumTestMixin , catch_signal
2826
2927from ....tests .utils import TestAdminMixin
@@ -349,7 +347,7 @@ def test_last_ip_management_command_queries(self):
349347 )
350348 args = ["--noinput" ]
351349
352- with self .assertNumQueries (7 ):
350+ with self .assertNumQueries (4 ):
353351 call_command ("clear_last_ip" , * args , stdout = out , stderr = StringIO ())
354352
355353 @mock .patch .object (app_settings , "WHOIS_CONFIGURED" , True )
@@ -1203,7 +1201,13 @@ def setUp(self):
12031201 @mock .patch .object (app_settings , "WHOIS_CONFIGURED" , True )
12041202 @mock .patch ("openwisp_controller.config.whois.service.fetch_whois_details.delay" )
12051203 def test_process_ip_skips_when_deactivated (self , mock_task ):
1206- self .device ._is_deactivated = True
1204+ # Public IP that would normally trigger a lookup, so the only reason
1205+ # the task is not enqueued is the deactivation guard under test.
1206+ self .device .last_ip = "8.8.8.8"
1207+ self .device .save ()
1208+ mock_task .reset_mock ()
1209+
1210+ self .device .deactivate ()
12071211
12081212 service = WHOISService (self .device )
12091213 service .process_ip_data_and_location ()
@@ -1213,10 +1217,39 @@ def test_process_ip_skips_when_deactivated(self, mock_task):
12131217 @mock .patch .object (app_settings , "WHOIS_CONFIGURED" , True )
12141218 @mock .patch ("openwisp_controller.config.whois.service.fetch_whois_details.delay" )
12151219 def test_process_ip_runs_when_active (self , mock_task ):
1216- self .device ._is_deactivated = False
12171220 self .device .last_ip = "8.8.8.8"
1221+ self .device .save ()
1222+
1223+ mock_task .reset_mock ()
12181224
12191225 service = WHOISService (self .device )
12201226 service .process_ip_data_and_location ()
12211227
1222- self .assertEqual (mock_task .call_count , 1 )
1228+ # transaction.on_commit executes immediately in TransactionTestCase,
1229+ # so the task is triggered synchronously here
1230+ mock_task .assert_called_once_with (
1231+ device_pk = self .device .pk ,
1232+ initial_ip_address = self .device ._initial_last_ip ,
1233+ )
1234+
1235+ @mock .patch .object (app_settings , "WHOIS_CONFIGURED" , True )
1236+ @mock .patch ("openwisp_controller.config.whois.service.fetch_whois_details.delay" )
1237+ def test_update_whois_skips_when_deactivated (self , mock_task ):
1238+ ip = "8.8.8.8"
1239+ self .device .last_ip = ip
1240+ self .device .save ()
1241+
1242+ threshold = app_settings .WHOIS_REFRESH_THRESHOLD_DAYS + 1
1243+ expired_time = timezone .now () - timedelta (days = threshold )
1244+
1245+ whois_obj = self ._create_whois_info (ip_address = ip )
1246+ WHOISInfo .objects .filter (pk = whois_obj .pk ).update (modified = expired_time )
1247+
1248+ self .device .deactivate ()
1249+
1250+ mock_task .reset_mock ()
1251+
1252+ service = WHOISService (self .device )
1253+ service .update_whois_info ()
1254+
1255+ mock_task .assert_not_called ()
0 commit comments