Skip to content

Commit 9d95908

Browse files
committed
[fixes] Remove whois related ips from device checksum view
Signed-off-by: DragnEmperor <[email protected]>
1 parent be5a60d commit 9d95908

File tree

7 files changed

+17
-22
lines changed

7 files changed

+17
-22
lines changed

openwisp_controller/config/base/device.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,10 @@ def whois_service(self):
511511
return WHOISService(self)
512512

513513
def _check_last_ip(self, creating=False):
514-
"""Trigger WHOIS lookup if last_ip is not deferred."""
514+
"""
515+
Process details and location related to last_ip if last_ip has
516+
changed or is being set for the first time.
517+
"""
515518
if self._initial_last_ip == models.DEFERRED:
516519
return
517520
if creating or self.last_ip != self._initial_last_ip:

openwisp_controller/config/base/whois.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def device_whois_info_delete_handler(instance, **kwargs):
115115
and instance._get_organization__config_settings().whois_enabled
116116
and not existing_devices.exists()
117117
):
118-
transaction.on_commit(lambda: delete_whois_record.delay(instance.last_ip))
118+
transaction.on_commit(lambda: delete_whois_record.delay(last_ip))
119119

120120
# this method is kept here instead of in OrganizationConfigSettings because
121121
# currently the caching is used only for WHOIS feature
@@ -155,8 +155,8 @@ def _location_name(self):
155155
if address:
156156
parts = [part.strip() for part in address.split(",")[:2] if part.strip()]
157157
location = ", ".join(parts)
158-
return f"{location} (Estimated Location: {self.ip_address})"
159-
return f"Estimated Location: {self.ip_address}"
158+
return _(f"{location} (Estimated Location: {self.ip_address})")
159+
return _(f"Estimated Location: {self.ip_address}")
160160

161161
def _get_defaults_for_estimated_location(self):
162162
"""

openwisp_controller/config/controller/views.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ def get(self, request, pk):
153153
# updates cache if ip addresses changed
154154
if updated:
155155
self.update_device_cache(device)
156-
# When update fields are present then save() will run the WHOIS
157-
# lookup. But if there are no update fields, we still want to
158-
# trigger the WHOIS lookup which checks if WHOIS lookup is required
159-
# or not
160-
elif app_settings.WHOIS_CONFIGURED:
161-
device.whois_service.process_ip_data_and_location()
162156
checksum_requested.send(
163157
sender=device.__class__, instance=device, request=request
164158
)

openwisp_controller/config/management/commands/clear_last_ip.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from django.db.models import OuterRef, Subquery
33
from swapper import load_model
44

5-
from openwisp_controller.config import settings as app_settings
6-
75

86
class Command(BaseCommand):
97
help = "Clear the last IP address, if set, of active devices of all organizations."
@@ -19,10 +17,6 @@ def add_arguments(self, parser):
1917
return super().add_arguments(parser)
2018

2119
def handle(self, *args, **options):
22-
if not app_settings.WHOIS_CONFIGURED:
23-
self.stdout.write("WHOIS must be configured to use this option.")
24-
return
25-
2620
Device = load_model("config", "Device")
2721
WHOISInfo = load_model("config", "WHOISInfo")
2822

openwisp_controller/config/whois/test_whois.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ def test_whois_details_device_api(self):
267267
self.assertEqual(response.status_code, 200)
268268
self.assertNotIn("whois_info", response.data)
269269

270-
@mock.patch.object(app_settings, "WHOIS_CONFIGURED", True)
271270
def test_last_ip_management_command(self):
272271
out = StringIO()
273272
device = self._create_device(last_ip="172.217.22.11")
@@ -430,6 +429,7 @@ def test_whois_multiple_orgs(self, mocked_task):
430429
{"key": device1.key},
431430
REMOTE_ADDR="172.217.22.20",
432431
)
432+
device1.refresh_from_db()
433433
self.assertEqual(response.status_code, 200)
434434
mocked_task.assert_called()
435435
mocked_task.reset_mock()
@@ -438,12 +438,13 @@ def test_whois_multiple_orgs(self, mocked_task):
438438
{"key": device2.key},
439439
REMOTE_ADDR="172.217.22.30",
440440
)
441+
device2.refresh_from_db()
441442
self.assertEqual(response.status_code, 200)
442443
mocked_task.assert_not_called()
443444
mocked_task.reset_mock()
444445

445446
with self.subTest(
446-
"task called via DeviceChecksumView when a device has no WHOIS record"
447+
"Task not called via DeviceChecksumView when a device has no WHOIS record"
447448
):
448449
WHOISInfo.objects.all().delete()
449450
response = self.client.get(
@@ -452,7 +453,7 @@ def test_whois_multiple_orgs(self, mocked_task):
452453
REMOTE_ADDR=device1.last_ip,
453454
)
454455
self.assertEqual(response.status_code, 200)
455-
mocked_task.assert_called()
456+
mocked_task.assert_not_called()
456457
mocked_task.reset_mock()
457458
response = self.client.get(
458459
reverse("controller:device_checksum", args=[device2.pk]),

openwisp_controller/config/whois/tests_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def _task_called(self, mocked_task, task_name="WHOIS lookup"):
108108
# Invalidates old org config settings cache
109109
org.config_settings.save(update_fields=["whois_enabled"])
110110
# config is required for checksum view to work
111+
device.refresh_from_db()
111112
self._create_config(device=device)
112113
# setting remote address field to a public IP to trigger WHOIS task
113114
# since the view uses this header for tracking the device's IP
@@ -124,19 +125,21 @@ def _task_called(self, mocked_task, task_name="WHOIS lookup"):
124125
f"{task_name} task called via DeviceChecksumView for no WHOIS record"
125126
):
126127
WHOISInfo.objects.all().delete()
128+
device.refresh_from_db()
127129
response = self.client.get(
128130
reverse("controller:device_checksum", args=[device.pk]),
129131
{"key": device.key},
130132
REMOTE_ADDR=device.last_ip,
131133
)
132134
self.assertEqual(response.status_code, 200)
133-
mocked_task.assert_called()
135+
mocked_task.assert_not_called()
134136
mocked_task.reset_mock()
135137

136138
with self.subTest(
137139
f"{task_name} task not called via DeviceChecksumView when WHOIS is disabled"
138140
):
139141
WHOISInfo.objects.all().delete()
142+
device.refresh_from_db()
140143
org.config_settings.whois_enabled = False
141144
org.config_settings.save(update_fields=["whois_enabled"])
142145
response = self.client.get(

openwisp_controller/geo/estimated_location/test_estimated_location.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def test_estimated_location_task_called(
153153

154154
with self.subTest(
155155
"Estimated location task not called via DeviceChecksumView when "
156-
"last_ip has related WhoIsInfo"
156+
"last_ip has no related WhoIsInfo"
157157
):
158158
WHOISInfo.objects.all().delete()
159159
self._create_whois_info(ip_address=device.last_ip)
@@ -164,7 +164,7 @@ def test_estimated_location_task_called(
164164
REMOTE_ADDR=device.last_ip,
165165
)
166166
self.assertEqual(response.status_code, 200)
167-
mocked_estimated_location_task.assert_called()
167+
mocked_estimated_location_task.assert_not_called()
168168
mocked_estimated_location_task.reset_mock()
169169

170170
@mock.patch.object(config_app_settings, "WHOIS_CONFIGURED", True)

0 commit comments

Comments
 (0)