Skip to content

Commit 16320a2

Browse files
committed
[fix] Add regression tests and formatting fixes for VpnClient post_delete openwisp#1221
Added regression tests to verify that VpnClient.post_delete signal fires correctly during VPN template removal and device deactivation. Tests assert that the VPN peer cache is invalidated via _invalidate_peer_cache and that certificates are properly revoked for auto_cert VpnClient instances. Applied Black and isort formatting fixes to config.py and serializers.py where per-instance deletion with select_related('vpn', 'cert', 'ip') replaced bulk QuerySet.delete() within transaction.atomic() blocks to avoid N+1 queries. Closes openwisp#1221
1 parent 0df5530 commit 16320a2

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

openwisp_controller/config/api/serializers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ def _update_config(self, device, config_data):
202202
with transaction.atomic():
203203
vpn_list = config.templates.filter(type="vpn").values_list("vpn")
204204
if vpn_list:
205-
for vpnclient in config.vpnclient_set.select_related(
206-
'vpn', 'cert', 'ip'
207-
).exclude(
208-
vpn__in=vpn_list
209-
).iterator():
205+
for vpnclient in (
206+
config.vpnclient_set.select_related("vpn", "cert", "ip")
207+
.exclude(vpn__in=vpn_list)
208+
.iterator()
209+
):
210210
vpnclient.delete()
211211
config.templates.set(config_templates, clear=True)
212212
config.save()

openwisp_controller/config/base/config.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def manage_vpn_clients(cls, action, instance, pk_set, **kwargs):
340340
# delete all vpn clients and return.
341341
with transaction.atomic():
342342
for vpnclient in instance.vpnclient_set.select_related(
343-
'vpn', 'cert', 'ip'
343+
"vpn", "cert", "ip"
344344
).iterator():
345345
vpnclient.delete()
346346
return
@@ -375,11 +375,13 @@ def manage_vpn_clients(cls, action, instance, pk_set, **kwargs):
375375
# ones, have been fully added. At that point, we can identify and
376376
# delete VpnClient objects not linked to the final template set.
377377
with transaction.atomic():
378-
for vpnclient in instance.vpnclient_set.select_related(
379-
'vpn', 'cert', 'ip'
380-
).exclude(
381-
template_id__in=instance.templates.values_list("id", flat=True)
382-
).iterator():
378+
for vpnclient in (
379+
instance.vpnclient_set.select_related("vpn", "cert", "ip")
380+
.exclude(
381+
template_id__in=instance.templates.values_list("id", flat=True)
382+
)
383+
.iterator()
384+
):
383385
vpnclient.delete()
384386

385387
if action == "post_add":

openwisp_controller/config/tests/test_vpn.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,45 @@ def _assert_vpn_client_cert(cert, vpn_client, cert_ct, vpn_client_ct):
286286
vpnclient.save()
287287
_assert_vpn_client_cert(cert, vpnclient, 1, 0)
288288

289+
def test_vpn_client_post_delete_on_template_removal(self):
290+
"""Regression test for #1221: VpnClient.post_delete must fire
291+
when a VPN template is removed so that peer cache is invalidated
292+
and certificates are properly revoked."""
293+
org = self._get_org()
294+
vpn = self._create_vpn()
295+
t = self._create_template(name="vpn-test", type="vpn", vpn=vpn, auto_cert=True)
296+
c = self._create_config(organization=org)
297+
c.templates.add(t)
298+
vpnclient = c.vpnclient_set.first()
299+
self.assertIsNotNone(vpnclient)
300+
cert_pk = vpnclient.cert.pk
301+
with mock.patch.object(Vpn, "_invalidate_peer_cache") as mock_invalidate:
302+
c.templates.remove(t)
303+
mock_invalidate.assert_called()
304+
self.assertFalse(VpnClient.objects.filter(pk=vpnclient.pk).exists())
305+
# Certificate should be revoked (auto_cert=True)
306+
self.assertTrue(Cert.objects.get(pk=cert_pk).revoked)
307+
308+
def test_vpn_client_post_delete_on_device_deactivation(self):
309+
"""Regression test for #1221: VpnClient.post_delete must fire
310+
when a device is deactivated so that peer cache is invalidated
311+
and certificates are properly revoked."""
312+
org = self._get_org()
313+
vpn = self._create_vpn()
314+
t = self._create_template(name="vpn-test", type="vpn", vpn=vpn, auto_cert=True)
315+
d = self._create_device(organization=org)
316+
c = self._create_config(device=d)
317+
c.templates.add(t)
318+
vpnclient = c.vpnclient_set.first()
319+
self.assertIsNotNone(vpnclient)
320+
cert_pk = vpnclient.cert.pk
321+
with mock.patch.object(Vpn, "_invalidate_peer_cache") as mock_invalidate:
322+
d.deactivate()
323+
mock_invalidate.assert_called()
324+
self.assertFalse(VpnClient.objects.filter(pk=vpnclient.pk).exists())
325+
# Certificate should be revoked (auto_cert=True)
326+
self.assertTrue(Cert.objects.get(pk=cert_pk).revoked)
327+
289328
def test_vpn_client_get_common_name(self):
290329
vpn = self._create_vpn()
291330
d = self._create_device()

0 commit comments

Comments
 (0)