Skip to content

Commit 344fa8b

Browse files
committed
[tests] Fix deactivation test to exercise correct code path openwisp#1221
test_deactivation_fires_post_delete_signals was calling device.config.templates.clear() directly without first setting the config status to deactivating. This meant the per-instance delete path guarded by is_deactivating_or_deactivated() in manage_vpn_clients was never exercised, defeating the purpose of the regression test. Replace templates.clear() with device.deactivate() which sets the status to deactivating before clearing templates. Also replace global IP count checks with object-level assertions, remove the unused cert variable (F841), and replace the misleading "certificate revoked for OpenVPN-style auto_cert" subTest with a WireGuard-appropriate post_delete handler check. Related to openwisp#1221
1 parent d60ec13 commit 344fa8b

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

openwisp_controller/config/tests/test_vpn.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -883,51 +883,49 @@ def test_vpn_peers_changed(self):
883883
def test_template_removal_fires_post_delete_signals(self):
884884
"""Regression test for #1221: removing a VPN template must trigger
885885
VpnClient.post_delete so that the peer cache is invalidated,
886-
certificates are revoked, and IP addresses are released."""
886+
and IP addresses are released."""
887887
device, vpn, template = self._create_wireguard_vpn_template()
888888
vpn_client = device.config.vpnclient_set.first()
889889
self.assertIsNotNone(vpn_client)
890-
cert = vpn_client.cert
891-
ip = vpn_client.ip
892-
self.assertIsNotNone(ip)
893-
initial_ip_count = IpAddress.objects.count()
890+
vpn_client_pk = vpn_client.pk
891+
ip_pk = vpn_client.ip.pk
894892

895893
with self.subTest("peer cache invalidated on template removal"):
896894
with catch_signal(vpn_peers_changed) as handler:
897895
device.config.templates.remove(template)
898896
handler.assert_called_once()
899897

900898
with self.subTest("VpnClient deleted"):
901-
self.assertEqual(device.config.vpnclient_set.count(), 0)
899+
self.assertFalse(VpnClient.objects.filter(pk=vpn_client_pk).exists())
902900

903901
with self.subTest("IP address released"):
904-
self.assertLess(IpAddress.objects.count(), initial_ip_count)
905-
906-
with self.subTest("certificate revoked for OpenVPN-style auto_cert"):
907-
# For WireGuard there's no x509 cert to revoke, but the
908-
# post_delete handler should still have run without error.
909-
# The cert object should not exist anymore (CASCADE from VpnClient).
910-
self.assertFalse(
911-
VpnClient.objects.filter(pk=vpn_client.pk).exists()
912-
)
902+
self.assertFalse(IpAddress.objects.filter(pk=ip_pk).exists())
903+
904+
with self.subTest("post_delete handler ran without error"):
905+
# WireGuard does not use x509 certs, so there is nothing to
906+
# revoke. Verify the post_delete handler completed by checking
907+
# that the peer cache was invalidated (signal was already
908+
# asserted above) and no VpnClient remains.
909+
self.assertEqual(device.config.vpnclient_set.count(), 0)
913910

914911
def test_deactivation_fires_post_delete_signals(self):
915912
"""Regression test for #1221: deactivating a device must trigger
916913
VpnClient.post_delete for each client (not bulk QuerySet.delete)."""
917914
device, vpn, template = self._create_wireguard_vpn_template()
918-
self.assertEqual(device.config.vpnclient_set.count(), 1)
919-
initial_ip_count = IpAddress.objects.count()
915+
vpn_client = device.config.vpnclient_set.first()
916+
self.assertIsNotNone(vpn_client)
917+
ip_pk = vpn_client.ip.pk
920918

921919
with catch_signal(vpn_peers_changed) as handler:
922-
device.config.templates.clear()
923-
# post_clear with deactivating state deletes vpn clients
924-
# The signal should fire because per-instance delete is used
925-
if device.config.vpnclient_set.count() == 0:
926-
handler.assert_called()
920+
device.deactivate()
921+
handler.assert_called_once()
927922

928923
# Verify cleanup happened
929924
self.assertEqual(device.config.vpnclient_set.count(), 0)
930925

926+
with self.subTest("IP address released"):
927+
self.assertFalse(IpAddress.objects.filter(pk=ip_pk).exists())
928+
931929

932930
class TestVxlan(BaseTestVpn, TestVxlanWireguardVpnMixin, TestCase):
933931
def test_vxlan_config_creation(self):

0 commit comments

Comments
 (0)