Skip to content

Commit e6c24f5

Browse files
committed
[tests] Strengthen regression tests and add API coverage for openwisp#1221
- Upgrade assert_called() to assert_called_once() in both VpnClient post_delete tests for stricter signal-call verification - Add API regression test: PATCH device removing a VPN template via the serializer path must trigger VpnClient.post_delete, invalidate the peer cache, and revoke the auto-created certificate Co-Authored-By: mn-ram <mn-ram@users.noreply.github.com>
1 parent 5a17293 commit e6c24f5

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

openwisp_controller/config/tests/test_api.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Template = load_model("config", "Template")
2727
Vpn = load_model("config", "Vpn")
2828
VpnClient = load_model("config", "VpnClient")
29+
Cert = load_model("django_x509", "Cert")
2930
Device = load_model("config", "Device")
3031
Config = load_model("config", "Config")
3132
DeviceGroup = load_model("config", "DeviceGroup")
@@ -518,6 +519,32 @@ def test_device_patch_with_templates_of_different_org(self):
518519
f" do not match the organization of this configuration: {t3}",
519520
)
520521

522+
def test_device_patch_vpn_template_removal_triggers_post_delete(self):
523+
"""Regression test for #1221: removing a VPN template via PATCH API
524+
must trigger VpnClient.post_delete so peer cache is invalidated
525+
and the certificate is revoked."""
526+
org = self._get_org()
527+
vpn = self._create_vpn(organization=org)
528+
t_vpn = self._create_template(
529+
name="vpn-test", type="vpn", vpn=vpn, auto_cert=True, organization=org
530+
)
531+
device = self._create_device(organization=org)
532+
config = self._create_config(device=device)
533+
config.templates.add(t_vpn)
534+
vpnclient = config.vpnclient_set.first()
535+
self.assertIsNotNone(vpnclient)
536+
cert_pk = vpnclient.cert.pk
537+
538+
path = reverse("config_api:device_detail", args=[device.pk])
539+
# PATCH with empty templates list to remove the VPN template
540+
data = {"config": {"templates": []}}
541+
with patch.object(Vpn, "_invalidate_peer_cache") as mock_invalidate:
542+
response = self.client.patch(path, data, content_type="application/json")
543+
mock_invalidate.assert_called_once()
544+
self.assertEqual(response.status_code, 200)
545+
self.assertFalse(VpnClient.objects.filter(pk=vpnclient.pk).exists())
546+
self.assertTrue(Cert.objects.get(pk=cert_pk).revoked)
547+
521548
def test_device_change_organization_required_templates(self):
522549
org1 = self._create_org(name="org1")
523550
org2 = self._create_org(name="org2")

openwisp_controller/config/tests/test_vpn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def test_vpn_client_post_delete_on_template_removal(self):
300300
cert_pk = vpnclient.cert.pk
301301
with mock.patch.object(Vpn, "_invalidate_peer_cache") as mock_invalidate:
302302
c.templates.remove(t)
303-
mock_invalidate.assert_called()
303+
mock_invalidate.assert_called_once()
304304
self.assertFalse(VpnClient.objects.filter(pk=vpnclient.pk).exists())
305305
# Certificate should be revoked (auto_cert=True)
306306
self.assertTrue(Cert.objects.get(pk=cert_pk).revoked)
@@ -320,7 +320,7 @@ def test_vpn_client_post_delete_on_device_deactivation(self):
320320
cert_pk = vpnclient.cert.pk
321321
with mock.patch.object(Vpn, "_invalidate_peer_cache") as mock_invalidate:
322322
d.deactivate()
323-
mock_invalidate.assert_called()
323+
mock_invalidate.assert_called_once()
324324
self.assertFalse(VpnClient.objects.filter(pk=vpnclient.pk).exists())
325325
# Certificate should be revoked (auto_cert=True)
326326
self.assertTrue(Cert.objects.get(pk=cert_pk).revoked)

0 commit comments

Comments
 (0)