Skip to content

Commit

Permalink
[fix] Fixed VPN server backend validation logic
Browse files Browse the repository at this point in the history
Bug:
The validation logic prevented changing of VPN server's backend
if any VpnClients were present for the it. But the logic didn't
compare backend value of the object with what has been store in
the database.

Fix:
The validation logic compares backend values before raising
ValidationError.
  • Loading branch information
pandafy committed Feb 1, 2022
1 parent 16bf52d commit 0bfda7a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
7 changes: 6 additions & 1 deletion openwisp_controller/config/base/vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ def clean(self, *args, **kwargs):
def _validate_backend(self):
if self._state.adding:
return
if self.vpnclient_set.exists():
if (
'backend' not in self.get_deferred_fields()
and self._meta.model.objects.only('backend').get(id=self.id).backend
!= self.backend
and self.vpnclient_set.exists()
):
raise ValidationError(
{
'backend': _(
Expand Down
34 changes: 21 additions & 13 deletions openwisp_controller/config/tests/test_vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,19 +593,27 @@ def test_change_vpn_backend_with_vpnclient(self):
config.templates.add(template)
self.assertEqual(VpnClient.objects.count(), 1)

with self.assertRaises(ValidationError) as context_manager:
vpn.backend = self._BACKENDS['wireguard']
vpn.subnet = subnet
vpn.full_clean()
vpn.save()
expected_error_dict = {
'backend': [
'Backend cannot be changed because the VPN is currently in use.'
]
}
self.assertDictEqual(
context_manager.exception.message_dict, expected_error_dict
)
with self.subTest(
'Test validation error is not raised when backend is unchanged'
):
try:
vpn.full_clean()
except ValidationError as error:
self.fail(f'Unexpected ValidationError: {error}')

with self.subTest('Test validation error is raised when backend is changed'):
with self.assertRaises(ValidationError) as context_manager:
vpn.backend = self._BACKENDS['wireguard']
vpn.subnet = subnet
vpn.full_clean()
expected_error_dict = {
'backend': [
'Backend cannot be changed because the VPN is currently in use.'
]
}
self.assertDictEqual(
context_manager.exception.message_dict, expected_error_dict
)


class TestWireguardTransaction(BaseTestVpn, TestWireguardVpnMixin, TransactionTestCase):
Expand Down

0 comments on commit 0bfda7a

Please sign in to comment.