|
2 | 2 | import importlib |
3 | 3 | from datetime import timedelta |
4 | 4 | from io import StringIO |
5 | | -from unittest import mock |
| 5 | +from unittest import mock, mocke |
6 | 6 | from uuid import uuid4 |
7 | 7 |
|
8 | 8 | from django.conf import settings |
|
21 | 21 | from selenium.webdriver.common.by import By |
22 | 22 | from swapper import load_model |
23 | 23 |
|
| 24 | +from openwisp_controller.config import settings as app_settings |
24 | 25 | from openwisp_controller.config.signals import whois_fetched, whois_lookup_skipped |
| 26 | +from openwisp_controller.config.whois.service import WHOISService |
25 | 27 | from openwisp_utils.tests import SeleniumTestMixin, catch_signal |
26 | 28 |
|
27 | 29 | from ....tests.utils import TestAdminMixin |
@@ -346,8 +348,8 @@ def test_last_ip_management_command_queries(self): |
346 | 348 | name="default.test.device4", mac_address="66:33:44:55:66:77" |
347 | 349 | ) |
348 | 350 | 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): |
351 | 353 | call_command("clear_last_ip", *args, stdout=out, stderr=StringIO()) |
352 | 354 |
|
353 | 355 | @mock.patch.object(app_settings, "WHOIS_CONFIGURED", True) |
@@ -1186,3 +1188,30 @@ def _assert_no_js_errors(): |
1186 | 1188 | _assert_no_js_errors() |
1187 | 1189 | except UnexpectedAlertPresentException: |
1188 | 1190 | 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