2020from ... import registration
2121from ... import settings as app_settings
2222from ...counters .exceptions import MaxQuotaReached , SkipCheck
23+ from ...base .models import sanitize_mac_address
2324from ...signals import radius_accounting_success
2425from ...utils import load_model
2526from ...utils import logger as utils_logger
@@ -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
@@ -2001,7 +2006,7 @@ def test_mac_addr_roaming_accounting_view(self):
20012006 username = "tester" ,
20022007 stop_time = None ,
20032008 nas_ip_address = payload ["nas_ip_address" ],
2004- called_station_id = payload [ "called_station_id" ] ,
2009+ called_station_id = "66:55:44:33:22:11" ,
20052010 ).count (),
20062011 1 ,
20072012 )
@@ -2010,7 +2015,7 @@ def test_mac_addr_roaming_accounting_view(self):
20102015 username = "tester" ,
20112016 stop_time__isnull = False ,
20122017 nas_ip_address = acct_post_data ["nas_ip_address" ],
2013- called_station_id = acct_post_data ["called_station_id" ],
2018+ called_station_id = sanitize_mac_address ( acct_post_data ["called_station_id" ]) ,
20142019 ).count (),
20152020 1 ,
20162021 )
@@ -2090,8 +2095,8 @@ def test_emulate_roaming(self):
20902095 RadiusAccounting .objects .filter (
20912096 username = "tester" ,
20922097 stop_time = None ,
2098+ nas_ip_address = payload ["nas_ip_address" ],
20932099 called_station_id = payload ["called_station_id" ],
2094- calling_station_id = payload ["calling_station_id" ],
20952100 ).count (),
20962101 1 ,
20972102 )
@@ -2346,6 +2351,78 @@ def test_ip_from_radsetting_invalid(self):
23462351 self .assertEqual (response .status_code , 403 )
23472352 self .assertEqual (response .data ["detail" ], test_fail_msg )
23482353
2354+ def test_ip_from_radsetting_valid (self ):
2355+ with mock .patch (self .freeradius_hosts_path , []):
2356+ radsetting = OrganizationRadiusSettings .objects .get (
2357+ organization = self ._get_org ()
2358+ )
2359+ radsetting .freeradius_allowed_hosts = "127.0.0.1"
2360+ radsetting .save ()
2361+ response = self .client .post (reverse ("radius:authorize" ), self .params )
2362+ self .assertEqual (response .status_code , 200 )
2363+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2364+
2365+ def test_ip_from_setting_valid (self ):
2366+ response = self .client .post (reverse ("radius:authorize" ), self .params )
2367+ self .assertEqual (response .status_code , 200 )
2368+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2369+
2370+ @capture_any_output ()
2371+ def test_ip_from_radsetting_not_exist (self ):
2372+ org2 = self ._create_org (** {"name" : "test" , "slug" : "test" })
2373+ user = self ._create_user (username = "org2-tester" , email = "tester@org2.com" )
2374+ self ._create_org_user (** {"organization" : org2 , "user" : user })
2375+ params = self .params .copy ()
2376+ params ["username" ] = "org2-tester"
2377+ response = self .client .post (
2378+ reverse ("radius:user_auth_token" , args = [org2 .slug ]), params
2379+ )
2380+ self .assertEqual (response .status_code , 200 )
2381+ with self .subTest ("FREERADIUS_ALLOWED_HOSTS is 127.0.0.1" ):
2382+ response = self .client .post (reverse ("radius:authorize" ), params )
2383+ self .assertEqual (response .status_code , 200 )
2384+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2385+ with self .subTest ("Empty Settings" ), mock .patch (self .freeradius_hosts_path , []):
2386+ response = self .client .post (reverse ("radius:authorize" ), params )
2387+ self .assertEqual (response .status_code , 403 )
2388+ self .assertEqual (response .data ["detail" ], self .fail_msg )
2389+
2390+ def test_ip_from_radsetting_valid (self ):
2391+ with mock .patch (self .freeradius_hosts_path , []):
2392+ radsetting = OrganizationRadiusSettings .objects .get (
2393+ organization = self ._get_org ()
2394+ )
2395+ radsetting .freeradius_allowed_hosts = "127.0.0.1"
2396+ radsetting .save ()
2397+ response = self .client .post (reverse ("radius:authorize" ), self .params )
2398+ self .assertEqual (response .status_code , 200 )
2399+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2400+
2401+ def test_ip_from_setting_valid (self ):
2402+ response = self .client .post (reverse ("radius:authorize" ), self .params )
2403+ self .assertEqual (response .status_code , 200 )
2404+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2405+
2406+ @capture_any_output ()
2407+ def test_ip_from_radsetting_not_exist (self ):
2408+ org2 = self ._create_org (** {"name" : "test" , "slug" : "test" })
2409+ user = self ._create_user (username = "org2-tester" , email = "tester@org2.com" )
2410+ self ._create_org_user (** {"organization" : org2 , "user" : user })
2411+ params = self .params .copy ()
2412+ params ["username" ] = "org2-tester"
2413+ response = self .client .post (
2414+ reverse ("radius:user_auth_token" , args = [org2 .slug ]), params
2415+ )
2416+ self .assertEqual (response .status_code , 200 )
2417+ with self .subTest ("FREERADIUS_ALLOWED_HOSTS is 127.0.0.1" ):
2418+ response = self .client .post (reverse ("radius:authorize" ), params )
2419+ self .assertEqual (response .status_code , 200 )
2420+ self .assertEqual (response .data , _AUTH_TYPE_ACCEPT_RESPONSE )
2421+ with self .subTest ("Empty Settings" ), mock .patch (self .freeradius_hosts_path , []):
2422+ response = self .client .post (reverse ("radius:authorize" ), params )
2423+ self .assertEqual (response .status_code , 403 )
2424+ self .assertEqual (response .data ["detail" ], self .fail_msg )
2425+
23492426
23502427class TestTransactionClientIpApi (
23512428 TestClientIpApiMixin , ApiTokenMixin , BaseTransactionTestCase
@@ -2496,7 +2573,3 @@ def test_sms_phone_required(self):
24962573 self .assertIn ("sms_sender" , e .message_dict )
24972574 else :
24982575 self .fail ("ValidationError not raised" )
2499-
2500-
2501- del BaseTestCase
2502- del BaseTransactionTestCase
0 commit comments