Skip to content

Commit c5eaeff

Browse files
committed
[qa] Refactoring, added validations, test cases
Signed-off-by: DragnEmperor <[email protected]>
1 parent 9d95908 commit c5eaeff

File tree

5 files changed

+95
-10
lines changed

5 files changed

+95
-10
lines changed

openwisp_controller/config/whois/service.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,48 @@ def _get_whois_info_from_db(ip_address):
4343

4444
return WHOISInfo.objects.filter(ip_address=ip_address)
4545

46-
@property
47-
def is_whois_enabled(self):
46+
@staticmethod
47+
def get_org_config_settings(org_id):
4848
"""
49-
Check if the WHOIS lookup feature is enabled.
50-
The OrganizationConfigSettings are cached as these settings
49+
Caches the OrganizationConfigSettings as these settings
5150
are not expected to change frequently. The timeout for the cache
5251
is set to the same as the checksum cache timeout for consistency
5352
with DeviceChecksumView.
5453
"""
5554
OrganizationConfigSettings = load_model("config", "OrganizationConfigSettings")
5655
Config = load_model("config", "Config")
5756

58-
org_id = self.device.organization.pk
59-
org_settings = cache.get(self.get_cache_key(org_id=org_id))
57+
cache_key = WHOISService.get_cache_key(org_id=org_id)
58+
org_settings = cache.get(cache_key)
6059
if org_settings is None:
6160
try:
6261
org_settings = OrganizationConfigSettings.objects.get(
6362
organization=org_id
6463
)
6564
except OrganizationConfigSettings.DoesNotExist:
6665
# If organization settings do not exist, fall back to global setting
67-
return app_settings.WHOIS_ENABLED
66+
return None
6867
cache.set(
69-
self.get_cache_key(org_id=org_id),
68+
cache_key,
7069
org_settings,
7170
timeout=Config._CHECKSUM_CACHE_TIMEOUT,
7271
)
72+
return org_settings
73+
74+
@property
75+
def is_whois_enabled(self):
76+
"""
77+
Check if the WHOIS lookup feature is enabled.
78+
"""
79+
org_settings = self.get_org_config_settings(org_id=self.device.organization.pk)
7380
return getattr(org_settings, "whois_enabled", app_settings.WHOIS_ENABLED)
7481

