Skip to content

Commit

Permalink
[change] Made the suggested changes and edited the testcase openwisp#525
Browse files Browse the repository at this point in the history


Made the suggested changes and added the testcase so both old and new command could be tested.

Fixes openwisp#525
  • Loading branch information
kaushikaryan04 committed Jul 20, 2024
1 parent 27f23d4 commit a9af7a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ def add_arguments(self, parser):
'--older-than-days',
action='store',
type=int,
default=30 * BATCH_DELETE_EXPIRED,
help='Delete users that are older than days provided',
)
parser.add_argument(
'--older-than-months',
action='store',
type=int,
default=BATCH_DELETE_EXPIRED,
help='Delete users that are older than months provided',
)

def handle(self, *args, **options):
days = options.get('older_than_days')
months = options.get('older_than_months')

if days is not None:
threshold_date = now() - timedelta(days=days)
else:
if months is not None:
threshold_date = now() - timedelta(days=30 * months)
else:
threshold_date = now() - timedelta(days=days)

batches = RadiusBatch.objects.filter(expiration_date__lt=threshold_date)
time_period = threshold_date.strftime('%Y-%m-%d %H:%M:%S')
Expand Down
4 changes: 3 additions & 1 deletion openwisp_radius/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def deactivate_expired_users():


@shared_task
def delete_old_radiusbatch_users(older_than_days=365):
def delete_old_radiusbatch_users(
older_than_days=30 * app_settings.BATCH_DELETE_EXPIRED,
):
management.call_command(
'delete_old_radiusbatch_users', older_than_days=older_than_days
)
Expand Down
3 changes: 3 additions & 0 deletions openwisp_radius/tests/static/test_batch_users.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rahulyadav,cleartext$password,rahul.yadav@openwisp.com,Rahul,Yadav
rahulyadav,pbkdf2_sha256$100000$x3DUBnOFwraV$PU2dZZq1FcuBjagxVLPhhFvpicLn18fFCN5xiLsxATc=,rahul@openwisp.com,,
,,rahul.rajput@openwisp.com,,
15 changes: 13 additions & 2 deletions openwisp_radius/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,21 @@ def test_delete_old_radiusbatch_users_command(self):
name='test1',
)
self._call_command('batch_add_users', **options)
self.assertEqual(get_user_model().objects.all().count(), 6)
path = self._get_path('static/test_batch_users.csv')
expiration_date = (now() - timedelta(days=10)).strftime('%d-%m-%Y')
options = dict(
organization=self.default_org.slug,
file=path,
expiration=expiration_date,
name='test2',
)
self._call_command('batch_add_users', **options)
self.assertEqual(get_user_model().objects.all().count(), 9)
call_command('delete_old_radiusbatch_users')
self.assertEqual(get_user_model().objects.all().count(), 6)
call_command('delete_old_radiusbatch_users', older_than_months=12)
self.assertEqual(get_user_model().objects.all().count(), 3)
call_command('delete_old_radiusbatch_users', older_than_days=365)
call_command('delete_old_radiusbatch_users', older_than_days=9)
self.assertEqual(get_user_model().objects.all().count(), 0)

@capture_stdout()
Expand Down

0 comments on commit a9af7a0

Please sign in to comment.