Skip to content

Commit b559029

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 the conditional handler.assert_called() with an unconditional handler.assert_called_once() and add an IP release assertion. Remove unused variable cert (F841). Related to #1221
1 parent d60ec13 commit b559029

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

openwisp_controller/config/tests/test_vpn.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,6 @@ def test_template_removal_fires_post_delete_signals(self):
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
891890
ip = vpn_client.ip
892891
self.assertIsNotNone(ip)
893892
initial_ip_count = IpAddress.objects.count()
@@ -907,9 +906,7 @@ def test_template_removal_fires_post_delete_signals(self):
907906
# For WireGuard there's no x509 cert to revoke, but the
908907
# post_delete handler should still have run without error.
909908
# The cert object should not exist anymore (CASCADE from VpnClient).
910-
self.assertFalse(
911-
VpnClient.objects.filter(pk=vpn_client.pk).exists()
912-
)
909+
self.assertFalse(VpnClient.objects.filter(pk=vpn_client.pk).exists())
913910

914911
def test_deactivation_fires_post_delete_signals(self):
915912
"""Regression test for #1221: deactivating a device must trigger
@@ -919,15 +916,15 @@ def test_deactivation_fires_post_delete_signals(self):
919916
initial_ip_count = IpAddress.objects.count()
920917

921918
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()
919+
device.deactivate()
920+
handler.assert_called_once()
927921

928922
# Verify cleanup happened
929923
self.assertEqual(device.config.vpnclient_set.count(), 0)
930924

925+
with self.subTest("IP address released"):
926+
self.assertLess(IpAddress.objects.count(), initial_ip_count)
927+
931928

932929
class TestVxlan(BaseTestVpn, TestVxlanWireguardVpnMixin, TestCase):
933930
def test_vxlan_config_creation(self):

0 commit comments

Comments
 (0)