Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[change] Adds check for valid prefix in batch creation #365 #371

Merged
merged 2 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)