Skip to content

Commit

Permalink
[fix] Added validation for subnet division rule label openwisp#546
Browse files Browse the repository at this point in the history
SubnetDivisionRule label only allows aplhanumeric characters and
underscores

Closes openwisp#546
  • Loading branch information
pandafy committed Jan 19, 2022
1 parent ead58e1 commit 58881a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
11 changes: 11 additions & 0 deletions openwisp_controller/subnet_division/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,22 @@ def rule_class(self):

def clean(self):
super().clean()
self._validate_label()
self._validate_master_subnet_consistency()
self._validate_ip_address_consistency()
if not self._state.adding:
self._validate_existing_fields()

def _validate_label(self):
if not self.label.isidentifier():
raise ValidationError(
{
'label': _(
'Only alphanumeric characters and underscores are allowed.'
)
}
)

def _validate_existing_fields(self):
db_instance = self._meta.model.objects.get(id=self.id)
# The size field should not be changed
Expand Down
17 changes: 16 additions & 1 deletion openwisp_controller/subnet_division/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def ip_query(self):

def test_field_validations(self):
default_options = {
'label': 'OW',
'label': 'OW_1',
'size': 28,
'master_subnet': self.master_subnet,
'number_of_ips': 2,
Expand All @@ -61,6 +61,21 @@ def test_field_validations(self):
rule.full_clean()
self.assertEqual(str(rule), options['label'])

with self.subTest('Test label'):
options = default_options.copy()
options['label'] = 'OW_10.0.0.0/16'
rule = SubnetDivisionRule(**options)
with self.assertRaises(ValidationError) as error:
rule.full_clean()
self.assertDictEqual(
error.exception.message_dict,
{
'label': [
'Only alphanumeric characters and underscores are allowed.'
]
},
)

with self.subTest('Test rule size exceeds size of master_subnet'):
options = default_options.copy()
options['size'] = 8
Expand Down

0 comments on commit 58881a8

Please sign in to comment.