Skip to content

Commit b96fec8

Browse files
committed
[tests] Fix deactivation test to exercise correct code path #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 remove the redundant "certificate revoked for OpenVPN-style auto_cert" subTest that duplicated the VpnClient deletion assertion. Related to #1221
1 parent d60ec13 commit b96fec8

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

openwisp_controller/config/tests/test_vpn.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -883,51 +883,42 @@ 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())
913903

914904
def test_deactivation_fires_post_delete_signals(self):
915905
"""Regression test for #1221: deactivating a device must trigger
916906
VpnClient.post_delete for each client (not bulk QuerySet.delete)."""
917907
device, vpn, template = self._create_wireguard_vpn_template()
918-
self.assertEqual(device.config.vpnclient_set.count(), 1)
919-
initial_ip_count = IpAddress.objects.count()
908+
vpn_client = device.config.vpnclient_set.first()
909+
self.assertIsNotNone(vpn_client)
910+
ip_pk = vpn_client.ip.pk
920911

921912
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()
913+
device.deactivate()
914+
handler.assert_called_once()
927915

928916
# Verify cleanup happened
929917
self.assertEqual(device.config.vpnclient_set.count(), 0)
930918

919+
with self.subTest("IP address released"):
920+
self.assertFalse(IpAddress.objects.filter(pk=ip_pk).exists())
921+
931922

932923
class TestVxlan(BaseTestVpn, TestVxlanWireguardVpnMixin, TestCase):
933924
def test_vxlan_config_creation(self):

0 commit comments

Comments
 (0)