Skip to content

Commit 5e46f06

Browse files
committed
[tests] Added new test for DeviceChecksumView, checks for WHOIS model
Signed-off-by: DragnEmperor <[email protected]>
1 parent e91c3e5 commit 5e46f06

File tree

7 files changed

+99
-9
lines changed

7 files changed

+99
-9
lines changed

openwisp_controller/config/base/whois.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django.core.cache import cache
44
from django.core.exceptions import ValidationError
5+
from django.core.validators import MaxValueValidator, MinValueValidator
56
from django.db import models, transaction
67
from django.utils.translation import gettext_lazy as _
78
from jsonfield import JSONField
@@ -55,11 +56,13 @@ class AbstractWHOISInfo(TimeStampedEditableModel):
5556
null=True,
5657
blank=True,
5758
help_text=_("Latitude"),
59+
validators=[MinValueValidator(-90.0), MaxValueValidator(90.0)],
5860
)
5961
longitude = models.FloatField(
6062
null=True,
6163
blank=True,
6264
help_text=_("Longitude"),
65+
validators=[MinValueValidator(-180.0), MaxValueValidator(180.0)],
6366
)
6467

6568
class Meta:

openwisp_controller/config/migrations/0062_organizationconfigsettings_approximate_location_enabled_and_more.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by Django 5.2.1 on 2025-07-10 18:09
22

3+
import django.core.validators
34
from django.db import migrations, models
45

56
import openwisp_utils.fields
@@ -27,11 +28,27 @@ class Migration(migrations.Migration):
2728
migrations.AddField(
2829
model_name="whoisinfo",
2930
name="latitude",
30-
field=models.FloatField(blank=True, help_text="Latitude", null=True),
31+
field=models.FloatField(
32+
blank=True,
33+
help_text="Latitude",
34+
null=True,
35+
validators=[
36+
django.core.validators.MinValueValidator(-90.0),
37+
django.core.validators.MaxValueValidator(90.0),
38+
],
39+
),
3140
),
3241
migrations.AddField(
3342
model_name="whoisinfo",
3443
name="longitude",
35-
field=models.FloatField(blank=True, help_text="Longitude", null=True),
44+
field=models.FloatField(
45+
blank=True,
46+
help_text="Longitude",
47+
null=True,
48+
validators=[
49+
django.core.validators.MinValueValidator(-180.0),
50+
django.core.validators.MaxValueValidator(180.0),
51+
],
52+
),
3653
),
3754
]

openwisp_controller/config/whois/test_whois.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,37 @@ def test_whois_model_fields_validation(self):
221221
with self.assertRaises(ValidationError):
222222
self._create_whois_info(asn="InvalidASN")
223223

224+
# Common validation checks for latitude and longitude
225+
latitudes = [
226+
(100.0, "Ensure this value is less than or equal to 90.0."),
227+
(-100.0, "Ensure this value is greater than or equal to -90.0."),
228+
]
229+
for value, expected_msg in latitudes:
230+
with self.assertRaises(ValidationError) as context_manager:
231+
self._create_whois_info(latitude=value)
232+
try:
233+
self.assertEqual(
234+
context_manager.exception.message_dict["latitude"][0],
235+
expected_msg,
236+
)
237+
except AssertionError:
238+
self.fail("ValidationError message not equal to expected message.")
239+
240+
longitudes = [
241+
(200.0, "Ensure this value is less than or equal to 180.0."),
242+
(-200.0, "Ensure this value is greater than or equal to -180.0."),
243+
]
244+
for value, expected_msg in longitudes:
245+
with self.assertRaises(ValidationError) as context_manager:
246+
self._create_whois_info(longitude=value)
247+
try:
248+
self.assertEqual(
249+
context_manager.exception.message_dict["longitude"][0],
250+
expected_msg,
251+
)
252+
except AssertionError:
253+
self.fail("ValidationError message not equal to expected message.")
254+
224255

225256
class TestWHOISTransaction(
226257
CreateWHOISMixin, WHOISTransactionMixin, TransactionTestCase
@@ -355,6 +386,8 @@ def _verify_whois_details(instance, ip_address):
355386
instance.formatted_address,
356387
"Mountain View, United States, North America, 94043",
357388
)
389+
self.assertEqual(instance.latitude, 50)
390+
self.assertEqual(instance.longitude, 150)
358391

359392
# mocking the response from the geoip2 client
360393
mock_client.return_value.city.return_value = self._mocked_client_response()

