@@ -728,9 +728,18 @@ def test_trigger_vpn_server_endpoint_invalid_vpn_id(self):
728
728
729
729
730
730
class TestWireguardTransaction (BaseTestVpn , TestWireguardVpnMixin , TransactionTestCase ):
731
- def test_auto_peer_configuration (self ):
731
+ @mock .patch (
732
+ "openwisp_controller.config.tasks.requests.post" ,
733
+ return_value = HttpResponse (status = 200 ),
734
+ )
735
+ def test_auto_peer_configuration (self , * args ):
732
736
self .assertEqual (IpAddress .objects .count (), 0 )
733
- device , vpn , template = self ._create_wireguard_vpn_template ()
737
+ device , vpn , template = self ._create_wireguard_vpn_template (
738
+ vpn_options = {
739
+ "webhook_endpoint" : "https://example.com:8080/trigger-update" ,
740
+ "auth_token" : "openwisp" ,
741
+ }
742
+ )
734
743
vpnclient_qs = device .config .vpnclient_set
735
744
self .assertEqual (vpnclient_qs .count (), 1 )
736
745
self .assertEqual (IpAddress .objects .count (), 2 )
@@ -770,16 +779,12 @@ def test_auto_peer_configuration(self):
770
779
Vpn ,
771
780
"invalidate_checksum_cache" ,
772
781
return_value = vpn .invalidate_checksum_cache (),
773
- ) as mocked_invalidate_checksum_cache , mock .patch .object (
774
- Vpn ,
775
- "get_cached_configuration" ,
776
- return_value = vpn .get_cached_configuration (),
777
- ) as mocked_cached_configuration :
782
+ ) as mocked_invalidate_checksum_cache :
778
783
device2 .config .templates .add (template )
779
784
# The Vpn configuration cache is invalidated and re-populated
780
785
mocked_invalidate_checksum_cache .assert_called_once ()
781
- mocked_cached_configuration . assert_called_once ()
782
- # cache is invalidated and updated, hence no queries expected
786
+ # cache is invalidated and updated (by trigger_vpn_server_endpoint task),
787
+ # hence no queries expected
783
788
with self .assertNumQueries (0 ):
784
789
vpn_config = vpn .get_config ()["wireguard" ][0 ]
785
790
self .assertEqual (len (vpn_config .get ("peers" , [])), 2 )
@@ -789,17 +794,12 @@ def test_auto_peer_configuration(self):
789
794
Vpn ,
790
795
"invalidate_checksum_cache" ,
791
796
return_value = vpn .invalidate_checksum_cache (),
792
- ) as mocked_invalidate_checksum_cache , mock .patch .object (
793
- Vpn ,
794
- "get_cached_configuration" ,
795
- return_value = vpn .get_cached_configuration (),
796
- ) as mocked_cached_configuration :
797
+ ) as mocked_invalidate_checksum_cache :
797
798
device2 .delete (check_deactivated = False )
798
799
mocked_invalidate_checksum_cache .assert_called_once ()
799
- mocked_cached_configuration .assert_not_called ()
800
- # cache is invalidated but not updated
801
- # hence we expect queries to be generated
802
- with self .assertNumQueries (1 ):
800
+ # cache is invalidated and is updated by the
801
+ # trigger_vpn_server_endpoint task
802
+ with self .assertNumQueries (0 ):
803
803
vpn_config = vpn .get_config ()["wireguard" ][0 ]
804
804
self .assertEqual (len (vpn_config .get ("peers" , [])), 1 )
805
805
@@ -985,9 +985,18 @@ def test_auto_client(self):
985
985
class TestVxlanTransaction (
986
986
BaseTestVpn , TestVxlanWireguardVpnMixin , TransactionTestCase
987
987
):
988
- def test_auto_peer_configuration (self ):
988
+ @mock .patch (
989
+ "openwisp_controller.config.tasks.requests.post" ,
990
+ return_value = HttpResponse (status = 200 ),
991
+ )
992
+ def test_auto_peer_configuration (self , * args ):
989
993
self .assertEqual (IpAddress .objects .count (), 0 )
990
- device , vpn , template = self ._create_vxlan_vpn_template ()
994
+ device , vpn , template = self ._create_vxlan_vpn_template (
995
+ vpn_options = {
996
+ "webhook_endpoint" : "https://example.com:8080/trigger-update" ,
997
+ "auth_token" : "openwisp" ,
998
+ }
999
+ )
991
1000
vpnclient_qs = device .config .vpnclient_set
992
1001
self .assertEqual (vpnclient_qs .count (), 1 )
993
1002
self .assertEqual (IpAddress .objects .count (), 2 )
@@ -1014,17 +1023,17 @@ def test_auto_peer_configuration(self):
1014
1023
}
1015
1024
)
1016
1025
device2 .config .templates .add (template )
1017
- # cache is invalidated and updated, hence no queries expected
1026
+ # cache is invalidated and updated (by trigger_vpn_server_endpoint task),
1027
+ # hence no queries expected
1018
1028
with self .assertNumQueries (0 ):
1019
1029
config = vpn .get_config ()
1020
1030
peers = json .loads (config ["files" ][0 ]["contents" ])
1021
1031
self .assertEqual (len (peers ), 2 )
1022
1032
1023
1033
with self .subTest ("cache updated when a new peer is deleted" ):
1024
1034
device2 .delete (check_deactivated = False )
1025
- # cache is invalidated but not updated
1026
- # hence we expect queries to be generated
1027
- with self .assertNumQueries (2 ):
1035
+ # cache is invalidated and updated (by trigger_vpn_server_endpoint task)
1036
+ with self .assertNumQueries (0 ):
1028
1037
config = vpn .get_config ()
1029
1038
peers = json .loads (config ["files" ][0 ]["contents" ])
1030
1039
self .assertEqual (len (peers ), 1 )
0 commit comments