Skip to content

Commit

Permalink
[change] Adds check for valid prefix in batch creation (Closes openwi…
Browse files Browse the repository at this point in the history
…sp#365)

* [change] Adds check to prevent invalid username in batch creation openwisp#365
* [change] Adds check for valid prefix in batch creation openwisp#365

Closes openwisp#365
  • Loading branch information
bsande6 authored Feb 8, 2022
1 parent 81cefaf commit e23afdc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions openwisp_radius/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import logging
import os
import string
from base64 import encodebytes
from datetime import timedelta
from hashlib import md5, sha1
Expand Down Expand Up @@ -893,6 +894,19 @@ def clean(self):
raise ValidationError(
{'prefix': _('This field cannot be blank.')}, code='invalid'
)
if self.strategy == 'prefix' and self.prefix:
valid_chars = string.ascii_letters + string.digits + "@.+-_"
for char in self.prefix:
if char not in valid_chars:
raise ValidationError(
{
'prefix': _(
'This value may contain only \
letters, numbers, and @/./+/-/_ characters.'
)
},
code='invalid',
)
if (
self.strategy == 'csv'
and self.prefix
Expand Down
29 changes: 29 additions & 0 deletions openwisp_radius/tests/test_batch_add_users.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.core.exceptions import ValidationError

from ..utils import load_model
from . import FileMixin
from .mixins import BaseTestCase
Expand Down Expand Up @@ -79,3 +81,30 @@ def test_hashed_password(self):
self.assertEqual(batch.users.all().count(), 1)
user = batch.users.first()
self.assertEqual(hashed_password, user.password)


class TestPrefixUpload(FileMixin, BaseTestCase):
def test_invalid_username(self):
self.assertRaises(
ValidationError,
self._create_radius_batch,
name='test',
strategy='prefix',
prefix="Test#1",
)

def test_valid_username(self):
batch = self._create_radius_batch(
name='test', strategy='prefix', prefix='Test1'
)
batch.prefix_add('test-prefix16', 5)
self.assertEqual(RadiusBatch.objects.all().count(), 1)
self.assertEqual(batch.users.all().count(), 5)

def test_valid_username_special_char(self):
batch = self._create_radius_batch(
name='test', strategy='prefix', prefix='Test_@+-.'
)
batch.prefix_add('test-prefix16', 5)
self.assertEqual(RadiusBatch.objects.all().count(), 1)
self.assertEqual(batch.users.all().count(), 5)

0 comments on commit e23afdc

Please sign in to comment.