Skip to content

Commit a8804f5

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

File tree

6 files changed

+80
-7
lines changed

6 files changed

+80
-7
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
@@ -303,6 +303,37 @@ def test_whois_model_fields_validation(self):
303303
with self.assertRaises(ValidationError):
304304
self._create_whois_info(asn="InvalidASN")
305305

306+
# Common validation checks for latitude and longitude
307+
latitudes = [
308+
(100.0, "Ensure this value is less than or equal to 90.0."),
309+
(-100.0, "Ensure this value is greater than or equal to -90.0."),
310+
]
311+
for value, expected_msg in latitudes:
312+
with self.assertRaises(ValidationError) as context_manager:
313+
self._create_whois_info(latitude=value)
314+
try:
315+
self.assertEqual(
316+
context_manager.exception.message_dict["latitude"][0],
317+
expected_msg,
318+
)
319+
except AssertionError:
320+
self.fail("ValidationError message not equal to expected message.")
321+
322+
longitudes = [
323+
(200.0, "Ensure this value is less than or equal to 180.0."),
324+
(-200.0, "Ensure this value is greater than or equal to -180.0."),
325+
]
326+
for value, expected_msg in longitudes:
327+
with self.assertRaises(ValidationError) as context_manager:
328+
self._create_whois_info(longitude=value)
329+
try:
330+
self.assertEqual(
331+
context_manager.exception.message_dict["longitude"][0],
332+
expected_msg,
333+
)
334+
except AssertionError:
335+
self.fail("ValidationError message not equal to expected message.")
336+
306337

307338
class TestWHOISTransaction(
308339
CreateWHOISMixin, WHOISTransactionMixin, TransactionTestCase
@@ -437,6 +468,8 @@ def _verify_whois_details(instance, ip_address):
437468
instance.formatted_address,
438469
"Mountain View, United States, North America, 94043",
439470
)
471+
self.assertEqual(instance.latitude, 50)
472+
self.assertEqual(instance.longitude, 150)
440473

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

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)