Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Fixed subnet division rule validation on empty subnet #866 #886

Merged
merged 5 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions openwisp_controller/subnet_division/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def rule_class(self):
def clean(self):
super().clean()
self._validate_label()
self._validate_master_subnet_validity()
self._validate_master_subnet_consistency()
self._validate_ip_address_consistency()
if not self._state.adding:
Expand All @@ -86,6 +87,10 @@ def _validate_label(self):
}
)

def _validate_master_subnet_validity(self):
if not self.master_subnet.subnet:
raise ValidationError({'master_subnet': _('Invalid master subnet.')})

def _validate_existing_fields(self):
db_instance = self._meta.model.objects.get(id=self.id)
# The size field should not be changed
Expand All @@ -104,10 +109,10 @@ def _validate_existing_fields(self):

def _validate_master_subnet_consistency(self):
master_subnet = self.master_subnet.subnet
# Ensure that the size of the generated subnet
# is not greater than size of master subnet
# Try to generate subnets and validate size
try:
next(master_subnet.subnets(new_prefix=self.size))
# if size is not consistent, raise error
except ValueError:
raise ValidationError(
{
Expand All @@ -118,7 +123,6 @@ def _validate_master_subnet_consistency(self):
)
}
)

praptisharma28 marked this conversation as resolved.
Show resolved Hide resolved
# Ensure that master subnet can accommodate
# the required number of generated subnets
available = 2 ** (self.size - master_subnet.prefixlen)
Expand All @@ -135,7 +139,6 @@ def _validate_master_subnet_consistency(self):
)
}
)

praptisharma28 marked this conversation as resolved.
Show resolved Hide resolved
# Validate organization of master subnet
if (
self.master_subnet.organization is not None
Expand Down
21 changes: 21 additions & 0 deletions openwisp_controller/subnet_division/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,27 @@ def test_single_subnet_vpn_device_rule(self):
device_rule.number_of_subnets,
)

def test_invalid_master_subnet(self):
# Instantiate a subnet without setting its 'subnet' attribute
invalid_subnet = Subnet(name='Invalid Subnet')
rule = SubnetDivisionRule(
type='openwisp_controller.subnet_division.rule_types.vpn.'
'VpnSubnetDivisionRuleType',
label='TEST',
size=28,
number_of_subnets=2,
number_of_ips=2,
organization=self.org,
master_subnet=invalid_subnet, # Invalid master_subnet
)
with self.assertRaises(ValidationError) as cm:
rule.full_clean()
e = cm.exception
self.assertIn(
'Invalid master subnet.',
e.message_dict.get('master_subnet', []),
)


class TestOpenVPNSubnetDivisionRule(
SubnetDivisionTestMixin,
Expand Down