2121from ... import settings as app_settings
2222from ...api .freeradius_views import logger as freeradius_api_logger
2323from ...counters .exceptions import MaxQuotaReached , SkipCheck
24+ from ...base .models import sanitize_mac_address
2425from ...signals import radius_accounting_success
2526from ...utils import load_model
2627from ..mixins import ApiTokenMixin , BaseTestCase , BaseTransactionTestCase
@@ -401,6 +402,11 @@ def assertAcctData(self, ra, data):
401402 continue
402403 ra_value = getattr (ra , key )
403404 data_value = data [key ]
405+ # Special handling for called_station_id to compare sanitized MAC addresses
406+ if key == "called_station_id" :
407+ # Both values should be sanitized for comparison
408+ ra_value = sanitize_mac_address (ra_value ) if ra_value else ra_value
409+ data_value = sanitize_mac_address (data_value ) if data_value else data_value
404410 _type = type (ra_value )
405411 if _type != type (data_value ):
406412 data_value = _type (data_value )
@@ -653,7 +659,7 @@ def test_accounting_update_conversion_200(self):
653659 self .assertIsNone (response .data )
654660 self .assertEqual (RadiusAccounting .objects .count (), 1 )
655661 ra .refresh_from_db ()
656- self .assertEqual (ra .called_station_id , "00-00-11-11-22- 22" )
662+ self .assertEqual (ra .called_station_id , "00:00:11:11:22: 22" )
657663
658664 with self .subTest ("should overwrite if different called station ID" ):
659665 ra .called_station_id = "00-00-11-11-22-22"
@@ -670,7 +676,7 @@ def test_accounting_update_conversion_200(self):
670676 self .assertIsNone (response .data )
671677 self .assertEqual (RadiusAccounting .objects .count (), 1 )
672678 ra .refresh_from_db ()
673- self .assertEqual (ra .called_station_id , "00-00-22-22-33- 33" )
679+ self .assertEqual (ra .called_station_id , "00:00:22:22:33: 33" )
674680
675681 @freeze_time (START_DATE )
676682 @capture_any_output ()
@@ -916,7 +922,6 @@ def test_accounting_interim_update_openwisp_closed_session(self, mocked_logger):
916922 response = self .post_json (data )
917923 self .assertEqual (response .status_code , 201 )
918924 self .assertEqual (RadiusAccounting .objects .count (), 1 )
919-
920925 # Create RadiusAccounting object with "org2" organization
921926 org2_data = self .acct_post_data
922927 org2_data ["status_type" ] = "Interim-Update"
@@ -994,7 +999,7 @@ def test_accounting_list_200(self):
994999 self .assertEqual (item ["output_octets" ], data2 ["output_octets" ])
9951000 self .assertEqual (item ["nas_ip_address" ], "172.16.64.91" )
9961001 self .assertEqual (item ["input_octets" ], data2 ["input_octets" ])
997- self .assertEqual (item ["called_station_id" ], "00-27-22-F3-FA-F1:hostname " )
1002+ self .assertEqual (item ["called_station_id" ], "00:27:22:f3:fa:f1 " )
9981003 response = self .client .get (
9991004 f"{ self ._acct_url } ?page_size=1&page=3" ,
10001005 HTTP_AUTHORIZATION = self .auth_header ,
@@ -1031,13 +1036,13 @@ def test_accounting_filter_called_station_id(self):
10311036 data2 .update (dict (called_station_id = "C0-CA-40-FE-E1-9D" , unique_id = "85144d60" ))
10321037 self ._create_radius_accounting (** data2 )
10331038 response = self .client .get (
1034- f"{ self ._acct_url } ?called_station_id=E0-CA-40-EE-D1-0D " ,
1039+ f"{ self ._acct_url } ?called_station_id=e0:ca:40:ee:d1:0d " ,
10351040 HTTP_AUTHORIZATION = self .auth_header ,
10361041 )
10371042 self .assertEqual (len (response .json ()), 1 )
10381043 self .assertEqual (response .status_code , 200 )
10391044 item = response .data [0 ]
1040- self .assertEqual (item ["called_station_id" ], "E0-CA-40-EE-D1-0D " )
1045+ self .assertEqual (item ["called_station_id" ], "e0:ca:40:ee:d1:0d " )
10411046
10421047 def test_accounting_filter_calling_station_id (self ):
10431048 data1 = self .acct_post_data
@@ -1839,7 +1844,7 @@ def test_mac_addr_roaming_accounting_view(self):
18391844 username = "tester" ,
18401845 stop_time = None ,
18411846 nas_ip_address = payload ["nas_ip_address" ],
1842- called_station_id = payload [ "called_station_id" ] ,
1847+ called_station_id = "66:55:44:33:22:11" ,
18431848 ).count (),
18441849 1 ,
18451850 )
@@ -1848,7 +1853,7 @@ def test_mac_addr_roaming_accounting_view(self):
18481853 username = "tester" ,
18491854 stop_time__isnull = False ,
18501855 nas_ip_address = acct_post_data ["nas_ip_address" ],
1851- called_station_id = acct_post_data ["called_station_id" ],
1856+ called_station_id = sanitize_mac_address ( acct_post_data ["called_station_id" ]) ,
18521857 ).count (),
18531858 1 ,
18541859 )
@@ -1928,8 +1933,8 @@ def test_emulate_roaming(self):
19281933 RadiusAccounting .objects .filter (
19291934 username = "tester" ,
19301935 stop_time = None ,
1936+ nas_ip_address = payload ["nas_ip_address" ],
19311937 called_station_id = payload ["called_station_id" ],
1932- calling_station_id = payload ["calling_station_id" ],
19331938 ).count (),
19341939 1 ,
19351940 )
@@ -2184,6 +2189,78 @@ def test_ip_from_radsetting_invalid(self):
21842189 self .assertEqual (response .status_code , 403 )
21852190 self .assertEqual (response .data ["detail" ], test_fail_msg )
21862191
2192+ def test_ip_from_radsetting_valid (self ):
2193+ with mock .patch (self .freeradius_hosts_path , []):
2194+ radsetting = OrganizationRadiusSettings .objects .get (
2195+ organization = self ._get_org ()
2196+ )
2197+ radsetting .freeradius_allowed_hosts = "127.0.0.1"
2198+ radsetting .save ()
2199+ response = self .client .post (reverse ("radius:authorize" ), self .params )
2200+ self .assertEqual (response .status_code , 200 )
2201+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2202+
2203+ def test_ip_from_setting_valid (self ):
2204+ response = self .client .post (reverse ("radius:authorize" ), self .params )
2205+ self .assertEqual (response .status_code , 200 )
2206+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2207+
2208+ @capture_any_output ()
2209+ def test_ip_from_radsetting_not_exist (self ):
2210+ org2 = self ._create_org (** {"name" : "test" , "slug" : "test" })
2211+ user = self ._create_user (username = "org2-tester" , email = "tester@org2.com" )
2212+ self ._create_org_user (** {"organization" : org2 , "user" : user })
2213+ params = self .params .copy ()
2214+ params ["username" ] = "org2-tester"
2215+ response = self .client .post (
2216+ reverse ("radius:user_auth_token" , args = [org2 .slug ]), params
2217+ )
2218+ self .assertEqual (response .status_code , 200 )
2219+ with self .subTest ("FREERADIUS_ALLOWED_HOSTS is 127.0.0.1" ):
2220+ response = self .client .post (reverse ("radius:authorize" ), params )
2221+ self .assertEqual (response .status_code , 200 )
2222+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2223+ with self .subTest ("Empty Settings" ), mock .patch (self .freeradius_hosts_path , []):
2224+ response = self .client .post (reverse ("radius:authorize" ), params )
2225+ self .assertEqual (response .status_code , 403 )
2226+ self .assertEqual (response .data ["detail" ], self .fail_msg )
2227+
2228+ def test_ip_from_radsetting_valid (self ):
2229+ with mock .patch (self .freeradius_hosts_path , []):
2230+ radsetting = OrganizationRadiusSettings .objects .get (
2231+ organization = self ._get_org ()
2232+ )
2233+ radsetting .freeradius_allowed_hosts = "127.0.0.1"
2234+ radsetting .save ()
2235+ response = self .client .post (reverse ("radius:authorize" ), self .params )
2236+ self .assertEqual (response .status_code , 200 )
2237+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2238+
2239+ def test_ip_from_setting_valid (self ):
2240+ response = self .client .post (reverse ("radius:authorize" ), self .params )
2241+ self .assertEqual (response .status_code , 200 )
2242+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2243+
2244+ @capture_any_output ()
2245+ def test_ip_from_radsetting_not_exist (self ):
2246+ org2 = self ._create_org (** {"name" : "test" , "slug" : "test" })
2247+ user = self ._create_user (username = "org2-tester" , email = "tester@org2.com" )
2248+ self ._create_org_user (** {"organization" : org2 , "user" : user })
2249+ params = self .params .copy ()
2250+ params ["username" ] = "org2-tester"
2251+ response = self .client .post (
2252+ reverse ("radius:user_auth_token" , args = [org2 .slug ]), params
2253+ )
2254+ self .assertEqual (response .status_code , 200 )
2255+ with self .subTest ("FREERADIUS_ALLOWED_HOSTS is 127.0.0.1" ):
2256+ response = self .client .post (reverse ("radius:authorize" ), params )
2257+ self .assertEqual (response .status_code , 200 )
2258+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2259+ with self .subTest ("Empty Settings" ), mock .patch (self .freeradius_hosts_path , []):
2260+ response = self .client .post (reverse ("radius:authorize" ), params )
2261+ self .assertEqual (response .status_code , 403 )
2262+ self .assertEqual (response .data ["detail" ], self .fail_msg )
2263+
21872264
21882265class TestTransactionClientIpApi (
21892266 TestClientIpApiMixin , ApiTokenMixin , BaseTransactionTestCase
@@ -2334,7 +2411,3 @@ def test_sms_phone_required(self):
23342411 self .assertIn ("sms_sender" , e .message_dict )
23352412 else :
23362413 self .fail ("ValidationError not raised" )
2337-
2338-
2339- del BaseTestCase
2340- del BaseTransactionTestCase
0 commit comments