diff --git a/openwisp_controller/config/whois/service.py b/openwisp_controller/config/whois/service.py index 43a6ab3db..a98d44dd6 100644 --- a/openwisp_controller/config/whois/service.py +++ b/openwisp_controller/config/whois/service.py @@ -210,6 +210,8 @@ def process_ip_data_and_location(self, force_lookup=False): Trigger WHOIS lookup based on the conditions of `_need_whois_lookup`. Tasks are triggered on commit to ensure redundant data is not created. """ + if self.device.is_deactivated(): + return new_ip = self.device.last_ip initial_ip = self.device._initial_last_ip if force_lookup or self._need_whois_lookup(new_ip): @@ -229,6 +231,8 @@ def update_whois_info(self): when the data is older than ``OPENWISP_CONTROLLER_WHOIS_REFRESH_THRESHOLD_DAYS``. """ + if self.device.is_deactivated(): + return ip_address = self.device.last_ip if not self.is_valid_public_ip_address(ip_address): return diff --git a/openwisp_controller/config/whois/tests/tests.py b/openwisp_controller/config/whois/tests/tests.py index 05aaf644e..81f186c32 100644 --- a/openwisp_controller/config/whois/tests/tests.py +++ b/openwisp_controller/config/whois/tests/tests.py @@ -3,6 +3,7 @@ from datetime import timedelta from io import StringIO from unittest import mock +from unittest.mock import patch from uuid import uuid4 from django.conf import settings @@ -21,7 +22,22 @@ from selenium.webdriver.common.by import By from swapper import load_model +<<<<<<< HEAD +from openwisp_controller.config.models import Device from openwisp_controller.config.signals import whois_fetched, whois_lookup_skipped +from openwisp_controller.config.whois.service import WhoIsService +======= +<<<<<<< HEAD +from openwisp_controller.config import settings as app_settings +from openwisp_controller.config.models import Device +from openwisp_controller.config.signals import whois_fetched, whois_lookup_skipped +from openwisp_controller.config.whois.service import WHOISService +======= +from openwisp_controller.config.models import Device +from openwisp_controller.config.signals import whois_fetched, whois_lookup_skipped +from openwisp_controller.config.whois.service import WhoIsService +>>>>>>> 8d798b46448620c8536e84519b69134dc556bd56 +>>>>>>> 38cb7ad (test(geo): add regression tests for deactivated device WHOIS handling) from openwisp_utils.tests import SeleniumTestMixin, catch_signal from ....tests.utils import TestAdminMixin @@ -346,8 +362,15 @@ def test_last_ip_management_command_queries(self): name="default.test.device4", mac_address="66:33:44:55:66:77" ) args = ["--noinput"] +<<<<<<< HEAD + +======= # 4 queries (3 for each device's last_ip update) and 1 for fetching devices - with self.assertNumQueries(4): +<<<<<<< HEAD +======= +>>>>>>> 8d798b46448620c8536e84519b69134dc556bd56 +>>>>>>> 38cb7ad (test(geo): add regression tests for deactivated device WHOIS handling) + with self.assertNumQueries(7): call_command("clear_last_ip", *args, stdout=out, stderr=StringIO()) @mock.patch.object(app_settings, "WHOIS_CONFIGURED", True) @@ -1186,3 +1209,54 @@ def _assert_no_js_errors(): _assert_no_js_errors() except UnexpectedAlertPresentException: self.fail("XSS vulnerability detected in WHOIS details admin view.") + + +<<<<<<< HEAD +class TestWhoisDeactivated(TestCase): + def setUp(self): + self.device = Device.objects.create(name="test-device") + + @patch("openwisp_controller.config.whois.service.fetch_whois_details.delay") + def test_process_ip_skips_when_deactivated(self, mock_task): + self.device._is_deactivated = True + + service = WhoIsService(self.device) + service.process_ip_data_and_location() + + mock_task.assert_not_called() + + @patch("openwisp_controller.config.whois.service.fetch_whois_details.delay") + def test_update_whois_skips_when_deactivated(self, mock_task): + self.device._is_deactivated = True + + service = WhoIsService(self.device) + service.update_whois_info() + + mock_task.assert_not_called() +======= +class TestWHOISDeactivated(TransactionTestCase): + def setUp(self): + self.device = self._create_device() + self.device.last_ip = "8.8.8.8" # public IP + self.device.save() + + @mock.patch.object(app_settings, "WHOIS_CONFIGURED", True) + @mock.patch("openwisp_controller.config.whois.service.fetch_whois_details.delay") + def test_process_ip_skips_when_deactivated(self, mock_task): + self.device._is_deactivated = True + + service = WHOISService(self.device) + service.process_ip_data_and_location() + + self.assertEqual(mock_task.call_count, 0) + + @mock.patch.object(app_settings, "WHOIS_CONFIGURED", True) + @mock.patch("openwisp_controller.config.whois.service.fetch_whois_details.delay") + def test_process_ip_runs_when_active(self, mock_task): + self.device._is_deactivated = False + + service = WHOISService(self.device) + service.process_ip_data_and_location() + + self.assertEqual(mock_task.call_count, 1) +>>>>>>> 38cb7ad (test(geo): add regression tests for deactivated device WHOIS handling)