Skip to content

Commit ab40470

Browse files
committed
[fix] Fixed RADIUS Usage API response
1 parent 0cd60c5 commit ab40470

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

openwisp_radius/api/serializers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,10 @@ def get_result(self, obj):
304304
group=self.context["group"],
305305
group_check=obj,
306306
)
307-
return counter.consumed()
308-
except MaxQuotaReached:
309-
return int(obj.value)
307+
consumed = counter.consumed()
308+
if consumed > int(obj.value):
309+
consumed = int(obj.value)
310+
return consumed
310311
except (SkipCheck, ValueError, KeyError):
311312
return None
312313

openwisp_radius/tests/test_api/test_api.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,44 @@ def test_user_radius_usage_view(self):
11701170
},
11711171
)
11721172

1173+
data3 = self.acct_post_data
1174+
data3.update(
1175+
dict(
1176+
session_id="40111117",
1177+
unique_id="12234f70",
1178+
input_octets=1000000000,
1179+
output_octets=1000000000,
1180+
username="tester",
1181+
)
1182+
)
1183+
self._create_radius_accounting(**data3)
1184+
1185+
with self.subTest("User consumed more than allowed limit"):
1186+
response = self.client.get(usage_url, HTTP_AUTHORIZATION=authorization)
1187+
self.assertEqual(response.status_code, 200)
1188+
self.assertIn("checks", response.data)
1189+
checks = response.data["checks"]
1190+
self.assertDictEqual(
1191+
dict(checks[0]),
1192+
{
1193+
"attribute": "Max-Daily-Session",
1194+
"op": ":=",
1195+
"value": "10800",
1196+
"result": 783,
1197+
"type": "seconds",
1198+
},
1199+
)
1200+
self.assertDictEqual(
1201+
dict(checks[1]),
1202+
{
1203+
"attribute": "Max-Daily-Session-Traffic",
1204+
"op": ":=",
1205+
"value": "3000000000",
1206+
"result": 3000000000,
1207+
"type": "bytes",
1208+
},
1209+
)
1210+
11731211
with self.subTest("Test user does not have RadiusUserGroup"):
11741212
RadiusUserGroup.objects.all().delete()
11751213
response = self.client.get(usage_url, HTTP_AUTHORIZATION=authorization)

0 commit comments

Comments
 (0)