@@ -820,6 +820,87 @@ def test_device_registration_update_hw_info_no_config(self):
820820 self .assertEqual (d .system , params ["system" ])
821821 self .assertEqual (d .model , params ["model" ])
822822
823+ def test_device_registration_update_hostname (self ):
824+ """
825+ Test that hostname is updated when the name in payload
826+ is not the MAC address stored in OpenWISP
827+ """
828+ device = self ._create_device_config (
829+ device_opts = {
830+ "name" : "old-hostname" ,
831+ "mac_address" : TEST_MACADDR ,
832+ "key" : TEST_CONSISTENT_KEY ,
833+ }
834+ )
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+ }
845+ self .assertNotEqual (device .name , params ["name" ])
846+ response = self .client .post (self .register_url , params )
847+ self .assertEqual (response .status_code , 201 )
848+ device .refresh_from_db ()
849+ self .assertEqual (device .name , "new-custom-hostname" )
850+
851+ def test_device_registration_hostname_not_updated_when_mac_address (self ):
852+ """
853+ Test that hostname is not updated when the name in payload
854+ equals the MAC address (agents send MAC address as hostname
855+ when hostname is OpenWrt or if default_hostname is set to *)
856+ """
857+ device = self ._create_device_config (
858+ device_opts = {
859+ "name" : "meaningful-hostname" ,
860+ "key" : TEST_CONSISTENT_KEY ,
861+ }
862+ )
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+ }
873+ response = self .client .post (self .register_url , params )
874+ self .assertEqual (response .status_code , 201 )
875+ device .refresh_from_db ()
876+ self .assertEqual (device .name , "meaningful-hostname" )
877+
878+ def test_device_registration_hostname_comparison_case_insensitive (self ):
879+ """
880+ Test that MAC address comparison is case-insensitive and works
881+ with different formats (colons, dashes, no separators)
882+ """
883+ mac_address = "00:11:22:33:aa:BB"
884+ d = self ._create_device_config (
885+ device_opts = {
886+ "mac_address" : mac_address ,
887+ "name" : "configured-hostname" ,
888+ "key" : TEST_CONSISTENT_KEY ,
889+ }
890+ )
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+ }
898+ response = self .client .post (self .register_url , params )
899+ self .assertEqual (response .status_code , 201 )
900+ d .refresh_from_db ()
901+ # Hostname should not be changed
902+ self .assertEqual (d .name , "configured-hostname" )
903+
823904 def test_device_report_status_running (self ):
824905 """
825906 maintained for backward compatibility
0 commit comments