7582
@property
7683
def is_estimated_location_enabled(self):
7784
"""
7885
Check if the Estimated location feature is enabled.
79-
This does not require to set cache as `is_whois_enabled` already sets it
8086
"""
81-
org_settings = cache.get(self.get_cache_key(org_id=self.device.organization.pk))
87+
org_settings = self.get_org_config_settings(org_id=self.device.organization.pk)
8288
return getattr(
8389
org_settings,
8490
"estimated_location_enabled",

openwisp_controller/geo/admin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from ..admin import MultitenantAdminMixin
1818
from ..config.admin import DeactivatedDeviceReadOnlyMixin, DeviceAdminExportable
19+
from .estimated_location.utils import check_estimate_location_configured
1920
from .exportable import GeoDeviceResource
2021

2122
DeviceLocation = load_model("geo", "DeviceLocation")
@@ -100,6 +101,12 @@ class LocationAdmin(MultitenantAdminMixin, AbstractLocationAdmin):
100101
list_select_related = ("organization",)
101102
readonly_fields = ("is_estimated",)
102103

104+
def get_fields(self, request, obj=None):
105+
fields = super().get_fields(request, obj)
106+
if obj and not check_estimate_location_configured(obj.organization_id):
107+
fields.remove("is_estimated")
108+
return fields
109+
103110

104111
LocationAdmin.list_display.insert(1, "organization")
105112
LocationAdmin.list_filter.insert(0, MultitenantOrgFilter)

openwisp_controller/geo/base/models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.contrib.gis.db import models
2+
from django.core.exceptions import ValidationError
23
from django.utils.translation import gettext_lazy as _
34
from django_loci.base.models import (
45
AbstractFloorPlan,
@@ -9,6 +10,8 @@
910

1011
from openwisp_users.mixins import OrgMixin, ValidateOrgMixin
1112

13+
from ..estimated_location.utils import check_estimate_location_configured
14+
1215

1316
class BaseLocation(OrgMixin, AbstractLocation):
1417
is_estimated = models.BooleanField(
@@ -19,6 +22,19 @@ class BaseLocation(OrgMixin, AbstractLocation):
1922
class Meta(AbstractLocation.Meta):
2023
abstract = True
2124

25+
def clean(self):
26+
if self.is_estimated and not check_estimate_location_configured(
27+
self.organization_id
28+
):
29+
raise ValidationError(
30+
{
31+
"is_estimated": _(
32+
"Estimated Location feature required to be configured."
33+
)
34+
}
35+
)
36+
return super().clean()
37+
2238

2339
class BaseFloorPlan(OrgMixin, AbstractFloorPlan):
2440
location = models.ForeignKey(get_model_name("geo", "Location"), models.CASCADE)

openwisp_controller/geo/estimated_location/test_estimated_location.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from openwisp_controller.config.whois.tests_utils import WHOISTransactionMixin
1313

1414
from ...tests.utils import TestAdminMixin
15+
from ..tests.utils import TestGeoMixin
1516
from .tests_utils import TestEstimatedLocationMixin
1617

1718
Device = load_model("config", "Device")
@@ -98,6 +99,46 @@ def test_estimated_location_configuration_setting(self):
9899
)
99100

100101

102+
class TestEstimatedLocationField(TestEstimatedLocationMixin, TestGeoMixin, TestCase):
103+
location_model = Location
104+
105+
def test_estimated_location_field(self):
106+
org = self._get_org()
107+
org.config_settings.estimated_location_enabled = False
108+
org.config_settings.save()
109+
org.refresh_from_db()
110+
with self.assertRaises(ValidationError) as context_manager:
111+
self._create_location(organization=org, is_estimated=True)
112+
try:
113+
self.assertEqual(
114+
context_manager.exception.message_dict["is_estimated"][0],
115+
"Estimated Location feature required to be configured.",
116+
)
117+
except AssertionError:
118+
self.fail("ValidationError message not equal to expected message.")
119+
120+
@mock.patch.object(config_app_settings, "WHOIS_CONFIGURED", True)
121+
def test_estimated_location_admin(self):
122+
connect_whois_handlers()
123+
admin = self._create_admin()
124+
self.client.force_login(admin)
125+
org = self._get_org()
126+
location = self._create_location(organization=org, is_estimated=True)
127+
path = reverse("admin:geo_location_change", args=[location.pk])
128+
response = self.client.get(path)
129+
self.assertContains(response, "field-is_estimated")
130+
self.assertContains(
131+
response, "Whether the location's coordinates are estimated."
132+
)
133+
org.config_settings.estimated_location_enabled = False
134+
org.config_settings.save()
135+
response = self.client.get(path)
136+
self.assertNotContains(response, "field-is_estimated")
137+
self.assertNotContains(
138+
response, "Whether the location's coordinates are estimated."
139+
)
140+
141+
101142
class TestEstimatedLocationTransaction(
102143
TestEstimatedLocationMixin, WHOISTransactionMixin, TransactionTestCase
103144
):
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from openwisp_controller.config import settings as config_app_settings
2+
from openwisp_controller.config.whois.service import WHOISService
3+
4+
5+
def check_estimate_location_configured(org_id):
6+
if not org_id:
7+
return False
8+
if not config_app_settings.WHOIS_CONFIGURED:
9+
return False
10+
org_settings = WHOISService.get_org_config_settings(org_id=org_id)
11+
return getattr(
12+
org_settings,
13+
"estimated_location_enabled",
14+
config_app_settings.ESTIMATED_LOCATION_ENABLED,
15+
)

0 commit comments

Comments
 (0)