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

Conversation

praptisharma28
Copy link
Member

Fixes #866

-The issue occurs because the code assumes that self.master_subnet and self.master_subnet.subnet are always set, but in this case, they can be None. By adding a check at the beginning of the method, we ensure that a proper validation error is raised when the master subnet is empty or not set.
-The validation error will be caught and displayed in the UI instead of causing a 500 internal server error.

@coveralls
Copy link

coveralls commented Jul 22, 2024

Coverage Status

coverage: 98.225% (+0.001%) from 98.224%
when pulling 21ad1b9 on subnetvalidation
into 2302509 on master.

@nemesifier nemesifier changed the title [bug] Proper validation error is raised #866 [fix] Fixed subnet division rule validation on empty subnet #866 Jul 22, 2024
Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @praptisharma28, please see my comments below.

if not master_subnet:
raise ValidationError(
{'master_subnet': _('Master subnet must have a valid subnet.')}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this code be triggered at all? Why there's not test for it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @nemesifier this will be triggered, I am adding a test for it.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great progress @praptisharma28, see my comments below.

master_subnet = self.master_subnet.subnet
if not master_subnet:
raise ValidationError(
{'master_subnet': _('Master subnet must have a valid subnet.')}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{'master_subnet': _('Master subnet must have a valid subnet.')}
{'master_subnet': _('Master subnet must be a valid subnet.')}

try:
invalid_subnet = Subnet(
name='Invalid Subnet'
) # Create a subnet without setting its 'subnet' attribute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
) # Create a subnet without setting its 'subnet' attribute
) # Instantiate a subnet without setting its 'subnet' attribute

Please move this comment before the instantiation to increase readability.

)
# Ensure default error message is also present
self.assertIn(
'This field cannot be null.', e.message_dict.get('master_subnet', [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are two validation error raised for this field?

If that's the case, we do not need to add another custom error message, it's redundant.
Let's just ensure the default error provided by the framework is there.
Change the code accordingly, preventing the exception from being raised may be enough (add pass preceded by a comment).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, two were raised and caused the test to fail hence I had to consider both. Doing as you say.

)
rule.full_clean()
except ValidationError as e:
# Ensure custom error message is present
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove these comments because the test code is pretty self explanatory

number_of_subnets=2,
number_of_ips=2,
organization=self.org,
# master_subnet is intentionally omitted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a good comment, leave it here

number_of_subnets=2,
number_of_ips=2,
organization=self.org,
master_subnet=invalid_subnet, # Set an invalid master_subnet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
master_subnet=invalid_subnet, # Set an invalid master_subnet
master_subnet=invalid_subnet, # Invalid master_subnet

)
rule.full_clean()
except ValidationError as e:
# Ensure custom error message is present
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@praptisharma28 it seems I had forgot to submit my review, sorry for that. Please let me know if the comments above are clear, if not I will give you further explanations via DM.

@@ -108,7 +108,7 @@ def _validate_master_subnet_consistency(self):
master_subnet = self.master_subnet.subnet
if not master_subnet:
raise ValidationError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't explain myself.
If Django is already raising an error if this field is empty, do not raise another error. Just remove this raise and let Django raise its own error.

e.message_dict.get('master_subnet', []),
)
else:
self.fail('ValidationError not raised')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should not be removed.

nemesifier
nemesifier previously approved these changes Sep 25, 2024
- the most important check is to verify that the
  master subnet is not empty, this check is now
  performed by a dedicated method _validate_master_subnet_validity
- removed test_empty_master_subnet, because it's
  not adding any value since that case is already
  covered by Django
@praptisharma28
Copy link
Member Author

Cool, got what you did here, thanks @nemesifier !

@nemesifier nemesifier merged commit bab9db1 into master Sep 26, 2024
15 checks passed
@nemesifier nemesifier deleted the subnetvalidation branch September 26, 2024 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

[bug] Subnet division rule validation fails with uncaught exception if subnet is left empty
3 participants