Skip to content
This repository has been archived by the owner on Sep 10, 2020. It is now read-only.

Commit

Permalink
Merge 364d1f2 into 3674216
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivekrajput20 committed Jul 11, 2019
2 parents 3674216 + 364d1f2 commit 7b7daa2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
1 change: 1 addition & 0 deletions django_freeradius/base/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def clean(self):
number_of_users = data.get('number_of_users')
if strategy == 'prefix' and not number_of_users:
self.add_error('number_of_users', 'This field is required')
super().clean()
return data

def __init__(self, *args, **kwargs):
Expand Down
44 changes: 24 additions & 20 deletions django_freeradius/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,30 +740,11 @@ def clean(self):
super(AbstractRadiusBatch, self).clean()

def add(self, reader, password_length=BATCH_DEFAULT_PASSWORD_LENGTH):
User = get_user_model()
users_list = []
generated_passwords = []
for row in reader:
if len(row) == 5:
username, password, email, first_name, last_name = row
if not username and email:
username = email.split('@')[0]
username = find_available_username(username, users_list)
user = User(username=username,
email=email,
first_name=first_name,
last_name=last_name)
cleartext_delimiter = 'cleartext$'
if not password:
password = get_random_string(length=password_length)
user.set_password(password)
generated_passwords.append([username, password, email])
elif password and password.startswith(cleartext_delimiter):
password = password[len(cleartext_delimiter):]
user.set_password(password)
else:
user.password = password
user.full_clean()
user = self.get_or_create_user(row, users_list, generated_passwords, password_length)
users_list.append(user)
for user in users_list:
self.save_user(user)
Expand Down Expand Up @@ -796,6 +777,29 @@ def prefix_add(self, prefix, n, password_length=BATCH_DEFAULT_PASSWORD_LENGTH):
self.full_clean()
self.save()

def get_or_create_user(self, row, users_list, generated_passwords, password_length):
User = get_user_model()
username, password, email, first_name, last_name = row
if not username and email:
username = email.split('@')[0]
username = find_available_username(username, users_list)
user = User(username=username,
email=email,
first_name=first_name,
last_name=last_name)
cleartext_delimiter = 'cleartext$'
if not password:
password = get_random_string(length=password_length)
user.set_password(password)
generated_passwords.append([username, password, email])
elif password and password.startswith(cleartext_delimiter):
password = password[len(cleartext_delimiter):]
user.set_password(password)
else:
user.password = password
user.full_clean()
return user

def save_user(self, user):
user.save()
self.users.add(user)
Expand Down
2 changes: 1 addition & 1 deletion django_freeradius/tests/base/test_batch_add_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_generate_username_when_repeat(self):
hashed_password = 'pbkdf2_sha256$100000$x3DUBnOFwraV$PU2dZZq1FcuBjagxVLPhhFvpicLn18fFCN5xiLsxATc='
cleartext_password = 'cleartext$password'
reader = [['rohith', cleartext_password, 'rohith@openwisp.com', 'Rohith', 'ASRK'],
['rohith', hashed_password, 'rohith@openwisp.com', '', '']]
['rohith', hashed_password, 'rohith@openwisp.org', '', '']]
batch = self._create_radius_batch(name='test',
strategy='csv',
csvfile=self._get_csvfile(reader))
Expand Down
11 changes: 6 additions & 5 deletions django_freeradius/tests/base/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ def test_batch_add_users_command(self):
radiusbatch = self.radius_batch_model.objects.first()
self.assertEqual(get_user_model().objects.all().count(), 3)
self.assertEqual(radiusbatch.expiration_date.strftime('%d-%m-%y'), '28-01-18')
path = self._get_path('static/test_batch_new.csv')
options = dict(file=path, name='test1')
self._call_command('batch_add_users', **options)
self.assertEqual(self.radius_batch_model.objects.all().count(), 2)
self.assertEqual(get_user_model().objects.all().count(), 6)
invalid_csv_path = self._get_path('static/test_batch_invalid.csv')
with self.assertRaises(CommandError):
options = dict(file='doesnotexist.csv', name='test2')
options = dict(file='doesnotexist.csv', name='test3')
self._call_command('batch_add_users', **options)
with self.assertRaises(SystemExit):
options = dict(file=invalid_csv_path, name='test3')
options = dict(file=invalid_csv_path, name='test4')
self._call_command('batch_add_users', **options)

def test_deactivate_expired_users_command(self):
Expand All @@ -71,9 +72,9 @@ def test_delete_old_users_command(self):
path = self._get_path('static/test_batch.csv')
options = dict(file=path, expiration='28-01-1970', name='test')
self._call_command('batch_add_users', **options)
expiration_date = now() - timedelta(days=30 * 15)
options['expiration'] = expiration_date.strftime('%d-%m-%Y')
options['name'] = 'test1'
expiration_date = (now() - timedelta(days=30 * 15)).strftime('%d-%m-%Y')
path = self._get_path('static/test_batch_new.csv')
options = dict(file=path, expiration=expiration_date, name='test1')
self._call_command('batch_add_users', **options)
self.assertEqual(get_user_model().objects.all().count(), 6)
call_command('delete_old_users')
Expand Down
2 changes: 1 addition & 1 deletion django_freeradius/tests/static/test_batch.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rohithasrk,cleartext$password,rohith.asrk@openwisp.com,Rohith,ASRK
rohithasrk,pbkdf2_sha256$100000$x3DUBnOFwraV$PU2dZZq1FcuBjagxVLPhhFvpicLn18fFCN5xiLsxATc=,rohith@openwisp.com,,
,,rohith.asrk@openwisp.com,,
,,rohith.asrk1@openwisp.com,,
3 changes: 3 additions & 0 deletions django_freeradius/tests/static/test_batch_new.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vivekchand,cleartext$password,vivek.chand@openwisp.com,Vivek,Chand
vivekchand,pbkdf2_sha256$100000$x3DUBnOFwraV$PU2dZZq1FcuBjagxVLPhhFvpicLn18fFCN5xiLsxATc=,vivek@openwisp.com,,
,,vivek.rajput@openwisp.com,,

0 comments on commit 7b7daa2

Please sign in to comment.