openwisp_controller/config/whois/utils.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def _create_whois_info(self, **kwargs):
6060
isp="Google LLC",
6161
timezone="America/Los_Angeles",
6262
cidr="172.217.22.0/24",
63+
longitude=150.0,
64+
latitude=50.0,
6365
)
6466

6567
options.update(kwargs)
@@ -87,8 +89,8 @@ def _mocked_client_response():
8789
mock_response.traits.autonomous_system_number = 15169
8890
mock_response.traits.network = "172.217.22.0/24"
8991
mock_response.location.time_zone = "America/Los_Angeles"
90-
mock_response.location.latitude = 2
91-
mock_response.location.longitude = 23
92+
mock_response.location.latitude = 50
93+
mock_response.location.longitude = 150
9294
return mock_response
9395

9496
def _task_called(self, mocked_task, task_name="WHOIS lookup"):
@@ -159,3 +161,18 @@ def _task_called(self, mocked_task, task_name="WHOIS lookup"):
159161
self.assertEqual(response.status_code, 200)
160162
mocked_task.assert_called()
161163
mocked_task.reset_mock()
164+
165+
with self.subTest(
166+
f"{task_name} task not called via DeviceChecksumView when WHOIS is disabled"
167+
):
168+
WHOISInfo.objects.all().delete()
169+
org.config_settings.whois_enabled = False
170+
org.config_settings.save(update_fields=["whois_enabled"])
171+
response = self.client.get(
172+
reverse("controller:device_checksum", args=[device.pk]),
173+
{"key": device.key},
174+
REMOTE_ADDR=device.last_ip,
175+
)
176+
self.assertEqual(response.status_code, 200)
177+
mocked_task.assert_not_called()
178+
mocked_task.reset_mock()

openwisp_controller/geo/approximate_location/__init__.py

Whitespace-only changes.

openwisp_controller/geo/approximate_location/test_approximate_location.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class TestApproximateLocation(TestAdminMixin, TestCase):
2626
OPENWISP_CONTROLLER_WHOIS_GEOIP_ACCOUNT="test_account",
2727
OPENWISP_CONTROLLER_WHOIS_GEOIP_KEY="test_key",
2828
)
29-
def test_whois_configuration_setting(self):
29+
def test_approximate_location_configuration_setting(self):
3030
# reload app_settings to apply the overridden settings
3131
importlib.reload(config_app_settings)
3232
with self.subTest(
@@ -91,6 +91,9 @@ def test_approximate_location_task_called(
9191
with self.subTest(
9292
"Approximate location task called when last_ip has related WhoIsInfo"
9393
):
94+
device.organization.config_settings.whois_enabled = True
95+
device.organization.config_settings.approximate_location_enabled = True
96+
device.organization.config_settings.save()
9497
device.last_ip = "172.217.22.14"
9598
self._create_whois_info(ip_address=device.last_ip)
9699
device.save()
@@ -136,8 +139,8 @@ def _verify_location_details(device, mocked_response):
136139

137140
with self.subTest("Test Fuzzy location updated when last ip is updated"):
138141
device.last_ip = "172.217.22.10"
139-
mocked_response.location.latitude = 100
140-
mocked_response.location.longitude = 200
142+
mocked_response.location.latitude = 50
143+
mocked_response.location.longitude = 150
141144
mocked_response.city.name = "New City"
142145
mock_client.return_value.city.return_value = mocked_response
143146
device.save()

tests/openwisp2/sample_config/migrations/0009_organizationconfigsettings_approximate_location_enabled_and_more.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by Django 5.2.1 on 2025-07-11 16:28
22

3+
import django.core.validators
34
from django.db import migrations, models
45

56
import openwisp_utils.fields
@@ -27,11 +28,27 @@ class Migration(migrations.Migration):
2728
migrations.AddField(
2829
model_name="whoisinfo",
2930
name="latitude",
30-
field=models.FloatField(blank=True, help_text="Latitude", null=True),
31+
field=models.FloatField(
32+
blank=True,
33+
help_text="Latitude",
34+
null=True,
35+
validators=[
36+
django.core.validators.MinValueValidator(-90.0),
37+
django.core.validators.MaxValueValidator(90.0),
38+
],
39+
),
3140
),
3241
migrations.AddField(
3342
model_name="whoisinfo",
3443
name="longitude",
35-
field=models.FloatField(blank=True, help_text="Longitude", null=True),
44+
field=models.FloatField(
45+
blank=True,
46+
help_text="Longitude",
47+
null=True,
48+
validators=[
49+
django.core.validators.MinValueValidator(-180.0),
50+
django.core.validators.MaxValueValidator(180.0),
51+
],
52+
),
3653
),
3754
]

0 commit comments

Comments
 (0)