Skip to content

Commit

Permalink
[fix] Tests simplified #866
Browse files Browse the repository at this point in the history
Closes #866
  • Loading branch information
praptisharma28 committed Sep 9, 2024
1 parent 00125c3 commit 21ad1b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 46 deletions.
2 changes: 1 addition & 1 deletion openwisp_controller/subnet_division/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _validate_master_subnet_consistency(self):
master_subnet = self.master_subnet.subnet
if not master_subnet:
raise ValidationError(
{'master_subnet': _('Master subnet must have a valid subnet.')}
{'master_subnet': _('Master subnet must be a valid subnet.')}
)
except AttributeError:
raise ValidationError(
Expand Down
70 changes: 25 additions & 45 deletions openwisp_controller/subnet_division/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,55 +957,35 @@ def test_single_subnet_vpn_device_rule(self):
)

def test_empty_master_subnet(self):
try:
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 is intentionally omitted
)
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 is intentionally omitted
)
with self.assertRaises(ValidationError) as context:
rule.full_clean()
except ValidationError as e:
# Ensure custom error message is present
self.assertIn(
'Master subnet is required and cannot be empty.',
e.message_dict.get('master_subnet', []),
)
# Ensure default error message is also present
self.assertIn(
'This field cannot be null.', e.message_dict.get('master_subnet', [])
)
else:
self.fail('ValidationError not raised')
self.assertIn('This field cannot be null.', str(context.exception))

def test_invalid_master_subnet(self):
try:
invalid_subnet = Subnet(
name='Invalid Subnet'
) # Create a subnet without setting its 'subnet' attribute
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, # Set an invalid master_subnet
)
# 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):
rule.full_clean()
except ValidationError as e:
# Ensure custom error message is present
self.assertIn(
'Master subnet must have a valid subnet.',
e.message_dict.get('master_subnet', []),
)
else:
self.fail('ValidationError not raised')


class TestOpenVPNSubnetDivisionRule(
Expand Down

0 comments on commit 21ad1b9

Please sign in to comment.