Skip to content

Commit 7b202aa

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 replacing a VPN template with a generic one must trigger VpnClient.post_delete, invalidate the peer cache, and revoke the auto-created certificate (covers the serializers.py code path) Related to openwisp#1221 Co-Authored-By: mn-ram <mn-ram@users.noreply.github.com>
1 parent 5fbb813 commit 7b202aa

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

openwisp_controller/config/tests/test_api.py

Lines changed: 28 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,33 @@ 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: replacing a VPN template with a generic
524+
one via PATCH API must trigger VpnClient.post_delete so peer cache is
525+
invalidated 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+
t_generic = self._create_template(name="generic-test", organization=org)
532+
device = self._create_device(organization=org)
533+
config = self._create_config(device=device)
534+
config.templates.add(t_vpn)
535+
vpnclient = config.vpnclient_set.first()
536+
self.assertIsNotNone(vpnclient)
537+
cert_pk = vpnclient.cert.pk
538+
539+
path = reverse("config_api:device_detail", args=[device.pk])
540+
# PATCH replacing VPN template with a generic template
541+
data = {"config": {"templates": [str(t_generic.pk)]}}
542+
with patch.object(Vpn, "_invalidate_peer_cache") as mock_invalidate:
543+
response = self.client.patch(path, data, content_type="application/json")
544+
mock_invalidate.assert_called_once()
545+
self.assertEqual(response.status_code, 200)
546+
self.assertFalse(VpnClient.objects.filter(pk=vpnclient.pk).exists())
547+
self.assertTrue(Cert.objects.get(pk=cert_pk).revoked)
548+
521549
def test_device_change_organization_required_templates(self):
522550
org1 = self._create_org(name="org1")
523551
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)