Skip to content

Commit 59cb0a8

Browse files
authored
[fix] Fixed admin creation of a new RadiusGroup with inlines #604
Fixed a bug which prevented to create a Radius Group object with related Group Checks and/or Group Replies in a single request from the admin interface. Fixes #604
1 parent 1746ff4 commit 59cb0a8

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

openwisp_radius/base/models.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,27 @@ def clean(self):
200200

201201

202202
class AutoGroupnameMixin(object):
203+
def _set_groupname(self):
204+
if self.group:
205+
self.groupname = self.group.name
206+
203207
def clean(self):
204208
"""
205209
automatically sets groupname
206210
"""
211+
if self.group and not self.group.pk:
212+
return
207213
super().clean()
208-
if self.group:
209-
self.groupname = self.group.name
210-
elif not self.groupname:
214+
self._set_groupname()
215+
if not self.group and not self.groupname:
211216
raise ValidationError(
212217
{"groupname": _NOT_BLANK_MESSAGE, "group": _NOT_BLANK_MESSAGE}
213218
)
214219

220+
def save(self, *args, **kwargs):
221+
self._set_groupname()
222+
return super().save(*args, **kwargs)
223+
215224

216225
class AttributeValidationMixin(object):
217226
def _get_validation_queryset_kwargs(self):

openwisp_radius/tests/test_admin.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,3 +1494,57 @@ def test_admin_menu_groups(self):
14941494
with self.subTest("test radius group is registered"):
14951495
html = '<div class="mg-dropdown-label">RADIUS </div>'
14961496
self.assertContains(response, html, html=True)
1497+
1498+
1499+
class TestRadiusGroupAdmin(BaseTestCase):
1500+
def setUp(self):
1501+
self.organization = self._create_org()
1502+
self.admin = self._create_admin()
1503+
self.organization.add_user(self.admin, is_admin=True)
1504+
self.client.force_login(self.admin)
1505+
1506+
def test_add_radiusgroup_with_inline_check_succeeds(self):
1507+
add_url = reverse("admin:openwisp_radius_radiusgroup_add")
1508+
1509+
post_data = {
1510+
# Main RadiusGroup form
1511+
"organization": self.organization.pk,
1512+
"name": "test-group-with-inline",
1513+
"description": "A test group created with an inline check",
1514+
# Inline RadiusGroupCheck formset
1515+
"radiusgroupcheck_set-TOTAL_FORMS": "1",
1516+
"radiusgroupcheck_set-INITIAL_FORMS": "0",
1517+
"radiusgroupcheck_set-0-attribute": "Max-Daily-Session",
1518+
"radiusgroupcheck_set-0-op": ":=",
1519+
"radiusgroupcheck_set-0-value": "3600",
1520+
# Inline RadiusGroupReply formset
1521+
"radiusgroupreply_set-TOTAL_FORMS": "1",
1522+
"radiusgroupreply_set-INITIAL_FORMS": "0",
1523+
"radiusgroupreply_set-0-attribute": "Session-Timeout",
1524+
"radiusgroupreply_set-0-op": "=",
1525+
"radiusgroupreply_set-0-value": "1800",
1526+
}
1527+
1528+
response = self.client.post(add_url, data=post_data, follow=True)
1529+
1530+
self.assertEqual(response.status_code, 200)
1531+
final_group_name = f"{self.organization.slug}-test-group-with-inline"
1532+
1533+
self.assertContains(response, "The group")
1534+
self.assertContains(response, f"{final_group_name}</a>")
1535+
self.assertContains(response, "was added successfully.")
1536+
1537+
self.assertTrue(RadiusGroup.objects.filter(name=final_group_name).exists())
1538+
group = RadiusGroup.objects.get(name=final_group_name)
1539+
1540+
self.assertEqual(group.radiusgroupcheck_set.count(), 1)
1541+
check = group.radiusgroupcheck_set.first()
1542+
self.assertEqual(check.attribute, "Max-Daily-Session")
1543+
self.assertEqual(check.value, "3600")
1544+
self.assertEqual(check.groupname, group.name)
1545+
1546+
self.assertEqual(group.radiusgroupreply_set.count(), 1)
1547+
reply = group.radiusgroupreply_set.first()
1548+
self.assertEqual(reply.attribute, "Session-Timeout")
1549+
self.assertEqual(reply.value, "1800")
1550+
self.assertEqual(reply.groupname, group.name)

0 commit comments

Comments
 (0)