55from django .core .exceptions import ValidationError
66from django .http .response import Http404
77from django .test import TestCase
8- from django .urls import reverse
8+ from django .urls import reverse , reverse_lazy
99from swapper import load_model
1010
1111from openwisp_utils .tests import capture_any_output , catch_signal
3838Organization = load_model ("openwisp_users" , "Organization" )
3939
4040
41- class TestController (CreateConfigTemplateMixin , TestVpnX509Mixin , TestCase ):
42- """
43- tests for config.controller
44- """
45-
46- def setUp (self ):
47- self .register_url = reverse ("controller:device_register" )
41+ class TestControllerMixin :
42+ register_url = reverse_lazy ("controller:device_register" )
4843
4944 def _create_org (self , shared_secret = TEST_ORG_SHARED_SECRET , ** kwargs ):
5045 org = super ()._create_org (** kwargs )
@@ -53,6 +48,27 @@ def _create_org(self, shared_secret=TEST_ORG_SHARED_SECRET, **kwargs):
5348 )
5449 return org
5550
51+ def _get_reregistration_payload (self , device , ** kwargs ):
52+ data = {
53+ "secret" : str (device .organization .config_settings .shared_secret ),
54+ "key" : TEST_CONSISTENT_KEY ,
55+ "mac_address" : device .mac_address ,
56+ "backend" : "netjsonconfig.OpenWrt" ,
57+ "model" : "TP-Link TL-WDR4300 v2" ,
58+ "os" : "OpenWrt 18.06-SNAPSHOT r7312-e60be11330" ,
59+ "system" : "Atheros AR9344 rev 3" ,
60+ }
61+ data .update (** kwargs )
62+ return data
63+
64+
65+ class TestController (
66+ TestControllerMixin , CreateConfigTemplateMixin , TestVpnX509Mixin , TestCase
67+ ):
68+ """
69+ tests for config.controller
70+ """
71+
5672 def _check_header (self , response ):
5773 self .assertEqual (response ["X-Openwisp-Controller" ], "true" )
5874
@@ -820,7 +836,8 @@ def test_device_registration_update_hw_info_no_config(self):
820836 self .assertEqual (d .system , params ["system" ])
821837 self .assertEqual (d .model , params ["model" ])
822838
823- def test_device_registration_update_hostname (self ):
839+ @patch .object (Device , 'skip_push_update_on_save' )
840+ def test_device_registration_update_hostname (self , mocked_method ):
824841 """
825842 Test that hostname is updated when the name in payload
826843 is not the MAC address stored in OpenWISP
@@ -832,21 +849,16 @@ def test_device_registration_update_hostname(self):
832849 "key" : TEST_CONSISTENT_KEY ,
833850 }
834851 )
835- params = {
836- "secret" : TEST_ORG_SHARED_SECRET ,
837- "name" : "new-custom-hostname" ,
838- "mac_address" : TEST_MACADDR ,
839- "key" : TEST_CONSISTENT_KEY ,
840- "backend" : "netjsonconfig.OpenWrt" ,
841- "model" : "TP-Link TL-WDR4300 v2" ,
842- "os" : "OpenWrt 18.06-SNAPSHOT r7312-e60be11330" ,
843- "system" : "Atheros AR9344 rev 3" ,
844- }
852+ params = self ._get_reregistration_payload (
853+ device = device ,
854+ name = "new-custom-hostname" ,
855+ )
845856 self .assertNotEqual (device .name , params ["name" ])
846857 response = self .client .post (self .register_url , params )
847858 self .assertEqual (response .status_code , 201 )
848859 device .refresh_from_db ()
849860 self .assertEqual (device .name , "new-custom-hostname" )
861+ mocked_method .assert_called_once ()
850862
851863 def test_device_registration_hostname_not_updated_when_mac_address (self ):
852864 """
@@ -860,16 +872,10 @@ def test_device_registration_hostname_not_updated_when_mac_address(self):
860872 "key" : TEST_CONSISTENT_KEY ,
861873 }
862874 )
863- params = {
864- "secret" : TEST_ORG_SHARED_SECRET ,
865- "name" : TEST_MACADDR ,
866- "mac_address" : TEST_MACADDR ,
867- "key" : TEST_CONSISTENT_KEY ,
868- "backend" : "netjsonconfig.OpenWrt" ,
869- "model" : "TP-Link TL-WDR4300 v2" ,
870- "os" : "OpenWrt 18.06-SNAPSHOT r7312-e60be11330" ,
871- "system" : "Atheros AR9344 rev 3" ,
872- }
875+ params = self ._get_reregistration_payload (
876+ device = device ,
877+ name = TEST_MACADDR ,
878+ )
873879 response = self .client .post (self .register_url , params )
874880 self .assertEqual (response .status_code , 201 )
875881 device .refresh_from_db ()
@@ -881,25 +887,22 @@ def test_device_registration_hostname_comparison_case_insensitive(self):
881887 with different formats (colons, dashes, no separators)
882888 """
883889 mac_address = "00:11:22:33:aa:BB"
884- d = self ._create_device_config (
890+ device = self ._create_device_config (
885891 device_opts = {
886892 "mac_address" : mac_address ,
887893 "name" : "configured-hostname" ,
888894 "key" : TEST_CONSISTENT_KEY ,
889895 }
890896 )
891- params = {
892- "secret" : TEST_ORG_SHARED_SECRET ,
893- "name" : "00-11-22-33-AA-bb" ,
894- "mac_address" : mac_address ,
895- "key" : TEST_CONSISTENT_KEY ,
896- "backend" : "netjsonconfig.OpenWrt" ,
897- }
897+ params = self ._get_reregistration_payload (
898+ device = device ,
899+ name = "00-11-22-33-aa-bb" ,
900+ )
898901 response = self .client .post (self .register_url , params )
899902 self .assertEqual (response .status_code , 201 )
900- d .refresh_from_db ()
903+ device .refresh_from_db ()
901904 # Hostname should not be changed
902- self .assertEqual (d .name , "configured-hostname" )
905+ self .assertEqual (device .name , "configured-hostname" )
903906
904907 def test_device_report_status_running (self ):
905908 """
0 commit comments