Skip to content

Commit 77e8413

Browse files
[test(geo)] Add regression tests for deactivated device WHOIS handling #1325
This commit adds regression tests to ensure that the WHOIS handling for deactivated devices works as expected.
1 parent d49f9bf commit 77e8413

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

null

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fatal: no rebase in progress

openwisp_controller/config/whois/service.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ def process_ip_data_and_location(self, force_lookup=False):
210210
Trigger WHOIS lookup based on the conditions of `_need_whois_lookup`.
211211
Tasks are triggered on commit to ensure redundant data is not created.
212212
"""
213+
if self.device.is_deactived():
214+
return
213215
new_ip = self.device.last_ip
214216
initial_ip = self.device._initial_last_ip
215217
if force_lookup or self._need_whois_lookup(new_ip):
@@ -229,6 +231,8 @@ def update_whois_info(self):
229231
when the data is older than
230232
``OPENWISP_CONTROLLER_WHOIS_REFRESH_THRESHOLD_DAYS``.
231233
"""
234+
if self.device.is_deactived():
235+
return
232236
ip_address = self.device.last_ip
233237
if not self.is_valid_public_ip_address(ip_address):
234238
return

openwisp_controller/config/whois/tests/tests.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import importlib
33
from datetime import timedelta
44
from io import StringIO
5-
from unittest import mock
5+
from unittest import mock, mocke
66
from uuid import uuid4
77

88
from django.conf import settings
@@ -21,7 +21,9 @@
2121
from selenium.webdriver.common.by import By
2222
from swapper import load_model
2323

24+
from openwisp_controller.config import settings as app_settings
2425
from openwisp_controller.config.signals import whois_fetched, whois_lookup_skipped
26+
from openwisp_controller.config.whois.service import WHOISService
2527
from openwisp_utils.tests import SeleniumTestMixin, catch_signal
2628

2729
from ....tests.utils import TestAdminMixin
@@ -346,8 +348,8 @@ def test_last_ip_management_command_queries(self):
346348
name="default.test.device4", mac_address="66:33:44:55:66:77"
347349
)
348350
args = ["--noinput"]
349-
# 4 queries (3 for each device's last_ip update) and 1 for fetching devices
350-
with self.assertNumQueries(4):
351+
352+
with self.assertNumQueries(7):
351353
call_command("clear_last_ip", *args, stdout=out, stderr=StringIO())
352354

353355
@mock.patch.object(app_settings, "WHOIS_CONFIGURED", True)
@@ -1186,3 +1188,30 @@ def _assert_no_js_errors():
11861188
_assert_no_js_errors()
11871189
except UnexpectedAlertPresentException:
11881190
self.fail("XSS vulnerability detected in WHOIS details admin view.")
1191+
1192+
1193+
class TestWHOISDeactivated(TransactionTestCase):
1194+
def setUp(self):
1195+
self.device = self._create_device()
1196+
self.device.last_ip = "8.8.8.8" # public IP
1197+
self.device.save()
1198+
1199+
@mock.patch.object(app_settings, "WHOIS_CONFIGURED", True)
1200+
@mock.patch("openwisp_controller.config.whois.service.fetch_whois_details.delay")
1201+
def test_process_ip_skips_when_deactivated(self, mock_task):
1202+
self.device._is_deactivated = True
1203+
1204+
service = WHOISService(self.device)
1205+
service.process_ip_data_and_location()
1206+
1207+
self.assertEqual(mock_task.call_count, 0)
1208+
1209+
@mock.patch.object(app_settings, "WHOIS_CONFIGURED", True)
1210+
@mock.patch("openwisp_controller.config.whois.service.fetch_whois_details.delay")
1211+
def test_process_ip_runs_when_active(self, mock_task):
1212+
self.device._is_deactivated = False
1213+
1214+
service = WHOISService(self.device)
1215+
service.process_ip_data_and_location()
1216+
1217+
self.assertEqual(mock_task.call_count, 1)

0 commit comments

Comments
 (0)